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 @@ -2,24 +2,34 @@ package com.woocommerce.android.ui.bookings.list

import org.wordpress.android.fluxc.network.rest.wpcom.wc.bookings.BookingsFilterOption
import java.time.Clock
import java.time.Instant
import java.time.LocalDate
import java.time.LocalTime
import java.time.ZonedDateTime
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 = ZonedDateTime.now(clock).with(LocalTime.MAX).toInstant(),
after = ZonedDateTime.now(clock).with(LocalTime.MIN).toInstant()
before = todayAtEndOfDay(),
after = todayAtMidnight()
)

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

BookingListTab.All -> null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class BookingListFilterBuilderTest {
}

assertThat(filter).isNotNull()
assertThat(filter?.after).isEqualTo(Instant.parse("2025-01-01T00:00:00+02:00"))
assertThat(filter?.before).isEqualTo(Instant.parse("2025-01-01T23:59:59.999999999+02:00"))
assertThat(filter?.after).isEqualTo(Instant.parse("2025-01-01T00:00:00+00:00"))
assertThat(filter?.before).isEqualTo(Instant.parse("2025-01-01T23:59:59.999999999+00:00"))
}

@Test
Expand All @@ -32,7 +32,7 @@ class BookingListFilterBuilderTest {
}

assertThat(filter).isNotNull()
assertThat(filter?.after).isEqualTo(mockedNow)
assertThat(filter?.after).isEqualTo(Instant.parse("2025-01-01T23:59:59.999999999+00:00"))
assertThat(filter?.before).isNull()
}

Expand Down