// // RecipientListViewController.swift // GMERemittance // // Created by Fm-user on 12/21/17. // Copyright © 2017 Gobal Money Express Co. Ltd. All rights reserved. // import UIKit class RecipientListViewController: UIViewController { // 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! // MARK:- properties var reciepients: [Recipient]? { didSet { if (reciepients ?? []).isEmpty { }else { self.tableView.isHidden = false self.labelSwipeInfo.isHidden = false self.tableView.reloadData() } } } // MARK:- Life Cycle override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) self.setupNormalNavigation() self.navigationItem.title = "Select Recipient" fetchReceipients() } override func viewDidLoad() { super.viewDidLoad() self.setupDelegates() self.showProgressHud() viewAddRecipient.layer.cornerRadius = 10 } 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 } } 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) { startSendMoneyProcess(index: indexPath.row) } private func startSendMoneyProcess(index: Int) { if let navigation = self.navigationController { if let reciepient = self.reciepients?.elementAt(index: index) { let wireframe = SendMoneyParentWireframe() wireframe.open(for: reciepient, in: navigation) } } } func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? { let delete = UITableViewRowAction(style: .destructive, title: "Delete") { (action, indexPath) in self.alertWithOkCancel(message: "Do you want to delete this receipient?", title: "Alert!", 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() }) } // self.fetchReciepientList(username: myUsername, success: { (_recipients) in // self.hideProgressHud() // self.reciepients = _recipients // }, failure: { (error) in // self.hideProgressHud() // self.alert(message: error.localizedDescription) // }) }, failure: { (error) in self.alert(message: error.localizedDescription) self.hideProgressHud() }) } }) } let edit = UITableViewRowAction(style: .normal, title: "Edit") { (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() self.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 { }