An advanced Ethereum/EVM mobile wallet
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.
alpha-wallet-ios/Trust/Transactions/TransactionDataStore.swift

58 lines
1.3 KiB

// Copyright SIX DAY LLC. All rights reserved.
7 years ago
import Foundation
import APIKit
import Result
enum TransactionError: Error {
case failedToFetch
}
enum TokenError: Error {
case failedToFetch
}
7 years ago
protocol TransactionDataStoreDelegate: class {
func didUpdate(result: Result<TransactionsViewModel, TransactionError>)
7 years ago
}
class TransactionDataStore {
7 years ago
var viewModel: TransactionsViewModel {
return .init(transactions: transactions)
}
weak var delegate: TransactionDataStoreDelegate?
7 years ago
let account: Account
var transactions: [Transaction] = []
var tokens: [Token] = []
7 years ago
init(account: Account) {
self.account = account
}
7 years ago
func fetch() {
fetchTransactions()
7 years ago
}
7 years ago
func update(transactions: [Transaction]) {
self.transactions = transactions
delegate?.didUpdate(result: .success(viewModel))
7 years ago
}
func update(tokens: [Token]) {
self.tokens = tokens
7 years ago
}
func fetchTransactions() {
let request = FetchTransactionsRequest(address: account.address.address)
Session.send(request) { result in
switch result {
case .success(let response):
self.update(transactions: response)
case .failure:
self.delegate?.didUpdate(result: .failure(TransactionError.failedToFetch))
7 years ago
}
}
}
}