123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400 |
- import UIKit
- class CCNotification: UITableViewController, CCNotificationCelllDelegate {
-
- var resultSearchController = UISearchController()
- let appDelegate = UIApplication.shared.delegate as! AppDelegate
-
- override func viewDidLoad() {
-
- super.viewDidLoad()
-
- self.navigationController?.navigationBar.topItem?.title = NSLocalizedString("_notification_", comment: "")
- self.navigationController?.navigationBar.barTintColor = NCBrandColor.sharedInstance.brand
- self.navigationController?.navigationBar.tintColor = NCBrandColor.sharedInstance.brandText
- self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: NCBrandColor.sharedInstance.brandText]
- self.navigationController?.navigationBar.isTranslucent = false
- self.navigationItem.setRightBarButton(UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(viewClose)), animated: true)
-
- self.tableView.separatorColor = NCBrandColor.sharedInstance.seperator
- self.tableView.tableFooterView = UIView()
- self.tableView.rowHeight = UITableView.automaticDimension
- self.tableView.estimatedRowHeight = 50.0
-
- NotificationCenter.default.addObserver(self, selector: #selector(self.reloadDatasource), name: Notification.Name("notificationReloadData"), object: nil)
- reloadDatasource()
- }
-
- override func didReceiveMemoryWarning() {
- super.didReceiveMemoryWarning()
- }
- @objc func viewClose() {
-
-
- NotificationCenter.default.removeObserver(self, name: Notification.Name("notificationReloadData"), object: nil);
-
- self.dismiss(animated: true, completion: nil)
- }
-
-
- @objc func reloadDatasource() {
- self.tableView.reloadData()
- }
-
-
-
- override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
-
- if self.resultSearchController.isActive {
- return 0
- } else {
- let numRecord = appDelegate.listOfNotifications.count
- return numRecord
- }
- }
-
- override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
-
- let cell = self.tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CCNotificationCell
-
- let selectionColor : UIView = UIView.init()
- selectionColor.backgroundColor = NCBrandColor.sharedInstance.getColorSelectBackgrond()
- cell.selectedBackgroundView = selectionColor
- cell.delegate = self
-
- if self.resultSearchController.isActive {
-
- } else {
-
- let notification = appDelegate.listOfNotifications.object(at: indexPath.row) as! OCNotifications
- let urlIcon = URL(string: notification.icon)
- var image : UIImage?
-
- if let urlIcon = urlIcon {
- let pathFileName = CCUtility.getDirectoryUserData() + "/" + urlIcon.deletingPathExtension().lastPathComponent + ".png"
- image = UIImage(contentsOfFile: pathFileName)
- }
-
- if let image = image {
- cell.icon.image = CCGraphics.changeThemingColorImage(image, multiplier: 2, color: NCBrandColor.sharedInstance.brandElement)
- } else {
- cell.icon.image = CCGraphics.changeThemingColorImage(#imageLiteral(resourceName: "notification"), multiplier:2, color: NCBrandColor.sharedInstance.brandElement)
- }
-
-
-
-
- cell.notification = notification
- cell.date.text = CCUtility.dateDiff(notification.date)
- cell.date.textColor = .gray
- cell.subject.text = notification.subject
- cell.subject.textColor = .black
- cell.message.text = notification.message.replacingOccurrences(of: "<br />", with: "\n")
- cell.message.textColor = .gray
-
- cell.remove.setImage(CCGraphics.changeThemingColorImage(UIImage(named: "exit")!, width: 40, height: 40, color: UIColor.gray), for: .normal)
-
- cell.primary.titleLabel?.font = .systemFont(ofSize: 14)
- cell.primary.setTitleColor(.white, for: .normal)
- cell.primary.layer.cornerRadius = 15
- cell.primary.layer.masksToBounds = true
- cell.primary.layer.backgroundColor = NCBrandColor.sharedInstance.brand.cgColor
-
- cell.secondary.titleLabel?.font = .systemFont(ofSize: 14)
- cell.secondary.setTitleColor(.gray, for: .normal)
- cell.secondary.layer.cornerRadius = 15
- cell.secondary.layer.masksToBounds = true
- cell.secondary.layer.backgroundColor = NCBrandColor.sharedInstance.graySoft.withAlphaComponent(0.1).cgColor
- cell.secondary.layer.borderWidth = 0.3
- cell.secondary.layer.borderColor = UIColor.gray.cgColor
-
-
- if notification.actions.count == 0 {
-
- cell.primary.isEnabled = false
- cell.primary.isHidden = true
- cell.secondary.isEnabled = false
- cell.secondary.isHidden = true
- cell.messageBottomMargin.constant = 10
-
- } else {
-
- for action in notification.actions {
-
- let label = (action as! OCNotificationsAction).label
- let primary = (action as! OCNotificationsAction).primary
-
- if primary {
- cell.primary.setTitle(label, for: .normal)
- } else {
- cell.secondary.setTitle(label, for: .normal)
- }
- }
-
- cell.messageBottomMargin.constant = 40
- }
- }
-
- return cell
- }
-
- override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
-
- tableView.deselectRow(at: indexPath, animated: true)
- }
-
-
-
- func getDataFromUrl(url: URL, completion: @escaping (_ data: Data?, _ response: URLResponse?, _ error: Error?) -> Void) {
- URLSession.shared.dataTask(with: url) {
- (data, response, error) in
- completion(data, response, error)
- }.resume()
- }
-
- func downloadImage(url: URL) {
-
- print("Download Started")
- getDataFromUrl(url: url) { (data, response, error) in
- guard let data = data, error == nil else { return }
- let fileName = response?.suggestedFilename ?? url.lastPathComponent
- print("Download Finished")
- DispatchQueue.main.async() { () -> Void in
-
- do {
- let pathFileName = CCUtility.getDirectoryUserData() + "/" + fileName
- try data.write(to: URL(fileURLWithPath: pathFileName), options: .atomic)
-
- self.reloadDatasource()
- } catch {
- print(error)
- }
- }
- }
- }
-
-
-
- func tapRemove(with notification: OCNotifications?) {
-
- OCNetworking.sharedManager().setNotificationWithAccount(self.appDelegate.activeAccount, serverUrl: "\(self.appDelegate.activeUrl!)/\(k_url_acces_remote_notification_api)/\(notification!.idNotification)", type: "DELETE", completion: { (account, message, errorCode) in
-
- if errorCode == 0 && account == self.appDelegate.activeAccount {
-
- let listOfNotifications = self.appDelegate.listOfNotifications as NSArray as! [OCNotifications]
-
- if let index = listOfNotifications.firstIndex(where: {$0.idNotification == notification!.idNotification}) {
- self.appDelegate.listOfNotifications.removeObject(at: index)
- }
-
- self.reloadDatasource()
-
- if self.appDelegate.listOfNotifications.count == 0 {
- self.viewClose()
- }
-
- } else if errorCode != 0 {
- self.appDelegate.messageNotification("_error_", description: message, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
- } else {
- print("[LOG] It has been changed user during networking process, error.")
- }
- })
- }
- func tapAction(with notification: OCNotifications?, label: String) {
-
- for action in notification!.actions {
-
- if (action as! OCNotificationsAction).label == label {
-
- OCNetworking.sharedManager().setNotificationWithAccount(self.appDelegate.activeAccount, serverUrl: (action as! OCNotificationsAction).link, type: (action as! OCNotificationsAction).type, completion: { (account, message, errorCode) in
-
- if errorCode == 0 && account == self.appDelegate.activeAccount {
-
- let listOfNotifications = self.appDelegate.listOfNotifications as NSArray as! [OCNotifications]
-
- if let index = listOfNotifications.firstIndex(where: {$0.idNotification == notification!.idNotification}) {
- self.appDelegate.listOfNotifications.removeObject(at: index)
- }
-
- self.reloadDatasource()
-
- if self.appDelegate.listOfNotifications.count == 0 {
- self.viewClose()
- }
-
- } else if errorCode != 0 {
- self.appDelegate.messageNotification("_error_", description: message, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
- } else {
- print("[LOG] It has been changed user during networking process, error.")
- }
- })
- }
- }
- }
- }
- class CCNotificationCell: UITableViewCell {
-
- var delegate: CCNotificationCelllDelegate?
- var notification: OCNotifications?
- @IBOutlet weak var icon : UIImageView!
- @IBOutlet weak var date: UILabel!
- @IBOutlet weak var subject: UILabel!
- @IBOutlet weak var message: UILabel!
- @IBOutlet weak var remove: UIButton!
- @IBOutlet weak var primary: UIButton!
- @IBOutlet weak var secondary: UIButton!
- @IBOutlet weak var messageBottomMargin: NSLayoutConstraint!
-
- override func awakeFromNib() {
- super.awakeFromNib()
- }
-
- @IBAction func touchUpInsideRemove(_ sender: Any) {
- delegate?.tapRemove(with: notification)
- }
-
- @IBAction func touchUpInsidePrimary(_ sender: Any) {
- let button = sender as! UIButton
- delegate?.tapAction(with: notification, label: button.titleLabel!.text!)
- }
-
- @IBAction func touchUpInsideSecondary(_ sender: Any) {
- let button = sender as! UIButton
- delegate?.tapAction(with: notification, label: button.titleLabel!.text!)
- }
- }
- protocol CCNotificationCelllDelegate {
- func tapRemove(with notification: OCNotifications?)
- func tapAction(with notification: OCNotifications?, label: String)
- }
|