// // RecipientListViewController.swift // GMERemittance // // Created by Fm-user on 12/21/17. // Copyright © 2017 Gobal Money Express Co. Ltd. All rights reserved. // import UIKit import Localize_Swift class RecipientListViewController: UIViewController { struct StringConstant { static let swipeText = "" static let sendMoneyText = "Send Money" static let newRecipeintText = "New Recipient" static let navBarTitle = "Select Recipient" } // MARK:- IBOutlets @IBOutlet weak var tableView: UITableView! @IBOutlet weak var viewAddRecipient: UIView! // this is the view that contains add reciepient. should be header @IBOutlet weak var labelSwipeInfo: UILabel! @IBOutlet weak var newRecipeintLabel: UILabel! // MARK:- properties var reciepients: [Recipient]? { didSet { if (reciepients ?? []).isEmpty { }else { self.tableView.isHidden = false self.labelSwipeInfo.isHidden = false self.tableView.reloadData() } } } var accounts: [Account]? var selectedIndex: Int? // MARK:- Life Cycle override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) self.setupNormalNavigation() self.navigationItem.title = "recipient_listing_title_text".localized() configureLanguage() fetchReceipients() } override func viewDidLoad() { super.viewDidLoad() // self.navigationController?.interactivePopGestureRecognizer?.isEnabled = false self.setupDelegates() self.showProgressHud() viewAddRecipient.layer.cornerRadius = 10 NotificationCenter.default.addObserver(self, selector: #selector(setupTabItem), name: NSNotification.Name(LCLLanguageChangeNotification), object: nil) } func configureLanguage() { self.labelSwipeInfo.text = "edit_delete_hint_ios_text".localized() // self.barButton.title = StringConstant.sendMoneyText self.newRecipeintLabel.text = "new_recipient".localized() } override func viewWillDisappear(_ animated: Bool) { super.viewWillDisappear(animated) // self.title = "" self.navigationItem.title = "" } // MARK:- IBAction @IBAction func loadMoreAction(_ sender: Any) { print("load more") } @IBAction func addNewRecipientTap(_ sender: UITapGestureRecognizer) { self.showAddNewReciepientViewController() } // MARK:- other functions private func setupDelegates() { self.tableView.delegate = self self.tableView.dataSource = self } override func setupTabItem() { let image = UIImage.init(named: "ic-sendmoney") self.tabBarItem = UITabBarItem(title: "send_money_title_text".localized(), image: image, selectedImage: nil) self.tabBarItem.titlePositionAdjustment = UIOffset(horizontal: 0, vertical: UI_USER_INTERFACE_IDIOM() == .pad ? 2 : -6) } } extension RecipientListViewController: UITableViewDelegate,UITableViewDataSource { func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat { return 120.0 } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return self.reciepients?.count ?? 0 } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "recipientList", for: indexPath) as! RecipientListTableViewCell cell.model = self.reciepients?.elementAt(index: indexPath.row) cell.setup() return cell } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { // show the action sheet to choose the payment method. startSendMoneyProcess(index: indexPath.row) } private func showMethodSelection(for index: Int) { self.selectedIndex = index // let wireframe = PaymentSelectionWireframe() // wireframe.openPaymentSelection(accounts: self.accounts ?? [], onSelection: self.selectedAcunt, source: self) let wireframe = SelectPaymentWireframe() wireframe.delegate = self wireframe.openSelectPaymentUsingPanModal(with: self.accounts ?? [], in: self) } func selectedAcunt(acunt: Account) { guard let index = self.selectedIndex else {return} if let navigation = self.navigationController { if let reciepient = self.reciepients?.elementAt(index: index) { let wireframe = SendMoneyParentWireframe() wireframe.open(for: reciepient, with: acunt, in: navigation) } } } private func startSendMoneyProcess(index: Int) { // TODO:- for kftc enable this in next release self.showMethodSelection(for: index) } func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? { let delete = UITableViewRowAction(style: .destructive, title: "delete_text".localized()) { (action, indexPath) in self.alertWithOkCancel(message: "delete_recipient_confirmation_text".localized(), title: "alert_text".localized(), OkStyle: UIAlertActionStyle.destructive, okAction: { let defaults = UserDefaults.standard let myUsername = defaults.string(forKey: "com.gmeremit.username") ?? "" self.showProgressHud() if let reciepient = self.reciepients?.elementAt(index: indexPath.row) { self.showProgressHud() self.deleteRecipient(username: myUsername, reciepient: reciepient, success: { (reciepient) in DispatchQueue.main.async { self.hideProgressHud() } guard let deletedPerson = reciepient else {return} if let index = self.reciepients?.index(where: { ($0.recipientId ?? "") == (deletedPerson.recipientId ?? "") }) { UIView.animate(withDuration: 0.5, animations: { tableView.beginUpdates() self.reciepients?.remove(at: index) let indexPath = IndexPath(item: index, section: 0) self.tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.fade) tableView.endUpdates() }) } }, failure: { (error) in self.alert(message: error.localizedDescription) self.hideProgressHud() }) } }) } let edit = UITableViewRowAction(style: .normal, title: "edit_text".localized()) { (action, indexPath) in guard let navigation = self.navigationController else {return} if let reciepient = self.reciepients?.elementAt(index: indexPath.row) { let wireFrame = EditReciepientWireframe() wireFrame.edit(reciepient: reciepient, source: navigation) } } edit.backgroundColor = UIColor.init(hex: "#F39826") delete.backgroundColor = UIColor.init(hex: "DE333C") return [delete, edit] } func fetchReceipients() { let defaults = UserDefaults.standard let myUsername = defaults.string(forKey: "com.gmeremit.username") ?? "" self.fetchReciepientList(username: myUsername, success: { (reciepients) in self.hideProgressHud() // TODO self.accounts = reciepients?.accounts self.reciepients = reciepients?.reciepients }) { (error) in self.hideProgressHud() self.alert(message: error.localizedDescription) } } // private func private func showAddNewReciepientViewController() { let viewcontroller = AddReciepientWireframe().getMainView() self.navigationController?.pushViewController(viewcontroller, animated: true) } } extension RecipientListViewController: FetchRecipientList, DeleteRecipientService { } extension RecipientListViewController: SelectPaymentDelegate { func selectPayment(_ viewController: SelectPaymentViewController, selectedAccount: Account) { self.selectedAcunt(acunt: selectedAccount) viewController.dismiss(animated: true, completion: nil) } func selectPayment(_ viewController: SelectPaymentViewController, error: Error) { viewController.dismiss(animated: true, completion: nil) } }