CCNotification.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 {
  25. var resultSearchController = UISearchController()
  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.navigationController?.navigationBar.barTintColor = NCBrandColor.sharedInstance.brand
  31. self.navigationController?.navigationBar.tintColor = NCBrandColor.sharedInstance.brandText
  32. self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: NCBrandColor.sharedInstance.brandText]
  33. self.navigationController?.navigationBar.isTranslucent = false
  34. self.navigationItem.setRightBarButton(UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(viewClose)), animated: true)
  35. self.tableView.separatorColor = NCBrandColor.sharedInstance.seperator
  36. self.tableView.tableFooterView = UIView()
  37. self.tableView.rowHeight = UITableView.automaticDimension
  38. self.tableView.estimatedRowHeight = 50.0
  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, canEditRowAt indexPath: IndexPath) -> Bool {
  56. return true
  57. }
  58. override func tableView(_ tableView: UITableView, editActionsForRowAt: IndexPath) -> [UITableViewRowAction]? {
  59. let notification = appDelegate.listOfNotifications.object(at: editActionsForRowAt.row) as! OCNotifications
  60. // No Action request
  61. if notification.actions.count == 0 {
  62. let remove = UITableViewRowAction(style: .normal, title: NSLocalizedString("_remove_", comment: "")) { action, index in
  63. tableView.setEditing(false, animated: true)
  64. let ocNetworking = OCnetworking.init()
  65. ocNetworking.setNotificationWithAccount(self.appDelegate.activeAccount, serverUrl: "\(self.appDelegate.activeUrl!)/\(k_url_acces_remote_notification_api)/\(notification.idNotification)", type: "DELETE", completion: { (account, message, errorCode) in
  66. if (errorCode == 0 && account! == self.appDelegate.activeAccount) {
  67. let listOfNotifications = self.appDelegate.listOfNotifications as NSArray as! [OCNotifications]
  68. if let index = listOfNotifications.index(where: {$0.idNotification == notification.idNotification}) {
  69. self.appDelegate.listOfNotifications.removeObject(at: index)
  70. }
  71. self.reloadDatasource()
  72. if self.appDelegate.listOfNotifications.count == 0 {
  73. self.viewClose()
  74. }
  75. } else {
  76. if (errorCode != 0) {
  77. self.appDelegate.messageNotification("_error_", description: message, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  78. }
  79. }
  80. })
  81. }
  82. remove.backgroundColor = .red
  83. return [remove]
  84. } else {
  85. // Action request
  86. var buttons = [UITableViewRowAction]()
  87. for action in notification.actions {
  88. let button = UITableViewRowAction(style: .normal, title: (action as! OCNotificationsAction).label) { action, index in
  89. for actionNotification in notification.actions {
  90. if (actionNotification as! OCNotificationsAction).label == action.title {
  91. tableView.setEditing(false, animated: true)
  92. let ocNetworking = OCnetworking.init()
  93. ocNetworking.setNotificationWithAccount(self.appDelegate.activeAccount, serverUrl: (actionNotification as! OCNotificationsAction).link, type: (actionNotification as! OCNotificationsAction).type, completion: { (account, message, errorCode) in
  94. if (errorCode == 0 && account! == self.appDelegate.activeAccount) {
  95. let listOfNotifications = self.appDelegate.listOfNotifications as NSArray as! [OCNotifications]
  96. if let index = listOfNotifications.index(where: {$0.idNotification == notification.idNotification}) {
  97. self.appDelegate.listOfNotifications.removeObject(at: index)
  98. }
  99. self.reloadDatasource()
  100. if self.appDelegate.listOfNotifications.count == 0 {
  101. self.viewClose()
  102. }
  103. } else {
  104. if (errorCode != 0) {
  105. self.appDelegate.messageNotification("_error_", description: message, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  106. }
  107. }
  108. })
  109. }
  110. }
  111. }
  112. if (action as! OCNotificationsAction).type == "DELETE" {
  113. button.backgroundColor = .red
  114. } else {
  115. button.backgroundColor = .green
  116. }
  117. buttons.append(button)
  118. }
  119. return buttons
  120. }
  121. }
  122. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  123. if self.resultSearchController.isActive {
  124. return 0
  125. } else {
  126. let numRecord = appDelegate.listOfNotifications.count
  127. return numRecord
  128. }
  129. }
  130. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  131. let cell = self.tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CCNotificationCell
  132. let selectionColor : UIView = UIView.init()
  133. selectionColor.backgroundColor = NCBrandColor.sharedInstance.getColorSelectBackgrond()
  134. cell.selectedBackgroundView = selectionColor
  135. if self.resultSearchController.isActive {
  136. } else {
  137. let notification = appDelegate.listOfNotifications.object(at: indexPath.row) as! OCNotifications
  138. let urlIcon = URL(string: notification.icon)
  139. var image : UIImage?
  140. if let urlIcon = urlIcon {
  141. let pathFileName = CCUtility.getDirectoryUserData() + "/" + urlIcon.deletingPathExtension().lastPathComponent + ".png"
  142. image = UIImage(contentsOfFile: pathFileName)
  143. }
  144. if let image = image {
  145. cell.icon.image = CCGraphics.changeThemingColorImage(image, multiplier: 2, color: NCBrandColor.sharedInstance.brandElement)
  146. } else {
  147. cell.icon.image = CCGraphics.changeThemingColorImage(#imageLiteral(resourceName: "notification"), multiplier:2, color: NCBrandColor.sharedInstance.brandElement)
  148. }
  149. //
  150. //cell.date.text = DateFormatter.localizedString(from: notification.date, dateStyle: .medium, timeStyle: .medium)
  151. //
  152. cell.date.text = CCUtility.dateDiff(notification.date)
  153. cell.subject.text = notification.subject
  154. cell.message.text = notification.message
  155. }
  156. return cell
  157. }
  158. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  159. tableView.deselectRow(at: indexPath, animated: true)
  160. }
  161. // MARK: - Get Image from url
  162. func getDataFromUrl(url: URL, completion: @escaping (_ data: Data?, _ response: URLResponse?, _ error: Error?) -> Void) {
  163. URLSession.shared.dataTask(with: url) {
  164. (data, response, error) in
  165. completion(data, response, error)
  166. }.resume()
  167. }
  168. func downloadImage(url: URL) {
  169. print("Download Started")
  170. getDataFromUrl(url: url) { (data, response, error) in
  171. guard let data = data, error == nil else { return }
  172. let fileName = response?.suggestedFilename ?? url.lastPathComponent
  173. print("Download Finished")
  174. DispatchQueue.main.async() { () -> Void in
  175. do {
  176. let pathFileName = CCUtility.getDirectoryUserData() + "/" + fileName
  177. try data.write(to: URL(fileURLWithPath: pathFileName), options: .atomic)
  178. self.reloadDatasource()
  179. } catch {
  180. print(error)
  181. }
  182. }
  183. }
  184. }
  185. }
  186. // MARK: - Class UITableViewCell
  187. class CCNotificationCell: UITableViewCell {
  188. @IBOutlet weak var icon : UIImageView!
  189. @IBOutlet weak var date: UILabel!
  190. @IBOutlet weak var subject: UILabel!
  191. @IBOutlet weak var message: UILabel!
  192. }