You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

317 lines
14 KiB

6 years ago
  1. //
  2. // NotificationViewController.swift
  3. // GMERemittance
  4. //
  5. // Created by FMI-12 on 2/18/18.
  6. // Copyright © 2018 Gobal Money Express Co. Ltd. All rights reserved.
  7. //
  8. import UIKit
  9. class NotificationViewController: UIViewController,LoadMoreViewCellDelegate {
  10. var socialfeedviewmodel: SocialFeedViewModel?
  11. var feedId: String?
  12. var notificationArray = [NotificationModel]()
  13. var loadMoreButtonOutlet: UIButton?
  14. var page: Int = 0
  15. var size: Int = 20
  16. @IBOutlet weak var tableViewNotification: UITableView!
  17. var notificationviewmodel = NotificationViewModel()
  18. private var activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView()
  19. public static var notificationConnectionTimeOutCheck = 0
  20. override func viewDidAppear(_ animated: Bool) {
  21. setUpNavBar(id: 205, title: "Notifications")
  22. setUpAnotherLoginListener(genericviewmodel: notificationviewmodel)
  23. }
  24. override func viewDidLoad() {
  25. super.viewDidLoad()
  26. setUpNavBar(id: 205, title: "Notifications")
  27. setUpListener()
  28. notificationviewmodel.notificationConnectionTimeOut.value = nil
  29. /**
  30. connection timeout
  31. */
  32. notificationviewmodel.notificationConnectionTimeOut.bind { [unowned self] in
  33. guard $0 != nil else {
  34. return
  35. }
  36. self.enableUserInteractions()
  37. self.dismissActivityIndicator(activityIndicator: self.activityIndicator)
  38. if NotificationViewController.notificationConnectionTimeOutCheck == 0{
  39. NotificationViewController.notificationConnectionTimeOutCheck = NotificationViewController.notificationConnectionTimeOutCheck+1
  40. self.popUpMessage(value: 29)
  41. }
  42. }
  43. /**
  44. Internet check
  45. */
  46. guard Reachability.isConnectedToNetwork() else {
  47. self.popUpMessage(value: 15)
  48. return
  49. }
  50. self.tableViewNotification.estimatedRowHeight = 119
  51. self.tableViewNotification.rowHeight = UITableViewAutomaticDimension
  52. disableUserInteractions()
  53. showActivityIndicator(activityIndicator: activityIndicator)
  54. notificationviewmodel.fetchNotification(page: self.page, size: self.size)
  55. tableViewNotification.tableFooterView = UIView()
  56. }
  57. /**
  58. Update notification view
  59. */
  60. func setUpListener() {
  61. notificationviewmodel.notificationFetch.bind{ [unowned self] in
  62. guard $0 != nil else {
  63. return
  64. }
  65. self.dismissActivityIndicator(activityIndicator: self.activityIndicator)
  66. self.enableUserInteractions()
  67. guard $0! else {
  68. self.messageLabel()
  69. return
  70. }
  71. //Update the view
  72. self.dismissActivityIndicator(activityIndicator: self.activityIndicator)
  73. self.enableUserInteractions()
  74. self.loadTableView()
  75. if self.notificationArray.count == 0 {
  76. self.messageLabel()
  77. self.tableViewNotification.isHidden = true
  78. } else {
  79. self.tableViewNotification.isHidden = false
  80. }
  81. }
  82. }
  83. func loadTableView(){
  84. self.notificationArray.append(contentsOf:self.notificationviewmodel.getNotificationArray())
  85. if self.notificationviewmodel.getNotificationArray().count == self.size{
  86. loadMoreButtonVisibility(hiddenStatus: false)
  87. }else if (self.notificationviewmodel.getNotificationArray().count < self.size || self.notificationviewmodel.getNotificationArray().count == 0){
  88. loadMoreButtonVisibility(hiddenStatus: true)
  89. }
  90. self.tableViewNotification.reloadData()
  91. }
  92. @objc func loadMoreButtonAction(sender: NotificationTableViewCell) {
  93. if Reachability.isConnectedToNetwork() {
  94. self.disableUserInteractions()
  95. self.showActivityIndicator(activityIndicator: self.activityIndicator)
  96. page = page + 1
  97. notificationviewmodel.fetchNotification(page: self.page, size: self.size)
  98. } else {
  99. self.popUpMessage(value: 15)
  100. }
  101. }
  102. /**
  103. Dispaly message on screen if notification is empty
  104. */
  105. func messageLabel(){
  106. let label = UILabel(frame: CGRect(x: 0, y: 0, width: 250, height: 21))
  107. label.center = CGPoint(x:self.view.center.x, y:284)//self.view.center//
  108. label.textAlignment = NSTextAlignment.center
  109. label.text = "You don't have any notification."
  110. label.textColor = UIColor(hex:0x4A4A4A)
  111. self.view.addSubview(label)
  112. }
  113. override func didReceiveMemoryWarning() {
  114. super.didReceiveMemoryWarning()
  115. // Dispose of any resources that can be recreated.
  116. }
  117. func loadMoreButtonVisibility(hiddenStatus: Bool){
  118. if hiddenStatus == true{
  119. self.loadMoreButtonOutlet?.isHidden = true
  120. }else{
  121. self.loadMoreButtonOutlet?.isHidden = false
  122. }
  123. }
  124. }
  125. extension NotificationViewController: UITableViewDelegate, UITableViewDataSource {
  126. func numberOfSections(in tableView: UITableView) -> Int {
  127. return 2
  128. }
  129. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  130. switch section {
  131. case 0:
  132. return self.notificationArray.count
  133. default:
  134. return 1
  135. }
  136. }
  137. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  138. switch indexPath.section {
  139. case 0:
  140. let cell = tableView.dequeueReusableCell(withIdentifier: "notificationCell", for: indexPath) as! NotificationTableViewCell
  141. cell.layer.borderColor = UIColor(red:0.91, green:0.93, blue:0.95, alpha:1.0).cgColor
  142. cell.layer.borderWidth = 2
  143. cell.layer.cornerRadius = 10
  144. cell.imageViewUserImage.layer.cornerRadius = cell.imageViewUserImage.frame.height / 2
  145. cell.labelDate.text = unixTimeStampToDate(unixTimeStamp: self.notificationArray[indexPath.row].createdDate)
  146. cell.labelMessage.text = self.notificationArray[indexPath.row].notificationMessage
  147. cell.labelInitialLetter.isHidden = false
  148. cell.labelInitialLetter.text = notificationArray[indexPath.row].notifierName?.prefix(1).uppercased()
  149. if let notifireUrl = self.notificationArray[indexPath.row].notifierUrl{
  150. if let imageUrl = URL(string: notifireUrl) {
  151. cell.imageViewUserImage.isHidden = false
  152. cell.imageViewUserImage.sd_setImage(with: imageUrl, placeholderImage: nil, options: [.progressiveDownload,.scaleDownLargeImages], completed: nil)
  153. }
  154. cell.labelInitialLetter.isHidden = true
  155. }
  156. return cell
  157. default:
  158. let cell = tableView.dequeueReusableCell(withIdentifier: "loadMoreButtonCell", for: indexPath) as! NotificationTableViewCell
  159. self.loadMoreButtonOutlet = cell.loadMoreOutlet
  160. cell.loadMoreOutlet.addTarget(self, action: #selector(loadMoreButtonAction(sender:)), for: .touchUpInside)
  161. return cell
  162. }
  163. }
  164. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  165. notificationviewmodel.patchNotification(notificaitonId: notificationArray[indexPath.row].notificationId, viewed: "true")
  166. switch notificationArray[indexPath.row].notificationCode {
  167. // case "0":
  168. // print("redirect page 0")
  169. // case "1":
  170. // print("redirect page 1")
  171. // case "3":
  172. // print("redirect page 3")
  173. case "200":
  174. TranscationStatementViewController.notificationCode = "200"
  175. let storyboard = UIStoryboard.init(name: "TransactionStatement", bundle: Bundle.main)
  176. let vController = storyboard.instantiateViewController(withIdentifier: "TranscationStatementViewController") as! TranscationStatementViewController
  177. vController.fromNotification = 0
  178. self.navigationController!.pushViewController(vController, animated: true)
  179. case "201":
  180. let storyboard = UIStoryboard.init(name: "WalletTransfer", bundle: Bundle.main)
  181. let vController = storyboard.instantiateViewController(withIdentifier: "MoneyRequestViewController") as! MoneyRequestViewController
  182. self.navigationController!.pushViewController(vController, animated: true)
  183. case "202":
  184. let storyboard = UIStoryboard.init(name: "WalletTransfer", bundle: Bundle.main)
  185. let vController = storyboard.instantiateViewController(withIdentifier: "WalletTransactionListViewController") as! WalletTransactionListViewController
  186. vController.walletStatus = "walletBorrow"
  187. self.navigationController!.pushViewController(vController, animated: true)
  188. case "203":
  189. TranscationStatementViewController.notificationCode = "203"
  190. let storyboard = UIStoryboard.init(name: "TransactionStatement", bundle: Bundle.main)
  191. let vController = storyboard.instantiateViewController(withIdentifier: "TranscationStatementViewController") as! TranscationStatementViewController
  192. vController.fromNotification = 0
  193. self.navigationController!.pushViewController(vController, animated: true)
  194. case "204":
  195. TranscationStatementViewController.notificationCode = "204"
  196. let storyboard = UIStoryboard.init(name: "TransactionStatement", bundle: Bundle.main)
  197. let vController = storyboard.instantiateViewController(withIdentifier: "TranscationStatementViewController") as! TranscationStatementViewController
  198. vController.fromNotification = 0
  199. self.navigationController!.pushViewController(vController, animated: true)
  200. case "205":
  201. let storyboard = UIStoryboard.init(name: "WalletTransfer", bundle: Bundle.main)
  202. let vController = storyboard.instantiateViewController(withIdentifier: "WalletTransactionListViewController") as! WalletTransactionListViewController
  203. vController.walletStatus = "walletTransfer"
  204. self.navigationController!.pushViewController(vController, animated: true)
  205. case "206":
  206. TranscationStatementViewController.notificationCode = "206"
  207. let storyboard = UIStoryboard.init(name: "TransactionStatement", bundle: Bundle.main)
  208. let vController = storyboard.instantiateViewController(withIdentifier: "TranscationStatementViewController") as! TranscationStatementViewController
  209. vController.fromNotification = 0
  210. self.navigationController!.pushViewController(vController, animated: true)
  211. case "207":
  212. TranscationStatementViewController.notificationCode = "207"
  213. let storyboard = UIStoryboard.init(name: "TransactionStatement", bundle: Bundle.main)
  214. let vController = storyboard.instantiateViewController(withIdentifier: "TranscationStatementViewController") as! TranscationStatementViewController
  215. vController.fromNotification = 0
  216. self.navigationController!.pushViewController(vController, animated: true)
  217. case "208":
  218. TranscationStatementViewController.notificationCode = "208"
  219. let storyboard = UIStoryboard.init(name: "TransactionStatement", bundle: Bundle.main)
  220. let vController = storyboard.instantiateViewController(withIdentifier: "TranscationStatementViewController") as! TranscationStatementViewController
  221. vController.fromNotification = 0
  222. self.navigationController!.pushViewController(vController, animated: true)
  223. case "500":
  224. InviteViewController.notificationCode = "500"
  225. let storyboard = UIStoryboard.init(name: "Invite", bundle: Bundle.main)
  226. let vController = storyboard.instantiateViewController(withIdentifier: "InviteViewController") as! InviteViewController
  227. vController.fromNotification = 0
  228. self.navigationController!.pushViewController(vController, animated: true)
  229. case "700":
  230. let storyboard = UIStoryboard.init(name: "Reward", bundle: Bundle.main)
  231. let vController = storyboard.instantiateViewController(withIdentifier: "RewardViewController") as! RewardViewController
  232. vController.fromNotification = 0
  233. self.navigationController!.pushViewController(vController, animated: true)
  234. case "750":
  235. feedId = notificationArray[indexPath.row].relatedId!
  236. performSegue(withIdentifier: "notificationFeed", sender: nil)
  237. default:
  238. return
  239. }
  240. }
  241. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  242. if segue.identifier == "notificationFeed" {
  243. let commentsViewController = segue.destination as! CommentsViewController
  244. commentsViewController.feedIdFromNotification = feedId!
  245. commentsViewController.fromNotification = 0
  246. commentsViewController.socialfeedviewmodel = self.socialfeedviewmodel
  247. }
  248. }
  249. }