Skip to content

Commit

Permalink
Show only wallet chains in filter (#169)
Browse files Browse the repository at this point in the history
* Added WalletSupportedChains model to omit code duplications
* Updates for AddTokenScene
* added tests
  • Loading branch information
gemdev111 authored Sep 12, 2024
1 parent 78f7872 commit 6ebe62b
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 15 deletions.
16 changes: 16 additions & 0 deletions Gem.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
8325539A2C0FB11C00C9CA0C /* CurrencySceneViewModelTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 832553992C0FB11C00C9CA0C /* CurrencySceneViewModelTests.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 */; };
Expand Down Expand Up @@ -584,6 +586,8 @@
832553992C0FB11C00C9CA0C /* CurrencySceneViewModelTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CurrencySceneViewModelTests.swift; sourceTree = "<group>"; };
832A705D2C88CE0A00045367 /* SelectWalletScene.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SelectWalletScene.swift; sourceTree = "<group>"; };
832A705F2C88CE9600045367 /* SellectWalletViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SellectWalletViewModel.swift; sourceTree = "<group>"; };
83352B632C92F898003D52F3 /* WalletSupportedChains.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WalletSupportedChains.swift; sourceTree = "<group>"; };
83352B692C93435F003D52F3 /* WalletSupportedChainsTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WalletSupportedChainsTests.swift; sourceTree = "<group>"; };
833A0C842C80C11B0004DBA9 /* ChainsFilterType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChainsFilterType.swift; sourceTree = "<group>"; };
833A0C862C80C2A50004DBA9 /* ChainsFilterViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChainsFilterViewModel.swift; sourceTree = "<group>"; };
835644FC2C501C2200C6B4DB /* SwapError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwapError.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1027,6 +1031,14 @@
path = ViewModels;
sourceTree = "<group>";
};
83352B682C93433D003D52F3 /* Types */ = {
isa = PBXGroup;
children = (
83352B692C93435F003D52F3 /* WalletSupportedChainsTests.swift */,
);
path = Types;
sourceTree = "<group>";
};
83A681902C46F1E000A3636B /* Types */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -1175,6 +1187,7 @@
C3549B3229C4395B00B4BE01 /* Assets */ = {
isa = PBXGroup;
children = (
83352B682C93433D003D52F3 /* Types */,
C3549B3329C4396000B4BE01 /* ViewModels */,
);
path = Assets;
Expand Down Expand Up @@ -1422,6 +1435,7 @@
C3D1C4EF2A2EBB0C006E8EEA /* RecipientAddress.swift */,
8374DC592C6CDF6C00183109 /* AddTokenInput.swift */,
833A0C842C80C11B0004DBA9 /* ChainsFilterType.swift */,
83352B632C92F898003D52F3 /* WalletSupportedChains.swift */,
);
path = Types;
sourceTree = "<group>";
Expand Down Expand Up @@ -2457,6 +2471,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 */,
Expand Down Expand Up @@ -2664,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 */,
Expand Down
28 changes: 28 additions & 0 deletions Gem/Assets/Types/WalletSupportedChains.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c). Gem Wallet. All rights reserved.

import Foundation
import Primitives
import Settings

enum WalletSupportedChains {
case all
case withTokens
}

// 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) }
}
}
9 changes: 2 additions & 7 deletions Gem/Assets/ViewModels/AddTokenViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import Foundation
import Primitives
import SwiftUI
import Settings
import Blockchain
import Components
import Style
Expand All @@ -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 = wallet.chains(type: .withTokens)
self.input = AddTokenInput(chains: chains)
}

var title: String { Localized.Wallet.AddToken.title }
Expand Down
4 changes: 3 additions & 1 deletion Gem/Assets/ViewModels/AssetsFilterViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import Settings

struct AssetsFilterViewModel {
private let type: SelectAssetType
let allChains: [Chain] = AssetConfiguration.allChains

let allChains: [Chain]

var assetsRequest: AssetsRequest

Expand All @@ -30,6 +31,7 @@ struct AssetsFilterViewModel {
filters: Self.defaultFilters(type: type)
)
self.type = type
self.allChains = wallet.chains(type: .all)
}

static func defaultFilters(type: SelectAssetType) -> [AssetsRequestFilter] {
Expand Down
3 changes: 1 addition & 2 deletions Gem/Assets/ViewModels/SelectAssetViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import Foundation
import Keystore
import Primitives
import Store
import Settings
import Components

@Observable
Expand Down Expand Up @@ -53,7 +52,7 @@ class SelectAssetViewModel {
}

var showAddToken: Bool {
selectType == .manage && wallet.accounts.map { $0.chain }.asSet().intersection(AssetConfiguration.supportedChainsWithTokens).count > 0
selectType == .manage && !filterModel.allChains.isEmpty
}
}

Expand Down
5 changes: 1 addition & 4 deletions Gem/Wallet/Services/WalletsService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
41 changes: 41 additions & 0 deletions GemTests/Assets/Types/WalletSupportedChainsTests.swift
Original file line number Diff line number Diff line change
@@ -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)
}
}
17 changes: 17 additions & 0 deletions Packages/Primitives/TestKit/Account+PrimitivesTestKit.swift
Original file line number Diff line number Diff line change
@@ -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
)
}
}
3 changes: 2 additions & 1 deletion Packages/Primitives/TestKit/Wallet+PrimitivesTestKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down

0 comments on commit 6ebe62b

Please sign in to comment.