CCNotification.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. //
  2. // CCNotification.swift
  3. // Crypto Cloud Technology Nextcloud
  4. //
  5. // Created by Marino Faggiana on 27/01/17.
  6. // Copyright (c) 2017 TWS. All rights reserved.
  7. //
  8. // Author Marino Faggiana <m.faggiana@twsweb.it>
  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, OCNetworkingDelegate {
  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.navigationBarText
  32. self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: NCBrandColor.sharedInstance.navigationBarText]
  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. // Register to receive notification reload data
  37. NotificationCenter.default.addObserver(self, selector: #selector(self.tableView.reloadData), name: Notification.Name("notificationReloadData"), object: nil)
  38. self.tableView.reloadData()
  39. }
  40. override func didReceiveMemoryWarning() {
  41. super.didReceiveMemoryWarning()
  42. }
  43. func viewClose() {
  44. // Stop listening notification reload data
  45. NotificationCenter.default.removeObserver(self, name: Notification.Name("notificationReloadData"), object: nil);
  46. self.dismiss(animated: true, completion: nil)
  47. }
  48. // MARK: - Table
  49. override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
  50. return true
  51. }
  52. override func tableView(_ tableView: UITableView, editActionsForRowAt: IndexPath) -> [UITableViewRowAction]? {
  53. let notification = appDelegate.listOfNotifications.object(at: editActionsForRowAt.row) as! OCNotifications
  54. // No Action request
  55. if notification.actions.count == 0 {
  56. let remove = UITableViewRowAction(style: .normal, title: NSLocalizedString("_remove_", comment: "")) { action, index in
  57. tableView.setEditing(false, animated: true)
  58. let metadataNet = CCMetadataNet.init(account: self.appDelegate.activeAccount)!
  59. metadataNet.action = actionSetNotificationServer
  60. metadataNet.assetLocalIdentifier = "\(notification.idNotification)"
  61. metadataNet.options = "DELETE"
  62. metadataNet.serverUrl = "\(self.appDelegate.activeUrl!)/\(k_url_acces_remote_notification_api)/\(metadataNet.assetLocalIdentifier!)"
  63. self.appDelegate.addNetworkingOperationQueue(self.appDelegate.netQueue, delegate: self, metadataNet: metadataNet)
  64. }
  65. remove.backgroundColor = .red
  66. return [remove]
  67. } else {
  68. // Action request
  69. var buttons = [UITableViewRowAction]()
  70. for action in notification.actions {
  71. let button = UITableViewRowAction(style: .normal, title: (action as! OCNotificationsAction).label) { action, index in
  72. for actionNotification in notification.actions {
  73. if (actionNotification as! OCNotificationsAction).label == action.title {
  74. tableView.setEditing(false, animated: true)
  75. let metadataNet = CCMetadataNet.init(account: self.appDelegate.activeAccount)!
  76. metadataNet.action = actionSetNotificationServer
  77. metadataNet.assetLocalIdentifier = "\(notification.idNotification)"
  78. metadataNet.serverUrl = (actionNotification as! OCNotificationsAction).link
  79. metadataNet.options = (actionNotification as! OCNotificationsAction).type
  80. self.appDelegate.addNetworkingOperationQueue(self.appDelegate.netQueue, delegate: self, metadataNet: metadataNet)
  81. }
  82. }
  83. }
  84. if (action as! OCNotificationsAction).type == "DELETE" {
  85. button.backgroundColor = .red
  86. } else {
  87. button.backgroundColor = .green
  88. }
  89. buttons.append(button)
  90. }
  91. return buttons
  92. }
  93. }
  94. override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  95. let notification = appDelegate.listOfNotifications.object(at: indexPath.row) as! OCNotifications
  96. if notification.message.characters.count > 0 {
  97. return 160
  98. } else {
  99. return 120
  100. }
  101. }
  102. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  103. if self.resultSearchController.isActive {
  104. return 0
  105. } else {
  106. let numRecord = appDelegate.listOfNotifications.count
  107. return numRecord
  108. }
  109. }
  110. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  111. let cell = self.tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CCNotificationCell
  112. let selectionColor : UIView = UIView.init()
  113. selectionColor.backgroundColor = NCBrandColor.sharedInstance.getColorSelectBackgrond()
  114. cell.selectedBackgroundView = selectionColor
  115. if self.resultSearchController.isActive {
  116. } else {
  117. let notification = appDelegate.listOfNotifications.object(at: indexPath.row) as! OCNotifications
  118. let urlIcon = URL(string: notification.icon)
  119. var image : UIImage?
  120. if urlIcon != nil {
  121. let pathFileName = (appDelegate.directoryUser) + "/" + (urlIcon?.lastPathComponent)!
  122. image = UIImage(contentsOfFile: pathFileName)
  123. }
  124. if image == nil {
  125. cell.icon.image = CCGraphics.changeThemingColorImage(#imageLiteral(resourceName: "notification"), color: NCBrandColor.sharedInstance.brand)
  126. //downloadImage(url: urlIcon)
  127. } else {
  128. cell.icon.image = image
  129. }
  130. //
  131. //cell.date.text = DateFormatter.localizedString(from: notification.date, dateStyle: .medium, timeStyle: .medium)
  132. //
  133. cell.date.text = CCUtility.dateDiff(notification.date)
  134. cell.subject.text = notification.subject
  135. cell.message.text = notification.message
  136. }
  137. return cell
  138. }
  139. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  140. tableView.deselectRow(at: indexPath, animated: true)
  141. }
  142. // MARK: - Networking delegate
  143. func setNotificationServerFailure(_ metadataNet: CCMetadataNet!, message: String!, errorCode: Int) {
  144. appDelegate.messageNotification("_error_", description: message, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  145. }
  146. func setNotificationServerSuccess(_ metadataNet: CCMetadataNet!) {
  147. let listOfNotifications = appDelegate.listOfNotifications as NSArray as! [OCNotifications]
  148. if let index = listOfNotifications.index(where: {$0.idNotification == Int(metadataNet.assetLocalIdentifier)}) {
  149. appDelegate.listOfNotifications.removeObject(at: index)
  150. }
  151. self.tableView.reloadData()
  152. if appDelegate.listOfNotifications.count == 0 {
  153. viewClose()
  154. }
  155. }
  156. // MARK: - Get Image from url
  157. func getDataFromUrl(url: URL, completion: @escaping (_ data: Data?, _ response: URLResponse?, _ error: Error?) -> Void) {
  158. URLSession.shared.dataTask(with: url) {
  159. (data, response, error) in
  160. completion(data, response, error)
  161. }.resume()
  162. }
  163. func downloadImage(url: URL) {
  164. print("Download Started")
  165. getDataFromUrl(url: url) { (data, response, error) in
  166. guard let data = data, error == nil else { return }
  167. let fileName = response?.suggestedFilename ?? url.lastPathComponent
  168. print("Download Finished")
  169. DispatchQueue.main.async() { () -> Void in
  170. do {
  171. let pathFileName = (self.appDelegate.directoryUser) + "/" + fileName
  172. try data.write(to: URL(fileURLWithPath: pathFileName), options: .atomic)
  173. self.tableView.reloadData()
  174. } catch {
  175. print(error)
  176. }
  177. }
  178. }
  179. }
  180. }
  181. // MARK: - Class UITableViewCell
  182. class CCNotificationCell: UITableViewCell {
  183. @IBOutlet weak var icon : UIImageView!
  184. @IBOutlet weak var date: UILabel!
  185. @IBOutlet weak var subject: UILabel!
  186. @IBOutlet weak var message: UILabel!
  187. }