CCNotification.swift 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. //
  2. // CCNotification.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 27/01/17.
  6. // Copyright (c) 2017 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 CCNotification: UITableViewController, CCNotificationCelllDelegate {
  26. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  27. override func viewDidLoad() {
  28. super.viewDidLoad()
  29. self.navigationController?.navigationBar.topItem?.title = NSLocalizedString("_notification_", comment: "")
  30. self.navigationItem.setRightBarButton(UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(viewClose)), animated: true)
  31. self.tableView.tableFooterView = UIView()
  32. self.tableView.rowHeight = UITableView.automaticDimension
  33. self.tableView.estimatedRowHeight = 50.0
  34. self.tableView.allowsSelection = false
  35. // Register to receive notification reload data
  36. NotificationCenter.default.addObserver(self, selector: #selector(reloadDatasource), name: Notification.Name(rawValue: k_notificationCenter_reloadDataNotification), object: nil)
  37. // Theming view
  38. NotificationCenter.default.addObserver(self, selector: #selector(changeTheming), name: NSNotification.Name(rawValue: k_notificationCenter_changeTheming), object: nil)
  39. changeTheming()
  40. reloadDatasource()
  41. }
  42. @objc func changeTheming() {
  43. appDelegate.changeTheming(self, tableView: tableView, collectionView: nil, form: true)
  44. }
  45. @objc func viewClose() {
  46. // Stop listening notification reload data
  47. NotificationCenter.default.removeObserver(self, name: Notification.Name(k_notificationCenter_reloadDataNotification), object: nil);
  48. self.dismiss(animated: true, completion: nil)
  49. }
  50. // MARK: - Table
  51. @objc func reloadDatasource() {
  52. self.tableView.reloadData()
  53. }
  54. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  55. let numRecord = appDelegate.listOfNotifications.count
  56. return numRecord
  57. }
  58. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  59. let cell = self.tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CCNotificationCell
  60. cell.delegate = self
  61. let notification = appDelegate.listOfNotifications.object(at: indexPath.row) as! OCNotifications
  62. let urlIcon = URL(string: notification.icon)
  63. var image : UIImage?
  64. if let urlIcon = urlIcon {
  65. let pathFileName = CCUtility.getDirectoryUserData() + "/" + urlIcon.deletingPathExtension().lastPathComponent + ".png"
  66. image = UIImage(contentsOfFile: pathFileName)
  67. }
  68. if let image = image {
  69. cell.icon.image = CCGraphics.changeThemingColorImage(image, multiplier: 2, color: NCBrandColor.sharedInstance.brandElement)
  70. } else {
  71. cell.icon.image = CCGraphics.changeThemingColorImage(#imageLiteral(resourceName: "notification"), multiplier:2, color: NCBrandColor.sharedInstance.brandElement)
  72. }
  73. // Avatar
  74. cell.avatar.isHidden = true
  75. cell.avatarLeadingMargin.constant = 10
  76. if let parameter = notification.subjectRichParameters as? Dictionary<String, Any> {
  77. if let user = parameter["user"] as? Dictionary<String, Any> {
  78. if let name = user["id"] as? String {
  79. let fileNameLocalPath = CCUtility.getDirectoryUserData() + "/" + CCUtility.getStringUser(appDelegate.activeUser, activeUrl: appDelegate.activeUrl) + "-" + name + ".png"
  80. if FileManager.default.fileExists(atPath: fileNameLocalPath) {
  81. if let image = UIImage(contentsOfFile: fileNameLocalPath) {
  82. cell.avatar.isHidden = false
  83. cell.avatarLeadingMargin.constant = 50
  84. cell.avatar.image = image
  85. }
  86. } else {
  87. DispatchQueue.global().async {
  88. NCCommunication.sharedInstance.downloadAvatar(serverUrl: self.appDelegate.activeUrl, userID: name, fileNameLocalPath: fileNameLocalPath, size: Int(k_avatar_size), customUserAgent: nil, addCustomHeaders: nil, account: self.appDelegate.activeAccount) { (account, data, errorCode, errorMessage) in
  89. if errorCode == 0 && account == self.appDelegate.activeAccount && UIImage(data: data!) != nil {
  90. if let image = UIImage(contentsOfFile: fileNameLocalPath) {
  91. cell.avatar.isHidden = false
  92. cell.avatarLeadingMargin.constant = 50
  93. cell.avatar.image = image
  94. }
  95. }
  96. }
  97. }
  98. }
  99. }
  100. }
  101. }
  102. //
  103. //cell.date.text = DateFormatter.localizedString(from: notification.date, dateStyle: .medium, timeStyle: .medium)
  104. //
  105. cell.notification = notification
  106. cell.date.text = CCUtility.dateDiff(notification.date)
  107. cell.date.textColor = .gray
  108. cell.subject.text = notification.subject
  109. cell.subject.textColor = NCBrandColor.sharedInstance.textView
  110. cell.message.text = notification.message.replacingOccurrences(of: "<br />", with: "\n")
  111. cell.message.textColor = .gray
  112. cell.remove.setImage(CCGraphics.changeThemingColorImage(UIImage(named: "exit")!, width: 40, height: 40, color: .gray), for: .normal)
  113. cell.primary.isEnabled = false
  114. cell.primary.isHidden = true
  115. cell.primary.titleLabel?.font = .systemFont(ofSize: 14)
  116. cell.primary.setTitleColor(.white, for: .normal)
  117. cell.primary.layer.cornerRadius = 15
  118. cell.primary.layer.masksToBounds = true
  119. cell.primary.layer.backgroundColor = NCBrandColor.sharedInstance.brand.cgColor
  120. cell.secondary.isEnabled = false
  121. cell.secondary.isHidden = true
  122. cell.secondary.titleLabel?.font = .systemFont(ofSize: 14)
  123. cell.secondary.setTitleColor(.gray, for: .normal)
  124. cell.secondary.layer.cornerRadius = 15
  125. cell.secondary.layer.masksToBounds = true
  126. cell.secondary.layer.backgroundColor = NCBrandColor.sharedInstance.graySoft.withAlphaComponent(0.1).cgColor
  127. cell.secondary.layer.borderWidth = 0.3
  128. cell.secondary.layer.borderColor = UIColor.gray.cgColor
  129. cell.messageBottomMargin.constant = 10
  130. // Action
  131. if notification.actions.count > 0 {
  132. if notification.actions.count == 1 {
  133. let action = notification.actions[0] as! OCNotificationsAction
  134. cell.primary.isEnabled = true
  135. cell.primary.isHidden = false
  136. cell.primary.setTitle(action.label, for: .normal)
  137. } else if notification.actions.count == 2 {
  138. cell.primary.isEnabled = true
  139. cell.primary.isHidden = false
  140. cell.secondary.isEnabled = true
  141. cell.secondary.isHidden = false
  142. for action in notification.actions {
  143. let label = (action as! OCNotificationsAction).label
  144. let primary = (action as! OCNotificationsAction).primary
  145. if primary {
  146. cell.primary.setTitle(label, for: .normal)
  147. } else {
  148. cell.secondary.setTitle(label, for: .normal)
  149. }
  150. }
  151. }
  152. let widthPrimary = cell.primary.intrinsicContentSize.width + 30;
  153. let widthSecondary = cell.secondary.intrinsicContentSize.width + 30;
  154. if widthPrimary > widthSecondary {
  155. cell.primaryWidth.constant = widthPrimary
  156. cell.secondaryWidth.constant = widthPrimary
  157. } else {
  158. cell.primaryWidth.constant = widthSecondary
  159. cell.secondaryWidth.constant = widthSecondary
  160. }
  161. cell.messageBottomMargin.constant = 40
  162. }
  163. return cell
  164. }
  165. // MARK: tap Action
  166. func tapRemove(with notification: OCNotifications?) {
  167. let serverUrl = self.appDelegate.activeUrl + "/" + k_url_acces_remote_notification_api + "/" + String(notification!.idNotification)
  168. OCNetworking.sharedManager().setNotificationWithAccount(self.appDelegate.activeAccount, serverUrl: serverUrl, type: "DELETE", completion: { (account, message, errorCode) in
  169. if errorCode == 0 && account == self.appDelegate.activeAccount {
  170. let listOfNotifications = self.appDelegate.listOfNotifications as NSArray as! [OCNotifications]
  171. if let index = listOfNotifications.firstIndex(where: {$0.idNotification == notification!.idNotification}) {
  172. self.appDelegate.listOfNotifications.removeObject(at: index)
  173. }
  174. self.reloadDatasource()
  175. if self.appDelegate.listOfNotifications.count == 0 {
  176. self.viewClose()
  177. }
  178. } else if errorCode != 0 {
  179. NCContentPresenter.shared.messageNotification("_error_", description: message, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  180. } else {
  181. print("[LOG] It has been changed user during networking process, error.")
  182. }
  183. })
  184. }
  185. func tapAction(with notification: OCNotifications?, label: String) {
  186. for action in notification!.actions {
  187. if (action as! OCNotificationsAction).label == label {
  188. OCNetworking.sharedManager().setNotificationWithAccount(self.appDelegate.activeAccount, serverUrl: (action as! OCNotificationsAction).link, type: (action as! OCNotificationsAction).type, completion: { (account, message, errorCode) in
  189. if errorCode == 0 && account == self.appDelegate.activeAccount {
  190. let listOfNotifications = self.appDelegate.listOfNotifications as NSArray as! [OCNotifications]
  191. if let index = listOfNotifications.firstIndex(where: {$0.idNotification == notification!.idNotification}) {
  192. self.appDelegate.listOfNotifications.removeObject(at: index)
  193. }
  194. self.reloadDatasource()
  195. if self.appDelegate.listOfNotifications.count == 0 {
  196. self.viewClose()
  197. }
  198. } else if errorCode != 0 {
  199. NCContentPresenter.shared.messageNotification("_error_", description: message, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  200. } else {
  201. print("[LOG] It has been changed user during networking process, error.")
  202. }
  203. })
  204. }
  205. }
  206. }
  207. }
  208. // MARK: - Class UITableViewCell
  209. class CCNotificationCell: UITableViewCell {
  210. var delegate: CCNotificationCelllDelegate?
  211. var notification: OCNotifications?
  212. @IBOutlet weak var icon : UIImageView!
  213. @IBOutlet weak var avatar : UIImageView!
  214. @IBOutlet weak var date: UILabel!
  215. @IBOutlet weak var subject: UILabel!
  216. @IBOutlet weak var message: UILabel!
  217. @IBOutlet weak var remove: UIButton!
  218. @IBOutlet weak var primary: UIButton!
  219. @IBOutlet weak var secondary: UIButton!
  220. @IBOutlet weak var avatarLeadingMargin: NSLayoutConstraint!
  221. @IBOutlet weak var messageBottomMargin: NSLayoutConstraint!
  222. @IBOutlet weak var primaryWidth: NSLayoutConstraint!
  223. @IBOutlet weak var secondaryWidth: NSLayoutConstraint!
  224. override func awakeFromNib() {
  225. super.awakeFromNib()
  226. }
  227. @IBAction func touchUpInsideRemove(_ sender: Any) {
  228. delegate?.tapRemove(with: notification)
  229. }
  230. @IBAction func touchUpInsidePrimary(_ sender: Any) {
  231. let button = sender as! UIButton
  232. delegate?.tapAction(with: notification, label: button.titleLabel!.text!)
  233. }
  234. @IBAction func touchUpInsideSecondary(_ sender: Any) {
  235. let button = sender as! UIButton
  236. delegate?.tapAction(with: notification, label: button.titleLabel!.text!)
  237. }
  238. }
  239. protocol CCNotificationCelllDelegate {
  240. func tapRemove(with notification: OCNotifications?)
  241. func tapAction(with notification: OCNotifications?, label: String)
  242. }