Skip to content

Commit

Permalink
Show price change from last UTC midnight when 'Midnight UTC' option i…
Browse files Browse the repository at this point in the history
…s selected
  • Loading branch information
esen committed Jun 5, 2024
1 parent 6c0d523 commit 1c6d308
Show file tree
Hide file tree
Showing 30 changed files with 174 additions and 115 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13243,7 +13243,7 @@
repositoryURL = "https://github.com/horizontalsystems/MarketKit.Swift";
requirement = {
kind = exactVersion;
version = 3.0.9;
version = 3.0.10;
};
};
D3604E7D28F03C1D0066C366 /* XCRemoteSwiftPackageReference "Chart" */ = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Combine
import HsExtensions
import MarketKit

class PriceChangeModeManager {
private let keyPriceChangeMode = "price-change-mode"
Expand All @@ -12,6 +13,13 @@ class PriceChangeModeManager {
}
}

var day1Period: HsTimePeriod {
switch priceChangeMode {
case .hour24: .hour24
case .day1: .day1
}
}

init(userDefaultsStorage: UserDefaultsStorage) {
self.userDefaultsStorage = userDefaultsStorage

Expand All @@ -21,4 +29,12 @@ class PriceChangeModeManager {
priceChangeMode = .hour24
}
}

func convert(period: HsTimePeriod) -> HsTimePeriod {
guard [HsTimePeriod.day1, .hour24].contains(period) else {
return period
}

return day1Period
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import MarketKit
extension HsTimePeriod {
var statPeriod: StatPeriod {
switch self {
case .hour24: return .hour24
case .day1: return .day1
case .week1: return .week1
case .week2: return .week2
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
enum PriceChangeMode: String, CaseIterable, Codable {
case hour24 = "hour_24"
case midnightUtc = "midnight_utc"
case day1 = "day_1"
}

extension PriceChangeMode {
var statName: String {
switch self {
case .hour24: return "hour_24"
case .midnightUtc: return "midnight_utc"
case .day1: return "day_1"
}
}
}
1 change: 1 addition & 0 deletions UnstoppableWallet/UnstoppableWallet/Models/Stats.swift
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,7 @@ enum StatSortType: String {
}

enum StatPeriod: String {
case hour24 = "24h"
case day1 = "1d"
case week1 = "1w"
case week2 = "2w"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class CoinChartService {
self.indicatorRepository = indicatorRepository
self.coinUid = coinUid

periodType = .byCustomPoints(.day1, indicatorRepository.extendedPointCount)
periodType = .byCustomPoints(App.shared.priceChangeModeManager.day1Period, indicatorRepository.extendedPointCount)
indicatorRepository.updatedPublisher
.sink { [weak self] in
self?.fetchWithUpdatedIndicators()
Expand Down Expand Up @@ -122,7 +122,8 @@ class CoinChartService {
let item = Item(
coinUid: coinUid,
rate: coinPrice.value,
rateDiff24h: coinPrice.diff,
rateDiff24h: coinPrice.diff24h,
rateDiff1d: coinPrice.diff1d,
timestamp: coinPrice.timestamp,
chartPointsItem: chartPointsItem,
indicators: indicatorRepository.indicators,
Expand Down Expand Up @@ -223,6 +224,7 @@ extension CoinChartService {
let coinUid: String
let rate: Decimal
let rateDiff24h: Decimal?
let rateDiff1d: Decimal?
let timestamp: TimeInterval
let chartPointsItem: ChartPointsItem
let indicators: [ChartIndicator]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,30 @@ class CoinChartFactory {
points.append(point)
lastPoint = point

// for daily chart we need change oldest visible point to 24h back timestamp-same point
if periodType.in([.day1]), let rateDiff24 = item.rateDiff24h {
var pointToPrepend: ChartPoint?

if periodType.in([.hour24]), let rateDiff24 = item.rateDiff24h {
// for 24h chart we need change oldest visible point to 24h back timestamp-same point
let timestamp = item.timestamp - 24 * 60 * 60
let value = 100 * item.rate / (100 + rateDiff24)

let point = ChartPoint(timestamp: timestamp, value: value)
pointToPrepend = ChartPoint(timestamp: timestamp, value: value)
} else if periodType.in([.day1]), let rateDiff1d = item.rateDiff1d {
// for 1day chart we need change oldest visible point to 24h back timestamp-same point
let value = 100 * item.rate / (100 + rateDiff1d)

pointToPrepend = ChartPoint(timestamp: TimeInterval.midnightUTC(), value: value)
}

if let index = points.firstIndex(where: { $0.timestamp > timestamp }) {
points.insert(point, at: index)
if let pointToPrepend {
if let index = points.firstIndex(where: { $0.timestamp > pointToPrepend.timestamp }) {
points.insert(pointToPrepend, at: index)
if index > 0 {
points.remove(at: index - 1)
}
}

firstPoint = point
firstPoint = pointToPrepend
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ class CoinOverviewViewItemFactory {

private func roiTitle(timePeriod: HsTimePeriod) -> String {
switch timePeriod {
case .day1: return "coin_overview.roi.hour24".localized
case .hour24: return "coin_overview.roi.hour24".localized
case .day1: return "coin_overview.roi.day1".localized
case .week1: return "coin_overview.roi.day7".localized
case .week2: return "coin_overview.roi.day14".localized
case .month1: return "coin_overview.roi.day30".localized
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class MarketAdvancedSearchViewModel: ObservableObject {

private let marketKit = App.shared.marketKit
private let currencyManager = App.shared.currencyManager
private let priceChangeModeManager = App.shared.priceChangeModeManager
private var cancellables = Set<AnyCancellable>()
private var tasks = Set<AnyTask>()

private var internalState: State = .loading {
Expand Down Expand Up @@ -102,7 +104,7 @@ class MarketAdvancedSearchViewModel: ObservableObject {
}
}

@Published var priceChangePeriod: HsTimePeriod = .day1 {
@Published var priceChangePeriod: HsTimePeriod {
didSet {
syncState()
}
Expand Down Expand Up @@ -152,6 +154,16 @@ class MarketAdvancedSearchViewModel: ObservableObject {
allBlockchains = []
}

priceChangePeriod = priceChangeModeManager.day1Period

priceChangeModeManager.$priceChangeMode
.sink { [weak self] _ in
if let strongSelf = self {
strongSelf.priceChangePeriod = strongSelf.priceChangeModeManager.day1Period
}
}
.store(in: &cancellables)

syncMarketInfos()
}

Expand Down Expand Up @@ -179,7 +191,7 @@ class MarketAdvancedSearchViewModel: ObservableObject {
|| !blockchains.isEmpty
|| signal != nil
|| priceChange != .none
|| priceChangePeriod != .day1
|| priceChangePeriod != priceChangeModeManager.day1Period
|| outperformedBtc != false
|| outperformedEth != false
|| outperformedBnb != false
Expand Down Expand Up @@ -307,7 +319,7 @@ extension MarketAdvancedSearchViewModel {
}

var priceChangePeriods: [HsTimePeriod] {
[.day1, .week1, .week2, .month1, .month6, .year1]
[priceChangeModeManager.day1Period, .week1, .week2, .month1, .month6, .year1]
}

func syncMarketInfos() {
Expand Down Expand Up @@ -343,7 +355,7 @@ extension MarketAdvancedSearchViewModel {
blockchains = Set()
signal = nil
priceChange = .none
priceChangePeriod = .day1
priceChangePeriod = priceChangeModeManager.day1Period
outperformedBtc = false
outperformedEth = false
outperformedBnb = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ class MarketCoinsViewModel: ObservableObject {
private let marketKit = App.shared.marketKit
private let currencyManager = App.shared.currencyManager
private let appManager = App.shared.appManager
private let priceChangeModeManager = App.shared.priceChangeModeManager

private var cancellables = Set<AnyCancellable>()
private var tasks = Set<AnyTask>()

Expand All @@ -32,13 +34,27 @@ class MarketCoinsViewModel: ObservableObject {
}
}

var timePeriod: HsTimePeriod = .day1 {
var timePeriod: HsTimePeriod {
didSet {
stat(page: .markets, section: .coins, event: .switchPeriod(period: timePeriod.statPeriod))
syncState()
}
}

init() {
timePeriod = priceChangeModeManager.day1Period

priceChangeModeManager.$priceChangeMode
.sink { [weak self] _ in
self?.syncPeriod()
}
.store(in: &cancellables)
}

private func syncPeriod() {
timePeriod = priceChangeModeManager.convert(period: timePeriod)
}

private func syncMarketInfos() {
tasks = Set()

Expand Down Expand Up @@ -94,7 +110,7 @@ extension MarketCoinsViewModel {
}

var timePeriods: [HsTimePeriod] {
[.day1, .week1, .month1, .month3]
[priceChangeModeManager.day1Period, .week1, .month1, .month3]
}

func load() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ enum MarketModule {
extension MarketKit.MarketInfo {
func priceChangeValue(timePeriod: HsTimePeriod) -> Decimal? {
switch timePeriod {
case .day1: return priceChange24h
case .day1: return priceChange1d
case .hour24: return priceChange24h
case .week1: return priceChange7d
case .week2: return priceChange14d
case .month1: return priceChange30d
Expand All @@ -91,7 +92,7 @@ extension MarketKit.MarketInfo {

func priceChangeValue(timePeriod: WatchlistTimePeriod) -> Decimal? {
switch timePeriod {
case .day1: return priceChange24h
case .day1: return priceChange1d
case .week1: return priceChange7d
case .month1: return priceChange30d
case .month3: return priceChange90d
Expand Down Expand Up @@ -189,17 +190,11 @@ extension [MarketKit.TopPlatform] {

extension HsTimePeriod {
var title: String {
switch self {
case .day1: return "market.time_period.24h".localized
default: return "market.time_period.\(rawValue)".localized
}
"market.time_period.\(rawValue)".localized
}

var shortTitle: String {
switch self {
case .day1: return "market.time_period.24h.short".localized
default: return "market.time_period.\(rawValue).short".localized
}
"market.time_period.\(rawValue).short".localized
}

init?(_ periodType: HsPeriodType) {
Expand All @@ -212,17 +207,11 @@ extension HsTimePeriod {

extension WatchlistTimePeriod {
var title: String {
switch self {
case .day1: return "market.time_period.24h".localized
default: return "market.time_period.\(rawValue)".localized
}
"market.time_period.\(rawValue)".localized
}

var shortTitle: String {
switch self {
case .day1: return "market.time_period.24h.short".localized
default: return "market.time_period.\(rawValue).short".localized
}
"market.time_period.\(rawValue).short".localized
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ enum NftModule {
static func viewController() -> UIViewController? {
let coinPriceService = WalletCoinPriceService(
currencyManager: App.shared.currencyManager,
priceChangeModeManager: App.shared.priceChangeModeManager,
marketKit: App.shared.marketKit
)

Expand Down
17 changes: 8 additions & 9 deletions UnstoppableWallet/UnstoppableWallet/Modules/Nft/NftService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -263,17 +263,16 @@ class NftService {
}

extension NftService: IWalletCoinPriceServiceDelegate {
func didUpdateBaseCurrency() {
func didUpdate(itemsMap: [String: WalletCoinPriceService.Item]?) {
queue.async {
self.updatePriceItems(items: self.items, map: self.coinPriceService.itemMap(coinUids: Array(self.allCoinUids(items: self.items))))
self.items = self.sort(items: self.items)
self.syncTotalItem()
}
}
let _itemsMap: [String: WalletCoinPriceService.Item]
if let itemsMap {
_itemsMap = itemsMap
} else {
_itemsMap = self.coinPriceService.itemMap(coinUids: Array(self.allCoinUids(items: self.items)))
}

func didUpdate(itemsMap: [String: WalletCoinPriceService.Item]) {
queue.async {
self.updatePriceItems(items: self.items, map: itemsMap)
self.updatePriceItems(items: self.items, map: _itemsMap)
self.items = self.sort(items: self.items)
self.syncTotalItem()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ enum NftAssetOverviewModule {
static func viewController(providerCollectionUid: String, nftUid: NftUid) -> NftAssetOverviewViewController {
let coinPriceService = WalletCoinPriceService(
currencyManager: App.shared.currencyManager,
priceChangeModeManager: App.shared.priceChangeModeManager,
marketKit: App.shared.marketKit
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,24 +160,18 @@ class NftAssetOverviewService {
}

extension NftAssetOverviewService: IWalletCoinPriceServiceDelegate {
func didUpdateBaseCurrency() {
func didUpdate(itemsMap: [String: WalletCoinPriceService.Item]?) {
queue.async {
guard case let .completed(item) = self.state else {
return
}

self._fillCoinPrices(item: item, coinUids: self._allCoinUids(item: item))
self.state = .completed(item)
}
}

func didUpdate(itemsMap: [String: WalletCoinPriceService.Item]) {
queue.async {
guard case let .completed(item) = self.state else {
return
if let itemsMap {
self._fillCoinPrices(item: item, map: itemsMap)
} else {
self._fillCoinPrices(item: item, coinUids: self._allCoinUids(item: item))
}

self._fillCoinPrices(item: item, map: itemsMap)
self.state = .completed(item)
}
}
Expand Down
Loading

0 comments on commit 1c6d308

Please sign in to comment.