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.

170 lines
6.3 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. class RecipientListViewController: UIViewController {
  10. @IBOutlet weak var tableView: UITableView!
  11. @IBOutlet weak var viewAddRecipient: UIView! // this is the view that contains add reciepient. should be header
  12. @IBOutlet weak var labelSwipeInfo: UILabel!
  13. var reciepients: [Recipient]? {
  14. didSet {
  15. if (reciepients ?? []).isEmpty {
  16. // self.showAddNewReciepientViewController()
  17. }else {
  18. self.tableView.isHidden = false
  19. self.labelSwipeInfo.isHidden = false
  20. self.tableView.reloadData()
  21. }
  22. }
  23. }
  24. override func viewWillAppear(_ animated: Bool) {
  25. super.viewWillAppear(animated)
  26. setupNavigation()
  27. // self.title = "Send Money"
  28. self.navigationItem.title = "Send Money"
  29. fetchReceipients()
  30. }
  31. override func viewDidAppear(_ animated: Bool) {
  32. super.viewDidAppear(animated)
  33. }
  34. override func viewDidLoad() {
  35. super.viewDidLoad()
  36. self.setupDelegates()
  37. self.showProgressHud()
  38. viewAddRecipient.layer.cornerRadius = 10
  39. }
  40. override func viewDidDisappear(_ animated: Bool){
  41. super.viewDidDisappear(animated)
  42. }
  43. private func setupDelegates() {
  44. self.tableView.delegate = self
  45. self.tableView.dataSource = self
  46. }
  47. @IBAction func loadMoreAction(_ sender: Any) {
  48. print("load more")
  49. }
  50. override func viewWillDisappear(_ animated: Bool) {
  51. super.viewWillDisappear(animated)
  52. // self.title = ""
  53. self.navigationItem.title = ""
  54. }
  55. @IBAction func addNewRecipientTap(_ sender: UITapGestureRecognizer) {
  56. self.showAddNewReciepientViewController()
  57. }
  58. override func didReceiveMemoryWarning() {
  59. super.didReceiveMemoryWarning()
  60. }
  61. }
  62. extension RecipientListViewController: UITableViewDelegate,UITableViewDataSource {
  63. func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
  64. return 120.0
  65. }
  66. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  67. return self.reciepients?.count ?? 0
  68. }
  69. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  70. let cell = tableView.dequeueReusableCell(withIdentifier: "recipientList", for: indexPath) as! RecipientListTableViewCell
  71. cell.model = self.reciepients?.elementAt(index: indexPath.row)
  72. cell.setup()
  73. return cell
  74. }
  75. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  76. if let navigation = self.navigationController {
  77. if let reciepient = self.reciepients?.elementAt(index: indexPath.row) {
  78. let wireframe = SendMoneyParentWireframe()
  79. wireframe.open(for: reciepient, in: navigation)
  80. }
  81. }
  82. }
  83. func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
  84. let delete = UITableViewRowAction(style: .destructive, title: "Delete") { (action, indexPath) in
  85. self.alertWithOkCancel(message: "Do you want to delete this receipient?", title: "Alert!", OkStyle: UIAlertActionStyle.destructive, okAction: {
  86. let defaults = UserDefaults.standard
  87. let myUsername = defaults.string(forKey: "com.gmeremit.username") ?? ""
  88. self.showProgressHud()
  89. if let reciepient = self.reciepients?.elementAt(index: indexPath.row) {
  90. self.showProgressHud()
  91. self.deleteRecipient(username: myUsername, reciepient: reciepient, success: { (reciepient) in
  92. self.fetchReciepientList(username: myUsername, success: { (_recipients) in
  93. self.hideProgressHud()
  94. self.reciepients = _recipients
  95. }, failure: { (error) in
  96. self.hideProgressHud()
  97. self.alert(message: error.localizedDescription)
  98. })
  99. }, failure: { (error) in
  100. self.alert(message: error.localizedDescription)
  101. self.hideProgressHud()
  102. })
  103. }
  104. })
  105. }
  106. let edit = UITableViewRowAction(style: .normal, title: "Edit") { (action, indexPath) in
  107. guard let navigation = self.navigationController else {return}
  108. if let reciepient = self.reciepients?.elementAt(index: indexPath.row) {
  109. let wireFrame = EditReciepientWireframe()
  110. wireFrame.edit(reciepient: reciepient, source: navigation)
  111. }
  112. }
  113. edit.backgroundColor = UIColor.init(hex: "#F39826")
  114. delete.backgroundColor = UIColor.init(hex: "DE333C")
  115. return [delete, edit]
  116. }
  117. func fetchReceipients() {
  118. let defaults = UserDefaults.standard
  119. let myUsername = defaults.string(forKey: "com.gmeremit.username") ?? ""
  120. self.fetchReciepientList(username: myUsername, success: { (reciepients) in
  121. self.hideProgressHud()
  122. self.reciepients = reciepients
  123. }) { (error) in
  124. self.hideProgressHud()
  125. self.alert(message: error.localizedDescription)
  126. }
  127. }
  128. // private func
  129. private func setupNavigation() {
  130. self.navigationController?.navigationBar
  131. self.navigationController?.navigationBar.backIndicatorImage = UIImage(named: "backIconBlack")
  132. self.navigationController?.navigationBar.backIndicatorTransitionMaskImage = UIImage(named: "backIconBlack")
  133. self.navigationController?.navigationBar.tintColor = .black
  134. self.navigationController?.navigationBar.barTintColor = .white
  135. }
  136. private func showAddNewReciepientViewController() {
  137. let viewcontroller = AddReciepientWireframe().getMainView()
  138. self.navigationController?.pushViewController(viewcontroller, animated: true)
  139. }
  140. }
  141. extension RecipientListViewController: FetchRecipientList, DeleteRecipientService {
  142. }