NCShareComments.swift 13 KB

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