diff --git a/AlphaWallet/InCoordinator.swift b/AlphaWallet/InCoordinator.swift index 6387d4941..698810c27 100644 --- a/AlphaWallet/InCoordinator.swift +++ b/AlphaWallet/InCoordinator.swift @@ -114,7 +114,7 @@ class InCoordinator: NSObject, Coordinator { private func createTransactionsStorage(server: RPCServer) -> TransactionsStorage { 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) { @@ -665,7 +665,7 @@ extension InCoordinator: SettingsCoordinatorDelegate { func delete(account: Wallet, in coordinator: SettingsCoordinator) { let realm = self.realm(forAccount: account) for each in RPCServer.allCases { - let transactionsStorage = TransactionsStorage(realm: realm, server: each) + let transactionsStorage = TransactionsStorage(realm: realm, server: each, delegate: nil) 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) + } + } +} diff --git a/AlphaWallet/Tokens/Coordinators/SingleChainTokenCoordinator.swift b/AlphaWallet/Tokens/Coordinators/SingleChainTokenCoordinator.swift index 2946ad45e..43b3d41ec 100644 --- a/AlphaWallet/Tokens/Coordinators/SingleChainTokenCoordinator.swift +++ b/AlphaWallet/Tokens/Coordinators/SingleChainTokenCoordinator.swift @@ -360,7 +360,7 @@ class SingleChainTokenCoordinator: Coordinator { private func createTransactionsStore() -> TransactionsStorage? { guard let wallet = keystore.recentlyUsedWallet else { return nil } 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 { @@ -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) func fetchContractDataFor(address: AlphaWallet.Address, storage: TokensDataStore, assetDefinitionStore: AssetDefinitionStore, completion: @escaping (ContractData) -> Void) { var completedName: String? diff --git a/AlphaWallet/Transactions/Storage/TransactionsStorage.swift b/AlphaWallet/Transactions/Storage/TransactionsStorage.swift index 21bf5b9d3..5ddf0b011 100644 --- a/AlphaWallet/Transactions/Storage/TransactionsStorage.swift +++ b/AlphaWallet/Transactions/Storage/TransactionsStorage.swift @@ -1,13 +1,20 @@ import Foundation import RealmSwift +protocol TransactionsStorageDelegate: class { + func didAddTokensWith(contracts: [AlphaWallet.Address], inTransactionsStorage: TransactionsStorage) +} + class TransactionsStorage { - let realm: Realm + private let realm: Realm + private let delegate: TransactionsStorageDelegate? + let server: RPCServer - init(realm: Realm, server: RPCServer) { + init(realm: Realm, server: RPCServer, delegate: TransactionsStorageDelegate?) { self.realm = realm self.server = server + self.delegate = delegate } var count: Int { @@ -40,6 +47,7 @@ class TransactionsStorage { private func addTokensWithContractAddresses(fromTransactions transactions: [Transaction], contractsAndTokenTypes: [AlphaWallet.Address: TokenType]) { let tokens = self.tokens(from: transactions, contractsAndTokenTypes: contractsAndTokenTypes) + delegate?.didAddTokensWith(contracts: Array(Set(tokens.map { $0.address})), inTransactionsStorage: self) if !tokens.isEmpty { TokensDataStore.update(in: realm, tokens: tokens) } diff --git a/AlphaWalletTests/Factories/FakeTransactionsStorage.swift b/AlphaWalletTests/Factories/FakeTransactionsStorage.swift index dae8eabeb..d9c8f683c 100644 --- a/AlphaWalletTests/Factories/FakeTransactionsStorage.swift +++ b/AlphaWalletTests/Factories/FakeTransactionsStorage.swift @@ -7,6 +7,6 @@ import RealmSwift class FakeTransactionsStorage: TransactionsStorage { convenience init() { let realm = try! Realm(configuration: Realm.Configuration(inMemoryIdentifier: "MyInMemoryRealm")) - self.init(realm: realm, server: .main) + self.init(realm: realm, server: .main, delegate: nil) } }