-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAssetsListViewModel.swift
123 lines (102 loc) · 3.87 KB
/
AssetsListViewModel.swift
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
//
// AssetsListViewModel.swift
// CryptocurrencyInfo
//
// Created by Denis Simon on 19.12.2020.
//
import Foundation
import SwiftEvents
class AssetsListViewModel {
private var assetRepository: AssetRepository
// Bindings
var data: Observable<[Asset]> = Observable([])
private var dataCopy = [Asset]()
var priceCurrency: Observable<PriceCurrency> = Observable(AppConfiguration.Other.selectedCurrency)
let makeToast: Observable<String> = Observable("")
let activityIndicatorVisibility: Observable<Bool> = Observable(false)
let getAssetsCompletionHandler: Observable<Bool?> = Observable(nil)
private(set) var currentPage = 0
private(set) var assetsAreLoadingFromServer = false
var searchMode = false
let screenTitle = NSLocalizedString("Today's Cryptocurrency Info", comment: "")
init(assetRepository: AssetRepository) {
self.assetRepository = assetRepository
bind()
}
private func bind() {
SharedEvents.get.priceChanged.subscribe(self, queue: .main) { [weak self] assetPrice in
guard let self = self else { return }
let data = self.data.value
for asset in data {
if asset.symbol == assetPrice.symbol {
asset.priceUsd = assetPrice.priceUsd
}
}
self.data.value = data
}
}
private func showError(_ msg: String = "") {
if !msg.isEmpty {
makeToast.value = msg
}
activityIndicatorVisibility.value = false
}
func getDataSource() -> AssetsListDataSource {
AssetsListDataSource(with: data.value)
}
func getAssets(page: Int) {
// If search mode is enabled and there are some results, we do not load new batches of assets
guard searchMode == false || (searchMode && data.value.count == dataCopy.count) else { return }
assetsAreLoadingFromServer = true
activityIndicatorVisibility.value = true
Task.detached {
let result = await self.assetRepository.getAssets(page: page)
switch result {
case .success(let assets):
if assets.data.count > 0 {
self.data.value += assets.data
self.currentPage = page
self.dataCopy = self.data.value
}
self.activityIndicatorVisibility.value = false
case .failure(let error):
if error.error != nil {
self.showError(error.error!.localizedDescription)
} else if error.statusCode != nil {
if error.statusCode! == 404 {
self.showError(NSLocalizedString("Next page not found", comment: ""))
} else {
self.showError()
}
} else {
self.showError()
}
}
self.getAssetsCompletionHandler.value = nil
}
}
func clearData() {
data.value.removeAll()
}
func setAssetsAreLoadingFromServer(_ newValue: Bool) {
assetsAreLoadingFromServer = newValue
}
func searchAsset(_ searchText: String) {
if searchText.isEmpty {
data.value = dataCopy
return
}
var resultData = [Asset]()
for asset in dataCopy {
let editedSearchText = searchText.lowercased().trimmingCharacters(in: .whitespacesAndNewlines)
if asset.symbol.lowercased().contains(editedSearchText) || asset.name.lowercased().contains(editedSearchText) {
resultData.append(asset)
}
}
data.value = resultData
}
func resetSearch() {
data.value = dataCopy
searchMode = false
}
}