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.
 
 
 
 

237 lines
7.4 KiB

//
// ResendViewController.swift
// GME Remit
//
// Created by gme_2 on 18/03/2019.
//Copyright © 2019 Gobal Money Express Co. Ltd. All rights reserved.
//
import UIKit
import Localize_Swift
// func getParams(model: SendMoneyRequestModel, reciepient: Recipient) -> [String: String] { // last api call
class ResendRequestModel {
var exchangeRateDetail: SendMoneyExchangeRateModel?
var autoDebitAccount: Account?
var paymemtMode: SendMoneyPayoutMode?
var bankAgent: SendMoneyBank?
var branch: SendMoneyBankBranch?
var accountNumber: String?
var payingAmount: String?
var transactionPassword: String?
var transaction: ResendTransactionModel?
var reciepient: Recipient?
}
class ResendViewController: UIViewController {
struct Constant {
static let heightOfDateRangeLabel: CGFloat = 35
}
struct StringConstants {
let searchPlaceholderText = "resend_search_hint_text".localized()
}
// MARK: IBOutlets
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var dateRangeLabelHeightConstraint: NSLayoutConstraint!
@IBOutlet weak var emptyLabel: UILabel!
@IBOutlet weak var fromDateLabel: UILabel!
@IBOutlet weak var toDateLabel: UILabel!
@IBOutlet weak var searchTextField: UITextField!
// MARK: Properties
var requestModel: ResendRequestModel?
var from: String?
var to: String?
var model: ResendListModel? {
didSet {
self.transactions = model?.transactions ?? []
self.accounts = model?.account ?? []
}
}
var accounts: [Account] = []
var transactions: [ResendTransactionModel] = [] {
didSet {
self.filteredTransaction = transactions
}
}
var filteredTransaction: [ResendTransactionModel] = [] {
didSet {
self.emptyLabel.isHidden = !transactions.isEmpty
self.tableView.reloadData()
guard let fromDate = from, let toDate = to else {
self.dateRangeLabelHeightConstraint.constant = 0
return
}
self.fromDateLabel.text = self.getHUmanReadableDate(date: fromDate)
self.toDateLabel.text = self.getHUmanReadableDate(date: toDate)
UIView.animate(withDuration: 0.33, animations: {
self.dateRangeLabelHeightConstraint.constant = Constant.heightOfDateRangeLabel
})
}
}
var selectedIndex: Int?
var presenter: ResendModuleInterface?
// MARK: VC's Life cycle
override func viewDidLoad() {
super.viewDidLoad()
self.setupNormalNavigation()
self.setup()
self.presenter?.viewIsReady()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationItem.title = "resend_money_text".localized()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationItem.title = ""
}
// MARK: IBActions
@IBAction func openSearchFilter(_ sender: UIButton) {
// open search filter
let viewController = UIStoryboard.init(name: "TransactionHistoryDatePicker", bundle: nil).instantiateViewController(withIdentifier: "DatePickerViewController") as! DatePickerViewController
viewController.completion = self.selectedDate
self.present(viewController, animated: true, completion: nil)
}
// MARK: Other Functions
private func setup() {
// all setup should be done here
self.requestModel = ResendRequestModel()
setupSearchTextField()
setupDelegates()
self.dateRangeLabelHeightConstraint.constant = 0
}
private func selectedDate(from: String?, to: String?) {
self.from = from
self.to = to
self.fetchList(fromDate: from, toDate: to)
// TODO
}
private func fetchList(fromDate: String? = nil, toDate: String? = nil) {
guard let fromDate = fromDate, let toDate = toDate else { return }
self.presenter?.fetchList(fromDate: fromDate, toDate: toDate)
}
private func setupSearchTextField() {
searchTextField.placeholder = StringConstants().searchPlaceholderText
let imageview = UIImageView(image: #imageLiteral(resourceName: "ic_search"))
self.searchTextField.leftView = imageview
self.searchTextField.leftViewMode = .always
self.searchTextField.addTarget(self, action: #selector(search(sender:)), for: UIControlEvents.editingChanged)
}
@objc func search(sender: UITextField) {
let searchText = sender.text!.lowercased()
self.presenter?.filter(searchText: searchText)
}
private func setupDelegates() {
self.tableView.delegate = self
self.tableView.dataSource = self
}
private func showMethodSelection(for index: Int) {
self.selectedIndex = index
let wireframe = PaymentSelectionWireframe()
wireframe.openPaymentSelection(accounts: self.accounts ?? [], onSelection: self.selectedAcunt, source: self)
}
func selectedAcunt(acunt: Account) {
guard let index = self.selectedIndex, let transaction = self.filteredTransaction.elementAt(index: index), let requestModel = self.requestModel, let transactionId = self.filteredTransaction.elementAt(index: index)?.transactionId else {return}
requestModel.autoDebitAccount = acunt
requestModel.transaction = transaction
if let navigation = self.navigationController {
let wireframe = ResendExchangeWireframe()
wireframe.openExchangeWireFrame(model: requestModel, transactionId: transactionId , source: navigation)
}
}
private func getHUmanReadableDate(date: String?) -> String? {
if let date = DateFormatter.toDate(dateString: date ?? "", format: AppConstants.dateFormat) {
let dateString = DateFormatter.toString(date: date, format: AppConstants.humanReadableDateFormat)
return dateString
}
return nil
}
}
// MARK: TransactionHistoryViewInterface
extension ResendViewController: ResendViewInterface {
func show(model: ResendListModel) {
self.model = model
}
func showLoading() {
self.showProgressHud()
}
func hideLoading() {
self.hideProgressHud()
}
func show(error: String) {
self.alert(message: error)
}
func show(models: [ResendTransactionModel]) {
self.filteredTransaction = models
}
}
// MARK: TableViewDelegate
extension ResendViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.selectedIndex = indexPath.row
self.showMethodSelection(for: indexPath.row)
}
}
// MARK: TableViewDatasource
extension ResendViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return filteredTransaction.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ResendTableViewCell") as! ResendTableViewCell
cell.model = self.filteredTransaction.elementAt(index: indexPath.row)
cell.setup()
return cell
}
}