This repository was archived by the owner on Mar 26, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathAssetMarketViewModel.swift
More file actions
121 lines (102 loc) · 3.85 KB
/
AssetMarketViewModel.swift
File metadata and controls
121 lines (102 loc) · 3.85 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
// Copyright (c). Gem Wallet. All rights reserved.
import Foundation
import Primitives
import Components
import Formatters
import Localization
import PrimitivesComponents
import Style
import InfoSheet
struct AssetMarketViewModel {
private let market: AssetMarket
private let assetSymbol: String
private let currencyFormatter: CurrencyFormatter
private let currency: String
init(
market: AssetMarket,
assetSymbol: String,
currency: String
) {
self.market = market
self.assetSymbol = assetSymbol
self.currency = currency
self.currencyFormatter = CurrencyFormatter(type: .abbreviated, currencyCode: currency)
}
// MARK: - Market
var marketCap: MarketValueViewModel {
if let rank = market.marketCapRank, Int(rank).isBetween(1, and: 1000) {
return MarketValueViewModel(
title: Localized.Asset.marketCap,
subtitle: formatCurrency(market.marketCap),
titleTag: " #\(rank) ",
titleTagStyle: TextStyle(font: .system(.body), color: Colors.grayLight, background: Colors.grayVeryLight)
)
}
return MarketValueViewModel(title: Localized.Asset.marketCap, subtitle: formatCurrency(market.marketCap))
}
var tradingVolume: MarketValueViewModel {
MarketValueViewModel(title: Localized.Asset.tradingVolume, subtitle: formatCurrency(market.totalVolume))
}
var fdv: MarketValueViewModel {
MarketValueViewModel(
title: Localized.Info.FullyDilutedValuation.title,
subtitle: formatCurrency(market.marketCapFdv),
action: .info(.fullyDilutedValuation)
)
}
// MARK: - Supply
var circulatingSupply: MarketValueViewModel {
MarketValueViewModel(
title: Localized.Asset.circulatingSupply,
subtitle: formatSupply(market.circulatingSupply),
action: .info(.circulatingSupply)
)
}
var totalSupply: MarketValueViewModel {
MarketValueViewModel(
title: Localized.Asset.totalSupply,
subtitle: formatSupply(market.totalSupply),
action: .info(.totalSupply)
)
}
var maxSupply: MarketValueViewModel {
MarketValueViewModel(
title: Localized.Info.MaxSupply.title,
subtitle: market.maxSupply == 0 ? "∞ \(assetSymbol)" : formatSupply(market.maxSupply),
action: .info(.maxSupply)
)
}
// MARK: - All Time
var allTimeHigh: MarketValueViewModel {
marketValueViewModel(Localized.Asset.allTimeHigh, chartValue: market.allTimeHighValue)
}
var allTimeLow: MarketValueViewModel {
marketValueViewModel(Localized.Asset.allTimeLow, chartValue: market.allTimeLowValue)
}
// MARK: - Private
private var allTime: AllTimeValueViewModel {
AllTimeValueViewModel(
priceFormatter: CurrencyFormatter(currencyCode: currency),
percentFormatter: CurrencyFormatter(type: .percent, currencyCode: currency)
)
}
private func marketValueViewModel(_ title: String, chartValue: ChartValuePercentage?) -> MarketValueViewModel {
guard let chartValue else {
return MarketValueViewModel(title: title, subtitle: nil)
}
let item = allTime.model(title: title, chartValue: chartValue)
return MarketValueViewModel(
title: title,
titleExtra: item.titleExtra,
subtitle: item.subtitle,
subtitleExtra: item.subtitleExtra,
subtitleExtraStyle: item.subtitleStyleExtra
)
}
private func formatCurrency(_ value: Double?) -> String? {
value.map { currencyFormatter.string($0) }
}
private func formatSupply(_ value: Double?) -> String? {
value.map { currencyFormatter.string(double: $0, symbol: assetSymbol) }
}
}