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.
 
 
 
 

421 lines
14 KiB

//
// 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 loadMoreOutlet: UIButton!
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var viewAddRecipient: UIView!
@IBOutlet weak var labelSwipeInfo: UILabel!
private var isNewRecipient: Bool = false
private var recipientIndex: Int?
// private var tracktransactionviewmodel = TrackTransactionViewModel()
private var recipientviewmodel = RecipientViewModel()
private var activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView()
var nameArray: [String] = [String]()
var recipientArray : [Recipient] = []
static var recipientPageNumber: Int?
private var page: Int = 0
private var size: Int = 20
private var checkConnectionTimeOutDialog: Int = 0
var selectedRow: Int?
static var recipientStatus = true
var vietnamStatus = false
override func viewWillAppear(_ animated: Bool) {
}
override func viewDidAppear(_ animated: Bool) {
setUpAnotherLoginListener(genericviewmodel: recipientviewmodel)
}
override func viewDidLoad() {
super.viewDidLoad()
self.reloadRecipientData()
viewAddRecipient.layer.cornerRadius = 10
setUpNavBar(id: 2, title: "Send Money")
viewAddRecipient.isHidden = true
loadMoreOutlet.isHidden = true
self.startLoading()
// recipientviewmodel.internetConnection.value = nil
internetCheck()
recipientviewmodel.recipientConnectionTimeOut.value = nil
/**
connection timeout
*/
recipientviewmodel.recipientConnectionTimeOut.bind { [unowned self] in
guard $0 != nil else {
return
}
self.stopLoading()
if self.checkConnectionTimeOutDialog == 0{
self.checkConnectionTimeOutDialog = self.checkConnectionTimeOutDialog+1
self.popUpMessage(value: 20)
}
}
recipientviewmodel.recipientDeletedTimeOut.value = nil
/**
connection timeout
*/
recipientviewmodel.recipientDeletedTimeOut.bind { [unowned self] in
guard $0 != nil else {
return
}
self.stopLoading()
if self.checkConnectionTimeOutDialog == 0{
self.checkConnectionTimeOutDialog = self.checkConnectionTimeOutDialog+1
self.popUpMessage(value: 20)
}
}
recipientList()
recipientDelete()
}
override func viewDidDisappear(_ animated: Bool){
self.checkConnectionTimeOutDialog = 0
}
/**
Disable user interaction while fetching data from api
*/
func startLoading(){
self.showActivityIndicator(activityIndicator: self.activityIndicator)
self.disableUserInteractions()
}
/**
Enable user interaction while fetching data from api
*/
func stopLoading(){
self.dismissActivityIndicator(activityIndicator: self.activityIndicator)
self.enableUserInteractions()
}
/**
Check internet connection
*/
func internetCheck(){
recipientviewmodel.internetConnection.bind { [unowned self] in
guard $0 != nil else {
return
}
self.stopLoading()
self.popUpMessage(value: 15)
}
}
/**
Request api for recipient list
*/
func recipientList(){
self.recipientviewmodel.recipientListObtained.bind { [unowned self] in
guard $0 != nil else {
return
}
self.stopLoading()
guard $0! else {
//self.popUpMessageError(value: 10, message: self.recipientviewmodel.getErrorMessage())
return
}
self.addRecipientValueInPagination()
guard self.recipientArray.count == 0 else {
self.viewAddRecipient.isHidden = false
self.tableView.isHidden = false
self.labelSwipeInfo.isHidden = false
self.loadTableView()
return
}
self.loadTableView()
self.isNewRecipient = true
if RecipientListViewController.recipientStatus == true {
self.performSegue(withIdentifier: "addRecipient", sender: nil)
}
}
}
/**
Recall the recipient list after deleting the recipient
*/
func recipientDelete(){
recipientviewmodel.recipientDeleted.bind { [unowned self] in
guard $0 != nil else {
return
}
self.stopLoading()
guard $0! else {
// self.popUpMessageError(value: 10, message: self.recipientviewmodel.getErrorMessage())
return
}
self.stopLoading()
self.nameArray.removeAll()
if let row = self.selectedRow{
self.recipientArray.remove(at: row)
self.loadTableView()
}
}
}
/**
Reloading the tableview
*/
func loadTableView() {
for recipient in self.recipientArray {
let middleName = recipient.middleName
if middleName == nil {
self.nameArray.append(recipient.firstName + " " + recipient.lastName)
} else {
self.nameArray.append(recipient.firstName + " " + recipient.middleName! + " " + recipient.lastName)
}
}
self.tableView.delegate = self
self.tableView.dataSource = self
self.tableView.reloadData()
loadMoreVisibility(size: self.recipientviewmodel.getRecipientArray().count)
if self.nameArray.count == 0 {
labelSwipeInfo.isHidden = true
} else {
labelSwipeInfo.isHidden = false
}
}
/**
Add load more value in existing collection object
*/
func addRecipientValueInPagination(){
self.nameArray.removeAll()
self.page = self.page+1
if self.recipientviewmodel.getRecipientArray().count >= self.size{
self.recipientArray.append(contentsOf:self.recipientviewmodel.getRecipientArray())
}else if self.recipientviewmodel.getRecipientArray().count < self.size{
self.recipientArray.append(contentsOf:self.recipientviewmodel.getRecipientArray())
self.loadMoreOutlet.isHidden = true
}else{
self.loadMoreOutlet.isHidden = false
}
}
/**
Call when load more button is click
*/
func reloadRecipientData(){
self.startLoading()
self.tableView.isHidden = true
self.labelSwipeInfo.isHidden = true
nameArray.removeAll()
recipientArray.removeAll()
loadMoreOutlet.isHidden = true
recipientviewmodel.getAllRecipientsThroughPagination(size: self.size, page: self.page)
}
/**
Show load more button only if more data is available
*/
func loadMoreVisibility(size: Int){
if self.recipientviewmodel.getRecipientArray().count < self.size || self.recipientviewmodel.getRecipientArray().count == 0{
self.loadMoreOutlet.isHidden = true
}else{
self.loadMoreOutlet.isHidden = false
}
}
/**
Api request to get recipeints data through pagination
- parameter page: data fetch as per page number
- parameter size: total size to be fetched
*/
func fetchDataAndRestoreTableList(page:Int,size:Int){
self.recipientviewmodel.getAllRecipientsThroughPagination(size: size, page: page)
}
@IBAction func loadMoreAction(_ sender: Any) {
showActivityIndicator(activityIndicator: activityIndicator)
disableUserInteractions()
fetchDataAndRestoreTableList(page: self.page, size: self.size)
}
override func viewWillDisappear(_ animated: Bool) {
RecipientListViewController.recipientStatus = true
self.viewAddRecipient.isHidden = false
}
@IBAction func addNewRecipientTap(_ sender: UITapGestureRecognizer) {
isNewRecipient = true
performSegue(withIdentifier: "addRecipient", sender: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func getRandomColor() -> UIColor{
//Generate between 0 to 1
let red: CGFloat = CGFloat(drand48())
let green: CGFloat = CGFloat(drand48())
let blue: CGFloat = CGFloat(drand48())
return UIColor(red:red, green: green, blue: blue, alpha: 1.0)
}
}
extension RecipientListViewController: UITableViewDelegate,UITableViewDataSource {
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return 120.0
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return nameArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "recipientList", for: indexPath) as! RecipientListTableViewCell
if nameArray.indices.contains(indexPath.row){
cell.labelRecipientName.text = nameArray[indexPath.row]
cell.labelInitial.text = nameArray[indexPath.row].prefix(1).uppercased()
cell.labelInitial.layer.cornerRadius = cell.labelInitial.frame.size.width/2
cell.labelInitial.backgroundColor = getRandomColor()
cell.layer.borderColor = UIColor(red:0.91, green:0.93, blue:0.95, alpha:1.0).cgColor
cell.layer.borderWidth = 1
cell.layer.cornerRadius = 10
cell.clipsToBounds = true
}
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.recipientIndex = indexPath.row
performSegue(withIdentifier: "paymentMethod", sender: nil)
}
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let delete = UITableViewRowAction(style: .destructive, title: "Delete") { (action, indexPath) in
let alert = UIAlertController(title: "Are you sure?",
message: "",
preferredStyle: .alert)
let cancelAction = UIAlertAction(title: "No", style: .destructive) { (action) -> Void in
alert.dismiss(animated: true, completion: nil)
}
cancelAction.setValue(UIColor.black, forKey: "titleTextColor")
let deleteAction = UIAlertAction(title: "Yes", style: .destructive) { (action) -> Void in
alert.dismiss(animated: true, completion: nil)
self.startLoading()
self.selectedRow = indexPath.row
self.recipientviewmodel.removeRecipient(recipientId: self.recipientArray[indexPath.row].recipientId)
}
alert.addAction(cancelAction)
alert.addAction(deleteAction)
self.present(alert, animated: true, completion: nil)
}
let edit = UITableViewRowAction(style: .normal, title: "Edit") { (action, indexPath) in
self.selectedRow = indexPath.row
self.isNewRecipient = false
self.recipientIndex = indexPath.row
self.performSegue(withIdentifier: "addRecipient", sender: nil)
}
edit.backgroundColor = UIColor(hex:0xEC1C24)
delete.backgroundColor = UIColor.black
return [delete, edit]
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
switch segue.identifier {
case "addRecipient"?:
if (!isNewRecipient) {
let recipientViewController
= segue.destination as! RecipientFormViewController
recipientViewController.setPageNumber(pageNumber: self.page);
recipientViewController.paginationDelegate = self
if let index = self.recipientIndex{
recipientViewController.recipient = self.recipientArray[index]
}
}else {
let recipientViewController
= segue.destination as! RecipientFormViewController
recipientViewController.setPageNumber(pageNumber: self.page);
recipientViewController.paginationDelegate = self
}
case "paymentMethod"?:
let paymentViewController
= segue.destination as! PaymentMethodViewController
paymentViewController.recipient = self.recipientArray[self.recipientIndex!]
default:
return
}
}
}
extension RecipientListViewController: PaginationDelegate {
func updateCurrentIndex(status: String, recipient: Recipient, pageNumber: Int) {
self.page = pageNumber
if status == "update"{
self.startLoading()
nameArray.removeAll()
recipientArray[self.selectedRow!] = recipient
self.loadTableView()
self.stopLoading()
}else if status == "add"{
self.page = 0
reloadRecipientData()
}
}
}