Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Packages/FeatureServices/WalletService/WalletService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public struct WalletService: Sendable {
self.keystore = keystore
self.walletStore = walletStore
self.avatarService = avatarService
walletSessionService = WalletSessionService(walletStore: walletStore, preferences: preferences)
self.walletSessionService = WalletSessionService(walletStore: walletStore, preferences: preferences)
self.preferences = preferences
}

Expand Down Expand Up @@ -103,11 +103,15 @@ public struct WalletService: Sendable {
try await keystore.deleteKey(for: wallet)
try walletStore.deleteWallet(for: wallet.walletId)
try avatarService.remove(for: wallet)
WalletPreferences(walletId: wallet.walletId).clear()

await MainActor.run {
if currentWalletId == wallet.walletId {
walletSessionService.setCurrent(walletId: wallets.first?.walletId)
}
if wallets.isEmpty {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be be under WalletPreferences(walletId: wallet.walletId).clear()

Preferences.standard.clear()
}
}
}

Expand Down
34 changes: 10 additions & 24 deletions Packages/Preferences/Sources/WalletPreferences.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,18 @@ public final class WalletPreferences: @unchecked Sendable {
}

private let defaults: UserDefaults
private let suiteName: String

public init(walletId: WalletId) {
self.defaults = Self.suite(walletId: walletId.id)
self.suiteName = Self.suiteName(walletId: walletId.id)
self.defaults = UserDefaults(suiteName: suiteName)!
}

public var completeInitialLoadAssets: Bool {
set { defaults.setValue(newValue, forKey: Keys.completeInitialLoadAssets) }
get { defaults.bool(forKey: Keys.completeInitialLoadAssets) }
}

public var transactionsTimestamp: Int {
set { defaults.setValue(newValue, forKey: Keys.transactionsTimestamp) }
get { defaults.integer(forKey: Keys.transactionsTimestamp) }
Expand All @@ -38,17 +40,17 @@ public final class WalletPreferences: @unchecked Sendable {
set { defaults.setValue(newValue, forKey: Keys.assetsTimestamp) }
get { defaults.integer(forKey: Keys.assetsTimestamp) }
}

public var completeInitialAddressStatus: Bool {
set { defaults.setValue(newValue, forKey: Keys.completeInitialAddressStatus) }
get { defaults.bool(forKey: Keys.completeInitialAddressStatus) }
}

public func completeInitialSynchronization() {
completeInitialAddressStatus = true
completeInitialLoadAssets = true
}

// transactions
public func setTransactionsForAssetTimestamp(assetId: String, value: Int) {
defaults.setValue(value, forKey: String(format: "%@_%@", Keys.transactionsForAsset, assetId))
Expand All @@ -59,26 +61,10 @@ public final class WalletPreferences: @unchecked Sendable {
}

public func clear() {
defaults.dictionaryRepresentation().keys.forEach {
defaults.removeObject(forKey: $0)
}
}

private func encode(key: String, data: Codable) {
if let encoded = try? JSONEncoder().encode(data) {
defaults.set(encoded, forKey: key)
}
}

private func decode<T: Codable>(key: String) -> T? {
if let data = defaults.object(forKey: key) as? Data,
let typedData = try? JSONDecoder().decode(T.self, from: data){
return typedData
}
return .none
UserDefaults.standard.removePersistentDomain(forName: suiteName)
}

private static func suite(walletId: String) -> UserDefaults {
UserDefaults(suiteName: "wallet_preferences_\(walletId)_v2")!
private static func suiteName(walletId: String) -> String {
"wallet_preferences_\(walletId)_v2"
}
}
1 change: 0 additions & 1 deletion Packages/Preferences/Tests/WalletPreferencesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,5 @@ struct WalletPreferencesTests {
#expect(!preferences.completeInitialLoadAssets)
#expect(!preferences.completeInitialAddressStatus)
#expect(preferences.transactionsForAssetTimestamp(assetId: asset.id.identifier) == 0)

}
}