-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSelectAssetViewModel.swift
More file actions
416 lines (362 loc) · 11.7 KB
/
SelectAssetViewModel.swift
File metadata and controls
416 lines (362 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
// Copyright (c). Gem Wallet. All rights reserved.
import ActivityService
import AssetsService
import BalanceService
import Components
import Foundation
import Localization
import Preferences
import PriceAlertService
import Primitives
import PrimitivesComponents
import Store
import Style
import SwiftUI
@Observable
@MainActor
public final class SelectAssetViewModel {
let preferences: Preferences
let selectType: SelectAssetType
let searchService: AssetSearchService
let assetsEnabler: any AssetsEnabler
let priceAlertService: PriceAlertService
let activityService: ActivityService
public let wallet: Wallet
var state: StateViewType<[AssetBasic]> = .noData
var searchModel: AssetSearchViewModel
public let assetsQuery: ObservableQuery<AssetsRequest>
public let recentsQuery: ObservableQuery<RecentActivityRequest>
var assets: [AssetData] {
assetsQuery.value
}
var recents: [RecentAsset] {
recentsQuery.value
}
var isSearching: Bool = false
var isDismissSearch: Bool = false
var isPresentingCopyToast: Bool = false
var copyTypeViewModel: CopyTypeViewModel?
public var isPresentingAddToken: Bool = false
public var isPresentingRecents: Bool = false
public var assetSelection: AssetSelectionType?
public var filterModel: AssetsFilterViewModel
public var onSelectAssetAction: AssetAction
public init(
preferences: Preferences = Preferences.standard,
wallet: Wallet,
selectType: SelectAssetType,
searchService: AssetSearchService,
assetsEnabler: any AssetsEnabler,
priceAlertService: PriceAlertService,
activityService: ActivityService,
selectAssetAction: AssetAction = .none,
) {
self.preferences = preferences
self.wallet = wallet
self.selectType = selectType
self.searchService = searchService
self.assetsEnabler = assetsEnabler
self.priceAlertService = priceAlertService
self.activityService = activityService
onSelectAssetAction = selectAssetAction
let filter = AssetsFilterViewModel(
type: selectType,
model: ChainsFilterViewModel(
chains: wallet.chains,
),
)
filterModel = filter
searchModel = AssetSearchViewModel(selectType: selectType)
assetsQuery = ObservableQuery(AssetsRequest(walletId: wallet.id, filters: filter.filters), initialValue: [])
recentsQuery = ObservableQuery(
RecentActivityRequest(
walletId: wallet.id,
limit: 10,
types: selectType.recentActivityTypes,
filters: filter.defaultFilters,
),
initialValue: [],
)
}
var title: String {
switch selectType {
case .send: Localized.Wallet.send
case let .receive(type):
switch type {
case .asset: Localized.Wallet.receive
case .collection: Localized.Wallet.receiveCollection
}
case .buy: Localized.Wallet.buy
case let .swap(type):
switch type {
case .pay: Localized.Swap.youPay
case .receive: Localized.Swap.youReceive
}
case .manage: Localized.Wallet.manageTokenList
case .priceAlert: Localized.Assets.selectAsset
case .deposit: Localized.Wallet.deposit
case .withdraw: Localized.Wallet.withdraw
}
}
var sections: AssetsSections {
AssetsSections.from(assets)
}
var enablePopularSection: Bool {
[.buy, .priceAlert].contains(selectType)
}
var showPopularSection: Bool {
enablePopularSection && sections.popular.isNotEmpty
}
var showPinnedSection: Bool {
sections.pinned.isNotEmpty
}
var showAssetsSection: Bool {
sections.assets.isNotEmpty
}
var popularImage: Image {
Images.System.starFill
}
var popularTitle: String {
Localized.Assets.popular
}
var pinnedImage: Image {
Images.System.pin
}
var pinnedTitle: String {
Localized.Common.pinned
}
var assetsTitle: String {
switch selectType {
case .send, .buy, .swap, .manage, .priceAlert, .deposit, .withdraw, .receive(.asset):
Localized.Assets.title
case .receive(.collection):
Localized.Settings.Networks.title
}
}
public var showAddToken: Bool {
selectType == .manage && wallet.hasTokenSupport && !filterModel.chainsFilter.isEmpty
}
public var showFilter: Bool {
switch selectType {
case let .receive(type):
switch type {
case .asset:
wallet.isMultiCoins && !filterModel.chainsFilter.isEmpty
case .collection: false
}
case .buy, .manage, .priceAlert, .send, .swap:
wallet.isMultiCoins && !filterModel.chainsFilter.isEmpty
case .deposit, .withdraw: false
}
}
var isNetworkSearchEnabled: Bool {
switch selectType {
case .manage, .receive, .buy, .priceAlert: true
case let .swap(type):
switch type {
case .pay: false
case .receive: true
}
case .send, .deposit, .withdraw: false
}
}
var showTags: Bool {
!isSearching && searchModel.searchableQuery.isEmpty
}
var showLoading: Bool {
state.isLoading && showEmpty
}
var showEmpty: Bool {
sections.pinned.isEmpty && sections.assets.isEmpty
}
var showRecents: Bool {
switch selectType {
case .send, .receive, .buy, .swap: searchModel.searchableQuery.isEmpty && recents.isNotEmpty
case .manage, .priceAlert, .deposit, .withdraw: false
}
}
var recentModels: [AssetViewModel] {
recents.map { AssetViewModel(asset: $0.asset) }
}
var currencyCode: String {
preferences.currency
}
}
// MARK: - Business Logic
extension SelectAssetViewModel {
public func updateRecent(assetId: AssetId) {
guard let data = selectType.recentActivityData(assetId: assetId) else { return }
do {
try activityService.updateRecent(data: data, walletId: wallet.id)
} catch {
debugLog("Failed to update recent activity: \(error)")
}
}
func selectAsset(asset: Asset) {
switch selectType {
case .priceAlert:
Task {
await setPriceAlert(assetId: asset.id, enabled: true)
}
case .swap:
updateRecent(assetId: asset.id)
case .manage, .send, .receive, .buy, .deposit, .withdraw:
break
}
onSelectAssetAction?(asset)
}
func search(query: String) async {
let query = query.trim()
if query.isEmpty {
return
}
await searchAssets(
query: query,
priorityAssetsQuery: searchModel.priorityAssetsQuery,
tag: nil,
)
}
func handleAction(assetId: AssetId, enabled: Bool) async {
switch selectType {
case .manage:
do {
try await assetsEnabler.enableAssets(wallet: wallet, assetIds: [assetId], enabled: enabled)
} catch {
debugLog("SelectAssetViewModel handleAction error: \(error)")
}
case .send, .receive, .buy, .swap, .priceAlert, .deposit, .withdraw: break
}
}
func setSelected(tag: AssetTagSelection) {
isDismissSearch.toggle()
searchModel.tagsViewModel.selectedTag = tag
searchModel.focus = .tags
updateRequest()
Task {
await searchAssets(
query: .empty,
priorityAssetsQuery: searchModel.priorityAssetsQuery,
tag: searchModel.tagsViewModel.selectedTag.tag,
)
}
}
func updateRequest() {
assetsQuery.request.searchBy = searchModel.priorityAssetsQuery.or(.empty)
state = isNetworkSearchEnabled ? .loading : .noData
}
func onChangeFocus(_: Bool, isSearchable: Bool) {
if isSearchable {
searchModel.focus = .search
searchModel.tagsViewModel.selectedTag = .all
updateRequest()
}
}
func onChangeFilterModel(_: AssetsFilterViewModel, model: AssetsFilterViewModel) {
assetsQuery.request.filters = model.filters
}
}
// MARK: - Actions
extension SelectAssetViewModel {
func onAssetAction(action: ListAssetItemAction, assetData: AssetData) {
let asset = assetData.asset
switch action {
case let .switcher(enabled):
Task {
await handleAction(assetId: asset.id, enabled: enabled)
}
case .copy:
let address = assetData.account.address
copyTypeViewModel = CopyTypeViewModel(
type: .address(asset, address: address),
copyValue: address,
)
isPresentingCopyToast = true
Task {
await handleAction(assetId: asset.id, enabled: true)
}
}
}
func onSelectRecents() {
isPresentingRecents = true
}
func onSelectAsset(_ assetData: AssetData) {
assetSelection = .regular(SelectAssetInput(type: selectType, assetAddress: assetData.assetAddress))
}
public func onSelectRecent(_ asset: Asset) {
switch selectType {
case .send, .receive, .buy:
assetSelection = .recent(SelectAssetInput(type: selectType, assetAddress: assetAddress(for: asset)))
case .swap:
onSelectAssetAction?(asset)
case .manage, .priceAlert, .deposit, .withdraw:
break
}
isPresentingRecents = false
}
func onSelectAddCustomToken() {
isPresentingAddToken.toggle()
}
}
// MARK: - Private
extension SelectAssetViewModel {
private func assetAddress(for asset: Asset) -> AssetAddress {
let address: String = {
do {
return try wallet.account(for: asset.chain).address
} catch {
debugLog(error.localizedDescription)
return ""
}
}()
return AssetAddress(asset: asset, address: address)
}
private func searchAssets(
query: String,
priorityAssetsQuery: String?,
tag: AssetTag?,
) async {
do {
let assets = try await searchService.searchAssets(
wallet: wallet,
query: query,
priorityAssetsQuery: priorityAssetsQuery,
tag: tag,
)
state = .data(assets)
} catch {
handle(error: error)
}
}
private func setPriceAlert(assetId: AssetId, enabled: Bool) async {
do {
let currency = Preferences.standard.currency
if enabled {
try await priceAlertService.add(priceAlert: .default(for: assetId, currency: currency))
try await priceAlertService.enablePriceAlerts()
} else {
try await priceAlertService.delete(priceAlerts: [.default(for: assetId, currency: currency)])
}
} catch {
handle(error: error)
}
}
private func handle(error: any Error) {
state.setError(error)
debugLog("SelectAssetScene scene error: \(error)")
}
}
// MARK: - Models extensions
extension SelectAssetType {
var listType: AssetListType {
switch self {
case .send,
.buy,
.swap,
.deposit,
.withdraw: .view
case let .receive(type): .copy(type)
case .manage: .manage
case .priceAlert: .price
}
}
}