NCShareComments.swift 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 UIKit
  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. // MARK: - View Life Cycle
  35. override func viewDidLoad() {
  36. super.viewDidLoad()
  37. view.backgroundColor = NCBrandColor.shared.systemBackground
  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.shared.systemBackground
  46. tableView.separatorColor = NCBrandColor.shared.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 activeAccount = NCManageDatabase.shared.getActiveAccount() else {
  51. return
  52. }
  53. if activeAccount.displayName.isEmpty {
  54. labelUser.text = activeAccount.user
  55. }
  56. else{
  57. labelUser.text = activeAccount.displayName
  58. }
  59. labelUser.textColor = NCBrandColor.shared.label
  60. let fileName = String(CCUtility.getUserUrlBase(appDelegate.user, urlBase: appDelegate.urlBase)) + "-" + appDelegate.user + ".png"
  61. let fileNameLocalPath = String(CCUtility.getDirectoryUserData()) + "/" + fileName
  62. if let image = UIImage(contentsOfFile: fileNameLocalPath) {
  63. imageItem.image = image
  64. } else {
  65. imageItem.image = UIImage(named: "avatar")
  66. }
  67. // Mark comment ad read
  68. if metadata != nil && metadata!.commentsUnread {
  69. NCCommunication.shared.markAsReadComments(fileId: metadata!.fileId) { (account, errorCode, errorDescription) in
  70. if errorCode == 0 {
  71. NCManageDatabase.shared.readMarkerMetadata(account: account, fileId: self.metadata!.fileId)
  72. }
  73. }
  74. }
  75. // changeTheming
  76. NotificationCenter.default.addObserver(self, selector: #selector(changeTheming), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterChangeTheming), object: nil)
  77. changeTheming()
  78. }
  79. override func viewWillAppear(_ animated: Bool) {
  80. super.viewWillAppear(animated)
  81. reloadData()
  82. }
  83. @objc func changeTheming() {
  84. tableView.reloadData()
  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 != NCGlobal.shared.errorResourceNotFound {
  94. NCContentPresenter.shared.messageNotification("_share_", description: errorDescription, delay: NCGlobal.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: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  111. }
  112. }
  113. }
  114. func tapMenu(with tableComments: tableComments?, sender: Any) {
  115. toggleMenu(with: tableComments)
  116. }
  117. }
  118. // MARK: - UITableViewDelegate
  119. extension NCShareComments: UITableViewDelegate {
  120. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  121. return UITableView.automaticDimension
  122. }
  123. }
  124. // MARK: - UITableViewDataSource
  125. extension NCShareComments: UITableViewDataSource {
  126. func numberOfSections(in tableView: UITableView) -> Int {
  127. return 1
  128. }
  129. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  130. let comments = NCManageDatabase.shared.getComments(account: metadata!.account, objectId: metadata!.fileId)
  131. return comments.count
  132. }
  133. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  134. let comments = NCManageDatabase.shared.getComments(account: metadata!.account, objectId: metadata!.fileId)
  135. let tableComments = comments[indexPath.row]
  136. if let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? NCShareCommentsCell {
  137. cell.tableComments = tableComments
  138. cell.delegate = self
  139. cell.sizeToFit()
  140. // Image
  141. let fileName = String(CCUtility.getUserUrlBase(appDelegate.user, urlBase: appDelegate.urlBase)) + "-" + tableComments.actorId + ".png"
  142. NCOperationQueue.shared.downloadAvatar(user: tableComments.actorId, fileName: fileName, placeholder: UIImage(named: "avatar"), cell: cell, view: tableView)
  143. // Username
  144. cell.labelUser.text = tableComments.actorDisplayName
  145. cell.labelUser.textColor = NCBrandColor.shared.label
  146. // Date
  147. cell.labelDate.text = CCUtility.dateDiff(tableComments.creationDateTime as Date)
  148. cell.labelDate.textColor = NCBrandColor.shared.systemGray4
  149. // Message
  150. cell.labelMessage.text = tableComments.message
  151. cell.labelMessage.textColor = NCBrandColor.shared.label
  152. // Button Menu
  153. if tableComments.actorId == appDelegate.userId {
  154. cell.buttonMenu.isHidden = false
  155. } else {
  156. cell.buttonMenu.isHidden = true
  157. }
  158. return cell
  159. }
  160. return UITableViewCell()
  161. }
  162. }
  163. // MARK: - NCShareCommentsCell
  164. class NCShareCommentsCell: UITableViewCell, NCCellProtocol {
  165. @IBOutlet weak var imageItem: UIImageView!
  166. @IBOutlet weak var labelUser: UILabel!
  167. @IBOutlet weak var buttonMenu: UIButton!
  168. @IBOutlet weak var labelDate: UILabel!
  169. @IBOutlet weak var labelMessage: UILabel!
  170. var tableComments: tableComments?
  171. var delegate: NCShareCommentsCellDelegate?
  172. var filePreviewImageView : UIImageView? {
  173. get{
  174. return nil
  175. }
  176. }
  177. var fileAvatarImageView: UIImageView? {
  178. get{
  179. return imageItem
  180. }
  181. }
  182. var fileObjectId: String? {
  183. get {
  184. return nil
  185. }
  186. }
  187. var fileUser: String? {
  188. get{
  189. return tableComments?.actorId
  190. }
  191. }
  192. override func awakeFromNib() {
  193. super.awakeFromNib()
  194. buttonMenu.setImage(UIImage.init(named: "shareMenu")!.image(color: .lightGray, size: 50), for: .normal)
  195. }
  196. @IBAction func touchUpInsideMenu(_ sender: Any) {
  197. delegate?.tapMenu(with: tableComments, sender: sender)
  198. }
  199. }
  200. protocol NCShareCommentsCellDelegate {
  201. func tapMenu(with tableComments: tableComments?, sender: Any)
  202. }