Before fixes

mail@pastecode.io avatar
unknown
swift
2 years ago
3.3 kB
2
Indexable
Never
//
//  TransactionsViewModel.swift
//  eToto
//
//  Created by Hristo Hristov on 16.03.21.
//  Copyright © 2021 Mobile Wave Solutions. All rights reserved.
//

import UIKit
import SkeletonView

class TransactionsViewModel {
    
    private weak var view: TransactionHistoryViewController?
    private var networkManager: NetworkManagerType { NetworkManager.shared }
    private(set) var isLoading = false
    
    lazy var fromDate = toDate.addingTimeInterval(-3600*24*7)
    lazy var toDate = Date()
    private(set) var transactions: [Transaction]? {
        didSet {
            view?.tableView.backgroundLabelText = (transactions?.isEmpty ?? true) ? NSLocalizedString("NoOperationsForFilters") : nil
            view?.view.hideSkeleton(reloadDataAfter: true, transition: .crossDissolve(0.25))
            view?.tableView.reloadData()
        }
    }
    private var offset: Int = 0
    var filter: String? {
        didSet {
            transactions = nil
            offset = 0
            getTransactions()
        }
    }
    
    init(view: TransactionHistoryViewController) {
        self.view = view
    }
    
    func getTransactions(completion: (() -> ())? = nil) {
        isLoading = true
        networkManager.transactions(from: fromDate, to: toDate, pageSize: 50, offset: offset)
        { [weak self] (result) in
            self?.isLoading = false
            DispatchQueue.main.async {
                switch result {
                case .success(let transactions):
                    if self?.transactions == nil {
                        if let newTransactions = transactions {
                            self?.offset = newTransactions.count
                            if self?.filter == nil {
                                self?.transactions = newTransactions.filter{$0.typeOfOperation != "NA"}
                            } else {
                                self?.transactions = newTransactions.filter{$0.typeOfOperation != "NA" && $0.typeOfOperation == self?.filter}
                            }
                        }
                        else {
                            self?.view?.tableView.backgroundLabelText = NSLocalizedString("NoOperationsForFilters")
                            self?.view?.view.hideSkeleton(reloadDataAfter: true, transition: .crossDissolve(0.25))
                        }
                    }
                    else if let newTransactions = transactions{
                        self?.offset = (self?.offset ?? 0) + newTransactions.count
                        if self?.filter == nil {
                            self?.transactions?.append(contentsOf: newTransactions.filter{$0.typeOfOperation != "NA"})
                        } else {
                            self?.transactions?.append(contentsOf: newTransactions.filter{$0.typeOfOperation != "NA" && $0.typeOfOperation == self?.filter})
                        }
                    }
                case .failure:
                    self?.view?.tableView.backgroundLabelText = NSLocalizedString("Operation Failed. Try again.")
                    self?.view?.view.hideSkeleton(reloadDataAfter: true, transition: .crossDissolve(0.25))
                }
            }
            completion?()
        }
    }
    
    func clearTransactions() {
        transactions = nil
        offset = 0
    }
}