Skip to content

Instantly share code, notes, and snippets.

@gilbok
Created June 19, 2020 07:45
Show Gist options
  • Save gilbok/78e87407fbdb8ac53384d4170b581b87 to your computer and use it in GitHub Desktop.
Save gilbok/78e87407fbdb8ac53384d4170b581b87 to your computer and use it in GitHub Desktop.
Issue
//
// HistoryTransactionViewModel.swift
// MamiKos
//
// Created by Mamikos on 19/05/20.
// Copyright © 2020 GIT. All rights reserved.
//
import RxSwift
import RxCocoa
import SwiftyJSON
class HistoryTransactionViewModel: NetworkViewModel {
let disposeBag = DisposeBag()
var transactionHistoryResponse = BehaviorRelay<NetworkResponse?>(value: nil)
var transactionHistories = BehaviorRelay<[TransactionHistoryModel]?>(value: nil)
var total = 0
var offset = 0
static let limit = 10
override init() {
super.init()
registerObserver()
}
fileprivate func registerObserver() {
transactionHistoryResponse.subscribe(onNext: { [weak self] response in
self?.isLoading.accept(false)
self?.process(response: response, onSuccess: self?.handleTransactionHistoryResponse(response:))
}).disposed(by: disposeBag)
}
fileprivate func handleTransactionHistoryResponse(response networkResponse: NetworkResponse) {
let json = JSON(networkResponse.response!)
printIfDebug("transaction history: \(json)")
if json["status"].boolValue == true {
total = json["data"]["total"].intValue
var transactions: [TransactionHistoryModel] = []
if let tempTransactions = transactionHistories.value, tempTransactions.count > 0 {
printIfDebug(tempTransactions.count)
transactions = tempTransactions
}
for object in json["data"]["transactions"].arrayValue {
transactions.append(TransactionHistoryModel(object))
}
printIfDebug(transactions.count)
transactionHistories.accept(transactions)
} else {
transactionHistories.accept([])
}
}
func loadTransactionHistory() {
if offset <= total || transactionHistories.value == nil {
isLoading.accept(true)
let params: [String: String] = [
"offset": offset.string,
"limit": HistoryTransactionViewModel.limit.string
]
HistoryTransactionDataSource.getTransactionHistory(params: params, networkRespose: transactionHistoryResponse)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment