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.

216 lines
7.9 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
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. import RAMAnimatedTabBarController
  10. class RecipientListViewController: UIViewController {
  11. struct StringConstant {
  12. static let swipeText = "Swipe left to “Edit” or “Delete” recipient profile."
  13. static let sendMoneyText = "Send Money"
  14. static let newRecipeintText = "New Recipient"
  15. static let navBarTitle = "Select Recipient"
  16. }
  17. // MARK:- IBOutlets
  18. @IBOutlet weak var tableView: UITableView!
  19. @IBOutlet weak var viewAddRecipient: UIView! // this is the view that contains add reciepient. should be header
  20. @IBOutlet weak var labelSwipeInfo: UILabel!
  21. @IBOutlet weak var barButton: RAMAnimatedTabBarItem!
  22. @IBOutlet weak var newRecipeintLabel: UILabel!
  23. // MARK:- properties
  24. var reciepients: [Recipient]? {
  25. didSet {
  26. if (reciepients ?? []).isEmpty {
  27. }else {
  28. self.tableView.isHidden = false
  29. self.labelSwipeInfo.isHidden = false
  30. self.tableView.reloadData()
  31. }
  32. }
  33. }
  34. var acunts: [Account]?
  35. var selectedIndex: Int?
  36. // MARK:- Life Cycle
  37. override func viewWillAppear(_ animated: Bool) {
  38. super.viewWillAppear(animated)
  39. self.setupNormalNavigation()
  40. self.navigationItem.title = StringConstant.navBarTitle
  41. fetchReceipients()
  42. }
  43. override func viewDidLoad() {
  44. super.viewDidLoad()
  45. // self.navigationController?.interactivePopGestureRecognizer?.isEnabled = false
  46. self.setupDelegates()
  47. self.showProgressHud()
  48. viewAddRecipient.layer.cornerRadius = 10
  49. configureLanguage()
  50. }
  51. func configureLanguage() {
  52. self.labelSwipeInfo.text = StringConstant.swipeText
  53. // self.barButton.title = StringConstant.sendMoneyText
  54. self.newRecipeintLabel.text = StringConstant.newRecipeintText
  55. }
  56. override func viewWillDisappear(_ animated: Bool) {
  57. super.viewWillDisappear(animated)
  58. // self.title = ""
  59. self.navigationItem.title = ""
  60. }
  61. // MARK:- IBAction
  62. @IBAction func loadMoreAction(_ sender: Any) {
  63. print("load more")
  64. }
  65. @IBAction func addNewRecipientTap(_ sender: UITapGestureRecognizer) {
  66. self.showAddNewReciepientViewController()
  67. }
  68. // MARK:- other functions
  69. private func setupDelegates() {
  70. self.tableView.delegate = self
  71. self.tableView.dataSource = self
  72. }
  73. }
  74. extension RecipientListViewController: UITableViewDelegate,UITableViewDataSource {
  75. func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
  76. return 120.0
  77. }
  78. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  79. return self.reciepients?.count ?? 0
  80. }
  81. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  82. let cell = tableView.dequeueReusableCell(withIdentifier: "recipientList", for: indexPath) as! RecipientListTableViewCell
  83. cell.model = self.reciepients?.elementAt(index: indexPath.row)
  84. cell.setup()
  85. return cell
  86. }
  87. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  88. // show the action sheet to choose the payment method.
  89. startSendMoneyProcess(index: indexPath.row)
  90. }
  91. private func showMethodSelection(for index: Int) {
  92. self.selectedIndex = index
  93. let wireframe = PaymentSelectionWireframe()
  94. wireframe.openPaymentSelection(acunts: self.acunts ?? [], onSelection: self.selectedAcunt, source: self)
  95. }
  96. func selectedAcunt(acunt: Account) {
  97. print(acunt.bankName)
  98. guard let index = self.selectedIndex else {return}
  99. if let navigation = self.navigationController {
  100. if let reciepient = self.reciepients?.elementAt(index: index) {
  101. let wireframe = SendMoneyParentWireframe()
  102. wireframe.open(for: reciepient, with: acunt, in: navigation)
  103. }
  104. }
  105. }
  106. private func startSendMoneyProcess(index: Int) {
  107. // first show the account selection page
  108. // then start the send money process
  109. self.showMethodSelection(for: index)
  110. }
  111. func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
  112. let delete = UITableViewRowAction(style: .destructive, title: "Delete") { (action, indexPath) in
  113. self.alertWithOkCancel(message: "Do you want to delete this receipient?", title: "Alert!", OkStyle: UIAlertActionStyle.destructive, okAction: {
  114. let defaults = UserDefaults.standard
  115. let myUsername = defaults.string(forKey: "com.gmeremit.username") ?? ""
  116. self.showProgressHud()
  117. if let reciepient = self.reciepients?.elementAt(index: indexPath.row) {
  118. self.showProgressHud()
  119. self.deleteRecipient(username: myUsername, reciepient: reciepient, success: { (reciepient) in
  120. DispatchQueue.main.async {
  121. self.hideProgressHud()
  122. }
  123. guard let deletedPerson = reciepient else {return}
  124. if let index = self.reciepients?.index(where: {
  125. ($0.recipientId ?? "") == (deletedPerson.recipientId ?? "")
  126. }) {
  127. UIView.animate(withDuration: 0.5, animations: {
  128. tableView.beginUpdates()
  129. self.reciepients?.remove(at: index)
  130. let indexPath = IndexPath(item: index, section: 0)
  131. self.tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.fade)
  132. tableView.endUpdates()
  133. })
  134. }
  135. }, failure: { (error) in
  136. self.alert(message: error.localizedDescription)
  137. self.hideProgressHud()
  138. })
  139. }
  140. })
  141. }
  142. let edit = UITableViewRowAction(style: .normal, title: "Edit") { (action, indexPath) in
  143. guard let navigation = self.navigationController else {return}
  144. if let reciepient = self.reciepients?.elementAt(index: indexPath.row) {
  145. let wireFrame = EditReciepientWireframe()
  146. wireFrame.edit(reciepient: reciepient, source: navigation)
  147. }
  148. }
  149. edit.backgroundColor = UIColor.init(hex: "#F39826")
  150. delete.backgroundColor = UIColor.init(hex: "DE333C")
  151. return [delete, edit]
  152. }
  153. func fetchReceipients() {
  154. let defaults = UserDefaults.standard
  155. let myUsername = defaults.string(forKey: "com.gmeremit.username") ?? ""
  156. self.fetchReciepientList(username: myUsername, success: { (reciepients) in
  157. self.hideProgressHud()
  158. // TODO
  159. self.acunts = reciepients?.acunts
  160. self.reciepients = reciepients?.reciepients
  161. }) { (error) in
  162. self.hideProgressHud()
  163. self.alert(message: error.localizedDescription)
  164. }
  165. }
  166. // private func
  167. private func showAddNewReciepientViewController() {
  168. let viewcontroller = AddReciepientWireframe().getMainView()
  169. self.navigationController?.pushViewController(viewcontroller, animated: true)
  170. }
  171. }
  172. extension RecipientListViewController: FetchRecipientList, DeleteRecipientService {
  173. }