CCNotification.swift 14 KB

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