NCShareComments.swift 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. //
  2. // NCShareComments.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 28/07/2019.
  6. // Copyright © 2019 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import Foundation
  24. import NCCommunication
  25. class NCShareComments: UIViewController, NCShareCommentsCellDelegate {
  26. @IBOutlet weak var viewContainerConstraint: NSLayoutConstraint!
  27. @IBOutlet weak var tableView: UITableView!
  28. @IBOutlet weak var imageItem: UIImageView!
  29. @IBOutlet weak var labelUser: UILabel!
  30. @IBOutlet weak var newCommentField: UITextField!
  31. private let appDelegate = UIApplication.shared.delegate as! AppDelegate
  32. var metadata: tableMetadata?
  33. public var height: CGFloat = 0
  34. override func viewDidLoad() {
  35. super.viewDidLoad()
  36. viewContainerConstraint.constant = height
  37. tableView.dataSource = self
  38. tableView.delegate = self
  39. tableView.tableFooterView = UIView()
  40. tableView.rowHeight = UITableView.automaticDimension
  41. tableView.estimatedRowHeight = tableView.bounds.height
  42. tableView.allowsSelection = false
  43. tableView.backgroundColor = NCBrandColor.sharedInstance.backgroundForm
  44. tableView.separatorColor = NCBrandColor.sharedInstance.separator
  45. tableView.register(UINib.init(nibName: "NCShareCommentsCell", bundle: nil), forCellReuseIdentifier: "cell")
  46. newCommentField.placeholder = NSLocalizedString("_new_comment_", comment: "")
  47. // Display Name user & Quota
  48. guard let tabAccount = NCManageDatabase.sharedInstance.getAccountActive() else {
  49. return
  50. }
  51. if tabAccount.displayName.isEmpty {
  52. labelUser.text = tabAccount.user
  53. }
  54. else{
  55. labelUser.text = tabAccount.displayName
  56. }
  57. let fileNameLocalPath = CCUtility.getDirectoryUserData() + "/" + CCUtility.getStringUser(appDelegate.activeUser, activeUrl: appDelegate.activeUrl) + "-" + appDelegate.activeUser + ".png"
  58. if FileManager.default.fileExists(atPath: fileNameLocalPath) {
  59. if let image = UIImage(contentsOfFile: fileNameLocalPath) {
  60. imageItem.image = image
  61. }
  62. }
  63. // Mark comment ad read
  64. if metadata != nil && metadata!.commentsUnread {
  65. OCNetworking.sharedManager()?.readMarkComments(withAccount: self.appDelegate.activeAccount, fileId: metadata!.fileId, completion: { (account, message, errorCode) in
  66. if errorCode == 0 {
  67. NCManageDatabase.sharedInstance.readMarkerMetadata(account: account!, fileId: self.metadata!.fileId)
  68. }
  69. })
  70. }
  71. // changeTheming
  72. NotificationCenter.default.addObserver(self, selector: #selector(changeTheming), name: NSNotification.Name(rawValue: k_notificationCenter_changeTheming), object: nil)
  73. changeTheming()
  74. }
  75. override func viewWillAppear(_ animated: Bool) {
  76. super.viewWillAppear(animated)
  77. reloadData()
  78. }
  79. @objc func changeTheming() {
  80. appDelegate.changeTheming(self, tableView: tableView, collectionView: nil, form: true)
  81. labelUser.textColor = NCBrandColor.sharedInstance.textView
  82. }
  83. @objc func reloadData() {
  84. guard let metadata = self.metadata else { return }
  85. OCNetworking.sharedManager()?.getCommentsWithAccount(appDelegate.activeAccount, fileId: metadata.fileId, completion: { (account, items, message, errorCode) in
  86. if errorCode == 0 {
  87. let itemsNCComments = items as! [NCComments]
  88. NCManageDatabase.sharedInstance.addComments(itemsNCComments, account: metadata.account, objectId: metadata.fileId)
  89. self.tableView.reloadData()
  90. } else {
  91. NCContentPresenter.shared.messageNotification("_share_", description: message, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  92. }
  93. })
  94. tableView.reloadData()
  95. }
  96. // MARK: - IBAction & Tap
  97. @IBAction func newCommentFieldDidEndOnExit(textField: UITextField) {
  98. guard let message = textField.text else { return }
  99. guard let metadata = self.metadata else { return }
  100. if message.count == 0 { return }
  101. OCNetworking.sharedManager()?.putComments(withAccount: appDelegate.activeAccount, fileId: metadata.fileId, message: message, completion: { (account, message, errorCode) in
  102. if errorCode == 0 {
  103. self.newCommentField.text = ""
  104. self.reloadData()
  105. } else {
  106. NCContentPresenter.shared.messageNotification("_share_", description: message, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  107. }
  108. })
  109. }
  110. func tapMenu(with tableComments: tableComments?, sender: Any) {
  111. let mainMenuViewController = UIStoryboard.init(name: "NCMenu", bundle: nil).instantiateViewController(withIdentifier: "NCMainMenuTableViewController") as! NCMainMenuTableViewController
  112. var actions = [NCMenuAction]()
  113. actions.append(
  114. NCMenuAction(
  115. title: NSLocalizedString("_edit_comment_", comment: ""),
  116. icon: CCGraphics.changeThemingColorImage(UIImage(named: "edit"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  117. action: { menuAction in
  118. guard let metadata = self.metadata else { return }
  119. guard let tableComments = tableComments else { return }
  120. let alert = UIAlertController(title: NSLocalizedString("_edit_comment_", comment: ""), message: nil, preferredStyle: .alert)
  121. alert.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel, handler: nil))
  122. alert.addTextField(configurationHandler: { textField in
  123. textField.placeholder = NSLocalizedString("_new_comment_", comment: "")
  124. })
  125. alert.addAction(UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default, handler: { action in
  126. if let message = alert.textFields?.first?.text {
  127. if message != "" {
  128. OCNetworking.sharedManager()?.updateComments(withAccount: metadata.account, fileId: metadata.fileId, messageID: tableComments.messageID, message: message, completion: { (account, message, errorCode) in
  129. if errorCode == 0 {
  130. self.reloadData()
  131. } else {
  132. NCContentPresenter.shared.messageNotification("_share_", description: message, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  133. }
  134. })
  135. }
  136. }
  137. }))
  138. self.present(alert, animated: true)
  139. }
  140. )
  141. )
  142. actions.append(
  143. NCMenuAction(
  144. title: NSLocalizedString("_delete_comment_", comment: ""),
  145. icon: CCGraphics.changeThemingColorImage(UIImage(named: "trash"), width: 50, height: 50, color: .red),
  146. action: { menuAction in
  147. guard let metadata = self.metadata else { return }
  148. guard let tableComments = tableComments else { return }
  149. OCNetworking.sharedManager()?.deleteComments(withAccount: metadata.account, fileId: metadata.fileId, messageID: tableComments.messageID, completion: { (account, message, errorCode) in
  150. if errorCode == 0 {
  151. self.reloadData()
  152. } else {
  153. NCContentPresenter.shared.messageNotification("_share_", description: message, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  154. }
  155. })
  156. }
  157. )
  158. )
  159. actions.append(
  160. NCMenuAction(
  161. title: NSLocalizedString("_cancel_", comment: ""),
  162. icon: CCGraphics.changeThemingColorImage(UIImage(named: "cancel"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  163. action: { menuAction in
  164. }
  165. )
  166. )
  167. mainMenuViewController.actions = actions
  168. let menuPanelController = NCMenuPanelController()
  169. menuPanelController.parentPresenter = self
  170. menuPanelController.delegate = mainMenuViewController
  171. menuPanelController.set(contentViewController: mainMenuViewController)
  172. menuPanelController.track(scrollView: mainMenuViewController.tableView)
  173. self.present(menuPanelController, animated: true, completion: nil)
  174. }
  175. }
  176. // MARK: - UITableViewDelegate
  177. extension NCShareComments: UITableViewDelegate {
  178. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  179. return UITableView.automaticDimension
  180. }
  181. }
  182. // MARK: - UITableViewDataSource
  183. extension NCShareComments: UITableViewDataSource {
  184. func numberOfSections(in tableView: UITableView) -> Int {
  185. return 1
  186. }
  187. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  188. let comments = NCManageDatabase.sharedInstance.getComments(account: metadata!.account, objectId: metadata!.fileId)
  189. return comments.count
  190. }
  191. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  192. let comments = NCManageDatabase.sharedInstance.getComments(account: metadata!.account, objectId: metadata!.fileId)
  193. let tableComments = comments[indexPath.row]
  194. if let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? NCShareCommentsCell {
  195. cell.tableComments = tableComments
  196. cell.delegate = self
  197. cell.sizeToFit()
  198. // Image
  199. let fileNameLocalPath = CCUtility.getDirectoryUserData() + "/" + CCUtility.getStringUser(appDelegate.activeUser, activeUrl: appDelegate.activeUrl) + "-" + tableComments.actorId + ".png"
  200. if FileManager.default.fileExists(atPath: fileNameLocalPath) {
  201. if let image = UIImage(contentsOfFile: fileNameLocalPath) { cell.imageItem.image = image }
  202. } else {
  203. DispatchQueue.global().async {
  204. NCCommunication.shared.downloadAvatar(serverUrl: self.appDelegate.activeUrl, userID: tableComments.actorId, fileNameLocalPath: fileNameLocalPath, size: 128, customUserAgent: nil, addCustomHeaders: nil, account: self.appDelegate.activeAccount) { (account, data, errorCode, errorMessage) in
  205. if errorCode == 0 && UIImage(data: data!) != nil {
  206. cell.imageItem.image = UIImage(named: "avatar")
  207. }
  208. }
  209. /*
  210. let url = self.appDelegate.activeUrl + k_avatar + tableComments.actorId + "/" + k_avatar_size
  211. let encodedString = url.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
  212. OCNetworking.sharedManager()?.downloadContents(ofUrl: encodedString, completion: { (data, message, errorCode) in
  213. if errorCode == 0 && UIImage(data: data!) != nil {
  214. do {
  215. try data!.write(to: NSURL(fileURLWithPath: fileNameLocalPath) as URL, options: .atomic)
  216. if let image = UIImage(contentsOfFile: fileNameLocalPath) { cell.imageItem.image = image }
  217. } catch { return }
  218. } else {
  219. cell.imageItem.image = UIImage(named: "avatar")
  220. }
  221. })
  222. */
  223. }
  224. }
  225. // Username
  226. cell.labelUser.text = tableComments.actorDisplayName
  227. cell.labelUser.textColor = NCBrandColor.sharedInstance.textView
  228. // Date
  229. cell.labelDate.text = CCUtility.dateDiff(tableComments.creationDateTime as Date)
  230. cell.labelDate.textColor = NCBrandColor.sharedInstance.graySoft
  231. // Message
  232. cell.labelMessage.text = tableComments.message
  233. cell.labelMessage.textColor = NCBrandColor.sharedInstance.textView
  234. // Button Menu
  235. if tableComments.actorId == appDelegate.activeUserID {
  236. cell.buttonMenu.isHidden = false
  237. } else {
  238. cell.buttonMenu.isHidden = true
  239. }
  240. return cell
  241. }
  242. return UITableViewCell()
  243. }
  244. }
  245. // MARK: - NCShareCommentsCell
  246. class NCShareCommentsCell: UITableViewCell {
  247. @IBOutlet weak var imageItem: UIImageView!
  248. @IBOutlet weak var labelUser: UILabel!
  249. @IBOutlet weak var buttonMenu: UIButton!
  250. @IBOutlet weak var labelDate: UILabel!
  251. @IBOutlet weak var labelMessage: UILabel!
  252. var tableComments: tableComments?
  253. var delegate: NCShareCommentsCellDelegate?
  254. override func awakeFromNib() {
  255. super.awakeFromNib()
  256. buttonMenu.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "shareMenu"), width:100, height: 100, color: UIColor.lightGray), for: .normal)
  257. }
  258. @IBAction func touchUpInsideMenu(_ sender: Any) {
  259. delegate?.tapMenu(with: tableComments, sender: sender)
  260. }
  261. }
  262. protocol NCShareCommentsCellDelegate {
  263. func tapMenu(with tableComments: tableComments?, sender: Any)
  264. }