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.
 
 
 
 

179 lines
4.5 KiB

//
// SelectPaymentViewController.swift
// GME Remit
//
// Created by InKwon Devik Kim on 30/04/2019.
//Copyright © 2019 Gobal Money Express Co. Ltd. All rights reserved.
//
import UIKit
import PanModal
protocol SelectPaymentDelegate: class {
func selectPayment(_ viewController: SelectPaymentViewController, selectedAccount: Account)
func selectPayment(_ viewController: SelectPaymentViewController, error: Error)
}
class SelectPaymentViewController: UIViewController {
// MARK: Properties
var presenter: SelectPaymentModuleInterface?
weak var delegate: SelectPaymentDelegate?
private var accounts: [Account]? {
didSet {
self.wallet = self.accounts?.filter {$0.type == "wallet"}.first
self.autoDebits = self.accounts?.filter {$0.type == "autodebit"}
self.tableView.reloadData()
}
}
private var wallet: Account?
private var autoDebits: [Account]?
private enum Section: Int {
case wallet = 0
case autoDebit
}
private struct StringConstants {
let closeText = "penny_test_close_text".localized()
}
// MARK: IBOutlets
@IBOutlet weak var tableView: UITableView!
// MARK: VC's Life cycle
override func viewDidLoad() {
super.viewDidLoad()
self.setup()
self.setLargeTitle()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.title = "Select Payment"
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.title = ""
}
// MARK: IBActions
// MARK: Other Functions
private func setup() {
// all setup should be done here
self.tableView.delegate = self
self.tableView.dataSource = self
self.presenter?.viewIsReady()
}
}
// MARK: SelectPaymentViewInterface
extension SelectPaymentViewController: SelectPaymentViewInterface {
func setAccountModels(with models: [Account]?) {
self.accounts = models
}
}
extension SelectPaymentViewController: UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
guard let sectionType = Section(rawValue: section) else {return 0}
switch sectionType {
case .wallet:
return self.wallet != nil ? 1 : 0
case .autoDebit:
return self.autoDebits?.count ?? 0
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "PaymentCell") as? PaymentCell else {
return UITableViewCell()
}
guard let sectionType = Section(rawValue: indexPath.section) else {return UITableViewCell()}
switch sectionType {
case .wallet:
cell.setModel(with: self.wallet)
return cell
case .autoDebit:
cell.setModel(with: self.autoDebits?[indexPath.row])
return cell
}
}
}
extension SelectPaymentViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
guard let sectionType = Section(rawValue: section) else {return ""}
switch sectionType {
case .wallet: return "GME WALLET"
case .autoDebit: return "AUTO DEBIT"
}
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 70.0
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
guard let sectionType = Section(rawValue: indexPath.section) else {
return
}
let account: Account?
switch sectionType {
case .wallet:
account = self.wallet
case .autoDebit:
account = self.autoDebits?[indexPath.row]
}
guard let selectedAccount = account else {
let error = NSError(
domain: "Error",
code: 0,
userInfo: [NSLocalizedDescriptionKey : "Raised something error"]
)
self.delegate?.selectPayment(self, error: error)
return
}
self.delegate?.selectPayment(self, selectedAccount: selectedAccount)
}
}
extension SelectPaymentViewController: PanModalPresentable {
var panScrollable: UIScrollView? {
return self.tableView
}
var shortFormHeight: PanModalHeight {
let height: CGFloat = CGFloat(self.accounts?.count ?? 0) * 70.0 + (tableView.sectionHeaderHeight * 2)
return .contentHeight(height)
}
var longFormHeight: PanModalHeight {
return .maxHeight
}
}