CCNotification.swift 14 KB

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