-
Notifications
You must be signed in to change notification settings - Fork 250
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
324 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 0 additions & 15 deletions
15
UnstoppableWallet/UnstoppableWallet/Modules/Market/Platform/MarketPlatformView.swift
This file was deleted.
Oops, something went wrong.
94 changes: 94 additions & 0 deletions
94
UnstoppableWallet/UnstoppableWallet/Modules/Market/Platform/MarketPlatformViewModel.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import Combine | ||
import Foundation | ||
import HsExtensions | ||
import MarketKit | ||
|
||
class MarketPlatformViewModel: ObservableObject { | ||
private let marketKit = App.shared.marketKit | ||
private let currencyManager = App.shared.currencyManager | ||
|
||
let platform: TopPlatform | ||
|
||
private var cancellables = Set<AnyCancellable>() | ||
private var tasks = Set<AnyTask>() | ||
|
||
private var internalState: State = .loading { | ||
didSet { | ||
syncState() | ||
} | ||
} | ||
|
||
@Published var state: State = .loading | ||
|
||
var sortBy: MarketModule.SortBy = .highestCap { | ||
didSet { | ||
stat(page: .topPlatform, event: .switchSortType(sortType: sortBy.statSortType)) | ||
syncState() | ||
} | ||
} | ||
|
||
init(platform: TopPlatform) { | ||
self.platform = platform | ||
currencyManager.$baseCurrency | ||
.sink { [weak self] _ in | ||
self?.sync() | ||
} | ||
.store(in: &cancellables) | ||
|
||
sync() | ||
} | ||
|
||
private func syncState() { | ||
switch internalState { | ||
case .loading: | ||
state = .loading | ||
case let .loaded(marketInfos): | ||
state = .loaded(marketInfos: marketInfos.sorted(sortBy: sortBy, timePeriod: .day1)) | ||
case let .failed(error): | ||
state = .failed(error: error) | ||
} | ||
} | ||
} | ||
|
||
extension MarketPlatformViewModel { | ||
var currency: Currency { | ||
currencyManager.baseCurrency | ||
} | ||
|
||
var sortBys: [MarketModule.SortBy] { | ||
[.highestCap, .lowestCap, .gainers, .losers] | ||
} | ||
|
||
func sync() { | ||
tasks = Set() | ||
|
||
if case .failed = internalState { | ||
internalState = .loading | ||
} | ||
|
||
let platform = platform | ||
|
||
Task { [weak self, marketKit, currency] in | ||
do { | ||
let marketInfos = try await marketKit.topPlatformMarketInfos(blockchain: platform.blockchain.uid, currencyCode: currency.code) | ||
|
||
await MainActor.run { [weak self] in | ||
self?.internalState = .loaded(marketInfos: marketInfos) | ||
} | ||
} catch { | ||
await MainActor.run { [weak self] in | ||
self?.internalState = .failed(error: error) | ||
} | ||
} | ||
} | ||
.store(in: &tasks) | ||
} | ||
} | ||
|
||
extension MarketPlatformViewModel { | ||
enum State { | ||
case loading | ||
case loaded(marketInfos: [MarketInfo]) | ||
case failed(error: Error) | ||
} | ||
} |
Oops, something went wrong.