CCNotification.swift 7.1 KB

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