NCShareComments.swift 12 KB

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