// // NotificationViewController.swift // GMERemittance // // Created by FMI-12 on 2/18/18. // Copyright © 2018 Gobal Money Express Co. Ltd. All rights reserved. // import UIKit class NotificationViewController: UIViewController,LoadMoreViewCellDelegate { var socialfeedviewmodel: SocialFeedViewModel? var feedId: String? var notificationArray = [NotificationModel]() var loadMoreButtonOutlet: UIButton? var page: Int = 0 var size: Int = 20 @IBOutlet weak var tableViewNotification: UITableView! var notificationviewmodel = NotificationViewModel() private var activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView() public static var notificationConnectionTimeOutCheck = 0 override func viewDidAppear(_ animated: Bool) { setUpNavBar(id: 205, title: "Notifications") setUpAnotherLoginListener(genericviewmodel: notificationviewmodel) } override func viewDidLoad() { super.viewDidLoad() setUpNavBar(id: 205, title: "Notifications") setUpListener() notificationviewmodel.notificationConnectionTimeOut.value = nil /** connection timeout */ notificationviewmodel.notificationConnectionTimeOut.bind { [unowned self] in guard $0 != nil else { return } self.enableUserInteractions() self.dismissActivityIndicator(activityIndicator: self.activityIndicator) if NotificationViewController.notificationConnectionTimeOutCheck == 0{ NotificationViewController.notificationConnectionTimeOutCheck = NotificationViewController.notificationConnectionTimeOutCheck+1 self.popUpMessage(value: 29) } } /** Internet check */ guard Reachability.isConnectedToNetwork() else { self.popUpMessage(value: 15) return } self.tableViewNotification.estimatedRowHeight = 119 self.tableViewNotification.rowHeight = UITableViewAutomaticDimension disableUserInteractions() showActivityIndicator(activityIndicator: activityIndicator) notificationviewmodel.fetchNotification(page: self.page, size: self.size) tableViewNotification.tableFooterView = UIView() } /** Update notification view */ func setUpListener() { notificationviewmodel.notificationFetch.bind{ [unowned self] in guard $0 != nil else { return } self.dismissActivityIndicator(activityIndicator: self.activityIndicator) self.enableUserInteractions() guard $0! else { self.messageLabel() return } //Update the view self.dismissActivityIndicator(activityIndicator: self.activityIndicator) self.enableUserInteractions() self.loadTableView() if self.notificationArray.count == 0 { self.messageLabel() self.tableViewNotification.isHidden = true } else { self.tableViewNotification.isHidden = false } } } func loadTableView(){ self.notificationArray.append(contentsOf:self.notificationviewmodel.getNotificationArray()) if self.notificationviewmodel.getNotificationArray().count == self.size{ loadMoreButtonVisibility(hiddenStatus: false) }else if (self.notificationviewmodel.getNotificationArray().count < self.size || self.notificationviewmodel.getNotificationArray().count == 0){ loadMoreButtonVisibility(hiddenStatus: true) } self.tableViewNotification.reloadData() } @objc func loadMoreButtonAction(sender: NotificationTableViewCell) { if Reachability.isConnectedToNetwork() { self.disableUserInteractions() self.showActivityIndicator(activityIndicator: self.activityIndicator) page = page + 1 notificationviewmodel.fetchNotification(page: self.page, size: self.size) } else { self.popUpMessage(value: 15) } } /** Dispaly message on screen if notification is empty */ func messageLabel(){ let label = UILabel(frame: CGRect(x: 0, y: 0, width: 250, height: 21)) label.center = CGPoint(x:self.view.center.x, y:284)//self.view.center// label.textAlignment = NSTextAlignment.center label.text = "You don't have any notification." label.textColor = UIColor(hex:0x4A4A4A) self.view.addSubview(label) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } func loadMoreButtonVisibility(hiddenStatus: Bool){ if hiddenStatus == true{ self.loadMoreButtonOutlet?.isHidden = true }else{ self.loadMoreButtonOutlet?.isHidden = false } } } extension NotificationViewController: UITableViewDelegate, UITableViewDataSource { func numberOfSections(in tableView: UITableView) -> Int { return 2 } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { switch section { case 0: return self.notificationArray.count default: return 1 } } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { switch indexPath.section { case 0: let cell = tableView.dequeueReusableCell(withIdentifier: "notificationCell", for: indexPath) as! NotificationTableViewCell cell.layer.borderColor = UIColor(red:0.91, green:0.93, blue:0.95, alpha:1.0).cgColor cell.layer.borderWidth = 2 cell.layer.cornerRadius = 10 cell.imageViewUserImage.layer.cornerRadius = cell.imageViewUserImage.frame.height / 2 cell.labelDate.text = unixTimeStampToDate(unixTimeStamp: self.notificationArray[indexPath.row].createdDate) cell.labelMessage.text = self.notificationArray[indexPath.row].notificationMessage cell.labelInitialLetter.isHidden = false cell.labelInitialLetter.text = notificationArray[indexPath.row].notifierName?.prefix(1).uppercased() if let notifireUrl = self.notificationArray[indexPath.row].notifierUrl{ if let imageUrl = URL(string: notifireUrl) { cell.imageViewUserImage.isHidden = false cell.imageViewUserImage.sd_setImage(with: imageUrl, placeholderImage: nil, options: [.progressiveDownload,.scaleDownLargeImages], completed: nil) } cell.labelInitialLetter.isHidden = true } return cell default: let cell = tableView.dequeueReusableCell(withIdentifier: "loadMoreButtonCell", for: indexPath) as! NotificationTableViewCell self.loadMoreButtonOutlet = cell.loadMoreOutlet cell.loadMoreOutlet.addTarget(self, action: #selector(loadMoreButtonAction(sender:)), for: .touchUpInside) return cell } } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { notificationviewmodel.patchNotification(notificaitonId: notificationArray[indexPath.row].notificationId, viewed: "true") switch notificationArray[indexPath.row].notificationCode { // case "0": // print("redirect page 0") // case "1": // print("redirect page 1") // case "3": // print("redirect page 3") case "200": TranscationStatementViewController.notificationCode = "200" let storyboard = UIStoryboard.init(name: "TransactionStatement", bundle: Bundle.main) let vController = storyboard.instantiateViewController(withIdentifier: "TranscationStatementViewController") as! TranscationStatementViewController vController.fromNotification = 0 self.navigationController!.pushViewController(vController, animated: true) case "201": let storyboard = UIStoryboard.init(name: "WalletTransfer", bundle: Bundle.main) let vController = storyboard.instantiateViewController(withIdentifier: "MoneyRequestViewController") as! MoneyRequestViewController self.navigationController!.pushViewController(vController, animated: true) case "202": let storyboard = UIStoryboard.init(name: "WalletTransfer", bundle: Bundle.main) let vController = storyboard.instantiateViewController(withIdentifier: "WalletTransactionListViewController") as! WalletTransactionListViewController vController.walletStatus = "walletBorrow" self.navigationController!.pushViewController(vController, animated: true) case "203": TranscationStatementViewController.notificationCode = "203" let storyboard = UIStoryboard.init(name: "TransactionStatement", bundle: Bundle.main) let vController = storyboard.instantiateViewController(withIdentifier: "TranscationStatementViewController") as! TranscationStatementViewController vController.fromNotification = 0 self.navigationController!.pushViewController(vController, animated: true) case "204": TranscationStatementViewController.notificationCode = "204" let storyboard = UIStoryboard.init(name: "TransactionStatement", bundle: Bundle.main) let vController = storyboard.instantiateViewController(withIdentifier: "TranscationStatementViewController") as! TranscationStatementViewController vController.fromNotification = 0 self.navigationController!.pushViewController(vController, animated: true) case "205": let storyboard = UIStoryboard.init(name: "WalletTransfer", bundle: Bundle.main) let vController = storyboard.instantiateViewController(withIdentifier: "WalletTransactionListViewController") as! WalletTransactionListViewController vController.walletStatus = "walletTransfer" self.navigationController!.pushViewController(vController, animated: true) case "206": TranscationStatementViewController.notificationCode = "206" let storyboard = UIStoryboard.init(name: "TransactionStatement", bundle: Bundle.main) let vController = storyboard.instantiateViewController(withIdentifier: "TranscationStatementViewController") as! TranscationStatementViewController vController.fromNotification = 0 self.navigationController!.pushViewController(vController, animated: true) case "207": TranscationStatementViewController.notificationCode = "207" let storyboard = UIStoryboard.init(name: "TransactionStatement", bundle: Bundle.main) let vController = storyboard.instantiateViewController(withIdentifier: "TranscationStatementViewController") as! TranscationStatementViewController vController.fromNotification = 0 self.navigationController!.pushViewController(vController, animated: true) case "208": TranscationStatementViewController.notificationCode = "208" let storyboard = UIStoryboard.init(name: "TransactionStatement", bundle: Bundle.main) let vController = storyboard.instantiateViewController(withIdentifier: "TranscationStatementViewController") as! TranscationStatementViewController vController.fromNotification = 0 self.navigationController!.pushViewController(vController, animated: true) case "500": InviteViewController.notificationCode = "500" let storyboard = UIStoryboard.init(name: "Invite", bundle: Bundle.main) let vController = storyboard.instantiateViewController(withIdentifier: "InviteViewController") as! InviteViewController vController.fromNotification = 0 self.navigationController!.pushViewController(vController, animated: true) case "700": let storyboard = UIStoryboard.init(name: "Reward", bundle: Bundle.main) let vController = storyboard.instantiateViewController(withIdentifier: "RewardViewController") as! RewardViewController vController.fromNotification = 0 self.navigationController!.pushViewController(vController, animated: true) case "750": feedId = notificationArray[indexPath.row].relatedId! performSegue(withIdentifier: "notificationFeed", sender: nil) default: return } } override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "notificationFeed" { let commentsViewController = segue.destination as! CommentsViewController commentsViewController.feedIdFromNotification = feedId! commentsViewController.fromNotification = 0 commentsViewController.socialfeedviewmodel = self.socialfeedviewmodel } } }