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.

225 lines
8.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
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 Localize_Swift
  10. class RecipientListViewController: UIViewController {
  11. struct StringConstant {
  12. static let swipeText = ""
  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 newRecipeintLabel: UILabel!
  22. // MARK:- properties
  23. var reciepients: [Recipient]? {
  24. didSet {
  25. if (reciepients ?? []).isEmpty {
  26. }else {
  27. self.tableView.isHidden = false
  28. self.labelSwipeInfo.isHidden = false
  29. self.tableView.reloadData()
  30. }
  31. }
  32. }
  33. var accounts: [Account]?
  34. var selectedIndex: Int?
  35. // MARK:- Life Cycle
  36. override func viewWillAppear(_ animated: Bool) {
  37. super.viewWillAppear(animated)
  38. self.setupNormalNavigation()
  39. self.navigationItem.title = "recipient_listing_title_text".localized()
  40. configureLanguage()
  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. NotificationCenter.default.addObserver(self, selector: #selector(setupTabItem), name: NSNotification.Name(LCLLanguageChangeNotification), object: nil)
  50. }
  51. func configureLanguage() {
  52. self.labelSwipeInfo.text = "edit_delete_hint_ios_text".localized()
  53. // self.barButton.title = StringConstant.sendMoneyText
  54. self.newRecipeintLabel.text = "new_recipient".localized()
  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. override func setupTabItem() {
  74. let image = UIImage.init(named: "ic-sendmoney")
  75. self.tabBarItem = UITabBarItem(title: "send_money_title_text".localized(), image: image, selectedImage: nil)
  76. self.tabBarItem.titlePositionAdjustment = UIOffset(horizontal: 0, vertical: UI_USER_INTERFACE_IDIOM() == .pad ? 2 : -6)
  77. }
  78. }
  79. extension RecipientListViewController: UITableViewDelegate,UITableViewDataSource {
  80. func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
  81. return 120.0
  82. }
  83. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  84. return self.reciepients?.count ?? 0
  85. }
  86. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  87. let cell = tableView.dequeueReusableCell(withIdentifier: "recipientList", for: indexPath) as! RecipientListTableViewCell
  88. cell.model = self.reciepients?.elementAt(index: indexPath.row)
  89. cell.setup()
  90. return cell
  91. }
  92. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  93. // show the action sheet to choose the payment method.
  94. startSendMoneyProcess(index: indexPath.row)
  95. }
  96. private func showMethodSelection(for index: Int) {
  97. self.selectedIndex = index
  98. let wireframe = PaymentSelectionWireframe()
  99. wireframe.openPaymentSelection(accounts: self.accounts ?? [], onSelection: self.selectedAcunt, source: self)
  100. }
  101. func selectedAcunt(acunt: Account) {
  102. guard let index = self.selectedIndex else {return}
  103. if let navigation = self.navigationController {
  104. if let reciepient = self.reciepients?.elementAt(index: index) {
  105. let wireframe = SendMoneyParentWireframe()
  106. wireframe.open(for: reciepient, with: acunt, in: navigation)
  107. }
  108. }
  109. }
  110. private func startSendMoneyProcess(index: Int) {
  111. // TODO:- for kftc enable this in next release
  112. self.showMethodSelection(for: index)
  113. }
  114. func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
  115. let delete = UITableViewRowAction(style: .destructive, title: "delete_text".localized()) { (action, indexPath) in
  116. self.alertWithOkCancel(message: "delete_recipient_confirmation_text".localized(), title: "alert_text".localized(), OkStyle: UIAlertActionStyle.destructive, okAction: {
  117. let defaults = UserDefaults.standard
  118. let myUsername = defaults.string(forKey: "com.gmeremit.username") ?? ""
  119. self.showProgressHud()
  120. if let reciepient = self.reciepients?.elementAt(index: indexPath.row) {
  121. self.showProgressHud()
  122. self.deleteRecipient(username: myUsername, reciepient: reciepient, success: { (reciepient) in
  123. DispatchQueue.main.async {
  124. self.hideProgressHud()
  125. }
  126. guard let deletedPerson = reciepient else {return}
  127. if let index = self.reciepients?.index(where: {
  128. ($0.recipientId ?? "") == (deletedPerson.recipientId ?? "")
  129. }) {
  130. UIView.animate(withDuration: 0.5, animations: {
  131. tableView.beginUpdates()
  132. self.reciepients?.remove(at: index)
  133. let indexPath = IndexPath(item: index, section: 0)
  134. self.tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.fade)
  135. tableView.endUpdates()
  136. })
  137. }
  138. }, failure: { (error) in
  139. self.alert(message: error.localizedDescription)
  140. self.hideProgressHud()
  141. })
  142. }
  143. })
  144. }
  145. let edit = UITableViewRowAction(style: .normal, title: "edit_text".localized()) { (action, indexPath) in
  146. guard let navigation = self.navigationController else {return}
  147. if let reciepient = self.reciepients?.elementAt(index: indexPath.row) {
  148. let wireFrame = EditReciepientWireframe()
  149. wireFrame.edit(reciepient: reciepient, source: navigation)
  150. }
  151. }
  152. edit.backgroundColor = UIColor.init(hex: "#F39826")
  153. delete.backgroundColor = UIColor.init(hex: "DE333C")
  154. return [delete, edit]
  155. }
  156. func fetchReceipients() {
  157. let defaults = UserDefaults.standard
  158. let myUsername = defaults.string(forKey: "com.gmeremit.username") ?? ""
  159. self.fetchReciepientList(username: myUsername, success: { (reciepients) in
  160. self.hideProgressHud()
  161. // TODO
  162. self.accounts = reciepients?.accounts
  163. self.reciepients = reciepients?.reciepients
  164. }) { (error) in
  165. self.hideProgressHud()
  166. self.alert(message: error.localizedDescription)
  167. }
  168. }
  169. // private func
  170. private func showAddNewReciepientViewController() {
  171. let viewcontroller = AddReciepientWireframe().getMainView()
  172. self.navigationController?.pushViewController(viewcontroller, animated: true)
  173. }
  174. }
  175. extension RecipientListViewController: FetchRecipientList, DeleteRecipientService {
  176. }