Skip to content

Commit

Permalink
Rounding up and down seconds for query date ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
shimastripe committed Apr 6, 2024
1 parent 82175e5 commit 81c24bb
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions Sources/IAPModel/IAPModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<Data> = .waiting
Expand Down Expand Up @@ -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 }
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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 }
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
}
}

0 comments on commit 81c24bb

Please sign in to comment.