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.
 
 
 
 

216 lines
7.9 KiB

//
// RecipientListViewController.swift
// GMERemittance
//
// Created by Fm-user on 12/21/17.
// Copyright © 2017 Gobal Money Express Co. Ltd. All rights reserved.
//
import UIKit
import RAMAnimatedTabBarController
class RecipientListViewController: UIViewController {
struct StringConstant {
static let swipeText = "Swipe left to “Edit” or “Delete” recipient profile."
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 barButton: RAMAnimatedTabBarItem!
@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 acunts: [Account]?
var selectedIndex: Int?
// MARK:- Life Cycle
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.setupNormalNavigation()
self.navigationItem.title = StringConstant.navBarTitle
fetchReceipients()
}
override func viewDidLoad() {
super.viewDidLoad()
// self.navigationController?.interactivePopGestureRecognizer?.isEnabled = false
self.setupDelegates()
self.showProgressHud()
viewAddRecipient.layer.cornerRadius = 10
configureLanguage()
}
func configureLanguage() {
self.labelSwipeInfo.text = StringConstant.swipeText
// self.barButton.title = StringConstant.sendMoneyText
self.newRecipeintLabel.text = StringConstant.newRecipeintText
}
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) {
// 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(acunts: self.acunts ?? [], onSelection: self.selectedAcunt, source: self)
}
func selectedAcunt(acunt: Account) {
print(acunt.bankName)
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) {
// first show the account selection page
// then start the send money process
self.showMethodSelection(for: index)
}
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()
})
}
}, 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()
// TODO
self.acunts = reciepients?.acunts
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 {
}