From 4f21ea07ee1064285332309995cd8b4a73a11434 Mon Sep 17 00:00:00 2001 From: gemdev111 Date: Thu, 12 Sep 2024 13:39:01 +0300 Subject: [PATCH 1/4] Hide chains filter, when wallet with 1 chain selected Added WalletSupportedChains model to omit code dublications --- Gem.xcodeproj/project.pbxproj | 4 +++ .../SelectAssetSceneNavigationStack.swift | 27 ++++++++------- Gem/Assets/Types/WalletSupportedChains.swift | 33 +++++++++++++++++++ Gem/Assets/ViewModels/AddTokenViewModel.swift | 9 ++--- .../ViewModels/SelectAssetViewModel.swift | 11 +++++-- 5 files changed, 63 insertions(+), 21 deletions(-) create mode 100644 Gem/Assets/Types/WalletSupportedChains.swift diff --git a/Gem.xcodeproj/project.pbxproj b/Gem.xcodeproj/project.pbxproj index 561331b1..a5b717a6 100644 --- a/Gem.xcodeproj/project.pbxproj +++ b/Gem.xcodeproj/project.pbxproj @@ -20,6 +20,7 @@ 8314DA8D2C57D51E00C83F7D /* QRScanResources.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8314DA8C2C57D51E00C83F7D /* QRScanResources.swift */; }; 832553982C0F27F000C9CA0C /* ChartScene.swift in Sources */ = {isa = PBXBuildFile; fileRef = 832553972C0F27F000C9CA0C /* ChartScene.swift */; }; 8325539A2C0FB11C00C9CA0C /* CurrencySceneViewModelTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 832553992C0FB11C00C9CA0C /* CurrencySceneViewModelTests.swift */; }; + 83352B642C92F898003D52F3 /* WalletSupportedChains.swift in Sources */ = {isa = PBXBuildFile; fileRef = 83352B632C92F898003D52F3 /* WalletSupportedChains.swift */; }; 833A0C852C80C11B0004DBA9 /* ChainsFilterType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 833A0C842C80C11B0004DBA9 /* ChainsFilterType.swift */; }; 833A0C872C80C2A50004DBA9 /* ChainsFilterViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 833A0C862C80C2A50004DBA9 /* ChainsFilterViewModel.swift */; }; 835644FD2C501C2200C6B4DB /* SwapError.swift in Sources */ = {isa = PBXBuildFile; fileRef = 835644FC2C501C2200C6B4DB /* SwapError.swift */; }; @@ -580,6 +581,7 @@ 8314DA8C2C57D51E00C83F7D /* QRScanResources.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = QRScanResources.swift; sourceTree = ""; }; 832553972C0F27F000C9CA0C /* ChartScene.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ChartScene.swift; sourceTree = ""; }; 832553992C0FB11C00C9CA0C /* CurrencySceneViewModelTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CurrencySceneViewModelTests.swift; sourceTree = ""; }; + 83352B632C92F898003D52F3 /* WalletSupportedChains.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WalletSupportedChains.swift; sourceTree = ""; }; 833A0C842C80C11B0004DBA9 /* ChainsFilterType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChainsFilterType.swift; sourceTree = ""; }; 833A0C862C80C2A50004DBA9 /* ChainsFilterViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChainsFilterViewModel.swift; sourceTree = ""; }; 835644FC2C501C2200C6B4DB /* SwapError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwapError.swift; sourceTree = ""; }; @@ -1418,6 +1420,7 @@ C3D1C4EF2A2EBB0C006E8EEA /* RecipientAddress.swift */, 8374DC592C6CDF6C00183109 /* AddTokenInput.swift */, 833A0C842C80C11B0004DBA9 /* ChainsFilterType.swift */, + 83352B632C92F898003D52F3 /* WalletSupportedChains.swift */, ); path = Types; sourceTree = ""; @@ -2451,6 +2454,7 @@ D847E3512ABCD49C00A56F07 /* TransactionsService.swift in Sources */, D8B978022AAA4C28009933B5 /* NotificationsViewModel.swift in Sources */, C3FB448E2A7AE4070056F124 /* NameResolveViewModel.swift in Sources */, + 83352B642C92F898003D52F3 /* WalletSupportedChains.swift in Sources */, D8B978002AAA4B97009933B5 /* NotificationsScene.swift in Sources */, 83AE20732C7CB43500A38809 /* AssetsFilterScene.swift in Sources */, D80700502BBB6ABA00ED770B /* CreateWalletNavigationStack.swift in Sources */, diff --git a/Gem/Assets/Navigation/SelectAssetSceneNavigationStack.swift b/Gem/Assets/Navigation/SelectAssetSceneNavigationStack.swift index ccf28088..4875e0af 100644 --- a/Gem/Assets/Navigation/SelectAssetSceneNavigationStack.swift +++ b/Gem/Assets/Navigation/SelectAssetSceneNavigationStack.swift @@ -38,20 +38,23 @@ struct SelectAssetSceneNavigationStack: View { .accessibilityIdentifier("cancel") } if model.showAddToken { - ToolbarItem(placement: .navigationBarTrailing) { - Button { - isPresentingFilteringView = true - } label: { - if model.filterModel.isCusomFilteringSpecified { - Image(systemName: SystemImage.filterFill) - .symbolRenderingMode(.palette) - .foregroundStyle(Colors.whiteSolid, Colors.blue) - } else { - Image(systemName: SystemImage.filter) - .foregroundStyle(.primary) + + if model.showFiltering { + ToolbarItem(placement: .navigationBarTrailing) { + Button { + isPresentingFilteringView = true + } label: { + if model.filterModel.isCusomFilteringSpecified { + Image(systemName: SystemImage.filterFill) + .symbolRenderingMode(.palette) + .foregroundStyle(Colors.whiteSolid, Colors.blue) + } else { + Image(systemName: SystemImage.filter) + .foregroundStyle(.primary) + } } + .contentTransition(.symbolEffect(.replace)) } - .contentTransition(.symbolEffect(.replace)) } ToolbarItem(placement: .navigationBarTrailing) { diff --git a/Gem/Assets/Types/WalletSupportedChains.swift b/Gem/Assets/Types/WalletSupportedChains.swift new file mode 100644 index 00000000..2cb64f37 --- /dev/null +++ b/Gem/Assets/Types/WalletSupportedChains.swift @@ -0,0 +1,33 @@ +// Copyright (c). Gem Wallet. All rights reserved. + +import Foundation +import Primitives +import Settings + +struct WalletSupportedChains { + private let chains: [Chain] + + init(wallet: Wallet) { + let walletChains = wallet.accounts.map { $0.chain }.asSet() + let supportedChains = AssetConfiguration.supportedChainsWithTokens + + self.chains = walletChains.intersection(supportedChains).asArray() + } + + var sortedByRank: [Chain] { + chains.sorted { AssetScore.defaultRank(chain: $0) > AssetScore.defaultRank(chain: $1) } + } + + var hasMany: Bool { + chains.count > 1 + } + + var hasOne: Bool { + chains.count == 1 + } + + var isEmpty: Bool { + chains.isEmpty + } +} + diff --git a/Gem/Assets/ViewModels/AddTokenViewModel.swift b/Gem/Assets/ViewModels/AddTokenViewModel.swift index a100a37a..9c5f4b37 100644 --- a/Gem/Assets/ViewModels/AddTokenViewModel.swift +++ b/Gem/Assets/ViewModels/AddTokenViewModel.swift @@ -3,7 +3,6 @@ import Foundation import Primitives import SwiftUI -import Settings import Blockchain import Components import Style @@ -20,12 +19,8 @@ class AddTokenViewModel { init(wallet: Wallet, service: AddTokenService) { self.service = service - - let availableChains = AssetConfiguration.supportedChainsWithTokens.asSet() - .intersection(wallet.accounts.map { $0.chain }.asSet()) - .asArray() - .sorted { AssetScore.defaultRank(chain: $0) > AssetScore.defaultRank(chain: $1) } - self.input = AddTokenInput(chains: availableChains) + let chains = WalletSupportedChains(wallet: wallet) + self.input = AddTokenInput(chains: chains.sortedByRank) } var title: String { Localized.Wallet.AddToken.title } diff --git a/Gem/Assets/ViewModels/SelectAssetViewModel.swift b/Gem/Assets/ViewModels/SelectAssetViewModel.swift index 72b75163..b01b8a96 100644 --- a/Gem/Assets/ViewModels/SelectAssetViewModel.swift +++ b/Gem/Assets/ViewModels/SelectAssetViewModel.swift @@ -2,7 +2,6 @@ import Foundation import Keystore import Primitives import Store -import Settings import Components @Observable @@ -53,7 +52,11 @@ class SelectAssetViewModel { } var showAddToken: Bool { - selectType == .manage && wallet.accounts.map { $0.chain }.asSet().intersection(AssetConfiguration.supportedChainsWithTokens).count > 0 + selectType == .manage && !walletChains.isEmpty + } + + var showFiltering: Bool { + selectType == .manage && walletChains.hasMany } } @@ -78,6 +81,10 @@ extension SelectAssetViewModel { // MARK: - Private extension SelectAssetViewModel { + private var walletChains: WalletSupportedChains { + WalletSupportedChains(wallet: wallet) + } + private func chains(for type: WalletType) -> [Chain] { switch wallet.type { case .single, .view, .privateKey: [wallet.accounts.first?.chain].compactMap { $0 } From 32089cb4302856db0b7ba900ccd6870bc9540ace Mon Sep 17 00:00:00 2001 From: gemdev111 Date: Thu, 12 Sep 2024 17:19:14 +0300 Subject: [PATCH 2/4] Updates, show always filter only for wallet chains --- .../SelectAssetSceneNavigationStack.swift | 27 +++++++++---------- .../ViewModels/AssetsFilterViewModel.swift | 4 ++- .../ViewModels/SelectAssetViewModel.swift | 4 --- 3 files changed, 15 insertions(+), 20 deletions(-) diff --git a/Gem/Assets/Navigation/SelectAssetSceneNavigationStack.swift b/Gem/Assets/Navigation/SelectAssetSceneNavigationStack.swift index 4875e0af..ccf28088 100644 --- a/Gem/Assets/Navigation/SelectAssetSceneNavigationStack.swift +++ b/Gem/Assets/Navigation/SelectAssetSceneNavigationStack.swift @@ -38,23 +38,20 @@ struct SelectAssetSceneNavigationStack: View { .accessibilityIdentifier("cancel") } if model.showAddToken { - - if model.showFiltering { - ToolbarItem(placement: .navigationBarTrailing) { - Button { - isPresentingFilteringView = true - } label: { - if model.filterModel.isCusomFilteringSpecified { - Image(systemName: SystemImage.filterFill) - .symbolRenderingMode(.palette) - .foregroundStyle(Colors.whiteSolid, Colors.blue) - } else { - Image(systemName: SystemImage.filter) - .foregroundStyle(.primary) - } + ToolbarItem(placement: .navigationBarTrailing) { + Button { + isPresentingFilteringView = true + } label: { + if model.filterModel.isCusomFilteringSpecified { + Image(systemName: SystemImage.filterFill) + .symbolRenderingMode(.palette) + .foregroundStyle(Colors.whiteSolid, Colors.blue) + } else { + Image(systemName: SystemImage.filter) + .foregroundStyle(.primary) } - .contentTransition(.symbolEffect(.replace)) } + .contentTransition(.symbolEffect(.replace)) } ToolbarItem(placement: .navigationBarTrailing) { diff --git a/Gem/Assets/ViewModels/AssetsFilterViewModel.swift b/Gem/Assets/ViewModels/AssetsFilterViewModel.swift index e962df11..17ee61d7 100644 --- a/Gem/Assets/ViewModels/AssetsFilterViewModel.swift +++ b/Gem/Assets/ViewModels/AssetsFilterViewModel.swift @@ -7,7 +7,8 @@ import Settings struct AssetsFilterViewModel { private let type: SelectAssetType - let allChains: [Chain] = AssetConfiguration.allChains + + let allChains: [Chain] var assetsRequest: AssetsRequest @@ -25,6 +26,7 @@ struct AssetsFilterViewModel { } init(wallet: Wallet, type: SelectAssetType) { + self.allChains = WalletSupportedChains(wallet: wallet).sortedByRank self.assetsRequest = AssetsRequest( walletID: wallet.id, filters: Self.defaultFilters(type: type) diff --git a/Gem/Assets/ViewModels/SelectAssetViewModel.swift b/Gem/Assets/ViewModels/SelectAssetViewModel.swift index b01b8a96..e2dd6eef 100644 --- a/Gem/Assets/ViewModels/SelectAssetViewModel.swift +++ b/Gem/Assets/ViewModels/SelectAssetViewModel.swift @@ -54,10 +54,6 @@ class SelectAssetViewModel { var showAddToken: Bool { selectType == .manage && !walletChains.isEmpty } - - var showFiltering: Bool { - selectType == .manage && walletChains.hasMany - } } // MARK: - Business Logic From 21b34972279a776e15271491924c6df1edfb807f Mon Sep 17 00:00:00 2001 From: gemdev111 Date: Thu, 12 Sep 2024 17:50:36 +0300 Subject: [PATCH 3/4] Updates for model, separation supportedChainsWithTokens & allChains --- Gem/Assets/Types/WalletSupportedChains.swift | 43 ++++++++----------- Gem/Assets/ViewModels/AddTokenViewModel.swift | 4 +- .../ViewModels/AssetsFilterViewModel.swift | 2 +- .../ViewModels/SelectAssetViewModel.swift | 6 +-- Gem/Wallet/Services/WalletsService.swift | 5 +-- 5 files changed, 24 insertions(+), 36 deletions(-) diff --git a/Gem/Assets/Types/WalletSupportedChains.swift b/Gem/Assets/Types/WalletSupportedChains.swift index 2cb64f37..99347631 100644 --- a/Gem/Assets/Types/WalletSupportedChains.swift +++ b/Gem/Assets/Types/WalletSupportedChains.swift @@ -4,30 +4,25 @@ import Foundation import Primitives import Settings -struct WalletSupportedChains { - private let chains: [Chain] - - init(wallet: Wallet) { - let walletChains = wallet.accounts.map { $0.chain }.asSet() - let supportedChains = AssetConfiguration.supportedChainsWithTokens - - self.chains = walletChains.intersection(supportedChains).asArray() - } - - var sortedByRank: [Chain] { - chains.sorted { AssetScore.defaultRank(chain: $0) > AssetScore.defaultRank(chain: $1) } - } - - var hasMany: Bool { - chains.count > 1 - } - - var hasOne: Bool { - chains.count == 1 - } +enum WalletSupportedChains { + case all + case withTokens +} - var isEmpty: Bool { - chains.isEmpty +// MARK: - Model extensions + +extension Primitives.Wallet { + func chains(type: WalletSupportedChains) -> [Chain] { + let supportedChains: [Chain] + switch type { + case .all: + supportedChains = AssetConfiguration.allChains + case .withTokens: + supportedChains = AssetConfiguration.supportedChainsWithTokens + } + + let walletChains = accounts.map { $0.chain }.asSet() + return walletChains.intersection(supportedChains).asArray() + .sorted { AssetScore.defaultRank(chain: $0) > AssetScore.defaultRank(chain: $1) } } } - diff --git a/Gem/Assets/ViewModels/AddTokenViewModel.swift b/Gem/Assets/ViewModels/AddTokenViewModel.swift index 9c5f4b37..5341e88f 100644 --- a/Gem/Assets/ViewModels/AddTokenViewModel.swift +++ b/Gem/Assets/ViewModels/AddTokenViewModel.swift @@ -19,8 +19,8 @@ class AddTokenViewModel { init(wallet: Wallet, service: AddTokenService) { self.service = service - let chains = WalletSupportedChains(wallet: wallet) - self.input = AddTokenInput(chains: chains.sortedByRank) + let chains = wallet.chains(type: .withTokens) + self.input = AddTokenInput(chains: chains) } var title: String { Localized.Wallet.AddToken.title } diff --git a/Gem/Assets/ViewModels/AssetsFilterViewModel.swift b/Gem/Assets/ViewModels/AssetsFilterViewModel.swift index 17ee61d7..019b8cf1 100644 --- a/Gem/Assets/ViewModels/AssetsFilterViewModel.swift +++ b/Gem/Assets/ViewModels/AssetsFilterViewModel.swift @@ -26,12 +26,12 @@ struct AssetsFilterViewModel { } init(wallet: Wallet, type: SelectAssetType) { - self.allChains = WalletSupportedChains(wallet: wallet).sortedByRank self.assetsRequest = AssetsRequest( walletID: wallet.id, filters: Self.defaultFilters(type: type) ) self.type = type + self.allChains = wallet.chains(type: .all) } static func defaultFilters(type: SelectAssetType) -> [AssetsRequestFilter] { diff --git a/Gem/Assets/ViewModels/SelectAssetViewModel.swift b/Gem/Assets/ViewModels/SelectAssetViewModel.swift index e2dd6eef..3d5b5a8a 100644 --- a/Gem/Assets/ViewModels/SelectAssetViewModel.swift +++ b/Gem/Assets/ViewModels/SelectAssetViewModel.swift @@ -52,7 +52,7 @@ class SelectAssetViewModel { } var showAddToken: Bool { - selectType == .manage && !walletChains.isEmpty + selectType == .manage && !filterModel.allChains.isEmpty } } @@ -77,10 +77,6 @@ extension SelectAssetViewModel { // MARK: - Private extension SelectAssetViewModel { - private var walletChains: WalletSupportedChains { - WalletSupportedChains(wallet: wallet) - } - private func chains(for type: WalletType) -> [Chain] { switch wallet.type { case .single, .view, .privateKey: [wallet.accounts.first?.chain].compactMap { $0 } diff --git a/Gem/Wallet/Services/WalletsService.swift b/Gem/Wallet/Services/WalletsService.swift index 01287812..f5358061 100644 --- a/Gem/Wallet/Services/WalletsService.swift +++ b/Gem/Wallet/Services/WalletsService.swift @@ -230,10 +230,7 @@ class WalletsService { // func setupWallet(_ wallet: Wallet) throws { - let chains = wallet.accounts.map { $0.chain }.asSet() - .intersection(AssetConfiguration.allChains) - .map { $0 } - + let chains = wallet.chains(type: .all) try enableAssetBalances(wallet: wallet, chains: chains) } From 9b59aa0a83da77d8746cad256f62d48cad41472c Mon Sep 17 00:00:00 2001 From: gemdev111 Date: Thu, 12 Sep 2024 18:58:21 +0300 Subject: [PATCH 4/4] added tests --- Gem.xcodeproj/project.pbxproj | 16 +++++++- .../Types/WalletSupportedChainsTests.swift | 41 +++++++++++++++++++ .../TestKit/Account+PrimitivesTestKit.swift | 17 ++++++++ .../TestKit/Wallet+PrimitivesTestKit.swift | 3 +- 4 files changed, 74 insertions(+), 3 deletions(-) create mode 100644 GemTests/Assets/Types/WalletSupportedChainsTests.swift create mode 100644 Packages/Primitives/TestKit/Account+PrimitivesTestKit.swift diff --git a/Gem.xcodeproj/project.pbxproj b/Gem.xcodeproj/project.pbxproj index 2a85aec5..f959329c 100644 --- a/Gem.xcodeproj/project.pbxproj +++ b/Gem.xcodeproj/project.pbxproj @@ -20,9 +20,10 @@ 8314DA8D2C57D51E00C83F7D /* QRScanResources.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8314DA8C2C57D51E00C83F7D /* QRScanResources.swift */; }; 832553982C0F27F000C9CA0C /* ChartScene.swift in Sources */ = {isa = PBXBuildFile; fileRef = 832553972C0F27F000C9CA0C /* ChartScene.swift */; }; 8325539A2C0FB11C00C9CA0C /* CurrencySceneViewModelTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 832553992C0FB11C00C9CA0C /* CurrencySceneViewModelTests.swift */; }; - 83352B642C92F898003D52F3 /* WalletSupportedChains.swift in Sources */ = {isa = PBXBuildFile; fileRef = 83352B632C92F898003D52F3 /* WalletSupportedChains.swift */; }; 832A705E2C88CE0A00045367 /* SelectWalletScene.swift in Sources */ = {isa = PBXBuildFile; fileRef = 832A705D2C88CE0A00045367 /* SelectWalletScene.swift */; }; 832A70602C88CE9600045367 /* SellectWalletViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 832A705F2C88CE9600045367 /* SellectWalletViewModel.swift */; }; + 83352B642C92F898003D52F3 /* WalletSupportedChains.swift in Sources */ = {isa = PBXBuildFile; fileRef = 83352B632C92F898003D52F3 /* WalletSupportedChains.swift */; }; + 83352B6A2C93435F003D52F3 /* WalletSupportedChainsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 83352B692C93435F003D52F3 /* WalletSupportedChainsTests.swift */; }; 833A0C852C80C11B0004DBA9 /* ChainsFilterType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 833A0C842C80C11B0004DBA9 /* ChainsFilterType.swift */; }; 833A0C872C80C2A50004DBA9 /* ChainsFilterViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 833A0C862C80C2A50004DBA9 /* ChainsFilterViewModel.swift */; }; 835644FD2C501C2200C6B4DB /* SwapError.swift in Sources */ = {isa = PBXBuildFile; fileRef = 835644FC2C501C2200C6B4DB /* SwapError.swift */; }; @@ -583,9 +584,10 @@ 8314DA8C2C57D51E00C83F7D /* QRScanResources.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = QRScanResources.swift; sourceTree = ""; }; 832553972C0F27F000C9CA0C /* ChartScene.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ChartScene.swift; sourceTree = ""; }; 832553992C0FB11C00C9CA0C /* CurrencySceneViewModelTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CurrencySceneViewModelTests.swift; sourceTree = ""; }; - 83352B632C92F898003D52F3 /* WalletSupportedChains.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WalletSupportedChains.swift; sourceTree = ""; }; 832A705D2C88CE0A00045367 /* SelectWalletScene.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SelectWalletScene.swift; sourceTree = ""; }; 832A705F2C88CE9600045367 /* SellectWalletViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SellectWalletViewModel.swift; sourceTree = ""; }; + 83352B632C92F898003D52F3 /* WalletSupportedChains.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WalletSupportedChains.swift; sourceTree = ""; }; + 83352B692C93435F003D52F3 /* WalletSupportedChainsTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WalletSupportedChainsTests.swift; sourceTree = ""; }; 833A0C842C80C11B0004DBA9 /* ChainsFilterType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChainsFilterType.swift; sourceTree = ""; }; 833A0C862C80C2A50004DBA9 /* ChainsFilterViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChainsFilterViewModel.swift; sourceTree = ""; }; 835644FC2C501C2200C6B4DB /* SwapError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwapError.swift; sourceTree = ""; }; @@ -1029,6 +1031,14 @@ path = ViewModels; sourceTree = ""; }; + 83352B682C93433D003D52F3 /* Types */ = { + isa = PBXGroup; + children = ( + 83352B692C93435F003D52F3 /* WalletSupportedChainsTests.swift */, + ); + path = Types; + sourceTree = ""; + }; 83A681902C46F1E000A3636B /* Types */ = { isa = PBXGroup; children = ( @@ -1177,6 +1187,7 @@ C3549B3229C4395B00B4BE01 /* Assets */ = { isa = PBXGroup; children = ( + 83352B682C93433D003D52F3 /* Types */, C3549B3329C4396000B4BE01 /* ViewModels */, ); path = Assets; @@ -2668,6 +2679,7 @@ C3A7CB9129CFD6E600431341 /* WalletHeaderViewModelTests.swift in Sources */, D8C4A2352C8785A2006FABE8 /* FiatQuoteViewModelTests.swift in Sources */, C30952C6299C39D80004C0F9 /* walletTests.swift in Sources */, + 83352B6A2C93435F003D52F3 /* WalletSupportedChainsTests.swift in Sources */, C3A7CB9929D35EC200431341 /* WalletSceneViewModelTests.swift in Sources */, C3A7CB9729D3569C00431341 /* PriceViewModelTests.swift in Sources */, 8325539A2C0FB11C00C9CA0C /* CurrencySceneViewModelTests.swift in Sources */, diff --git a/GemTests/Assets/Types/WalletSupportedChainsTests.swift b/GemTests/Assets/Types/WalletSupportedChainsTests.swift new file mode 100644 index 00000000..233767d5 --- /dev/null +++ b/GemTests/Assets/Types/WalletSupportedChainsTests.swift @@ -0,0 +1,41 @@ +import XCTest +import Primitives +@testable import Gem + +final class WalletSupportedChainsTests: XCTestCase { + func testChainsAll() { + let wallet = Wallet.mock(accounts: [ + .mock(chain: .bitcoin), + .mock(chain: .doge), + .mock(chain: .ethereum) + ]) + + let result = wallet.chains(type: .all) + let expectedChains: [Chain] = [.bitcoin, .ethereum, .doge] + XCTAssertEqual(result, expectedChains) + } + + func testChainsWithTokens() { + let wallet = Wallet.mock(accounts: [ + .mock(chain: .bitcoin), + .mock(chain: .doge), + .mock(chain: .ethereum) + ]) + + let result = wallet.chains(type: .withTokens) + let expectedChains: [Chain] = [.ethereum] + XCTAssertEqual(result, expectedChains) + } + + func testChainSorting() { + let wallet = Wallet.mock(accounts: [ + .mock(chain: .doge), + .mock(chain: .ethereum), + .mock(chain: .bitcoin) + ]) + + let result = wallet.chains(type: .all) + let expectedChains: [Chain] = [.bitcoin, .ethereum, .doge] + XCTAssertEqual(result, expectedChains) + } +} diff --git a/Packages/Primitives/TestKit/Account+PrimitivesTestKit.swift b/Packages/Primitives/TestKit/Account+PrimitivesTestKit.swift new file mode 100644 index 00000000..3dfcbafe --- /dev/null +++ b/Packages/Primitives/TestKit/Account+PrimitivesTestKit.swift @@ -0,0 +1,17 @@ +// Copyright (c). Gem Wallet. All rights reserved. + +import Foundation +import Primitives + +public extension Account { + static func mock( + chain: Chain = .mock() + ) -> Account { + Account( + chain: chain, + address: .empty, + derivationPath: .empty, + extendedPublicKey: .empty + ) + } +} diff --git a/Packages/Primitives/TestKit/Wallet+PrimitivesTestKit.swift b/Packages/Primitives/TestKit/Wallet+PrimitivesTestKit.swift index ec58cd26..fee1ee1a 100644 --- a/Packages/Primitives/TestKit/Wallet+PrimitivesTestKit.swift +++ b/Packages/Primitives/TestKit/Wallet+PrimitivesTestKit.swift @@ -5,13 +5,14 @@ import Primitives public extension Wallet { static func mock( + accounts: [Account] = [] ) -> Wallet { Wallet( id: "", name: "", index: 0, type: .multicoin, - accounts: [], + accounts: accounts, order: 0, isPinned: false )