Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
@@ -1,38 +1,19 @@
package com.woocommerce.android.ui.bookings.list

import org.wordpress.android.fluxc.network.rest.wpcom.wc.bookings.BookingsDateRangePresets
import org.wordpress.android.fluxc.network.rest.wpcom.wc.bookings.BookingsFilterOption
import java.time.Clock
import java.time.LocalDate
import java.time.LocalTime
import java.time.ZoneOffset
import javax.inject.Inject

class BookingListFiltersBuilder @Inject constructor(
private val clock: Clock
) {
/**
* Returns a [BookingsFilterOption.DateRange] based on the selected [BookingListTab].
*
* We use UTC for the API calls, as the API stores the dates without timezone information, which means that
* when comparing dates, they are treated as UTC times.
* See p1759398245019489-slack-C09FHQNQERG
*/
fun BookingListTab.asDateRangeFilter(): BookingsFilterOption.DateRange? {
fun todayAtMidnight() = LocalDate.now(clock).atTime(LocalTime.MIDNIGHT).atOffset(ZoneOffset.UTC).toInstant()
fun todayAtEndOfDay() = LocalDate.now(clock).atTime(LocalTime.MAX).atOffset(ZoneOffset.UTC).toInstant()

return when (this) {
BookingListTab.Today -> BookingsFilterOption.DateRange(
before = todayAtEndOfDay(),
after = todayAtMidnight()
)

BookingListTab.Upcoming -> BookingsFilterOption.DateRange(
before = null,
after = todayAtEndOfDay()
)

BookingListTab.All -> null
}
fun BookingListTab.asDateRangeFilter(): BookingsFilterOption.DateRange? = when (this) {
BookingListTab.Today -> BookingsDateRangePresets.today(clock)
BookingListTab.Upcoming -> BookingsDateRangePresets.upcoming(clock)
BookingListTab.All -> null
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.wordpress.android.fluxc.network.rest.wpcom.wc.bookings

import java.time.Clock
import java.time.LocalDate
import java.time.LocalTime
import java.time.ZoneOffset

/**
* Factory for canonical booking date range presets used across app and data layers.
*
* Notes about time zone:
* The WC Bookings API stores dates without timezone information. To ensure
* consistent comparisons and filtering on both client and server, we construct
* ranges in UTC.
* See p1759398245019489-slack-C09FHQNQERG
*/
object BookingsDateRangePresets {
/**
* Returns a DateRange that spans the current day in UTC, inclusive of the
* full day when interpreted by the API (midnight to end-of-day).
*/
fun today(clock: Clock): BookingsFilterOption.DateRange {
val start = LocalDate.now(clock)
.atTime(LocalTime.MIDNIGHT)
.atOffset(ZoneOffset.UTC)
.toInstant()
val end = LocalDate.now(clock)
.atTime(LocalTime.MAX)
.atOffset(ZoneOffset.UTC)
.toInstant()
return BookingsFilterOption.DateRange(before = end, after = start)
}

/**
* Returns a DateRange that includes bookings strictly after the end of the
* current day in UTC (i.e., upcoming bookings from tomorrow onwards).
*
* before = null and after = end-of-today.
*/
fun upcoming(clock: Clock): BookingsFilterOption.DateRange {
val endOfToday = LocalDate.now(clock)
.atTime(LocalTime.MAX)
.atOffset(ZoneOffset.UTC)
.toInstant()
return BookingsFilterOption.DateRange(before = null, after = endOfToday)
}
}