Skip to content

Commit f7334be

Browse files
committed
Make delta tag and line chart colors computed properties of DeltaPercentage.Direction
1 parent 5a53a46 commit f7334be

File tree

2 files changed

+52
-44
lines changed

2 files changed

+52
-44
lines changed

WooCommerce/Classes/ViewRelated/Dashboard/Analytics Hub/AnalyticsHubViewModel.swift

Lines changed: 14 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -197,17 +197,17 @@ private extension AnalyticsHubViewModel {
197197
leadingValue: StatsDataTextFormatter.createTotalRevenueText(orderStats: currentPeriodStats,
198198
selectedIntervalIndex: nil),
199199
leadingDelta: totalDelta.string,
200-
leadingDeltaColor: Constants.deltaColor(for: totalDelta.direction),
201-
leadingDeltaTextColor: Constants.deltaTextColor(for: totalDelta.direction),
200+
leadingDeltaColor: totalDelta.direction.deltaBackgroundColor,
201+
leadingDeltaTextColor: totalDelta.direction.deltaTextColor,
202202
leadingChartData: StatsIntervalDataParser.getChartData(for: .totalRevenue, from: currentPeriodStats),
203-
leadingChartColor: Constants.chartColor(for: totalDelta.direction),
203+
leadingChartColor: totalDelta.direction.chartColor,
204204
trailingTitle: Localization.RevenueCard.trailingTitle,
205205
trailingValue: StatsDataTextFormatter.createNetRevenueText(orderStats: currentPeriodStats),
206206
trailingDelta: netDelta.string,
207-
trailingDeltaColor: Constants.deltaColor(for: netDelta.direction),
208-
trailingDeltaTextColor: Constants.deltaTextColor(for: netDelta.direction),
207+
trailingDeltaColor: netDelta.direction.deltaBackgroundColor,
208+
trailingDeltaTextColor: netDelta.direction.deltaTextColor,
209209
trailingChartData: StatsIntervalDataParser.getChartData(for: .netRevenue, from: currentPeriodStats),
210-
trailingChartColor: Constants.chartColor(for: netDelta.direction),
210+
trailingChartColor: netDelta.direction.chartColor,
211211
isRedacted: false,
212212
showSyncError: showSyncError,
213213
syncErrorMessage: Localization.RevenueCard.noRevenue)
@@ -223,17 +223,17 @@ private extension AnalyticsHubViewModel {
223223
leadingValue: StatsDataTextFormatter.createOrderCountText(orderStats: currentPeriodStats,
224224
selectedIntervalIndex: nil),
225225
leadingDelta: ordersCountDelta.string,
226-
leadingDeltaColor: Constants.deltaColor(for: ordersCountDelta.direction),
227-
leadingDeltaTextColor: Constants.deltaTextColor(for: ordersCountDelta.direction),
226+
leadingDeltaColor: ordersCountDelta.direction.deltaBackgroundColor,
227+
leadingDeltaTextColor: ordersCountDelta.direction.deltaTextColor,
228228
leadingChartData: StatsIntervalDataParser.getChartData(for: .orderCount, from: currentPeriodStats),
229-
leadingChartColor: Constants.chartColor(for: ordersCountDelta.direction),
229+
leadingChartColor: ordersCountDelta.direction.chartColor,
230230
trailingTitle: Localization.OrderCard.trailingTitle,
231231
trailingValue: StatsDataTextFormatter.createAverageOrderValueText(orderStats: currentPeriodStats),
232232
trailingDelta: orderValueDelta.string,
233-
trailingDeltaColor: Constants.deltaColor(for: orderValueDelta.direction),
234-
trailingDeltaTextColor: Constants.deltaTextColor(for: orderValueDelta.direction),
233+
trailingDeltaColor: orderValueDelta.direction.deltaBackgroundColor,
234+
trailingDeltaTextColor: orderValueDelta.direction.deltaTextColor,
235235
trailingChartData: StatsIntervalDataParser.getChartData(for: .averageOrderValue, from: currentPeriodStats),
236-
trailingChartColor: Constants.chartColor(for: orderValueDelta.direction),
236+
trailingChartColor: orderValueDelta.direction.chartColor,
237237
isRedacted: false,
238238
showSyncError: showSyncError,
239239
syncErrorMessage: Localization.OrderCard.noOrders)
@@ -251,8 +251,8 @@ private extension AnalyticsHubViewModel {
251251

252252
return AnalyticsProductCardViewModel(itemsSold: itemsSold,
253253
delta: itemsSoldDelta.string,
254-
deltaBackgroundColor: Constants.deltaColor(for: itemsSoldDelta.direction),
255-
deltaTextColor: Constants.deltaTextColor(for: itemsSoldDelta.direction),
254+
deltaBackgroundColor: itemsSoldDelta.direction.deltaBackgroundColor,
255+
deltaTextColor: itemsSoldDelta.direction.deltaTextColor,
256256
itemsSoldData: itemSoldRows(from: itemsSoldStats),
257257
isRedacted: false,
258258
showStatsError: showStatsError,
@@ -285,36 +285,6 @@ private extension AnalyticsHubViewModel {
285285
private extension AnalyticsHubViewModel {
286286
enum Constants {
287287
static let maxNumberOfTopItemsSold = 5
288-
289-
static func deltaColor(for direction: StatsDataTextFormatter.DeltaPercentage.Direction) -> UIColor {
290-
switch direction {
291-
case .positive:
292-
return .withColorStudio(.green, shade: .shade50)
293-
case .negative:
294-
return .withColorStudio(.red, shade: .shade40)
295-
case .zero:
296-
return .withColorStudio(.gray, shade: .shade0)
297-
}
298-
}
299-
static func deltaTextColor(for direction: StatsDataTextFormatter.DeltaPercentage.Direction) -> UIColor {
300-
switch direction {
301-
case .positive, .negative:
302-
return .textInverted
303-
case .zero:
304-
return .text
305-
}
306-
}
307-
308-
static func chartColor(for direction: StatsDataTextFormatter.DeltaPercentage.Direction) -> UIColor {
309-
switch direction {
310-
case .positive:
311-
return .withColorStudio(.green, shade: .shade50)
312-
case .negative:
313-
return .withColorStudio(.red, shade: .shade40)
314-
case .zero:
315-
return .withColorStudio(.gray, shade: .shade30)
316-
}
317-
}
318288
}
319289

320290
enum Localization {

WooCommerce/Classes/ViewRelated/Dashboard/Factories/StatsDataTextFormatter.swift

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,40 @@ extension StatsDataTextFormatter {
220220
case positive
221221
case negative
222222
case zero
223+
224+
/// Background color for a `DeltaTag`
225+
var deltaBackgroundColor: UIColor {
226+
switch self {
227+
case .positive:
228+
return Constants.green
229+
case .negative:
230+
return Constants.red
231+
case .zero:
232+
return Constants.lightGray
233+
}
234+
}
235+
236+
/// Text color for a `DeltaTag`
237+
var deltaTextColor: UIColor {
238+
switch self {
239+
case .positive, .negative:
240+
return .textInverted
241+
case .zero:
242+
return .text
243+
}
244+
}
245+
246+
/// Line color for an `AnalyticsLineChart`
247+
var chartColor: UIColor {
248+
switch self {
249+
case .positive:
250+
return Constants.green
251+
case .negative:
252+
return Constants.red
253+
case .zero:
254+
return Constants.darkGray
255+
}
256+
}
223257
}
224258
}
225259
}
@@ -292,6 +326,10 @@ private extension StatsDataTextFormatter {
292326

293327
enum Constants {
294328
static let placeholderText = "-"
329+
static let green: UIColor = .withColorStudio(.green, shade: .shade50)
330+
static let red: UIColor = .withColorStudio(.red, shade: .shade40)
331+
static let lightGray: UIColor = .withColorStudio(.gray, shade: .shade0)
332+
static let darkGray: UIColor = .withColorStudio(.gray, shade: .shade30)
295333
}
296334

297335

0 commit comments

Comments
 (0)