CCNotification.swift 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 {
  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(close)), animated: true)
  31. self.tableView.reloadData()
  32. }
  33. override func didReceiveMemoryWarning() {
  34. super.didReceiveMemoryWarning()
  35. }
  36. func close() {
  37. self.dismiss(animated: true) {
  38. }
  39. }
  40. override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
  41. return true
  42. }
  43. override func tableView(_ tableView: UITableView, editActionsForRowAt: IndexPath) -> [UITableViewRowAction]? {
  44. let idsNotification: [String] = appDelegate.listOfNotifications.allKeys as! [String]
  45. let idNotification : String! = idsNotification[editActionsForRowAt.row]
  46. let notification = appDelegate.listOfNotifications[idNotification] as! OCNotifications
  47. // No Action request
  48. if notification.actions.count == 0 {
  49. let delete = UITableViewRowAction(style: .normal, title: NSLocalizedString("_delete_", comment: "")) { action, index in
  50. print("delete button tapped")
  51. }
  52. delete.backgroundColor = .red
  53. return [delete]
  54. } else {
  55. // Action request
  56. var buttons = [UITableViewRowAction]()
  57. for action in notification.actions {
  58. let button = UITableViewRowAction(style: .normal, title: (action as! OCNotificationsAction).label) { action, index in
  59. for actionNotification in notification.actions {
  60. if (actionNotification as! OCNotificationsAction).label == action.title {
  61. print(action.title!)
  62. }
  63. }
  64. }
  65. if (action as! OCNotificationsAction).type == "DELETE" {
  66. button.backgroundColor = .red
  67. } else {
  68. button.backgroundColor = .green
  69. }
  70. buttons.append(button)
  71. }
  72. return buttons
  73. }
  74. }
  75. override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  76. return 120
  77. }
  78. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  79. if self.resultSearchController.isActive {
  80. return 0
  81. } else {
  82. let numRecord = appDelegate.listOfNotifications.count
  83. return numRecord
  84. }
  85. }
  86. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  87. let cell = self.tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CCNotificationCell
  88. if self.resultSearchController.isActive {
  89. } else {
  90. let idsNotification: [String] = appDelegate.listOfNotifications.allKeys as! [String]
  91. let idNotification : String! = idsNotification[indexPath.row]
  92. let notification = appDelegate.listOfNotifications[idNotification] as! OCNotifications
  93. let urlIcon = URL(string: notification.icon)!
  94. let pathFileName = (appDelegate.directoryUser) + "/" + urlIcon.lastPathComponent
  95. let image = UIImage(contentsOfFile: pathFileName)
  96. if image == nil {
  97. //downloadImage(url: urlIcon)
  98. } else {
  99. cell.icon.image = image
  100. }
  101. cell.date.text = DateFormatter.localizedString(from: notification.date, dateStyle: .medium, timeStyle: .medium)
  102. cell.subject.text = notification.subject
  103. cell.message.text = notification.message
  104. }
  105. return cell
  106. }
  107. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  108. tableView.deselectRow(at: indexPath, animated: true)
  109. }
  110. // MARK: - Get Image from url
  111. func getDataFromUrl(url: URL, completion: @escaping (_ data: Data?, _ response: URLResponse?, _ error: Error?) -> Void) {
  112. URLSession.shared.dataTask(with: url) {
  113. (data, response, error) in
  114. completion(data, response, error)
  115. }.resume()
  116. }
  117. func downloadImage(url: URL) {
  118. print("Download Started")
  119. getDataFromUrl(url: url) { (data, response, error) in
  120. guard let data = data, error == nil else { return }
  121. let fileName = response?.suggestedFilename ?? url.lastPathComponent
  122. print("Download Finished")
  123. DispatchQueue.main.async() { () -> Void in
  124. do {
  125. let pathFileName = (self.appDelegate.directoryUser) + "/" + fileName
  126. try data.write(to: URL(fileURLWithPath: pathFileName), options: .atomic)
  127. self.tableView.reloadData()
  128. } catch {
  129. print(error)
  130. }
  131. }
  132. }
  133. }
  134. }
  135. // MARK: - Class UITableViewCell
  136. class CCNotificationCell: UITableViewCell {
  137. @IBOutlet weak var icon : UIImageView!
  138. @IBOutlet weak var date: UILabel!
  139. @IBOutlet weak var subject: UILabel!
  140. @IBOutlet weak var message: UILabel!
  141. }