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.

182 lines
6.7 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. //
  2. // RecipientListViewController.swift
  3. // GMERemittance
  4. //
  5. // Created by Fm-user on 12/21/17.
  6. // Copyright © 2017 Gobal Money Express Co. Ltd. All rights reserved.
  7. //
  8. import UIKit
  9. class RecipientListViewController: UIViewController {
  10. // MARK:- IBOutlets
  11. @IBOutlet weak var tableView: UITableView!
  12. @IBOutlet weak var viewAddRecipient: UIView! // this is the view that contains add reciepient. should be header
  13. @IBOutlet weak var labelSwipeInfo: UILabel!
  14. // MARK:- properties
  15. var reciepients: [Recipient]? {
  16. didSet {
  17. if (reciepients ?? []).isEmpty {
  18. }else {
  19. self.tableView.isHidden = false
  20. self.labelSwipeInfo.isHidden = false
  21. self.tableView.reloadData()
  22. }
  23. }
  24. }
  25. // MARK:- Life Cycle
  26. override func viewWillAppear(_ animated: Bool) {
  27. super.viewWillAppear(animated)
  28. self.setupNormalNavigation()
  29. self.navigationItem.title = "Select Recipient"
  30. fetchReceipients()
  31. }
  32. override func viewDidLoad() {
  33. super.viewDidLoad()
  34. self.setupDelegates()
  35. self.showProgressHud()
  36. viewAddRecipient.layer.cornerRadius = 10
  37. }
  38. override func viewWillDisappear(_ animated: Bool) {
  39. super.viewWillDisappear(animated)
  40. // self.title = ""
  41. self.navigationItem.title = ""
  42. }
  43. // MARK:- IBAction
  44. @IBAction func loadMoreAction(_ sender: Any) {
  45. print("load more")
  46. }
  47. @IBAction func addNewRecipientTap(_ sender: UITapGestureRecognizer) {
  48. self.showAddNewReciepientViewController()
  49. }
  50. // MARK:- other functions
  51. private func setupDelegates() {
  52. self.tableView.delegate = self
  53. self.tableView.dataSource = self
  54. }
  55. }
  56. extension RecipientListViewController: UITableViewDelegate,UITableViewDataSource {
  57. func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
  58. return 120.0
  59. }
  60. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  61. return self.reciepients?.count ?? 0
  62. }
  63. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  64. let cell = tableView.dequeueReusableCell(withIdentifier: "recipientList", for: indexPath) as! RecipientListTableViewCell
  65. cell.model = self.reciepients?.elementAt(index: indexPath.row)
  66. cell.setup()
  67. return cell
  68. }
  69. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  70. startSendMoneyProcess(index: indexPath.row)
  71. }
  72. private func startSendMoneyProcess(index: Int) {
  73. if let navigation = self.navigationController {
  74. if let reciepient = self.reciepients?.elementAt(index: index) {
  75. let wireframe = SendMoneyParentWireframe()
  76. wireframe.open(for: reciepient, in: navigation)
  77. }
  78. }
  79. }
  80. func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
  81. let delete = UITableViewRowAction(style: .destructive, title: "Delete") { (action, indexPath) in
  82. self.alertWithOkCancel(message: "Do you want to delete this receipient?", title: "Alert!", OkStyle: UIAlertActionStyle.destructive, okAction: {
  83. let defaults = UserDefaults.standard
  84. let myUsername = defaults.string(forKey: "com.gmeremit.username") ?? ""
  85. self.showProgressHud()
  86. if let reciepient = self.reciepients?.elementAt(index: indexPath.row) {
  87. self.showProgressHud()
  88. self.deleteRecipient(username: myUsername, reciepient: reciepient, success: { (reciepient) in
  89. DispatchQueue.main.async {
  90. self.hideProgressHud()
  91. }
  92. guard let deletedPerson = reciepient else {return}
  93. if let index = self.reciepients?.index(where: {
  94. ($0.recipientId ?? "") == (deletedPerson.recipientId ?? "")
  95. }) {
  96. UIView.animate(withDuration: 0.5, animations: {
  97. tableView.beginUpdates()
  98. self.reciepients?.remove(at: index)
  99. let indexPath = IndexPath(item: index, section: 0)
  100. self.tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.fade)
  101. tableView.endUpdates()
  102. })
  103. }
  104. // self.fetchReciepientList(username: myUsername, success: { (_recipients) in
  105. // self.hideProgressHud()
  106. // self.reciepients = _recipients
  107. // }, failure: { (error) in
  108. // self.hideProgressHud()
  109. // self.alert(message: error.localizedDescription)
  110. // })
  111. }, failure: { (error) in
  112. self.alert(message: error.localizedDescription)
  113. self.hideProgressHud()
  114. })
  115. }
  116. })
  117. }
  118. let edit = UITableViewRowAction(style: .normal, title: "Edit") { (action, indexPath) in
  119. guard let navigation = self.navigationController else {return}
  120. if let reciepient = self.reciepients?.elementAt(index: indexPath.row) {
  121. let wireFrame = EditReciepientWireframe()
  122. wireFrame.edit(reciepient: reciepient, source: navigation)
  123. }
  124. }
  125. edit.backgroundColor = UIColor.init(hex: "#F39826")
  126. delete.backgroundColor = UIColor.init(hex: "DE333C")
  127. return [delete, edit]
  128. }
  129. func fetchReceipients() {
  130. let defaults = UserDefaults.standard
  131. let myUsername = defaults.string(forKey: "com.gmeremit.username") ?? ""
  132. self.fetchReciepientList(username: myUsername, success: { (reciepients) in
  133. self.hideProgressHud()
  134. self.reciepients = reciepients
  135. }) { (error) in
  136. self.hideProgressHud()
  137. self.alert(message: error.localizedDescription)
  138. }
  139. }
  140. // private func
  141. private func showAddNewReciepientViewController() {
  142. let viewcontroller = AddReciepientWireframe().getMainView()
  143. self.navigationController?.pushViewController(viewcontroller, animated: true)
  144. }
  145. }
  146. extension RecipientListViewController: FetchRecipientList, DeleteRecipientService {
  147. }