Display USD revenue in graph details

This commit is contained in:
Ilya Laktyushin 2024-08-27 03:38:41 +04:00
parent df62e25f5a
commit 4696ca34b8
7 changed files with 77 additions and 12 deletions

View File

@ -12851,3 +12851,7 @@ Sorry for the inconvenience.";
"Stars.Transaction.Giveaway.Prize" = "Prize";
"Stars.Transaction.Giveaway.Stars_1" = "%@ Star";
"Stars.Transaction.Giveaway.Stars_any" = "%@ Stars";
"Stats.RevenueInTon" = "Revenue in TON";
"Stats.RevenueInStars" = "Revenue in Stars";
"Stats.RevenueInUsd" = "Revenue in USD";

View File

@ -35,6 +35,9 @@ class GeneralChartComponentController: ChartThemeContainer {
var initialHorizontalRange: ClosedRange<CGFloat> = BaseConstants.defaultRange
var initialVerticalRange: ClosedRange<CGFloat> = BaseConstants.defaultRange
var currency: GraphCurrency?
var conversionRate: Double = 1.0
public var cartViewBounds: (() -> CGRect) = { fatalError() }
public var chartFrame: (() -> CGRect) = { fatalError() }
@ -201,7 +204,7 @@ class GeneralChartComponentController: ChartThemeContainer {
var detailsVisible = false
func showDetailsView(at chartPosition: CGFloat, detailsViewPosition: CGFloat, dataIndex: Int, date: Date, animated: Bool, feedback: Bool) {
setDetailsViewModel?(chartDetailsViewModel(closestDate: date, pointIndex: dataIndex), animated, feedback)
setDetailsViewModel?(chartDetailsViewModel(closestDate: date, pointIndex: dataIndex, currency: self.currency, rate: self.conversionRate), animated, feedback)
setDetailsChartVisibleClosure?(true, true)
setDetailsViewPositionClosure?(detailsViewPosition)
detailsVisible = true
@ -329,8 +332,8 @@ class GeneralChartComponentController: ChartThemeContainer {
return (updatedRange, verticalLabels)
}
func chartDetailsViewModel(closestDate: Date, pointIndex: Int) -> ChartDetailsViewModel {
let values: [ChartDetailsViewModel.Value] = chartsCollection.chartValues.enumerated().map { arg in
func chartDetailsViewModel(closestDate: Date, pointIndex: Int, currency: GraphCurrency? = nil, rate: Double = 1.0) -> ChartDetailsViewModel {
var values: [ChartDetailsViewModel.Value] = chartsCollection.chartValues.enumerated().map { arg in
let (index, component) = arg
return ChartDetailsViewModel.Value(prefix: nil,
title: component.name,
@ -338,6 +341,39 @@ class GeneralChartComponentController: ChartThemeContainer {
color: component.color,
visible: chartVisibility[index])
}
if let currency, let firstValue = values.first, let color = GColor(hexString: "#dda747") {
let updatedTitle: String
switch currency {
case .ton:
updatedTitle = self.strings.revenueInTon
case .xtr:
updatedTitle = self.strings.revenueInStars
}
values[0] = ChartDetailsViewModel.Value(
prefix: nil,
title: updatedTitle,
value: firstValue.value,
color: color,
visible: firstValue.visible
)
let convertedValue = (self.verticalLimitsNumberFormatter.number(from: firstValue.value) as? Double ?? 0.0) * rate
let convertedValueString: String
if convertedValue > 1.0 {
convertedValueString = String(format: "%0.1f", convertedValue)
} else {
convertedValueString = String(format: "%0.3f", convertedValue)
}
values.append(ChartDetailsViewModel.Value(
prefix: nil,
title: self.strings.revenueInUsd,
value: "≈$\(convertedValueString)",
color: color,
visible: firstValue.visible
))
}
let dateString: String
if isZoomed {
dateString = BaseConstants.timeDateFormatter.string(from: closestDate)

View File

@ -131,7 +131,7 @@ class PercentChartComponentController: GeneralChartComponentController {
verticalScalesRenderer.setVisible(visibility.contains(true), animated: animated)
}
override func chartDetailsViewModel(closestDate: Date, pointIndex: Int) -> ChartDetailsViewModel {
override func chartDetailsViewModel(closestDate: Date, pointIndex: Int, currency: GraphCurrency? = nil, rate: Double = 1.0) -> ChartDetailsViewModel {
let visibleValues = visibleDetailsChartValues
let total = visibleValues.map { $0.values[pointIndex] }.reduce(0, +)

View File

@ -149,7 +149,6 @@ class BarsComponentController: GeneralChartComponentController {
components: visibleComponents)
}
var conversionRate: Double = 1.0
func verticalLimitsLabels(verticalRange: ClosedRange<CGFloat>, secondary: Bool) -> (ClosedRange<CGFloat>, [LinesChartLabel]) {
var (range, labels) = super.verticalLimitsLabels(verticalRange: verticalRange)
if secondary {
@ -223,8 +222,8 @@ class BarsComponentController: GeneralChartComponentController {
return visibleCharts
}
override func chartDetailsViewModel(closestDate: Date, pointIndex: Int) -> ChartDetailsViewModel {
var viewModel = super.chartDetailsViewModel(closestDate: closestDate, pointIndex: pointIndex)
override func chartDetailsViewModel(closestDate: Date, pointIndex: Int, currency: GraphCurrency? = nil, rate: Double = 1.0) -> ChartDetailsViewModel {
var viewModel = super.chartDetailsViewModel(closestDate: closestDate, pointIndex: pointIndex, currency: currency, rate: rate)
let visibleChartValues = self.visibleChartValues
let totalSumm: CGFloat = visibleChartValues.map { CGFloat($0.values[pointIndex]) }.reduce(0, +)
viewModel.hideAction = { [weak self] in

View File

@ -28,8 +28,6 @@ public enum GraphCurrency : String {
}
public class StackedBarsChartController: BaseChartController {
let barsController: BarsComponentController
let zoomedBarsController: BarsComponentController
@ -55,6 +53,7 @@ public class StackedBarsChartController: BaseChartController {
secondaryScalesRenderer: secondaryScalesRenderer,
previewBarsChartRenderer: BarChartRenderer())
if let currency {
barsController.currency = currency
barsController.conversionRate = rate
barsController.verticalLimitsNumberFormatter = currency.formatter
barsController.detailsNumberFormatter = currency.formatter

View File

@ -30,13 +30,31 @@ public protocol ChartThemeContainer {
public class ChartStrings {
public let zoomOut: String
public let total: String
public let revenueInTon: String
public let revenueInStars: String
public let revenueInUsd: String
public init(zoomOut: String, total: String) {
public init(
zoomOut: String,
total: String,
revenueInTon: String,
revenueInStars: String,
revenueInUsd: String
) {
self.zoomOut = zoomOut
self.total = total
self.revenueInTon = revenueInTon
self.revenueInStars = revenueInStars
self.revenueInUsd = revenueInUsd
}
public static var defaultStrings = ChartStrings(zoomOut: "Zoom Out", total: "Total")
public static var defaultStrings = ChartStrings(
zoomOut: "Zoom Out",
total: "Total",
revenueInTon: "Revenue in TON",
revenueInStars: "Revenue in Stars",
revenueInUsd: "Revenue in USD"
)
}
public class ChartTheme {

View File

@ -295,7 +295,16 @@ public final class StatsGraphItemNode: ListViewItemNode {
strongSelf.activityIndicator.type = .custom(item.presentationData.theme.list.itemSecondaryTextColor, 16.0, 2.0, false)
if let updatedTheme = updatedTheme {
strongSelf.chartNode.setup(theme: ChartTheme(presentationTheme: updatedTheme), strings: ChartStrings(zoomOut: item.presentationData.strings.Stats_ZoomOut, total: item.presentationData.strings.Stats_Total))
strongSelf.chartNode.setup(
theme: ChartTheme(presentationTheme: updatedTheme),
strings: ChartStrings(
zoomOut: item.presentationData.strings.Stats_ZoomOut,
total: item.presentationData.strings.Stats_Total,
revenueInTon: item.presentationData.strings.Stats_RevenueInTon,
revenueInStars: item.presentationData.strings.Stats_RevenueInStars,
revenueInUsd: item.presentationData.strings.Stats_RevenueInUsd
)
)
}
if let updatedGraph = updatedGraph {