CCNotification.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. //
  2. // CCNotification.swift
  3. // Nextcloud iOS
  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.seperator
  35. self.tableView.tableFooterView = UIView()
  36. self.tableView.rowHeight = UITableView.automaticDimension
  37. self.tableView.estimatedRowHeight = 50.0
  38. self.tableView.allowsSelection = false
  39. // Register to receive notification reload data
  40. NotificationCenter.default.addObserver(self, selector: #selector(self.reloadDatasource), name: Notification.Name("notificationReloadData"), object: nil)
  41. reloadDatasource()
  42. }
  43. override func didReceiveMemoryWarning() {
  44. super.didReceiveMemoryWarning()
  45. }
  46. @objc func viewClose() {
  47. // Stop listening notification reload data
  48. NotificationCenter.default.removeObserver(self, name: Notification.Name("notificationReloadData"), object: nil);
  49. self.dismiss(animated: true, completion: nil)
  50. }
  51. // MARK: - Table
  52. @objc func reloadDatasource() {
  53. self.tableView.reloadData()
  54. }
  55. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  56. let numRecord = appDelegate.listOfNotifications.count
  57. return numRecord
  58. }
  59. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  60. let cell = self.tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CCNotificationCell
  61. cell.delegate = self
  62. let notification = appDelegate.listOfNotifications.object(at: indexPath.row) as! OCNotifications
  63. let urlIcon = URL(string: notification.icon)
  64. var image : UIImage?
  65. if let urlIcon = urlIcon {
  66. let pathFileName = CCUtility.getDirectoryUserData() + "/" + urlIcon.deletingPathExtension().lastPathComponent + ".png"
  67. image = UIImage(contentsOfFile: pathFileName)
  68. }
  69. if let image = image {
  70. cell.icon.image = CCGraphics.changeThemingColorImage(image, multiplier: 2, color: NCBrandColor.sharedInstance.brandElement)
  71. } else {
  72. cell.icon.image = CCGraphics.changeThemingColorImage(#imageLiteral(resourceName: "notification"), multiplier:2, color: NCBrandColor.sharedInstance.brandElement)
  73. }
  74. //
  75. //cell.date.text = DateFormatter.localizedString(from: notification.date, dateStyle: .medium, timeStyle: .medium)
  76. //
  77. cell.notification = notification
  78. cell.date.text = CCUtility.dateDiff(notification.date)
  79. cell.date.textColor = .gray
  80. cell.subject.text = notification.subject
  81. cell.subject.textColor = .black
  82. cell.message.text = notification.message.replacingOccurrences(of: "<br />", with: "\n")
  83. cell.message.textColor = .gray
  84. cell.remove.setImage(CCGraphics.changeThemingColorImage(UIImage(named: "exit")!, width: 40, height: 40, color: UIColor.gray), for: .normal)
  85. cell.primary.titleLabel?.font = .systemFont(ofSize: 14)
  86. cell.primary.setTitleColor(.white, for: .normal)
  87. cell.primary.layer.cornerRadius = 15
  88. cell.primary.layer.masksToBounds = true
  89. cell.primary.layer.backgroundColor = NCBrandColor.sharedInstance.brand.cgColor
  90. cell.secondary.titleLabel?.font = .systemFont(ofSize: 14)
  91. cell.secondary.setTitleColor(.gray, for: .normal)
  92. cell.secondary.layer.cornerRadius = 15
  93. cell.secondary.layer.masksToBounds = true
  94. cell.secondary.layer.backgroundColor = NCBrandColor.sharedInstance.graySoft.withAlphaComponent(0.1).cgColor
  95. cell.secondary.layer.borderWidth = 0.3
  96. cell.secondary.layer.borderColor = UIColor.gray.cgColor
  97. // Action
  98. if notification.actions.count == 0 {
  99. cell.primary.isEnabled = false
  100. cell.primary.isHidden = true
  101. cell.secondary.isEnabled = false
  102. cell.secondary.isHidden = true
  103. cell.messageBottomMargin.constant = 10
  104. } else {
  105. for action in notification.actions {
  106. let label = (action as! OCNotificationsAction).label
  107. let primary = (action as! OCNotificationsAction).primary
  108. if primary {
  109. cell.primary.setTitle(label, for: .normal)
  110. } else {
  111. cell.secondary.setTitle(label, for: .normal)
  112. }
  113. }
  114. cell.messageBottomMargin.constant = 40
  115. }
  116. return cell
  117. }
  118. // MARK: tap Action
  119. func tapRemove(with notification: OCNotifications?) {
  120. let serverUrl = self.appDelegate.activeUrl + "/" + k_url_acces_remote_notification_api + "/" + String(notification!.idNotification)
  121. OCNetworking.sharedManager().setNotificationWithAccount(self.appDelegate.activeAccount, serverUrl: serverUrl, type: "DELETE", completion: { (account, message, errorCode) in
  122. if errorCode == 0 && account == self.appDelegate.activeAccount {
  123. let listOfNotifications = self.appDelegate.listOfNotifications as NSArray as! [OCNotifications]
  124. if let index = listOfNotifications.firstIndex(where: {$0.idNotification == notification!.idNotification}) {
  125. self.appDelegate.listOfNotifications.removeObject(at: index)
  126. }
  127. self.reloadDatasource()
  128. if self.appDelegate.listOfNotifications.count == 0 {
  129. self.viewClose()
  130. }
  131. } else if errorCode != 0 {
  132. self.appDelegate.messageNotification("_error_", description: message, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  133. } else {
  134. print("[LOG] It has been changed user during networking process, error.")
  135. }
  136. })
  137. }
  138. func tapAction(with notification: OCNotifications?, label: String) {
  139. for action in notification!.actions {
  140. if (action as! OCNotificationsAction).label == label {
  141. OCNetworking.sharedManager().setNotificationWithAccount(self.appDelegate.activeAccount, serverUrl: (action as! OCNotificationsAction).link, type: (action as! OCNotificationsAction).type, completion: { (account, message, errorCode) in
  142. if errorCode == 0 && account == self.appDelegate.activeAccount {
  143. let listOfNotifications = self.appDelegate.listOfNotifications as NSArray as! [OCNotifications]
  144. if let index = listOfNotifications.firstIndex(where: {$0.idNotification == notification!.idNotification}) {
  145. self.appDelegate.listOfNotifications.removeObject(at: index)
  146. }
  147. self.reloadDatasource()
  148. if self.appDelegate.listOfNotifications.count == 0 {
  149. self.viewClose()
  150. }
  151. } else if errorCode != 0 {
  152. self.appDelegate.messageNotification("_error_", description: message, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  153. } else {
  154. print("[LOG] It has been changed user during networking process, error.")
  155. }
  156. })
  157. }
  158. }
  159. }
  160. }
  161. // MARK: - Class UITableViewCell
  162. class CCNotificationCell: UITableViewCell {
  163. var delegate: CCNotificationCelllDelegate?
  164. var notification: OCNotifications?
  165. @IBOutlet weak var icon : UIImageView!
  166. @IBOutlet weak var date: UILabel!
  167. @IBOutlet weak var subject: UILabel!
  168. @IBOutlet weak var message: UILabel!
  169. @IBOutlet weak var remove: UIButton!
  170. @IBOutlet weak var primary: UIButton!
  171. @IBOutlet weak var secondary: UIButton!
  172. @IBOutlet weak var messageBottomMargin: NSLayoutConstraint!
  173. override func awakeFromNib() {
  174. super.awakeFromNib()
  175. }
  176. @IBAction func touchUpInsideRemove(_ sender: Any) {
  177. delegate?.tapRemove(with: notification)
  178. }
  179. @IBAction func touchUpInsidePrimary(_ sender: Any) {
  180. let button = sender as! UIButton
  181. delegate?.tapAction(with: notification, label: button.titleLabel!.text!)
  182. }
  183. @IBAction func touchUpInsideSecondary(_ sender: Any) {
  184. let button = sender as! UIButton
  185. delegate?.tapAction(with: notification, label: button.titleLabel!.text!)
  186. }
  187. }
  188. protocol CCNotificationCelllDelegate {
  189. func tapRemove(with notification: OCNotifications?)
  190. func tapAction(with notification: OCNotifications?, label: String)
  191. }