NCShareComments.swift 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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 Sheeeeeeeeet
  25. import NCCommunication
  26. class NCShareComments: UIViewController, NCShareCommentsCellDelegate {
  27. @IBOutlet weak var viewContainerConstraint: NSLayoutConstraint!
  28. @IBOutlet weak var tableView: UITableView!
  29. @IBOutlet weak var imageItem: UIImageView!
  30. @IBOutlet weak var labelUser: UILabel!
  31. @IBOutlet weak var newCommentField: UITextField!
  32. private let appDelegate = UIApplication.shared.delegate as! AppDelegate
  33. var metadata: tableMetadata?
  34. public var height: CGFloat = 0
  35. private var actionSheet: ActionSheet?
  36. override func viewDidLoad() {
  37. super.viewDidLoad()
  38. viewContainerConstraint.constant = height
  39. tableView.dataSource = self
  40. tableView.delegate = self
  41. tableView.tableFooterView = UIView()
  42. tableView.rowHeight = UITableView.automaticDimension
  43. tableView.estimatedRowHeight = tableView.bounds.height
  44. tableView.allowsSelection = false
  45. tableView.backgroundColor = NCBrandColor.sharedInstance.backgroundForm
  46. tableView.separatorColor = NCBrandColor.sharedInstance.separator
  47. tableView.register(UINib.init(nibName: "NCShareCommentsCell", bundle: nil), forCellReuseIdentifier: "cell")
  48. newCommentField.placeholder = NSLocalizedString("_new_comment_", comment: "")
  49. // Display Name user & Quota
  50. guard let tabAccount = NCManageDatabase.sharedInstance.getAccountActive() else {
  51. return
  52. }
  53. if tabAccount.displayName.isEmpty {
  54. labelUser.text = tabAccount.user
  55. }
  56. else{
  57. labelUser.text = tabAccount.displayName
  58. }
  59. let fileNameLocalPath = CCUtility.getDirectoryUserData() + "/" + CCUtility.getStringUser(appDelegate.activeUser, activeUrl: appDelegate.activeUrl) + "-" + appDelegate.activeUser + ".png"
  60. if FileManager.default.fileExists(atPath: fileNameLocalPath) {
  61. if let image = UIImage(contentsOfFile: fileNameLocalPath) {
  62. imageItem.image = image
  63. }
  64. }
  65. // Mark comment ad read
  66. if metadata != nil && metadata!.commentsUnread {
  67. OCNetworking.sharedManager()?.readMarkComments(withAccount: self.appDelegate.activeAccount, fileId: metadata!.fileId, completion: { (account, message, errorCode) in
  68. if errorCode == 0 {
  69. NCManageDatabase.sharedInstance.readMarkerMetadata(account: account!, fileId: self.metadata!.fileId)
  70. }
  71. })
  72. }
  73. // changeTheming
  74. NotificationCenter.default.addObserver(self, selector: #selector(self.changeTheming), name: NSNotification.Name(rawValue: "changeTheming"), object: nil)
  75. changeTheming()
  76. }
  77. override func viewWillAppear(_ animated: Bool) {
  78. super.viewWillAppear(animated)
  79. reloadData()
  80. }
  81. @objc func changeTheming() {
  82. appDelegate.changeTheming(self, tableView: tableView, collectionView: nil, form: true)
  83. labelUser.textColor = NCBrandColor.sharedInstance.textView
  84. }
  85. @objc func reloadData() {
  86. guard let metadata = self.metadata else { return }
  87. OCNetworking.sharedManager()?.getCommentsWithAccount(appDelegate.activeAccount, fileId: metadata.fileId, completion: { (account, items, message, errorCode) in
  88. if errorCode == 0 {
  89. let itemsNCComments = items as! [NCComments]
  90. NCManageDatabase.sharedInstance.addComments(itemsNCComments, account: metadata.account, objectId: metadata.fileId)
  91. self.tableView.reloadData()
  92. } else {
  93. NCContentPresenter.shared.messageNotification("_share_", description: message, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  94. }
  95. })
  96. tableView.reloadData()
  97. }
  98. // MARK: - IBAction & Tap
  99. @IBAction func newCommentFieldDidEndOnExit(textField: UITextField) {
  100. guard let message = textField.text else { return }
  101. guard let metadata = self.metadata else { return }
  102. if message.count == 0 { return }
  103. OCNetworking.sharedManager()?.putComments(withAccount: appDelegate.activeAccount, fileId: metadata.fileId, message: message, completion: { (account, message, errorCode) in
  104. if errorCode == 0 {
  105. self.newCommentField.text = ""
  106. self.reloadData()
  107. } else {
  108. NCContentPresenter.shared.messageNotification("_share_", description: message, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  109. }
  110. })
  111. }
  112. func tapMenu(with tableComments: tableComments?, sender: Any) {
  113. var items = [MenuItem]()
  114. ActionSheetTableView.appearance().backgroundColor = NCBrandColor.sharedInstance.backgroundForm
  115. ActionSheetTableView.appearance().separatorColor = NCBrandColor.sharedInstance.separator
  116. ActionSheetItemCell.appearance().backgroundColor = NCBrandColor.sharedInstance.backgroundForm
  117. ActionSheetItemCell.appearance().titleColor = NCBrandColor.sharedInstance.textView
  118. items.append(MenuItem(title: NSLocalizedString("_edit_comment_", comment: ""), subtitle: nil, value: 0, image: CCGraphics.changeThemingColorImage(UIImage.init(named: "edit"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon), isEnabled: true, tapBehavior: .none))
  119. items.append(MenuItem(title: NSLocalizedString("_delete_comment_", comment: ""), value: 1, image: CCGraphics.changeThemingColorImage(UIImage.init(named: "trash"), width: 50, height: 50, color: .red)))
  120. items.append(CancelButton(title: NSLocalizedString("_cancel_", comment: "")))
  121. actionSheet = ActionSheet(menu: Menu(items: items), action: { (shhet, item) in
  122. if item.value as? Int == 0 {
  123. guard let metadata = self.metadata else { return }
  124. guard let tableComments = tableComments else { return }
  125. let alert = UIAlertController(title: NSLocalizedString("_edit_comment_", comment: ""), message: nil, preferredStyle: .alert)
  126. alert.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel, handler: nil))
  127. alert.addTextField(configurationHandler: { textField in
  128. textField.placeholder = NSLocalizedString("_new_comment_", comment: "")
  129. })
  130. alert.addAction(UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default, handler: { action in
  131. if let message = alert.textFields?.first?.text {
  132. if message != "" {
  133. OCNetworking.sharedManager()?.updateComments(withAccount: metadata.account, fileId: metadata.fileId, messageID: tableComments.messageID, message: message, completion: { (account, message, errorCode) in
  134. if errorCode == 0 {
  135. self.reloadData()
  136. } else {
  137. NCContentPresenter.shared.messageNotification("_share_", description: message, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  138. }
  139. })
  140. }
  141. }
  142. }))
  143. self.present(alert, animated: true)
  144. }
  145. if item.value as? Int == 1 {
  146. guard let metadata = self.metadata else { return }
  147. guard let tableComments = tableComments else { return }
  148. OCNetworking.sharedManager()?.deleteComments(withAccount: metadata.account, fileId: metadata.fileId, messageID: tableComments.messageID, completion: { (account, message, errorCode) in
  149. if errorCode == 0 {
  150. self.reloadData()
  151. } else {
  152. NCContentPresenter.shared.messageNotification("_share_", description: message, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  153. }
  154. })
  155. }
  156. if item is CancelButton { print("Cancel buttons has the value `true`") }
  157. })
  158. actionSheet?.present(in: self, from: sender as! UIButton)
  159. }
  160. }
  161. // MARK: - UITableViewDelegate
  162. extension NCShareComments: UITableViewDelegate {
  163. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  164. return UITableView.automaticDimension
  165. }
  166. }
  167. // MARK: - UITableViewDataSource
  168. extension NCShareComments: UITableViewDataSource {
  169. func numberOfSections(in tableView: UITableView) -> Int {
  170. return 1
  171. }
  172. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  173. let comments = NCManageDatabase.sharedInstance.getComments(account: metadata!.account, objectId: metadata!.fileId)
  174. return comments.count
  175. }
  176. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  177. let comments = NCManageDatabase.sharedInstance.getComments(account: metadata!.account, objectId: metadata!.fileId)
  178. let tableComments = comments[indexPath.row]
  179. if let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? NCShareCommentsCell {
  180. cell.tableComments = tableComments
  181. cell.delegate = self
  182. cell.sizeToFit()
  183. // Image
  184. let fileNameLocalPath = CCUtility.getDirectoryUserData() + "/" + CCUtility.getStringUser(appDelegate.activeUser, activeUrl: appDelegate.activeUrl) + "-" + tableComments.actorId + ".png"
  185. if FileManager.default.fileExists(atPath: fileNameLocalPath) {
  186. if let image = UIImage(contentsOfFile: fileNameLocalPath) { cell.imageItem.image = image }
  187. } else {
  188. DispatchQueue.global().async {
  189. NCCommunication.sharedInstance.downloadAvatar(urlString: self.appDelegate.activeUrl, userID: tableComments.actorId, fileNameLocalPath: fileNameLocalPath, size: 128, account: self.appDelegate.activeAccount) { (account, data, errorCode, errorMessage) in
  190. if errorCode == 0 && UIImage(data: data!) != nil {
  191. cell.imageItem.image = UIImage(named: "avatar")
  192. }
  193. }
  194. /*
  195. let url = self.appDelegate.activeUrl + k_avatar + tableComments.actorId + "/" + k_avatar_size
  196. let encodedString = url.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
  197. OCNetworking.sharedManager()?.downloadContents(ofUrl: encodedString, completion: { (data, message, errorCode) in
  198. if errorCode == 0 && UIImage(data: data!) != nil {
  199. do {
  200. try data!.write(to: NSURL(fileURLWithPath: fileNameLocalPath) as URL, options: .atomic)
  201. if let image = UIImage(contentsOfFile: fileNameLocalPath) { cell.imageItem.image = image }
  202. } catch { return }
  203. } else {
  204. cell.imageItem.image = UIImage(named: "avatar")
  205. }
  206. })
  207. */
  208. }
  209. }
  210. // Username
  211. cell.labelUser.text = tableComments.actorDisplayName
  212. cell.labelUser.textColor = NCBrandColor.sharedInstance.textView
  213. // Date
  214. cell.labelDate.text = CCUtility.dateDiff(tableComments.creationDateTime as Date)
  215. cell.labelDate.textColor = NCBrandColor.sharedInstance.graySoft
  216. // Message
  217. cell.labelMessage.text = tableComments.message
  218. cell.labelMessage.textColor = NCBrandColor.sharedInstance.textView
  219. // Button Menu
  220. if tableComments.actorId == appDelegate.activeUserID {
  221. cell.buttonMenu.isHidden = false
  222. } else {
  223. cell.buttonMenu.isHidden = true
  224. }
  225. return cell
  226. }
  227. return UITableViewCell()
  228. }
  229. }
  230. // MARK: - NCShareCommentsCell
  231. class NCShareCommentsCell: UITableViewCell {
  232. @IBOutlet weak var imageItem: UIImageView!
  233. @IBOutlet weak var labelUser: UILabel!
  234. @IBOutlet weak var buttonMenu: UIButton!
  235. @IBOutlet weak var labelDate: UILabel!
  236. @IBOutlet weak var labelMessage: UILabel!
  237. var tableComments: tableComments?
  238. var delegate: NCShareCommentsCellDelegate?
  239. override func awakeFromNib() {
  240. super.awakeFromNib()
  241. buttonMenu.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "shareMenu"), width:100, height: 100, color: UIColor.lightGray), for: .normal)
  242. }
  243. @IBAction func touchUpInsideMenu(_ sender: Any) {
  244. delegate?.tapMenu(with: tableComments, sender: sender)
  245. }
  246. }
  247. protocol NCShareCommentsCellDelegate {
  248. func tapMenu(with tableComments: tableComments?, sender: Any)
  249. }