From a50fd76dac7eaf4990c4cbcf7f018c2e5fc52e99 Mon Sep 17 00:00:00 2001 From: mike-dydx Date: Thu, 15 Feb 2024 17:15:20 -0500 Subject: [PATCH] hook up abacus data for trading rewards --- .../dydxFormatter/dydxFormatter.swift | 16 ++++ .../dydxProfileRewardsViewPresenter.swift | 8 +- .../dydxRewardsHistoryViewPresenter.swift | 88 ++++++++++--------- .../dydxRewardsSummaryPresenter.swift | 18 ++-- .../dydxTradingRewardsViewPresenter.swift | 37 ++++++-- .../Components/dydxRewardsHistoryView.swift | 5 +- .../Components/dydxRewardsRewardView.swift | 2 +- .../Components/dydxRewardsSummaryView.swift | 12 ++- .../dydxTradingRewardsView.swift | 4 +- 9 files changed, 122 insertions(+), 68 deletions(-) diff --git a/dydx/dydxFormatter/dydxFormatter/dydxFormatter.swift b/dydx/dydxFormatter/dydxFormatter/dydxFormatter.swift index 592e08a89..2b2159b74 100644 --- a/dydx/dydxFormatter/dydxFormatter/dydxFormatter.swift +++ b/dydx/dydxFormatter/dydxFormatter/dydxFormatter.swift @@ -11,6 +11,15 @@ import Utilities import Combine public final class dydxFormatter: NSObject, SingletonProtocol { + + public enum DateFormat: String { + /// "e.g. Jan 1" + case MMM_d = "MMM d" + + /// "e.g. Jan 1, 2024" + case MMM_d_yyyy = "MMM d, yyyy" + } + private var subscriptions = Set() public static let shared = dydxFormatter() @@ -609,6 +618,13 @@ public final class dydxFormatter: NSObject, SingletonProtocol { } } + public func millisecondsToDate(_ milliseconds: Double, format: DateFormat) -> String { + let date = Date(timeIntervalSince1970: TimeInterval(milliseconds) / 1000) + let formatter = datetimeFormatter + formatter.dateFormat = format.rawValue + return datetimeFormatter.string(from: date) + } + public func multiple(of tickText: String, is sizeText: String) -> Bool { let components: [String] = tickText.components(separatedBy: ".") if components.count == 2 { // decimal diff --git a/dydx/dydxPresenters/dydxPresenters/_v4/Profile/Components/dydxProfileRewardsViewPresenter.swift b/dydx/dydxPresenters/dydxPresenters/_v4/Profile/Components/dydxProfileRewardsViewPresenter.swift index e3ef600ce..ab4dcb3e9 100644 --- a/dydx/dydxPresenters/dydxPresenters/_v4/Profile/Components/dydxProfileRewardsViewPresenter.swift +++ b/dydx/dydxPresenters/dydxPresenters/_v4/Profile/Components/dydxProfileRewardsViewPresenter.swift @@ -36,10 +36,12 @@ public class dydxProfileRewardsViewPresenter: HostedViewPresenter period 2", amount: "1.000"), - .init(period: "period 2 -> period 3", amount: "2.000"), - .init(period: "period 3 -> period 4", amount: "3.000"), - .init(period: "period 4 -> period 5", amount: "4.000"), - .init(period: "period 5 -> period 6", amount: "5.000"), - .init(period: "period 6 -> period 7", amount: "6.000"), - .init(period: "period 7 -> period 8", amount: "7.000"), - .init(period: "period 8 -> period 9", amount: "8.000"), - .init(period: "period 9 -> period 10", amount: "9.000"), - .init(period: "period 10 -> period 11", amount: "10.000"), - .init(period: "period 11 -> period 12", amount: "11.000"), - .init(period: "period 12 -> period 2", amount: "1.000"), - .init(period: "period 13 -> period 3", amount: "2.000"), - .init(period: "period 14 -> period 4", amount: "3.000"), - .init(period: "period 15 -> period 5", amount: "4.000"), - .init(period: "period 16 -> period 6", amount: "5.000"), - .init(period: "period 17 -> period 7", amount: "6.000"), - .init(period: "period 18 -> period 8", amount: "7.000"), - .init(period: "period 19 -> period 9", amount: "8.000"), - .init(period: "period 20 -> period 10", amount: "9.000"), - .init(period: "period 21 -> period 11", amount: "10.000"), - .init(period: "period 22 -> period 3", amount: "2.000"), - .init(period: "period 23 -> period 4", amount: "3.000"), - .init(period: "period 24 -> period 5", amount: "4.000"), - .init(period: "period 25 -> period 6", amount: "5.000"), - .init(period: "period 26 -> period 7", amount: "6.000"), - .init(period: "period 27 -> period 8", amount: "7.000"), - .init(period: "period 28 -> period 9", amount: "8.000"), - .init(period: "period 29 -> period 10", amount: "9.000") - ] + private func updateItems(from historicalRewards: [String: [HistoricalTradingReward]]?) { + let period = Period.allCases[selectedPeriodIndex] + viewModel?.items = historicalRewards?[period.historicalMapKey]? + .map { reward in + let startedAt = dydxFormatter.shared.millisecondsToDate(reward.startedAtInMilliseconds, format: .MMM_d_yyyy) + let endedAt = dydxFormatter.shared.millisecondsToDate(reward.endedAtInMilliseconds, format: .MMM_d_yyyy) + let period = DataLocalizer.shared?.localize(path: "APP.GENERAL.TIME_STRINGS.PERIOD", params: ["START": startedAt, "END": endedAt]) ?? "" + return dydxRewardsRewardViewModel(period: period, + amount: dydxFormatter.shared.raw(number: NSNumber(value: reward.amount), digits: 4) ?? "--") + } ?? [] } } diff --git a/dydx/dydxPresenters/dydxPresenters/_v4/Profile/TradingRewards/Components/dydxRewardsSummaryPresenter.swift b/dydx/dydxPresenters/dydxPresenters/_v4/Profile/TradingRewards/Components/dydxRewardsSummaryPresenter.swift index a7bbc633c..bbd0c90c6 100644 --- a/dydx/dydxPresenters/dydxPresenters/_v4/Profile/TradingRewards/Components/dydxRewardsSummaryPresenter.swift +++ b/dydx/dydxPresenters/dydxPresenters/_v4/Profile/TradingRewards/Components/dydxRewardsSummaryPresenter.swift @@ -11,16 +11,17 @@ import ParticlesKit import Combine import dydxStateManager import dydxFormatter +import Utilities public protocol dydxRewardsSummaryPresenterProtocol: HostedViewPresenterProtocol { - var viewModel: dydxProfileRewardsViewModel? { get } + var viewModel: dydxRewardsSummaryViewModel? { get } } -public class dydxRewardsSummaryViewPresenter: HostedViewPresenter, dydxProfileRewardsViewPresenterProtocol { +public class dydxRewardsSummaryViewPresenter: HostedViewPresenter, dydxRewardsSummaryPresenterProtocol { override init() { super.init() - viewModel = dydxProfileRewardsViewModel() + viewModel = dydxRewardsSummaryViewModel() } public override func start() { @@ -28,10 +29,15 @@ public class dydxRewardsSummaryViewPresenter: HostedViewPresenter() -> T? { @@ -33,11 +35,17 @@ private protocol dydxTradingRewardsViewPresenterProtocol: HostedViewPresenterPro private class dydxTradingRewardsViewPresenter: HostedViewPresenter, dydxTradingRewardsViewPresenterProtocol { + private let summaryPresenter = dydxRewardsSummaryViewPresenter() private let helpPresenter = dydxRewardsHelpViewPresenter() private let historyPresenter = dydxRewardsHistoryViewPresenter() + private lazy var childPresenters: [HostedViewPresenterProtocol] = [ + summaryPresenter, + helpPresenter, + historyPresenter + ] + override init() { - super.init() let viewModel = dydxTradingRewardsViewModel() @@ -46,10 +54,6 @@ private class dydxTradingRewardsViewPresenter: HostedViewPresenter Void)? @Published public var items: [dydxRewardsRewardViewModel] = [] + @Published public var currentSelection: Int = 0 public var contentChanged: (() -> Void)? // MARK: private properties @@ -34,11 +35,11 @@ public class dydxRewardsHistoryViewModel: dydxTitledCardViewModel { public override func createTitleAccessoryView(parentStyle: ThemeStyle = ThemeStyle.defaultStyle, styleKey: String? = nil) -> AnyView? { let items = filters.compactMap { TabItemViewModel(value: $0, isSelected: false) } - let selectedItems = filters.compactMap {TabItemViewModel(value: $0, isSelected: true) } + let selectedItems = filters.compactMap { TabItemViewModel(value: $0, isSelected: true) } return ScrollView(.horizontal, showsIndicators: false) { TabGroupModel(items: items, selectedItems: selectedItems, - currentSelection: 0, + currentSelection: self.currentSelection, onSelectionChanged: { [weak self] index in self?.onSelectionChanged?(index) }, diff --git a/dydx/dydxViews/dydxViews/_v4/Profile/TradingRewards/Components/dydxRewardsRewardView.swift b/dydx/dydxViews/dydxViews/_v4/Profile/TradingRewards/Components/dydxRewardsRewardView.swift index 354af9df9..d20b4ad1a 100644 --- a/dydx/dydxViews/dydxViews/_v4/Profile/TradingRewards/Components/dydxRewardsRewardView.swift +++ b/dydx/dydxViews/dydxViews/_v4/Profile/TradingRewards/Components/dydxRewardsRewardView.swift @@ -25,7 +25,7 @@ public class dydxRewardsRewardViewModel: PlatformViewModel { let localizedString = DataLocalizer.shared?.localize(path: "APP.TRADING_REWARDS.FOR_TRADING", params: ["PERIOD": period]) ?? "" var attributedString = AttributedString(localizedString) - .themeFont(fontType: .text, fontSize: .smaller) + .themeFont(fontType: .text, fontSize: .smallest) attributedString = attributedString.themeColor(foreground: .textTertiary, to: nil) if let periodTextRange = attributedString.range(of: period) { diff --git a/dydx/dydxViews/dydxViews/_v4/Profile/TradingRewards/Components/dydxRewardsSummaryView.swift b/dydx/dydxViews/dydxViews/_v4/Profile/TradingRewards/Components/dydxRewardsSummaryView.swift index 0b0c2a2dd..217f9c223 100644 --- a/dydx/dydxViews/dydxViews/_v4/Profile/TradingRewards/Components/dydxRewardsSummaryView.swift +++ b/dydx/dydxViews/dydxViews/_v4/Profile/TradingRewards/Components/dydxRewardsSummaryView.swift @@ -19,12 +19,10 @@ public class dydxRewardsSummaryViewModel: dydxTitledCardViewModel { } override func createContentView(parentStyle: ThemeStyle = ThemeStyle.defaultStyle, styleKey: String? = nil) -> AnyView? { - HStack(spacing: 18) { - HStack { - titleValueStack(title: DataLocalizer.shared?.localize(path: "APP.GENERAL.TIME_STRINGS.THIS_WEEK", params: nil) ?? "", primaryValue: last7DaysRewardsAmount, secondaryValue: last7DaysRewardsPeriod) - if let allTimeRewardsAmount = allTimeRewardsAmount { - titleValueStack(title: DataLocalizer.shared?.localize(path: "APP.GENERAL.TIME_STRINGS.ALL_TIME", params: nil) ?? "", primaryValue: allTimeRewardsAmount, secondaryValue: nil) - } + HStack { + titleValueStack(title: DataLocalizer.shared?.localize(path: "APP.GENERAL.TIME_STRINGS.THIS_WEEK", params: nil) ?? "", primaryValue: last7DaysRewardsAmount, secondaryValue: last7DaysRewardsPeriod) + if let allTimeRewardsAmount = allTimeRewardsAmount { + titleValueStack(title: DataLocalizer.shared?.localize(path: "APP.GENERAL.TIME_STRINGS.ALL_TIME", params: nil) ?? "", primaryValue: allTimeRewardsAmount, secondaryValue: nil) } } .wrappedInAnyView() @@ -41,7 +39,7 @@ public class dydxRewardsSummaryViewModel: dydxTitledCardViewModel { .themeFont(fontType: .text, fontSize: .smaller) HStack(spacing: 6) { Text(primaryValue ?? "-") - .themeColor(foreground: .textSecondary) + .themeColor(foreground: .textPrimary) .themeFont(fontType: .number, fontSize: .medium) PlatformIconViewModel(type: .asset(name: "icon_dydx", bundle: .dydxView), clip: .noClip, size: .init(width: 24, height: 24), templateColor: nil) .createView() diff --git a/dydx/dydxViews/dydxViews/_v4/Profile/TradingRewards/dydxTradingRewardsView.swift b/dydx/dydxViews/dydxViews/_v4/Profile/TradingRewards/dydxTradingRewardsView.swift index de49fa714..9614ecc46 100644 --- a/dydx/dydxViews/dydxViews/_v4/Profile/TradingRewards/dydxTradingRewardsView.swift +++ b/dydx/dydxViews/dydxViews/_v4/Profile/TradingRewards/dydxTradingRewardsView.swift @@ -13,7 +13,7 @@ public class dydxTradingRewardsViewModel: PlatformViewModel { @Published public var headerViewModel: NavHeaderModel = NavHeaderModel() @Published public var launchIncentivesViewModel: dydxRewardsLaunchIncentivesViewModel = dydxRewardsLaunchIncentivesViewModel() - @Published public var rewardsSummary: dydxRewardsSummaryViewModel = dydxRewardsSummaryViewModel() + @Published public var rewardsSummary: dydxRewardsSummaryViewModel? = dydxRewardsSummaryViewModel() @Published public var help: dydxRewardsHelpViewModel? = dydxRewardsHelpViewModel() @Published public var history: dydxRewardsHistoryViewModel? = dydxRewardsHistoryViewModel() // removed as part of https://linear.app/dydx/issue/TRCL-3445/remove-governance-and-staking-cards @@ -37,7 +37,7 @@ public class dydxTradingRewardsViewModel: PlatformViewModel { ScrollView(showsIndicators: false) { VStack(spacing: 16) { self.launchIncentivesViewModel.createView(parentStyle: style) - self.rewardsSummary.createView(parentStyle: style) + self.rewardsSummary?.createView(parentStyle: style) self.help?.createView(parentStyle: style) self.history?.createView(parentStyle: style) // see comment near top