-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathRecentsSceneViewModel.swift
More file actions
85 lines (70 loc) · 2.27 KB
/
RecentsSceneViewModel.swift
File metadata and controls
85 lines (70 loc) · 2.27 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
// Copyright (c). Gem Wallet. All rights reserved.
import ActivityService
import Components
import Foundation
import Localization
import Primitives
import PrimitivesComponents
import Store
@Observable
@MainActor
public final class RecentsSceneViewModel {
private let activityService: ActivityService
private let walletId: WalletId
public let query: ObservableQuery<RecentActivityRequest>
public let onSelect: (Asset) -> Void
var searchQuery: String = ""
public var recentAssets: [RecentAsset] {
query.value
}
public init(
walletId: WalletId,
types: [RecentActivityType],
filters: [AssetsRequestFilter] = [],
activityService: ActivityService,
onSelect: @escaping (Asset) -> Void,
) {
self.walletId = walletId
self.activityService = activityService
query = ObservableQuery(RecentActivityRequest(walletId: walletId, limit: .max, types: types, filters: filters), initialValue: [])
self.onSelect = onSelect
}
var title: String {
Localized.RecentActivity.title
}
var clearTitle: String {
Localized.Filter.clear
}
var showEmpty: Bool {
recentAssets.isEmpty || (!searchQuery.isEmpty && filteredAssets.isEmpty)
}
var showClear: Bool {
recentAssets.isNotEmpty
}
var sections: [ListSection<RecentAsset>] {
DateSectionBuilder(items: filteredAssets, dateKeyPath: \.createdAt).build()
}
var emptyModel: any EmptyContentViewable {
if recentAssets.isEmpty {
return EmptyContentTypeViewModel(type: .recents)
}
return EmptyContentTypeViewModel(type: .search(type: .assets))
}
private var filteredAssets: [RecentAsset] {
guard !searchQuery.isEmpty else { return recentAssets }
return recentAssets.filter {
$0.asset.name.localizedCaseInsensitiveContains(searchQuery) ||
$0.asset.symbol.localizedCaseInsensitiveContains(searchQuery)
}
}
}
// MARK: - Actions
extension RecentsSceneViewModel {
func onSelectClear() {
do {
try activityService.clearRecent(walletId: walletId, types: query.request.types)
} catch {
debugLog("RecentsSceneViewModel clear error: \(error)")
}
}
}