Skip to content

Commit 7847e73

Browse files
authored
Merge pull request #8211 from woocommerce/issue/8189-add-analytics-hub-network-actions
[Analytics Hub] Add network actions
2 parents aea78e4 + 3541e25 commit 7847e73

File tree

5 files changed

+104
-4
lines changed

5 files changed

+104
-4
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import SwiftUI
55
/// Hosting Controller for the `AnalyticsHubView` view.
66
///
77
final class AnalyticsHubHostingViewController: UIHostingController<AnalyticsHubView> {
8-
init(timeRange: StatsTimeRangeV4) {
9-
let viewModel = AnalyticsHubViewModel()
8+
init(siteID: Int64, timeRange: StatsTimeRangeV4) {
9+
let viewModel = AnalyticsHubViewModel(siteID: siteID)
1010
super.init(rootView: AnalyticsHubView(viewModel: viewModel))
1111
}
1212

@@ -81,7 +81,7 @@ private extension AnalyticsHubView {
8181
struct AnalyticsHubPreview: PreviewProvider {
8282
static var previews: some View {
8383
NavigationView {
84-
AnalyticsHubView(viewModel: AnalyticsHubViewModel())
84+
AnalyticsHubView(viewModel: AnalyticsHubViewModel(siteID: 123))
8585
}
8686
}
8787
}

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

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,23 @@ import Yosemite
55
///
66
final class AnalyticsHubViewModel: ObservableObject {
77

8+
private let siteID: Int64
9+
private let stores: StoresManager
10+
11+
init(siteID: Int64,
12+
stores: StoresManager = ServiceLocator.stores) {
13+
self.siteID = siteID
14+
self.stores = stores
15+
16+
Task.init {
17+
do {
18+
try await retrieveOrderStats()
19+
} catch {
20+
DDLogWarn("⚠️ Error fetching analytics data: \(error)")
21+
}
22+
}
23+
}
24+
825
/// Revenue Card ViewModel
926
///
1027
@Published var revenueCard = AnalyticsReportCardViewModel(title: "REVENUE",
@@ -39,3 +56,44 @@ final class AnalyticsHubViewModel: ObservableObject {
3956
///
4057
@Published private var previousOrderStats: OrderStatsV4? = nil
4158
}
59+
60+
private extension AnalyticsHubViewModel {
61+
62+
@MainActor
63+
func retrieveOrderStats() async throws {
64+
// TODO: get dates from the selected period
65+
let currentMonthDate = Date()
66+
let previousMonthDate = Calendar.current.date(byAdding: .month, value: -1, to: Date())!
67+
68+
async let currentPeriodRequest = retrieveStats(earliestDateToInclude: currentMonthDate.startOfMonth(timezone: .current),
69+
latestDateToInclude: currentMonthDate.endOfMonth(timezone: .current),
70+
forceRefresh: true)
71+
async let previousPeriodRequest = retrieveStats(earliestDateToInclude: previousMonthDate.startOfMonth(timezone: .current),
72+
latestDateToInclude: previousMonthDate.endOfMonth(timezone: .current),
73+
forceRefresh: true)
74+
let (currentPeriodStats, previousPeriodStats) = try await (currentPeriodRequest, previousPeriodRequest)
75+
self.currentOrderStats = currentPeriodStats
76+
self.previousOrderStats = previousPeriodStats
77+
}
78+
79+
@MainActor
80+
func retrieveStats(earliestDateToInclude: Date,
81+
latestDateToInclude: Date,
82+
forceRefresh: Bool) async throws -> OrderStatsV4 {
83+
try await withCheckedThrowingContinuation { continuation in
84+
// TODO: get unit and quantity from the selected period
85+
let unit: StatsGranularityV4 = .daily
86+
let quantity = 31
87+
88+
let action = StatsActionV4.retrieveCustomStats(siteID: siteID,
89+
unit: unit,
90+
earliestDateToInclude: earliestDateToInclude,
91+
latestDateToInclude: latestDateToInclude,
92+
quantity: quantity,
93+
forceRefresh: forceRefresh) { result in
94+
continuation.resume(with: result)
95+
}
96+
stores.dispatch(action)
97+
}
98+
}
99+
}

WooCommerce/Classes/ViewRelated/Dashboard/Stats v4/StoreStatsAndTopPerformersPeriodViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ private extension StoreStatsAndTopPerformersPeriodViewController {
357357
}
358358

359359
@objc func seeMoreButtonTapped() {
360-
let analyticsHubVC = AnalyticsHubHostingViewController(timeRange: timeRange)
360+
let analyticsHubVC = AnalyticsHubHostingViewController(siteID: siteID, timeRange: timeRange)
361361
show(analyticsHubVC, sender: self)
362362
}
363363
}

Yosemite/Yosemite/Actions/StatsActionV4.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@ public enum StatsActionV4: Action {
1919
forceRefresh: Bool,
2020
onCompletion: (Result<Void, Error>) -> Void)
2121

22+
/// Retrieves `OrderStats` for the provided siteID, and time range, without saving them to the Storage layer.
23+
///
24+
case retrieveCustomStats(siteID: Int64,
25+
unit: StatsGranularityV4,
26+
earliestDateToInclude: Date,
27+
latestDateToInclude: Date,
28+
quantity: Int,
29+
forceRefresh: Bool,
30+
onCompletion: (Result<OrderStatsV4, Error>) -> Void)
31+
2232
/// Synchronizes `SiteVisitStats` for the provided siteID, time range, and date.
2333
///
2434
case retrieveSiteVisitStats(siteID: Int64,

Yosemite/Yosemite/Stores/StatsStoreV4.swift

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,20 @@ public final class StatsStoreV4: Store {
5050
quantity: quantity,
5151
forceRefresh: forceRefresh,
5252
onCompletion: onCompletion)
53+
case .retrieveCustomStats(let siteID,
54+
let unit,
55+
let earliestDateToInclude,
56+
let latestDateToInclude,
57+
let quantity,
58+
let forceRefresh,
59+
let onCompletion):
60+
retrieveCustomStats(siteID: siteID,
61+
unit: unit,
62+
earliestDateToInclude: earliestDateToInclude,
63+
latestDateToInclude: latestDateToInclude,
64+
quantity: quantity,
65+
forceRefresh: forceRefresh,
66+
onCompletion: onCompletion)
5367
case .retrieveSiteVisitStats(let siteID,
5468
let siteTimezone,
5569
let timeRange,
@@ -120,6 +134,24 @@ private extension StatsStoreV4 {
120134
}
121135
}
122136

137+
/// Retrieves the order stats for the provided siteID, and time range, without saving them to the Storage layer.
138+
///
139+
func retrieveCustomStats(siteID: Int64,
140+
unit: StatsGranularityV4,
141+
earliestDateToInclude: Date,
142+
latestDateToInclude: Date,
143+
quantity: Int,
144+
forceRefresh: Bool,
145+
onCompletion: @escaping (Result<OrderStatsV4, Error>) -> Void) {
146+
orderStatsRemote.loadOrderStats(for: siteID,
147+
unit: unit,
148+
earliestDateToInclude: earliestDateToInclude,
149+
latestDateToInclude: latestDateToInclude,
150+
quantity: quantity,
151+
forceRefresh: forceRefresh,
152+
completion: onCompletion)
153+
}
154+
123155
/// Retrieves the site visit stats associated with the provided Site ID (if any!).
124156
///
125157
func retrieveSiteVisitStats(siteID: Int64,

0 commit comments

Comments
 (0)