From 81c24bb2702c00c4fb96649bb8fd1f2067566379 Mon Sep 17 00:00:00 2001 From: Go Takagi <15936908+shimastripe@users.noreply.github.com> Date: Sat, 6 Apr 2024 20:23:07 +0900 Subject: [PATCH] Rounding up and down seconds for query date ranges --- Sources/IAPModel/IAPModel.swift | 35 +++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/Sources/IAPModel/IAPModel.swift b/Sources/IAPModel/IAPModel.swift index 6d99032..a1984b7 100644 --- a/Sources/IAPModel/IAPModel.swift +++ b/Sources/IAPModel/IAPModel.swift @@ -13,6 +13,11 @@ import Observation @Observable public final class IAPModel { + // MARK: - Dependency + + @ObservationIgnored + @Dependency(\.calendar) var calendar + // MARK: - State model public private(set) var rootCertificateState: LoadingViewState = .waiting @@ -180,6 +185,8 @@ extension IAPModel { case .fetchNotificationHistory(let startDate, let endDate, let transactionID): guard !fetchNotificationHistoryState.isLoading, + let startDate = truncateSeconds(of: startDate), + let endDate = roundUpSeconds(of: endDate), let credential = credentialState.value, let rootCertificate = rootCertificateState.value else { return } @@ -201,6 +208,8 @@ extension IAPModel { case .appendFetchNotificationHistory(let startDate, let endDate, let transactionID): guard !fetchNotificationHistoryState.isAppendLoading, + let startDate = truncateSeconds(of: startDate), + let endDate = roundUpSeconds(of: endDate), let credential = credentialState.value, let rootCertificate = rootCertificateState.value, let notificationHistory = fetchNotificationHistoryState.value @@ -226,6 +235,8 @@ extension IAPModel { case .bulkAppendFetchNotificationHistory(let startDate, let endDate, let transactionID): guard !fetchNotificationHistoryState.isAppendLoading, + let startDate = truncateSeconds(of: startDate), + let endDate = roundUpSeconds(of: endDate), let credential = credentialState.value, let rootCertificate = rootCertificateState.value, let notificationHistory = fetchNotificationHistoryState.value @@ -268,6 +279,8 @@ extension IAPModel { case .fetchTransactionHistory(let startDate, let endDate, let transactionID): guard !fetchTransactionHistoryState.isLoading, + let startDate = truncateSeconds(of: startDate), + let endDate = roundUpSeconds(of: endDate), let credential = credentialState.value, let rootCertificate = rootCertificateState.value else { return } @@ -288,6 +301,8 @@ extension IAPModel { case .appendFetchTransactionHistory(let startDate, let endDate, let transactionID): guard !fetchTransactionHistoryState.isAppendLoading, + let startDate = truncateSeconds(of: startDate), + let endDate = roundUpSeconds(of: endDate), let credential = credentialState.value, let rootCertificate = rootCertificateState.value, let transactionHistory = fetchTransactionHistoryState.value @@ -312,6 +327,8 @@ extension IAPModel { case .bulkAppendFetchTransactionHistory(let startDate, let endDate, let transactionID): guard !fetchTransactionHistoryState.isAppendLoading, + let startDate = truncateSeconds(of: startDate), + let endDate = roundUpSeconds(of: endDate), let credential = credentialState.value, let rootCertificate = rootCertificateState.value, let transactionHistory = fetchTransactionHistoryState.value @@ -373,3 +390,21 @@ extension IAPModel { } } } + +// MARK: - Helper + +extension IAPModel { + + func truncateSeconds(of date: Date) -> Date? { + calendar.date( + from: calendar.dateComponents([.year, .month, .day, .hour, .minute], from: date)) + } + + func roundUpSeconds(of date: Date) -> Date? { + var components = calendar.dateComponents([.year, .month, .day, .hour, .minute], from: date) + if let minutes = components.minute { + components.minute = minutes + 1 + } + return calendar.date(from: components) + } +}