NCShareComments.swift 14 KB

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