NCShareComments.swift 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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.shared.backgroundForm
  44. tableView.separatorColor = NCBrandColor.shared.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.shared.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. var fileNameLocalPath = CCUtility.getDirectoryUserData() + "/" + CCUtility.getStringUser(appDelegate.user, urlBase: appDelegate.urlBase) + "-" + appDelegate.user
  58. fileNameLocalPath = fileNameLocalPath + ".png"
  59. if FileManager.default.fileExists(atPath: fileNameLocalPath) {
  60. if let image = UIImage(contentsOfFile: fileNameLocalPath) {
  61. imageItem.image = image
  62. }
  63. }
  64. // Mark comment ad read
  65. if metadata != nil && metadata!.commentsUnread {
  66. NCCommunication.shared.markAsReadComments(fileId: metadata!.fileId) { (account, errorCode, errorDescription) in
  67. if errorCode == 0 {
  68. NCManageDatabase.shared.readMarkerMetadata(account: account, fileId: self.metadata!.fileId)
  69. }
  70. }
  71. }
  72. // changeTheming
  73. NotificationCenter.default.addObserver(self, selector: #selector(changeTheming), name: NSNotification.Name(rawValue: NCBrandGlobal.shared.notificationCenterChangeTheming), object: nil)
  74. changeTheming()
  75. }
  76. override func viewWillAppear(_ animated: Bool) {
  77. super.viewWillAppear(animated)
  78. reloadData()
  79. }
  80. @objc func changeTheming() {
  81. view.backgroundColor = NCBrandColor.shared.backgroundForm
  82. tableView.backgroundColor = NCBrandColor.shared.backgroundForm
  83. tableView.reloadData()
  84. labelUser.textColor = NCBrandColor.shared.textView
  85. }
  86. @objc func reloadData() {
  87. guard let metadata = self.metadata else { return }
  88. NCCommunication.shared.getComments(fileId: metadata.fileId) { (account, comments, errorCode, errorDescription) in
  89. if errorCode == 0 && comments != nil {
  90. NCManageDatabase.shared.addComments(comments!, account: metadata.account, objectId: metadata.fileId)
  91. self.tableView.reloadData()
  92. } else {
  93. if errorCode != NCBrandGlobal.shared.ErrorResourceNotFound {
  94. NCContentPresenter.shared.messageNotification("_share_", description: errorDescription, delay: NCBrandGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  95. }
  96. }
  97. }
  98. tableView.reloadData()
  99. }
  100. // MARK: - IBAction & Tap
  101. @IBAction func newCommentFieldDidEndOnExit(textField: UITextField) {
  102. guard let message = textField.text else { return }
  103. guard let metadata = self.metadata else { return }
  104. if message.count == 0 { return }
  105. NCCommunication.shared.putComments(fileId: metadata.fileId, message: message) { (account, errorCode, errorDescription) in
  106. if errorCode == 0 {
  107. self.newCommentField.text = ""
  108. self.reloadData()
  109. } else {
  110. NCContentPresenter.shared.messageNotification("_share_", description: errorDescription, delay: NCBrandGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  111. }
  112. }
  113. }
  114. func tapMenu(with tableComments: tableComments?, sender: Any) {
  115. let mainMenuViewController = UIStoryboard.init(name: "NCMenu", bundle: nil).instantiateViewController(withIdentifier: "NCMainMenuTableViewController") as! NCMainMenuTableViewController
  116. var actions = [NCMenuAction]()
  117. actions.append(
  118. NCMenuAction(
  119. title: NSLocalizedString("_edit_comment_", comment: ""),
  120. icon: UIImage(named: "edit")!.image(color: NCBrandColor.shared.icon, size: 50),
  121. action: { menuAction in
  122. guard let metadata = self.metadata else { return }
  123. guard let tableComments = tableComments else { return }
  124. let alert = UIAlertController(title: NSLocalizedString("_edit_comment_", comment: ""), message: nil, preferredStyle: .alert)
  125. alert.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel, handler: nil))
  126. alert.addTextField(configurationHandler: { textField in
  127. textField.placeholder = NSLocalizedString("_new_comment_", comment: "")
  128. })
  129. alert.addAction(UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default, handler: { action in
  130. if let message = alert.textFields?.first?.text {
  131. if message != "" {
  132. NCCommunication.shared.updateComments(fileId: metadata.fileId, messageId: tableComments.messageId, message: message) { (account, errorCode, errorDescription) in
  133. if errorCode == 0 {
  134. self.reloadData()
  135. } else {
  136. NCContentPresenter.shared.messageNotification("_share_", description: errorDescription, delay: NCBrandGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  137. }
  138. }
  139. }
  140. }
  141. }))
  142. self.present(alert, animated: true)
  143. }
  144. )
  145. )
  146. actions.append(
  147. NCMenuAction(
  148. title: NSLocalizedString("_delete_comment_", comment: ""),
  149. icon: UIImage(named: "trash")!.image(color: NCBrandColor.shared.icon, size: 50),
  150. action: { menuAction in
  151. guard let metadata = self.metadata else { return }
  152. guard let tableComments = tableComments else { return }
  153. NCCommunication.shared.deleteComments(fileId: metadata.fileId, messageId: tableComments.messageId) { (account, errorCode, errorDescription) in
  154. if errorCode == 0 {
  155. self.reloadData()
  156. } else {
  157. NCContentPresenter.shared.messageNotification("_share_", description: errorDescription, delay: NCBrandGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  158. }
  159. }
  160. }
  161. )
  162. )
  163. actions.append(
  164. NCMenuAction(
  165. title: NSLocalizedString("_cancel_", comment: ""),
  166. icon: UIImage(named: "cancel")!.image(color: NCBrandColor.shared.icon, size: 50),
  167. action: { menuAction in
  168. }
  169. )
  170. )
  171. mainMenuViewController.actions = actions
  172. let menuPanelController = NCMenuPanelController()
  173. menuPanelController.parentPresenter = self
  174. menuPanelController.delegate = mainMenuViewController
  175. menuPanelController.set(contentViewController: mainMenuViewController)
  176. menuPanelController.track(scrollView: mainMenuViewController.tableView)
  177. self.present(menuPanelController, animated: true, completion: nil)
  178. }
  179. }
  180. // MARK: - UITableViewDelegate
  181. extension NCShareComments: UITableViewDelegate {
  182. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  183. return UITableView.automaticDimension
  184. }
  185. }
  186. // MARK: - UITableViewDataSource
  187. extension NCShareComments: UITableViewDataSource {
  188. func numberOfSections(in tableView: UITableView) -> Int {
  189. return 1
  190. }
  191. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  192. let comments = NCManageDatabase.shared.getComments(account: metadata!.account, objectId: metadata!.fileId)
  193. return comments.count
  194. }
  195. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  196. let comments = NCManageDatabase.shared.getComments(account: metadata!.account, objectId: metadata!.fileId)
  197. let tableComments = comments[indexPath.row]
  198. if let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? NCShareCommentsCell {
  199. cell.tableComments = tableComments
  200. cell.delegate = self
  201. cell.sizeToFit()
  202. // Image
  203. var fileNameLocalPath = CCUtility.getDirectoryUserData() + "/" + CCUtility.getStringUser(appDelegate.user, urlBase: appDelegate.urlBase) + "-" + tableComments.actorId
  204. fileNameLocalPath = fileNameLocalPath + ".png"
  205. if FileManager.default.fileExists(atPath: fileNameLocalPath) {
  206. if let image = UIImage(contentsOfFile: fileNameLocalPath) { cell.imageItem.image = image }
  207. } else {
  208. DispatchQueue.global().async {
  209. NCCommunication.shared.downloadAvatar(userId: tableComments.actorId, fileNameLocalPath: fileNameLocalPath, size: 128) { (account, data, errorCode, errorMessage) in
  210. if errorCode == 0 && UIImage(data: data!) != nil {
  211. cell.imageItem.image = UIImage(named: "avatar")
  212. }
  213. }
  214. /*
  215. let url = self.appDelegate.urlBase + k_avatar + tableComments.actorId + "/" + k_avatar_size
  216. let encodedString = url.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
  217. OCNetworking.sharedManager()?.downloadContents(ofUrl: encodedString, completion: { (data, message, errorCode) in
  218. if errorCode == 0 && UIImage(data: data!) != nil {
  219. do {
  220. try data!.write(to: NSURL(fileURLWithPath: fileNameLocalPath) as URL, options: .atomic)
  221. if let image = UIImage(contentsOfFile: fileNameLocalPath) { cell.imageItem.image = image }
  222. } catch { return }
  223. } else {
  224. cell.imageItem.image = UIImage(named: "avatar")
  225. }
  226. })
  227. */
  228. }
  229. }
  230. // Username
  231. cell.labelUser.text = tableComments.actorDisplayName
  232. cell.labelUser.textColor = NCBrandColor.shared.textView
  233. // Date
  234. cell.labelDate.text = CCUtility.dateDiff(tableComments.creationDateTime as Date)
  235. cell.labelDate.textColor = NCBrandColor.shared.graySoft
  236. // Message
  237. cell.labelMessage.text = tableComments.message
  238. cell.labelMessage.textColor = NCBrandColor.shared.textView
  239. // Button Menu
  240. if tableComments.actorId == appDelegate.userId {
  241. cell.buttonMenu.isHidden = false
  242. } else {
  243. cell.buttonMenu.isHidden = true
  244. }
  245. return cell
  246. }
  247. return UITableViewCell()
  248. }
  249. }
  250. // MARK: - NCShareCommentsCell
  251. class NCShareCommentsCell: UITableViewCell {
  252. @IBOutlet weak var imageItem: UIImageView!
  253. @IBOutlet weak var labelUser: UILabel!
  254. @IBOutlet weak var buttonMenu: UIButton!
  255. @IBOutlet weak var labelDate: UILabel!
  256. @IBOutlet weak var labelMessage: UILabel!
  257. var tableComments: tableComments?
  258. var delegate: NCShareCommentsCellDelegate?
  259. override func awakeFromNib() {
  260. super.awakeFromNib()
  261. buttonMenu.setImage(UIImage.init(named: "shareMenu")!.image(color: .lightGray, size: 50), for: .normal)
  262. }
  263. @IBAction func touchUpInsideMenu(_ sender: Any) {
  264. delegate?.tapMenu(with: tableComments, sender: sender)
  265. }
  266. }
  267. protocol NCShareCommentsCellDelegate {
  268. func tapMenu(with tableComments: tableComments?, sender: Any)
  269. }