// // 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 { @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! var reciepients: [Recipient]? { didSet { if (reciepients ?? []).isEmpty { // self.showAddNewReciepientViewController() }else { self.tableView.isHidden = false self.labelSwipeInfo.isHidden = false self.tableView.reloadData() } } } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) setupNavigation() // self.title = "Send Money" self.navigationItem.title = "Send Money" fetchReceipients() } override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) } override func viewDidLoad() { super.viewDidLoad() self.setupDelegates() self.showProgressHud() viewAddRecipient.layer.cornerRadius = 10 } override func viewDidDisappear(_ animated: Bool){ super.viewDidDisappear(animated) } private func setupDelegates() { self.tableView.delegate = self self.tableView.dataSource = self } @IBAction func loadMoreAction(_ sender: Any) { print("load more") } override func viewWillDisappear(_ animated: Bool) { super.viewWillDisappear(animated) // self.title = "" self.navigationItem.title = "" } @IBAction func addNewRecipientTap(_ sender: UITapGestureRecognizer) { self.showAddNewReciepientViewController() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } } 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) { if let navigation = self.navigationController { if let reciepient = self.reciepients?.elementAt(index: indexPath.row) { 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 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 setupNavigation() { self.navigationController?.navigationBar self.navigationController?.navigationBar.backIndicatorImage = UIImage(named: "backIconBlack") self.navigationController?.navigationBar.backIndicatorTransitionMaskImage = UIImage(named: "backIconBlack") self.navigationController?.navigationBar.tintColor = .black self.navigationController?.navigationBar.barTintColor = .white } private func showAddNewReciepientViewController() { let viewcontroller = AddReciepientWireframe().getMainView() self.navigationController?.pushViewController(viewcontroller, animated: true) } } extension RecipientListViewController: FetchRecipientList, DeleteRecipientService { }