Skip to content

Commit

Permalink
Merge pull request #7707 from woocommerce/issue/7564-store-tz-settings
Browse files Browse the repository at this point in the history
Widgets: Reuse Date extension
  • Loading branch information
ealeksandrov authored Sep 13, 2022
2 parents ab6274c + eedc08e commit 67b8432
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 38 deletions.
32 changes: 4 additions & 28 deletions WooCommerce/StoreWidgets/StoreInfoDataService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,15 @@ final class StoreInfoDataService {
private extension StoreInfoDataService {

/// Async wrapper that fetches todays revenues & orders.
/// TODO: Update dates with the correct timezone.
///
func fetchTodaysRevenueAndOrders(for storeID: Int64) async throws -> OrderStatsV4 {
try await withCheckedThrowingContinuation { continuation in
// `WKWebView` is accessed internally, we are foreced to dispatch the call in the main thread.
// `WKWebView` is accessed internally, we are forced to dispatch the call in the main thread.
Task { @MainActor in
orderStatsRemoteV4.loadOrderStats(for: storeID,
unit: .hourly,
earliestDateToInclude: Date.startOfToday,
latestDateToInclude: Date.endOfToday,
earliestDateToInclude: Date().startOfDay(timezone: .current),
latestDateToInclude: Date().endOfDay(timezone: .current),
quantity: 24,
forceRefresh: true) { result in
continuation.resume(with: result)
Expand All @@ -74,41 +73,18 @@ private extension StoreInfoDataService {
}

/// Async wrapper that fetches todays visitors.
/// TODO: Update dates with the correct timezone.
///
func fetchTodaysVisitors(for storeID: Int64) async throws -> SiteVisitStats {
try await withCheckedThrowingContinuation { continuation in
// `WKWebView` is accessed internally, we are foreced to dispatch the call in the main thread.
Task { @MainActor in
siteVisitStatsRemote.loadSiteVisitorStats(for: storeID,
unit: .day,
latestDateToInclude: Date.startOfToday,
latestDateToInclude: Date().endOfDay(timezone: .current),
quantity: 1) { result in
continuation.resume(with: result)
}
}
}
}
}

// TEMP: Update dates with the correct timezones mimic the app behaviour
private extension Date {

/// Temporary function to get the start of day in the device timezone.
///
static var startOfToday: Date {
var calendar = Calendar(identifier: .gregorian)
calendar.timeZone = .current
return calendar.startOfDay(for: Date())
}

/// Temporary function to get the end of day in the device timezone.
///
static var endOfToday: Date {
var calendar = Calendar(identifier: .gregorian)
calendar.timeZone = .current
let startDate = calendar.startOfDay(for: Date())
let components = DateComponents(day: 1, second: -1)
return calendar.date(byAdding: components, to: startDate) ?? Date()
}
}
8 changes: 0 additions & 8 deletions WooCommerce/WooCommerce.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -333,8 +333,6 @@
02B2828E27C35061004A332A /* RefreshableInfiniteScrollList.swift in Sources */ = {isa = PBXBuildFile; fileRef = 02B2828D27C35061004A332A /* RefreshableInfiniteScrollList.swift */; };
02B2829027C352DA004A332A /* RefreshableScrollView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 02B2828F27C352DA004A332A /* RefreshableScrollView.swift */; };
02B2829227C4808D004A332A /* InfiniteScrollIndicator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 02B2829127C4808D004A332A /* InfiniteScrollIndicator.swift */; };
02B296A722FA6DB500FD7A4C /* Date+StartAndEnd.swift in Sources */ = {isa = PBXBuildFile; fileRef = 02B296A622FA6DB500FD7A4C /* Date+StartAndEnd.swift */; };
02B296A922FA6E0000FD7A4C /* DateStartAndEndTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 02B296A822FA6E0000FD7A4C /* DateStartAndEndTests.swift */; };
02B2C831249C4C8D0040C83C /* TextFieldTextAlignmentTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 02B2C830249C4C8D0040C83C /* TextFieldTextAlignmentTests.swift */; };
02B653AC2429F7BF00A9C839 /* MockTaxClassStoresManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 02B653AB2429F7BF00A9C839 /* MockTaxClassStoresManager.swift */; };
02B8650F24A9E2D800265779 /* Product+SwiftUIPreviewHelpers.swift in Sources */ = {isa = PBXBuildFile; fileRef = 02B8650E24A9E2D800265779 /* Product+SwiftUIPreviewHelpers.swift */; };
Expand Down Expand Up @@ -2240,8 +2238,6 @@
02B2828D27C35061004A332A /* RefreshableInfiniteScrollList.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RefreshableInfiniteScrollList.swift; sourceTree = "<group>"; };
02B2828F27C352DA004A332A /* RefreshableScrollView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RefreshableScrollView.swift; sourceTree = "<group>"; };
02B2829127C4808D004A332A /* InfiniteScrollIndicator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InfiniteScrollIndicator.swift; sourceTree = "<group>"; };
02B296A622FA6DB500FD7A4C /* Date+StartAndEnd.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Date+StartAndEnd.swift"; sourceTree = "<group>"; };
02B296A822FA6E0000FD7A4C /* DateStartAndEndTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DateStartAndEndTests.swift; sourceTree = "<group>"; };
02B2C830249C4C8D0040C83C /* TextFieldTextAlignmentTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TextFieldTextAlignmentTests.swift; sourceTree = "<group>"; };
02B653AB2429F7BF00A9C839 /* MockTaxClassStoresManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockTaxClassStoresManager.swift; sourceTree = "<group>"; };
02B8650E24A9E2D800265779 /* Product+SwiftUIPreviewHelpers.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Product+SwiftUIPreviewHelpers.swift"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -6852,7 +6848,6 @@
B5980A6621AC91AA00EBF596 /* BundleWooTests.swift */,
B57C5C9821B80E7100FF82B2 /* DataWooTests.swift */,
746FC23C2200A62B00C3096C /* DateWooTests.swift */,
02B296A822FA6E0000FD7A4C /* DateStartAndEndTests.swift */,
B57C5C9721B80E7100FF82B2 /* DictionaryWooTests.swift */,
748C7783211E2D8400814F2C /* DoubleWooTests.swift */,
B5C6CE602190D28E00515926 /* NSAttributedStringHelperTests.swift */,
Expand Down Expand Up @@ -7550,7 +7545,6 @@
D82DFB49225F22D400EFE2CB /* UISearchBar+Appearance.swift */,
02820F3322C257B700DE0D37 /* UITableView+HeaderFooterHelpers.swift */,
456417F3247D5434001203F6 /* UITableView+Helpers.swift */,
02B296A622FA6DB500FD7A4C /* Date+StartAndEnd.swift */,
02D45646231CB1FB008CF0A9 /* UIImage+Dot.swift */,
020DD48E232392C9005822B1 /* UIViewController+AppReview.swift */,
039D948C27610C6F0044EF38 /* UIView+SafeAreaConstraints.swift */,
Expand Down Expand Up @@ -10011,7 +10005,6 @@
3120491B26DD80E000A4EC4F /* ActivitySpinnerAndLabelTableViewCell.swift in Sources */,
DEC51AFD276AEAE3009F3DF4 /* SystemStatusReportView.swift in Sources */,
CECC759C23D61C1400486676 /* AggregateDataHelper.swift in Sources */,
02B296A722FA6DB500FD7A4C /* Date+StartAndEnd.swift in Sources */,
02645D7D27BA027B0065DC68 /* Inbox.swift in Sources */,
D81D9228222E7F0800FFA585 /* OrderStatusListViewController.swift in Sources */,
CEE006082077D14C0079161F /* OrderDetailsViewController.swift in Sources */,
Expand Down Expand Up @@ -10631,7 +10624,6 @@
4572641B27F1FDF2004E1F95 /* AddEditCouponViewModelTests.swift in Sources */,
AEE1D4FF25D1580E006A490B /* AttributeOptionListSelectorCommandTests.swift in Sources */,
CC5BA5F5287EDC900072F307 /* OrderCustomFieldsViewModelTests.swift in Sources */,
02B296A922FA6E0000FD7A4C /* DateStartAndEndTests.swift in Sources */,
02B653AC2429F7BF00A9C839 /* MockTaxClassStoresManager.swift in Sources */,
0277AEA5256CAA4200F45C4A /* MockShippingLabel.swift in Sources */,
02BAB02324D0250300F8B06E /* ProductVariation+ProductFormTests.swift in Sources */,
Expand Down
16 changes: 16 additions & 0 deletions WooFoundation/WooFoundation.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
689D11D32891B7D800F6A83F /* MockCrashLogger.swift in Sources */ = {isa = PBXBuildFile; fileRef = 689D11D22891B7D800F6A83F /* MockCrashLogger.swift */; };
68FBC5B328926B2C00A05461 /* Collection+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 68FBC5B228926B2C00A05461 /* Collection+Extensions.swift */; };
9FA5113235035AC9A6079B0D /* Pods_WooFoundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C1733C61561AE3A1AD3C16B7 /* Pods_WooFoundation.framework */; };
AE948D0A28CF67CF009F3246 /* Date+StartAndEnd.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE948D0928CF67CE009F3246 /* Date+StartAndEnd.swift */; };
AE948D0D28CF6D50009F3246 /* DateStartAndEndTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE948D0C28CF6D50009F3246 /* DateStartAndEndTests.swift */; };
B987B06F284540D300C53CF6 /* CurrencyCode.swift in Sources */ = {isa = PBXBuildFile; fileRef = B987B06E284540D300C53CF6 /* CurrencyCode.swift */; };
B9C9C63F283E703C001B879F /* WooFoundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B9C9C635283E703C001B879F /* WooFoundation.framework */; };
B9C9C659283E7195001B879F /* NSDecimalNumber+Helpers.swift in Sources */ = {isa = PBXBuildFile; fileRef = B9C9C658283E7195001B879F /* NSDecimalNumber+Helpers.swift */; };
Expand Down Expand Up @@ -62,6 +64,8 @@
81B8569CD52D20EAE64EE737 /* Pods-WooFoundation.release-alpha.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-WooFoundation.release-alpha.xcconfig"; path = "Target Support Files/Pods-WooFoundation/Pods-WooFoundation.release-alpha.xcconfig"; sourceTree = "<group>"; };
99861FECBD0975C25DA03D80 /* Pods-WooFoundation.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-WooFoundation.debug.xcconfig"; path = "Target Support Files/Pods-WooFoundation/Pods-WooFoundation.debug.xcconfig"; sourceTree = "<group>"; };
A21D73D352B4162AB096E276 /* Pods-WooFoundationTests.release-alpha.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-WooFoundationTests.release-alpha.xcconfig"; path = "Target Support Files/Pods-WooFoundationTests/Pods-WooFoundationTests.release-alpha.xcconfig"; sourceTree = "<group>"; };
AE948D0928CF67CE009F3246 /* Date+StartAndEnd.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Date+StartAndEnd.swift"; sourceTree = "<group>"; };
AE948D0C28CF6D50009F3246 /* DateStartAndEndTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DateStartAndEndTests.swift; sourceTree = "<group>"; };
B987B06E284540D300C53CF6 /* CurrencyCode.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CurrencyCode.swift; sourceTree = "<group>"; };
B9AED558283E7553002A2668 /* Yosemite.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = Yosemite.framework; sourceTree = BUILT_PRODUCTS_DIR; };
B9AED55B283E755A002A2668 /* Hardware.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = Hardware.framework; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -163,6 +167,14 @@
path = Mocks;
sourceTree = "<group>";
};
AE948D0B28CF6D34009F3246 /* Extensions */ = {
isa = PBXGroup;
children = (
AE948D0C28CF6D50009F3246 /* DateStartAndEndTests.swift */,
);
path = Extensions;
sourceTree = "<group>";
};
B9C9C5ED283E6FAA001B879F = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -201,6 +213,7 @@
B9C9C642283E703C001B879F /* WooFoundationTests */ = {
isa = PBXGroup;
children = (
AE948D0B28CF6D34009F3246 /* Extensions */,
686BE913288EE2BC00967C86 /* Utilities */,
);
path = WooFoundationTests;
Expand All @@ -209,6 +222,7 @@
B9C9C657283E7174001B879F /* Extensions */ = {
isa = PBXGroup;
children = (
AE948D0928CF67CE009F3246 /* Date+StartAndEnd.swift */,
B9C9C65F283E71F4001B879F /* Double+Woo.swift */,
B9C9C658283E7195001B879F /* NSDecimalNumber+Helpers.swift */,
68FBC5B228926B2C00A05461 /* Collection+Extensions.swift */,
Expand Down Expand Up @@ -442,6 +456,7 @@
B9C9C663283E7296001B879F /* Logging.swift in Sources */,
6874E81428998AD300074A97 /* LogErrorAndExit.swift in Sources */,
B9C9C65D283E71C8001B879F /* CurrencyFormatter.swift in Sources */,
AE948D0A28CF67CF009F3246 /* Date+StartAndEnd.swift in Sources */,
26AF1F5628B8362800937BA9 /* UIColor+SystemColors.swift in Sources */,
689D11D32891B7D800F6A83F /* MockCrashLogger.swift in Sources */,
B987B06F284540D300C53CF6 /* CurrencyCode.swift in Sources */,
Expand All @@ -462,6 +477,7 @@
files = (
686BE915288EE2CA00967C86 /* TypedPredicateTests.swift in Sources */,
265C99DF28B94271005E6117 /* MurielColorTests.swift in Sources */,
AE948D0D28CF6D50009F3246 /* DateStartAndEndTests.swift in Sources */,
265C99DD28B941D5005E6117 /* UIColor+Muriel-Tests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Foundation

extension Date {
public extension Date {
// MARK: Day

/// Returns self's start of day in the given time zone.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import XCTest
@testable import WooCommerce
@testable import WooFoundation

final class DateStartAndEndTests: XCTestCase {
private let gmtPlus8TimeZone: TimeZone = TimeZone(secondsFromGMT: 8 * 3600)!
Expand Down

0 comments on commit 67b8432

Please sign in to comment.