Merge pull request #3160 from vladyslav-iosdev/#3159

Remove dublication of TransactionsStorage #3159
pull/3161/head
Hwee-Boon Yar 3 years ago committed by GitHub
commit f505c5d97d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      AlphaWallet/Core/Coordinators/WalletBalance/WalletBalanceCoordinator.swift
  2. 32
      AlphaWallet/Core/Coordinators/WalletBalance/WalletBalanceFetcher.swift
  3. 5
      AlphaWallet/InCoordinator.swift

@ -18,6 +18,8 @@ protocol WalletBalanceCoordinatorType: AnyObject {
func start()
func refreshBalance()
func refreshEthBalance()
func transactionsStorage(wallet: Wallet, server: RPCServer) -> TransactionsStorage
}
class WalletBalanceCoordinator: NSObject, WalletBalanceCoordinatorType {
@ -94,6 +96,10 @@ class WalletBalanceCoordinator: NSObject, WalletBalanceCoordinatorType {
}
}
func transactionsStorage(wallet: Wallet, server: RPCServer) -> TransactionsStorage {
balanceFetchers[wallet]!.transactionsStorage(server: server)
}
private var availableTokenObjects: ServerDictionary<[TokenMappedToTicker]> {
let uniqueTokenObjectsOfAllWallets = Set(balanceFetchers.flatMap { (_, value) in value.tokenObjects })

@ -29,7 +29,9 @@ protocol WalletBalanceFetcherType: AnyObject {
func update(servers: [RPCServer])
func refreshEthBalance()
func refreshBalance()
func transactionsStorage(server: RPCServer) -> TransactionsStorage
}
typealias WalletBalanceFetcherSubServices = (tokensDataStore: PrivateTokensDatastoreType, balanceFetcher: PrivateBalanceFetcherType, transactionsStorage: TransactionsStorage)
class WalletBalanceFetcher: NSObject, WalletBalanceFetcherType {
private static let updateBalanceInterval: TimeInterval = 60
@ -38,10 +40,10 @@ class WalletBalanceFetcher: NSObject, WalletBalanceFetcherType {
private let assetDefinitionStore: AssetDefinitionStore
private (set) lazy var subscribableWalletBalance: Subscribable<WalletBalance> = .init(balance)
let tokensChangeSubscribable: Subscribable<Void> = .init(nil)
private var tokensDataStores: ServerDictionary<(PrivateTokensDatastoreType, PrivateBalanceFetcherType, TransactionsStorage)> = .init()
private var services: ServerDictionary<WalletBalanceFetcherSubServices> = .init()
var tokenObjects: [Activity.AssignedToken] {
tokensDataStores.flatMap { $0.value.0.tokenObjects }
services.flatMap { $0.value.tokensDataStore.tokenObjects }
}
private let queue: DispatchQueue
@ -67,7 +69,7 @@ class WalletBalanceFetcher: NSObject, WalletBalanceFetcherType {
balanceFetcher.erc721TokenIdsFetcher = transactionsStorage
balanceFetcher.delegate = self
self.tokensDataStores[each] = (tokensDatastore, balanceFetcher, transactionsStorage)
self.services[each] = (tokensDatastore, balanceFetcher, transactionsStorage)
}
coinTickersFetcher.tickersSubscribable.subscribe { [weak self] _ in
@ -80,9 +82,13 @@ class WalletBalanceFetcher: NSObject, WalletBalanceFetcherType {
}
}
func transactionsStorage(server: RPCServer) -> TransactionsStorage {
services[server].transactionsStorage
}
func update(servers: [RPCServer]) {
for each in servers {
if tokensDataStores[safe: each] != nil {
if services[safe: each] != nil {
//no-op
} else {
let transactionsStorage = TransactionsStorage(realm: realm, server: each, delegate: nil)
@ -91,20 +97,20 @@ class WalletBalanceFetcher: NSObject, WalletBalanceFetcherType {
balanceFetcher.erc721TokenIdsFetcher = transactionsStorage
balanceFetcher.delegate = self
tokensDataStores[each] = (tokensDatastore, balanceFetcher, transactionsStorage)
services[each] = (tokensDatastore, balanceFetcher, transactionsStorage)
}
}
let delatedServers = tokensDataStores.filter { !servers.contains($0.key) }.map { $0.key }
let delatedServers = services.filter { !servers.contains($0.key) }.map { $0.key }
for each in delatedServers {
tokensDataStores.remove(at: each)
services.remove(at: each)
}
}
private func notifyUpdateTokenBalancesSubscribers() {
for each in cache.values {
guard let tokensDatastore = tokensDataStores[safe: each.key.server] else { continue }
guard let tokenObject = tokensDatastore.0.tokenObject(contract: each.key.address) else {
guard let tokensDatastore = services[safe: each.key.server] else { continue }
guard let tokenObject = tokensDatastore.tokensDataStore.tokenObject(contract: each.key.address) else {
continue
}
@ -143,7 +149,7 @@ class WalletBalanceFetcher: NSObject, WalletBalanceFetcherType {
}
func subscribableTokenBalance(addressAndRPCServer: AddressAndRPCServer) -> Subscribable<BalanceBaseViewModel> {
guard let tokensDatastore = tokensDataStores[safe: addressAndRPCServer.server] else { return .init(nil) }
guard let tokensDatastore = services[safe: addressAndRPCServer.server] else { return .init(nil) }
guard let tokenObject = tokensDatastore.0.tokenObject(contract: addressAndRPCServer.address) else {
return .init(nil)
@ -207,19 +213,19 @@ class WalletBalanceFetcher: NSObject, WalletBalanceFetcherType {
}
private func timedCallForBalanceRefresh() {
for each in tokensDataStores {
for each in services {
each.value.1.refreshBalance(updatePolicy: .all, force: false)
}
}
func refreshEthBalance() {
for each in tokensDataStores {
for each in services {
each.value.1.refreshBalance(updatePolicy: .eth, force: true)
}
}
func refreshBalance() {
for each in tokensDataStores {
for each in services {
each.value.1.refreshBalance(updatePolicy: .ercTokens, force: true)
}
}

@ -243,7 +243,10 @@ class InCoordinator: NSObject, Coordinator {
}
private func createTransactionsStorage(server: RPCServer) -> TransactionsStorage {
return TransactionsStorage(realm: realm, server: server, delegate: self)
let storage = walletBalanceCoordinator.transactionsStorage(wallet: wallet, server: server)
storage.delegate = self
return storage
}
private func oneTimeCreationOfOneDatabaseToHoldAllChains() {

Loading…
Cancel
Save