CCNotification.swift 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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.navigationItem.setRightBarButton(UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(viewClose)), animated: true)
  31. self.tableView.separatorColor = UIColor(colorLiteralRed: 153.0/255.0, green: 153.0/255.0, blue: 153.0/255.0, alpha: 0.2)
  32. self.tableView.tableFooterView = UIView()
  33. self.tableView.reloadData()
  34. }
  35. override func didReceiveMemoryWarning() {
  36. super.didReceiveMemoryWarning()
  37. }
  38. func viewClose() {
  39. self.dismiss(animated: true) {
  40. }
  41. }
  42. // MARK: - Table
  43. func getSataSourceAt(indexPath: IndexPath) -> OCNotifications {
  44. let notification = appDelegate.listOfNotifications.object(at: indexPath.row) as! OCNotifications
  45. return notification
  46. }
  47. override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
  48. return true
  49. }
  50. override func tableView(_ tableView: UITableView, editActionsForRowAt: IndexPath) -> [UITableViewRowAction]? {
  51. let notification = self.getSataSourceAt(indexPath: editActionsForRowAt)
  52. // No Action request
  53. if notification.actions.count == 0 {
  54. let delete = UITableViewRowAction(style: .normal, title: NSLocalizedString("_delete_", comment: "")) { action, index in
  55. tableView.setEditing(false, animated: true)
  56. let metadataNet = CCMetadataNet.init(account: self.appDelegate.activeAccount)!
  57. metadataNet.action = actionSetNotificationServer
  58. metadataNet.identifier = "\(notification.idNotification)"
  59. metadataNet.options = "DELETE"
  60. metadataNet.serverUrl = "\(self.appDelegate.activeUrl!)/\(k_url_acces_remote_notification_api)/\(metadataNet.identifier!)"
  61. self.appDelegate.addNetworkingOperationQueue(self.appDelegate.netQueue, delegate: self, metadataNet: metadataNet)
  62. }
  63. delete.backgroundColor = CCConstant.GlobalConstants.kColor_cryptocloud
  64. return [delete]
  65. } else {
  66. // Action request
  67. var buttons = [UITableViewRowAction]()
  68. for action in notification.actions {
  69. let button = UITableViewRowAction(style: .normal, title: (action as! OCNotificationsAction).label) { action, index in
  70. for actionNotification in notification.actions {
  71. if (actionNotification as! OCNotificationsAction).label == action.title {
  72. tableView.setEditing(false, animated: true)
  73. let metadataNet = CCMetadataNet.init(account: self.appDelegate.activeAccount)!
  74. metadataNet.action = actionSetNotificationServer
  75. metadataNet.identifier = "\(notification.idNotification)"
  76. metadataNet.serverUrl = (actionNotification as! OCNotificationsAction).link
  77. metadataNet.options = (actionNotification as! OCNotificationsAction).type
  78. self.appDelegate.addNetworkingOperationQueue(self.appDelegate.netQueue, delegate: self, metadataNet: metadataNet)
  79. }
  80. }
  81. }
  82. if (action as! OCNotificationsAction).type == "DELETE" {
  83. button.backgroundColor = .red
  84. } else {
  85. button.backgroundColor = .green
  86. }
  87. buttons.append(button)
  88. }
  89. return buttons
  90. }
  91. }
  92. override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  93. let notification = self.getSataSourceAt(indexPath: indexPath)
  94. if notification.message.characters.count > 0 {
  95. return 120
  96. } else {
  97. return 80
  98. }
  99. }
  100. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  101. if self.resultSearchController.isActive {
  102. return 0
  103. } else {
  104. let numRecord = appDelegate.listOfNotifications.count
  105. return numRecord
  106. }
  107. }
  108. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  109. let cell = self.tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CCNotificationCell
  110. if self.resultSearchController.isActive {
  111. } else {
  112. let notification = self.getSataSourceAt(indexPath: indexPath)
  113. let urlIcon = URL(string: notification.icon)!
  114. let pathFileName = (appDelegate.directoryUser) + "/" + urlIcon.lastPathComponent
  115. let image = UIImage(contentsOfFile: pathFileName)
  116. if image == nil {
  117. //downloadImage(url: urlIcon)
  118. } else {
  119. cell.icon.image = image
  120. }
  121. cell.date.text = DateFormatter.localizedString(from: notification.date, dateStyle: .medium, timeStyle: .medium)
  122. cell.subject.text = notification.subject
  123. cell.message.text = notification.message
  124. }
  125. return cell
  126. }
  127. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  128. tableView.deselectRow(at: indexPath, animated: true)
  129. }
  130. // MARK: - Networking delegate
  131. func setNotificationServerFailure(_ metadataNet: CCMetadataNet!, message: String!, errorCode: Int) {
  132. appDelegate.messageNotification("_error_", description: message, visible: true, delay: TimeInterval(dismissAfterSecond), type: TWMessageBarMessageType.error)
  133. }
  134. func setNotificationServerSuccess(_ metadataNet: CCMetadataNet!) {
  135. let listOfNotifications = appDelegate.listOfNotifications as NSArray as! [OCNotifications]
  136. if let index = listOfNotifications.index(where: {$0.idNotification == Int(metadataNet.identifier)}) {
  137. appDelegate.listOfNotifications.removeObject(at: index)
  138. }
  139. self.tableView.reloadData()
  140. if appDelegate.listOfNotifications.count == 0 {
  141. viewClose()
  142. }
  143. }
  144. // MARK: - Get Image from url
  145. func getDataFromUrl(url: URL, completion: @escaping (_ data: Data?, _ response: URLResponse?, _ error: Error?) -> Void) {
  146. URLSession.shared.dataTask(with: url) {
  147. (data, response, error) in
  148. completion(data, response, error)
  149. }.resume()
  150. }
  151. func downloadImage(url: URL) {
  152. print("Download Started")
  153. getDataFromUrl(url: url) { (data, response, error) in
  154. guard let data = data, error == nil else { return }
  155. let fileName = response?.suggestedFilename ?? url.lastPathComponent
  156. print("Download Finished")
  157. DispatchQueue.main.async() { () -> Void in
  158. do {
  159. let pathFileName = (self.appDelegate.directoryUser) + "/" + fileName
  160. try data.write(to: URL(fileURLWithPath: pathFileName), options: .atomic)
  161. self.tableView.reloadData()
  162. } catch {
  163. print(error)
  164. }
  165. }
  166. }
  167. }
  168. }
  169. // MARK: - Class UITableViewCell
  170. class CCNotificationCell: UITableViewCell {
  171. @IBOutlet weak var icon : UIImageView!
  172. @IBOutlet weak var date: UILabel!
  173. @IBOutlet weak var subject: UILabel!
  174. @IBOutlet weak var message: UILabel!
  175. }