Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -189,51 +189,36 @@ private extension AnalyticsHubViewModel {

static func revenueCard(currentPeriodStats: OrderStatsV4?, previousPeriodStats: OrderStatsV4?) -> AnalyticsReportCardViewModel {
let showSyncError = currentPeriodStats == nil || previousPeriodStats == nil
let totalDelta = StatsDataTextFormatter.createTotalRevenueDelta(from: previousPeriodStats, to: currentPeriodStats)
let netDelta = StatsDataTextFormatter.createNetRevenueDelta(from: previousPeriodStats, to: currentPeriodStats)

return AnalyticsReportCardViewModel(title: Localization.RevenueCard.title,
leadingTitle: Localization.RevenueCard.leadingTitle,
leadingValue: StatsDataTextFormatter.createTotalRevenueText(orderStats: currentPeriodStats,
selectedIntervalIndex: nil),
leadingDelta: totalDelta.string,
leadingDeltaColor: totalDelta.direction.deltaBackgroundColor,
leadingDeltaTextColor: totalDelta.direction.deltaTextColor,
leadingDelta: StatsDataTextFormatter.createTotalRevenueDelta(from: previousPeriodStats, to: currentPeriodStats),
leadingChartData: StatsIntervalDataParser.getChartData(for: .totalRevenue, from: currentPeriodStats),
leadingChartColor: totalDelta.direction.chartColor,
trailingTitle: Localization.RevenueCard.trailingTitle,
trailingValue: StatsDataTextFormatter.createNetRevenueText(orderStats: currentPeriodStats),
trailingDelta: netDelta.string,
trailingDeltaColor: netDelta.direction.deltaBackgroundColor,
trailingDeltaTextColor: netDelta.direction.deltaTextColor,
trailingDelta: StatsDataTextFormatter.createNetRevenueDelta(from: previousPeriodStats, to: currentPeriodStats),
trailingChartData: StatsIntervalDataParser.getChartData(for: .netRevenue, from: currentPeriodStats),
trailingChartColor: netDelta.direction.chartColor,
isRedacted: false,
showSyncError: showSyncError,
syncErrorMessage: Localization.RevenueCard.noRevenue)
}

static func ordersCard(currentPeriodStats: OrderStatsV4?, previousPeriodStats: OrderStatsV4?) -> AnalyticsReportCardViewModel {
let showSyncError = currentPeriodStats == nil || previousPeriodStats == nil
let ordersCountDelta = StatsDataTextFormatter.createOrderCountDelta(from: previousPeriodStats, to: currentPeriodStats)
let orderValueDelta = StatsDataTextFormatter.createAverageOrderValueDelta(from: previousPeriodStats, to: currentPeriodStats)

return AnalyticsReportCardViewModel(title: Localization.OrderCard.title,
leadingTitle: Localization.OrderCard.leadingTitle,
leadingValue: StatsDataTextFormatter.createOrderCountText(orderStats: currentPeriodStats,
selectedIntervalIndex: nil),
leadingDelta: ordersCountDelta.string,
leadingDeltaColor: ordersCountDelta.direction.deltaBackgroundColor,
leadingDeltaTextColor: ordersCountDelta.direction.deltaTextColor,
leadingDelta: StatsDataTextFormatter.createOrderCountDelta(from: previousPeriodStats, to: currentPeriodStats),
leadingChartData: StatsIntervalDataParser.getChartData(for: .orderCount, from: currentPeriodStats),
leadingChartColor: ordersCountDelta.direction.chartColor,
trailingTitle: Localization.OrderCard.trailingTitle,
trailingValue: StatsDataTextFormatter.createAverageOrderValueText(orderStats: currentPeriodStats),
trailingDelta: orderValueDelta.string,
trailingDeltaColor: orderValueDelta.direction.deltaBackgroundColor,
trailingDeltaTextColor: orderValueDelta.direction.deltaTextColor,
trailingDelta: StatsDataTextFormatter.createAverageOrderValueDelta(from: previousPeriodStats,
to: currentPeriodStats),
trailingChartData: StatsIntervalDataParser.getChartData(for: .averageOrderValue, from: currentPeriodStats),
trailingChartColor: orderValueDelta.direction.chartColor,
isRedacted: false,
showSyncError: showSyncError,
syncErrorMessage: Localization.OrderCard.noOrders)
Expand All @@ -250,9 +235,7 @@ private extension AnalyticsHubViewModel {
let itemsSoldDelta = StatsDataTextFormatter.createOrderItemsSoldDelta(from: previousPeriodStats, to: currentPeriodStats)

return AnalyticsProductCardViewModel(itemsSold: itemsSold,
delta: itemsSoldDelta.string,
deltaBackgroundColor: itemsSoldDelta.direction.deltaBackgroundColor,
deltaTextColor: itemsSoldDelta.direction.deltaTextColor,
delta: itemsSoldDelta,
itemsSoldData: itemSoldRows(from: itemsSoldStats),
isRedacted: false,
showStatsError: showStatsError,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,9 @@ struct AnalyticsProductCardViewModel {
///
let itemsSold: String

/// Items Sold Delta
/// Items Sold Delta Percentage
///
let delta: String

/// Delta background color.
///
let deltaBackgroundColor: UIColor

/// Delta text color.
///
let deltaTextColor: UIColor
let delta: DeltaPercentage

/// Items Solds data to render.
///
Expand All @@ -45,9 +37,7 @@ extension AnalyticsProductCardViewModel {
var redacted: Self {
// Values here are placeholders and will be redacted in the UI
.init(itemsSold: "1000",
delta: "+50%",
deltaBackgroundColor: .lightGray,
deltaTextColor: .text,
delta: DeltaPercentage(string: "0%", direction: .zero),
itemsSoldData: [.init(imageURL: nil, name: "Product Name", details: "Net Sales", value: "$5678")],
isRedacted: true,
showStatsError: false,
Expand All @@ -61,9 +51,9 @@ extension AnalyticsProductCardViewModel {
extension AnalyticsProductCard {
init(viewModel: AnalyticsProductCardViewModel) {
self.itemsSold = viewModel.itemsSold
self.delta = viewModel.delta
self.deltaBackgroundColor = viewModel.deltaBackgroundColor
self.deltaTextColor = viewModel.deltaTextColor
self.delta = viewModel.delta.string
self.deltaBackgroundColor = viewModel.delta.direction.deltaBackgroundColor
self.deltaTextColor = viewModel.delta.direction.deltaTextColor
self.itemsSoldData = viewModel.itemsSoldData
self.isRedacted = viewModel.isRedacted
self.showStatsError = viewModel.showStatsError
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,14 @@ struct AnalyticsReportCardViewModel {
///
let leadingValue: String

/// First Column Delta Value
/// First Column Delta Percentage
///
let leadingDelta: String

/// First Column delta background color.
///
let leadingDeltaColor: UIColor

/// First Column delta text color.
///
let leadingDeltaTextColor: UIColor
let leadingDelta: DeltaPercentage

/// First Column Chart Data
///
let leadingChartData: [Double]

/// First Column Chart Color
///
let leadingChartColor: UIColor

/// Second Column Title
///
let trailingTitle: String
Expand All @@ -45,26 +33,14 @@ struct AnalyticsReportCardViewModel {
///
let trailingValue: String

/// Second Column Delta Value
/// Second Column Delta Percentage
///
let trailingDelta: String

/// Second Column Delta Background Color
///
let trailingDeltaColor: UIColor

/// Second Column delta text color.
///
let trailingDeltaTextColor: UIColor
let trailingDelta: DeltaPercentage

/// Second Column Chart Data
///
let trailingChartData: [Double]

/// Second Column Chart Color
///
let trailingChartColor: UIColor

/// Indicates if the values should be hidden (for loading state)
///
let isRedacted: Bool
Expand All @@ -87,18 +63,12 @@ extension AnalyticsReportCardViewModel {
.init(title: title,
leadingTitle: leadingTitle,
leadingValue: "$1000",
leadingDelta: "+50%",
leadingDeltaColor: .lightGray,
leadingDeltaTextColor: .text,
leadingDelta: DeltaPercentage(string: "0%", direction: .zero),
leadingChartData: [],
leadingChartColor: .lightGray,
trailingTitle: trailingTitle,
trailingValue: "$1000",
trailingDelta: "+50%",
trailingDeltaColor: .lightGray,
trailingDeltaTextColor: .text,
trailingDelta: DeltaPercentage(string: "0%", direction: .zero),
trailingChartData: [],
trailingChartColor: .lightGray,
isRedacted: true,
showSyncError: false,
syncErrorMessage: "")
Expand All @@ -112,18 +82,18 @@ extension AnalyticsReportCard {
self.title = viewModel.title
self.leadingTitle = viewModel.leadingTitle
self.leadingValue = viewModel.leadingValue
self.leadingDelta = viewModel.leadingDelta
self.leadingDeltaColor = viewModel.leadingDeltaColor
self.leadingDeltaTextColor = viewModel.leadingDeltaTextColor
self.leadingDelta = viewModel.leadingDelta.string
self.leadingDeltaColor = viewModel.leadingDelta.direction.deltaBackgroundColor
self.leadingDeltaTextColor = viewModel.leadingDelta.direction.deltaTextColor
self.leadingChartData = viewModel.leadingChartData
self.leadingChartColor = viewModel.leadingChartColor
self.leadingChartColor = viewModel.leadingDelta.direction.chartColor
self.trailingTitle = viewModel.trailingTitle
self.trailingValue = viewModel.trailingValue
self.trailingDelta = viewModel.trailingDelta
self.trailingDeltaColor = viewModel.trailingDeltaColor
self.trailingDeltaTextColor = viewModel.trailingDeltaTextColor
self.trailingDelta = viewModel.trailingDelta.string
self.trailingDeltaColor = viewModel.trailingDelta.direction.deltaBackgroundColor
self.trailingDeltaTextColor = viewModel.trailingDelta.direction.deltaTextColor
self.trailingChartData = viewModel.trailingChartData
self.trailingChartColor = viewModel.trailingChartColor
self.trailingChartColor = viewModel.trailingDelta.direction.chartColor
self.isRedacted = viewModel.isRedacted
self.showSyncError = viewModel.showSyncError
self.syncErrorMessage = viewModel.syncErrorMessage
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import Foundation
import class UIKit.UIColor

/// Represents a formatted delta percentage string and its direction of change
struct DeltaPercentage {
/// The delta percentage formatted as a localized string (e.g. `+100%`)
let string: String

/// The direction of change
let direction: Direction

init(string: String, direction: Direction) {
self.string = string
self.direction = direction
}

/// Convenience initializer
/// - Parameters:
/// - value: The percentage expressed as a `Decimal` (e.g. 0.5 for 50%).
/// - formatter: The formatter used to format the value as a string.
init(value: Decimal, formatter: NumberFormatter) {
self.string = formatter.string(from: value as NSNumber) ?? ""
self.direction = {
if value > 0 {
return .positive
} else if value < 0 {
return .negative
} else {
return .zero
}
}()
}

/// Represents the direction of change for a delta value
enum Direction {
case positive
case negative
case zero

/// Background color for a `DeltaTag`
var deltaBackgroundColor: UIColor {
switch self {
case .positive:
return Constants.green
case .negative:
return Constants.red
case .zero:
return Constants.lightGray
}
}

/// Text color for a `DeltaTag`
var deltaTextColor: UIColor {
switch self {
case .positive, .negative:
return .textInverted
case .zero:
return .text
}
}

/// Line color for an `AnalyticsLineChart`
var chartColor: UIColor {
switch self {
case .positive:
return Constants.green
case .negative:
return Constants.red
case .zero:
return Constants.darkGray
}
}
}
}

// MARK: Constants
extension DeltaPercentage {
enum Constants {
static let green: UIColor = .withColorStudio(.green, shade: .shade50)
static let red: UIColor = .withColorStudio(.red, shade: .shade40)
static let lightGray: UIColor = .withColorStudio(.gray, shade: .shade0)
static let darkGray: UIColor = .withColorStudio(.gray, shade: .shade30)
}
}
Loading