@@ -28,28 +28,19 @@ final class AnalyticsHubViewModel: ObservableObject {
2828
2929 /// Revenue Card ViewModel
3030 ///
31- /// Setting an `AnalyticsHubViewModel` with `nil` order data displays the revenue card with placeholders.
32- /// Setting the view model to `nil` displays an error message instead of the revenue card.
33- ///
34- @Published var revenueCard : AnalyticsReportCardViewModel ? = AnalyticsHubViewModel . revenueCard ( currentPeriodStats: nil , previousPeriodStats: nil )
31+ @Published var revenueCard = AnalyticsHubViewModel . revenueCard ( currentPeriodStats: nil , previousPeriodStats: nil )
3532
3633 /// Orders Card ViewModel
3734 ///
38- /// Setting an `AnalyticsHubViewModel` with `nil` order data displays the order card with placeholders.
39- /// Setting the view model to `nil` displays an error message instead of the order card.
40- ///
41- @Published var ordersCard : AnalyticsReportCardViewModel ? = AnalyticsHubViewModel . ordersCard ( currentPeriodStats: nil , previousPeriodStats: nil )
35+ @Published var ordersCard = AnalyticsHubViewModel . ordersCard ( currentPeriodStats: nil , previousPeriodStats: nil )
4236
4337 /// Time Range ViewModel
4438 ///
4539 @Published var timeRangeCard : AnalyticsTimeRangeCardViewModel
4640
4741 /// Products Card ViewModel
4842 ///
49- /// Setting an `AnalyticsProductCardViewModel` with `nil` order data displays the product card with placeholders.
50- /// Setting the view model to `nil` displays an error message instead of the product card.
51- ///
52- @Published var productCard : AnalyticsProductCardViewModel ? = AnalyticsHubViewModel . productCard ( currentPeriodStats: nil , previousPeriodStats: nil )
43+ @Published var productCard = AnalyticsHubViewModel . productCard ( currentPeriodStats: nil , previousPeriodStats: nil )
5344
5445 // MARK: Private data
5546
@@ -120,15 +111,14 @@ private extension AnalyticsHubViewModel {
120111private extension AnalyticsHubViewModel {
121112
122113 func switchToLoadingState( ) {
123- self . revenueCard = revenueCard? . redacted
124- self . ordersCard = ordersCard? . redacted
125- self . productCard = productCard? . redacted
114+ self . revenueCard = revenueCard. redacted
115+ self . ordersCard = ordersCard. redacted
116+ self . productCard = productCard. redacted
126117 }
127118
128119 func switchToErrorState( ) {
129- self . revenueCard = nil
130- self . ordersCard = nil
131- self . productCard = nil
120+ self . currentOrderStats = nil
121+ self . previousOrderStats = nil
132122 }
133123
134124 func bindViewModelsWithData( ) {
@@ -144,6 +134,7 @@ private extension AnalyticsHubViewModel {
144134 }
145135
146136 static func revenueCard( currentPeriodStats: OrderStatsV4 ? , previousPeriodStats: OrderStatsV4 ? ) -> AnalyticsReportCardViewModel {
137+ let showSyncError = currentPeriodStats == nil || previousPeriodStats == nil
147138 let totalDelta = StatsDataTextFormatter . createTotalRevenueDelta ( from: previousPeriodStats, to: currentPeriodStats)
148139 let netDelta = StatsDataTextFormatter . createNetRevenueDelta ( from: previousPeriodStats, to: currentPeriodStats)
149140
@@ -159,10 +150,13 @@ private extension AnalyticsHubViewModel {
159150 trailingDelta: netDelta. string,
160151 trailingDeltaColor: Constants . deltaColor ( for: netDelta. direction) ,
161152 trailingChartData: StatsIntervalDataParser . getChartData ( for: . netRevenue, from: currentPeriodStats) ,
162- isRedacted: false )
153+ isRedacted: false ,
154+ showSyncError: showSyncError,
155+ syncErrorMessage: Localization . RevenueCard. noRevenue)
163156 }
164157
165158 static func ordersCard( currentPeriodStats: OrderStatsV4 ? , previousPeriodStats: OrderStatsV4 ? ) -> AnalyticsReportCardViewModel {
159+ let showSyncError = currentPeriodStats == nil || previousPeriodStats == nil
166160 let ordersCountDelta = StatsDataTextFormatter . createOrderCountDelta ( from: previousPeriodStats, to: currentPeriodStats)
167161 let orderValueDelta = StatsDataTextFormatter . createAverageOrderValueDelta ( from: previousPeriodStats, to: currentPeriodStats)
168162
@@ -178,17 +172,21 @@ private extension AnalyticsHubViewModel {
178172 trailingDelta: orderValueDelta. string,
179173 trailingDeltaColor: Constants . deltaColor ( for: orderValueDelta. direction) ,
180174 trailingChartData: StatsIntervalDataParser . getChartData ( for: . averageOrderValue, from: currentPeriodStats) ,
181- isRedacted: false )
175+ isRedacted: false ,
176+ showSyncError: showSyncError,
177+ syncErrorMessage: Localization . OrderCard. noOrders)
182178 }
183179
184180 static func productCard( currentPeriodStats: OrderStatsV4 ? , previousPeriodStats: OrderStatsV4 ? ) -> AnalyticsProductCardViewModel {
181+ let showSyncError = currentPeriodStats == nil || previousPeriodStats == nil
185182 let itemsSold = StatsDataTextFormatter . createItemsSoldText ( orderStats: currentPeriodStats)
186183 let itemsSoldDelta = StatsDataTextFormatter . createOrderItemsSoldDelta ( from: previousPeriodStats, to: currentPeriodStats)
187184
188185 return AnalyticsProductCardViewModel ( itemsSold: itemsSold,
189186 delta: itemsSoldDelta. string,
190187 deltaBackgroundColor: Constants . deltaColor ( for: itemsSoldDelta. direction) ,
191- isRedacted: false )
188+ isRedacted: false ,
189+ showSyncError: showSyncError)
192190 }
193191}
194192
@@ -210,12 +208,16 @@ private extension AnalyticsHubViewModel {
210208 static let title = NSLocalizedString ( " REVENUE " , comment: " Title for revenue analytics section in the Analytics Hub " )
211209 static let leadingTitle = NSLocalizedString ( " Total Sales " , comment: " Label for total sales (gross revenue) in the Analytics Hub " )
212210 static let trailingTitle = NSLocalizedString ( " Net Sales " , comment: " Label for net sales (net revenue) in the Analytics Hub " )
211+ static let noRevenue = NSLocalizedString ( " Unable to load revenue analytics " ,
212+ comment: " Text displayed when there is an error loading revenue stats data. " )
213213 }
214214
215215 enum OrderCard {
216216 static let title = NSLocalizedString ( " ORDERS " , comment: " Title for order analytics section in the Analytics Hub " )
217217 static let leadingTitle = NSLocalizedString ( " Total Orders " , comment: " Label for total number of orders in the Analytics Hub " )
218218 static let trailingTitle = NSLocalizedString ( " Average Order Value " , comment: " Label for average value of orders in the Analytics Hub " )
219+ static let noOrders = NSLocalizedString ( " Unable to load order analytics " ,
220+ comment: " Text displayed when there is an error loading order stats data. " )
219221 }
220222 }
221223}
0 commit comments