diff --git a/Packages/FeatureServices/WalletService/WalletService.swift b/Packages/FeatureServices/WalletService/WalletService.swift index aae19e9e0..268494ab0 100644 --- a/Packages/FeatureServices/WalletService/WalletService.swift +++ b/Packages/FeatureServices/WalletService/WalletService.swift @@ -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 } @@ -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 { + Preferences.standard.clear() + } } } diff --git a/Packages/Preferences/Sources/WalletPreferences.swift b/Packages/Preferences/Sources/WalletPreferences.swift index df9b6ca32..3be13e4f0 100644 --- a/Packages/Preferences/Sources/WalletPreferences.swift +++ b/Packages/Preferences/Sources/WalletPreferences.swift @@ -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) } @@ -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)) @@ -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(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" } } diff --git a/Packages/Preferences/Tests/WalletPreferencesTests.swift b/Packages/Preferences/Tests/WalletPreferencesTests.swift index 28d177a97..9a7444c9b 100644 --- a/Packages/Preferences/Tests/WalletPreferencesTests.swift +++ b/Packages/Preferences/Tests/WalletPreferencesTests.swift @@ -60,6 +60,5 @@ struct WalletPreferencesTests { #expect(!preferences.completeInitialLoadAssets) #expect(!preferences.completeInitialAddressStatus) #expect(preferences.transactionsForAssetTimestamp(assetId: asset.id.identifier) == 0) - } }