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.

236 lines
8.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
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. // remove this
  114. // if let navigation = self.navigationController {
  115. // if let reciepient = self.reciepients?.elementAt(index: index) {
  116. // let wireframe = SendMoneyParentWireframe()
  117. // let acunt = Account()
  118. // acunt.type = "wallet"
  119. // wireframe.open(for: reciepient, with: acunt, in: navigation)
  120. // }
  121. // }
  122. }
  123. func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
  124. let delete = UITableViewRowAction(style: .destructive, title: "delete_text".localized()) { (action, indexPath) in
  125. self.alertWithOkCancel(message: "delete_recipient_confirmation_text".localized(), title: "alert_text".localized(), OkStyle: UIAlertActionStyle.destructive, okAction: {
  126. let defaults = UserDefaults.standard
  127. let myUsername = defaults.string(forKey: "com.gmeremit.username") ?? ""
  128. self.showProgressHud()
  129. if let reciepient = self.reciepients?.elementAt(index: indexPath.row) {
  130. self.showProgressHud()
  131. self.deleteRecipient(username: myUsername, reciepient: reciepient, success: { (reciepient) in
  132. DispatchQueue.main.async {
  133. self.hideProgressHud()
  134. }
  135. guard let deletedPerson = reciepient else {return}
  136. if let index = self.reciepients?.index(where: {
  137. ($0.recipientId ?? "") == (deletedPerson.recipientId ?? "")
  138. }) {
  139. UIView.animate(withDuration: 0.5, animations: {
  140. tableView.beginUpdates()
  141. self.reciepients?.remove(at: index)
  142. let indexPath = IndexPath(item: index, section: 0)
  143. self.tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.fade)
  144. tableView.endUpdates()
  145. })
  146. }
  147. }, failure: { (error) in
  148. self.alert(message: error.localizedDescription)
  149. self.hideProgressHud()
  150. })
  151. }
  152. })
  153. }
  154. let edit = UITableViewRowAction(style: .normal, title: "edit_text".localized()) { (action, indexPath) in
  155. guard let navigation = self.navigationController else {return}
  156. if let reciepient = self.reciepients?.elementAt(index: indexPath.row) {
  157. let wireFrame = EditReciepientWireframe()
  158. wireFrame.edit(reciepient: reciepient, source: navigation)
  159. }
  160. }
  161. edit.backgroundColor = UIColor.init(hex: "#F39826")
  162. delete.backgroundColor = UIColor.init(hex: "DE333C")
  163. return [delete, edit]
  164. }
  165. func fetchReceipients() {
  166. let defaults = UserDefaults.standard
  167. let myUsername = defaults.string(forKey: "com.gmeremit.username") ?? ""
  168. self.fetchReciepientList(username: myUsername, success: { (reciepients) in
  169. self.hideProgressHud()
  170. // TODO
  171. self.accounts = reciepients?.accounts
  172. self.reciepients = reciepients?.reciepients
  173. }) { (error) in
  174. self.hideProgressHud()
  175. self.alert(message: error.localizedDescription)
  176. }
  177. }
  178. // private func
  179. private func showAddNewReciepientViewController() {
  180. let viewcontroller = AddReciepientWireframe().getMainView()
  181. self.navigationController?.pushViewController(viewcontroller, animated: true)
  182. }
  183. }
  184. extension RecipientListViewController: FetchRecipientList, DeleteRecipientService {
  185. }