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 @@ -5,8 +5,8 @@ import SwiftUI
/// Hosting Controller for the `AnalyticsHubView` view.
///
final class AnalyticsHubHostingViewController: UIHostingController<AnalyticsHubView> {
init(timeRange: StatsTimeRangeV4) {
let viewModel = AnalyticsHubViewModel()
init(siteID: Int64, timeRange: StatsTimeRangeV4) {
let viewModel = AnalyticsHubViewModel(siteID: siteID)
super.init(rootView: AnalyticsHubView(viewModel: viewModel))
}

Expand Down Expand Up @@ -81,7 +81,7 @@ private extension AnalyticsHubView {
struct AnalyticsHubPreview: PreviewProvider {
static var previews: some View {
NavigationView {
AnalyticsHubView(viewModel: AnalyticsHubViewModel())
AnalyticsHubView(viewModel: AnalyticsHubViewModel(siteID: 123))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,23 @@ import Yosemite
///
final class AnalyticsHubViewModel: ObservableObject {

private let siteID: Int64
private let stores: StoresManager

init(siteID: Int64,
stores: StoresManager = ServiceLocator.stores) {
self.siteID = siteID
self.stores = stores

Task.init {
do {
try await retrieveOrderStats()
} catch {
DDLogWarn("⚠️ Error fetching analytics data: \(error)")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have a task to handle these errors later?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now we have #8213!

}
}
}

/// Revenue Card ViewModel
///
@Published var revenueCard = AnalyticsReportCardViewModel(title: "REVENUE",
Expand Down Expand Up @@ -39,3 +56,44 @@ final class AnalyticsHubViewModel: ObservableObject {
///
@Published private var previousOrderStats: OrderStatsV4? = nil
}

private extension AnalyticsHubViewModel {

@MainActor
func retrieveOrderStats() async throws {
// TODO: get dates from the selected period
let currentMonthDate = Date()
let previousMonthDate = Calendar.current.date(byAdding: .month, value: -1, to: Date())!
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is temporary right? The ! concerned me 😅

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, dates should come from #8193 🙂


async let currentPeriodRequest = retrieveStats(earliestDateToInclude: currentMonthDate.startOfMonth(timezone: .current),
latestDateToInclude: currentMonthDate.endOfMonth(timezone: .current),
forceRefresh: true)
async let previousPeriodRequest = retrieveStats(earliestDateToInclude: previousMonthDate.startOfMonth(timezone: .current),
latestDateToInclude: previousMonthDate.endOfMonth(timezone: .current),
forceRefresh: true)
let (currentPeriodStats, previousPeriodStats) = try await (currentPeriodRequest, previousPeriodRequest)
self.currentOrderStats = currentPeriodStats
self.previousOrderStats = previousPeriodStats
}

@MainActor
func retrieveStats(earliestDateToInclude: Date,
latestDateToInclude: Date,
forceRefresh: Bool) async throws -> OrderStatsV4 {
try await withCheckedThrowingContinuation { continuation in
// TODO: get unit and quantity from the selected period
let unit: StatsGranularityV4 = .daily
let quantity = 31

let action = StatsActionV4.retrieveCustomStats(siteID: siteID,
unit: unit,
earliestDateToInclude: earliestDateToInclude,
latestDateToInclude: latestDateToInclude,
quantity: quantity,
forceRefresh: forceRefresh) { result in
continuation.resume(with: result)
}
stores.dispatch(action)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ private extension StoreStatsAndTopPerformersPeriodViewController {
}

@objc func seeMoreButtonTapped() {
let analyticsHubVC = AnalyticsHubHostingViewController(timeRange: timeRange)
let analyticsHubVC = AnalyticsHubHostingViewController(siteID: siteID, timeRange: timeRange)
show(analyticsHubVC, sender: self)
}
}
Expand Down
10 changes: 10 additions & 0 deletions Yosemite/Yosemite/Actions/StatsActionV4.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ public enum StatsActionV4: Action {
forceRefresh: Bool,
onCompletion: (Result<Void, Error>) -> Void)

/// Retrieves `OrderStats` for the provided siteID, and time range, without saving them to the Storage layer.
///
case retrieveCustomStats(siteID: Int64,
unit: StatsGranularityV4,
earliestDateToInclude: Date,
latestDateToInclude: Date,
quantity: Int,
forceRefresh: Bool,
onCompletion: (Result<OrderStatsV4, Error>) -> Void)

/// Synchronizes `SiteVisitStats` for the provided siteID, time range, and date.
///
case retrieveSiteVisitStats(siteID: Int64,
Expand Down
32 changes: 32 additions & 0 deletions Yosemite/Yosemite/Stores/StatsStoreV4.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,20 @@ public final class StatsStoreV4: Store {
quantity: quantity,
forceRefresh: forceRefresh,
onCompletion: onCompletion)
case .retrieveCustomStats(let siteID,
let unit,
let earliestDateToInclude,
let latestDateToInclude,
let quantity,
let forceRefresh,
let onCompletion):
retrieveCustomStats(siteID: siteID,
unit: unit,
earliestDateToInclude: earliestDateToInclude,
latestDateToInclude: latestDateToInclude,
quantity: quantity,
forceRefresh: forceRefresh,
onCompletion: onCompletion)
case .retrieveSiteVisitStats(let siteID,
let siteTimezone,
let timeRange,
Expand Down Expand Up @@ -120,6 +134,24 @@ private extension StatsStoreV4 {
}
}

/// Retrieves the order stats for the provided siteID, and time range, without saving them to the Storage layer.
///
func retrieveCustomStats(siteID: Int64,
unit: StatsGranularityV4,
earliestDateToInclude: Date,
latestDateToInclude: Date,
quantity: Int,
forceRefresh: Bool,
onCompletion: @escaping (Result<OrderStatsV4, Error>) -> Void) {
orderStatsRemote.loadOrderStats(for: siteID,
unit: unit,
earliestDateToInclude: earliestDateToInclude,
latestDateToInclude: latestDateToInclude,
quantity: quantity,
forceRefresh: forceRefresh,
completion: onCompletion)
}

/// Retrieves the site visit stats associated with the provided Site ID (if any!).
///
func retrieveSiteVisitStats(siteID: Int64,
Expand Down