-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cache additional balances of domain (#458)
- Loading branch information
1 parent
1bbd2ca
commit 2233391
Showing
3 changed files
with
54 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
unstoppable-ios-app/domains-manager-ios/Entities/Storages/WalletBalancesStorage.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// | ||
// WalletBalancesStorage.swift | ||
// domains-manager-ios | ||
// | ||
// Created by Oleg Kuplin on 25.03.2024. | ||
// | ||
|
||
import Foundation | ||
|
||
final class WalletBalancesStorage { | ||
|
||
private static let storageFileName = "wallets-balances-entities.data" | ||
|
||
private init() {} | ||
static let instance = WalletBalancesStorage() | ||
private var storage = SpecificStorage<[StoredWalletBalances]>(fileName: WalletBalancesStorage.storageFileName) | ||
|
||
func getCachedBalancesFor(domainName: DomainName) -> [WalletTokenPortfolio] { | ||
(storage.retrieve() ?? []).first(where: { $0.domainName == domainName })?.balances ?? [] | ||
} | ||
|
||
func cacheBalances(_ balances: [WalletTokenPortfolio], for domainName: DomainName) { | ||
var storedBalances = storage.retrieve() ?? [] | ||
let newBalance = StoredWalletBalances(domainName: domainName, balances: balances) | ||
if let i = storedBalances.firstIndex(where: { $0.domainName == domainName }) { | ||
storedBalances[i] = newBalance | ||
} else { | ||
storedBalances.append(newBalance) | ||
} | ||
|
||
set(newBalances: storedBalances) | ||
} | ||
|
||
private func set(newBalances: [StoredWalletBalances]) { | ||
storage.store(newBalances) | ||
} | ||
} | ||
|
||
// MARK: - Private methods | ||
private extension WalletBalancesStorage { | ||
struct StoredWalletBalances: Codable { | ||
let domainName: String | ||
let balances: [WalletTokenPortfolio] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters