Fetch TokenScript files for contracts that are auto-detected from transactions immediately instead of waiting till the next launch

pull/1623/head
Hwee-Boon Yar 5 years ago
parent 2b53f3b2e8
commit dc3ae6c4ce
  1. 12
      AlphaWallet/InCoordinator.swift
  2. 10
      AlphaWallet/Tokens/Coordinators/SingleChainTokenCoordinator.swift
  3. 12
      AlphaWallet/Transactions/Storage/TransactionsStorage.swift
  4. 2
      AlphaWalletTests/Factories/FakeTransactionsStorage.swift

@ -114,7 +114,7 @@ class InCoordinator: NSObject, Coordinator {
private func createTransactionsStorage(server: RPCServer) -> TransactionsStorage { private func createTransactionsStorage(server: RPCServer) -> TransactionsStorage {
let realm = self.realm(forAccount: wallet) let realm = self.realm(forAccount: wallet)
return TransactionsStorage(realm: realm, server: server) return TransactionsStorage(realm: realm, server: server, delegate: self)
} }
private func fetchCryptoPrice(forServer server: RPCServer) { private func fetchCryptoPrice(forServer server: RPCServer) {
@ -665,7 +665,7 @@ extension InCoordinator: SettingsCoordinatorDelegate {
func delete(account: Wallet, in coordinator: SettingsCoordinator) { func delete(account: Wallet, in coordinator: SettingsCoordinator) {
let realm = self.realm(forAccount: account) let realm = self.realm(forAccount: account)
for each in RPCServer.allCases { for each in RPCServer.allCases {
let transactionsStorage = TransactionsStorage(realm: realm, server: each) let transactionsStorage = TransactionsStorage(realm: realm, server: each, delegate: nil)
transactionsStorage.deleteAll() transactionsStorage.deleteAll()
} }
} }
@ -739,3 +739,11 @@ extension InCoordinator: UITabBarControllerDelegate {
} }
} }
} }
extension InCoordinator: TransactionsStorageDelegate {
func didAddTokensWith(contracts: [AlphaWallet.Address], inTransactionsStorage: TransactionsStorage) {
for each in contracts {
assetDefinitionStore.fetchXML(forContract: each)
}
}
}

@ -360,7 +360,7 @@ class SingleChainTokenCoordinator: Coordinator {
private func createTransactionsStore() -> TransactionsStorage? { private func createTransactionsStore() -> TransactionsStorage? {
guard let wallet = keystore.recentlyUsedWallet else { return nil } guard let wallet = keystore.recentlyUsedWallet else { return nil }
let realm = self.realm(forAccount: wallet) let realm = self.realm(forAccount: wallet)
return TransactionsStorage(realm: realm, server: session.server) return TransactionsStorage(realm: realm, server: session.server, delegate: self)
} }
private func realm(forAccount account: Wallet) -> Realm { private func realm(forAccount account: Wallet) -> Realm {
@ -562,6 +562,14 @@ extension SingleChainTokenCoordinator: TokenInstanceActionViewControllerDelegate
} }
} }
extension SingleChainTokenCoordinator: TransactionsStorageDelegate {
func didAddTokensWith(contracts: [AlphaWallet.Address], inTransactionsStorage: TransactionsStorage) {
for each in contracts {
assetDefinitionStore.fetchXML(forContract: each)
}
}
}
/// Failure to obtain contract data may be due to no-connectivity. So we should check .failed(networkReachable: Bool) /// Failure to obtain contract data may be due to no-connectivity. So we should check .failed(networkReachable: Bool)
func fetchContractDataFor(address: AlphaWallet.Address, storage: TokensDataStore, assetDefinitionStore: AssetDefinitionStore, completion: @escaping (ContractData) -> Void) { func fetchContractDataFor(address: AlphaWallet.Address, storage: TokensDataStore, assetDefinitionStore: AssetDefinitionStore, completion: @escaping (ContractData) -> Void) {
var completedName: String? var completedName: String?

@ -1,13 +1,20 @@
import Foundation import Foundation
import RealmSwift import RealmSwift
protocol TransactionsStorageDelegate: class {
func didAddTokensWith(contracts: [AlphaWallet.Address], inTransactionsStorage: TransactionsStorage)
}
class TransactionsStorage { class TransactionsStorage {
let realm: Realm private let realm: Realm
private let delegate: TransactionsStorageDelegate?
let server: RPCServer let server: RPCServer
init(realm: Realm, server: RPCServer) { init(realm: Realm, server: RPCServer, delegate: TransactionsStorageDelegate?) {
self.realm = realm self.realm = realm
self.server = server self.server = server
self.delegate = delegate
} }
var count: Int { var count: Int {
@ -40,6 +47,7 @@ class TransactionsStorage {
private func addTokensWithContractAddresses(fromTransactions transactions: [Transaction], contractsAndTokenTypes: [AlphaWallet.Address: TokenType]) { private func addTokensWithContractAddresses(fromTransactions transactions: [Transaction], contractsAndTokenTypes: [AlphaWallet.Address: TokenType]) {
let tokens = self.tokens(from: transactions, contractsAndTokenTypes: contractsAndTokenTypes) let tokens = self.tokens(from: transactions, contractsAndTokenTypes: contractsAndTokenTypes)
delegate?.didAddTokensWith(contracts: Array(Set(tokens.map { $0.address})), inTransactionsStorage: self)
if !tokens.isEmpty { if !tokens.isEmpty {
TokensDataStore.update(in: realm, tokens: tokens) TokensDataStore.update(in: realm, tokens: tokens)
} }

@ -7,6 +7,6 @@ import RealmSwift
class FakeTransactionsStorage: TransactionsStorage { class FakeTransactionsStorage: TransactionsStorage {
convenience init() { convenience init() {
let realm = try! Realm(configuration: Realm.Configuration(inMemoryIdentifier: "MyInMemoryRealm")) let realm = try! Realm(configuration: Realm.Configuration(inMemoryIdentifier: "MyInMemoryRealm"))
self.init(realm: realm, server: .main) self.init(realm: realm, server: .main, delegate: nil)
} }
} }

Loading…
Cancel
Save