From 4f1afebbebd5e4d394d9ab10de8b16ba7f291c23 Mon Sep 17 00:00:00 2001 From: Kizito Nwose Date: Thu, 18 Jul 2024 21:31:40 +0200 Subject: [PATCH] Add `previous` and `next` YearMonth properties --- .../library/api/android/library.api | 2 ++ .../library/api/desktop/library.api | 2 ++ .../kizitonwose/calendar/core/YearMonth.kt | 22 +++++++++++++++++++ .../kotlin/ContinuousSelectionHelper.kt | 2 ++ .../src/commonMain/kotlin/Example1Page.kt | 2 ++ .../src/commonMain/kotlin/Example3Page.kt | 2 ++ .../src/commonMain/kotlin/Example9Page.kt | 2 ++ .../sample/src/commonMain/kotlin/Utils.kt | 4 ---- 8 files changed, 34 insertions(+), 4 deletions(-) diff --git a/compose-multiplatform/library/api/android/library.api b/compose-multiplatform/library/api/android/library.api index a64c47e0..cef6c873 100644 --- a/compose-multiplatform/library/api/android/library.api +++ b/compose-multiplatform/library/api/android/library.api @@ -302,6 +302,8 @@ public final class com/kizitonwose/calendar/core/YearMonthKt { public static final fun atDay (Lcom/kizitonwose/calendar/core/YearMonth;I)Lkotlinx/datetime/LocalDate; public static final fun atEndOfMonth (Lcom/kizitonwose/calendar/core/YearMonth;)Lkotlinx/datetime/LocalDate; public static final fun atStartOfMonth (Lcom/kizitonwose/calendar/core/YearMonth;)Lkotlinx/datetime/LocalDate; + public static final fun getNext (Lcom/kizitonwose/calendar/core/YearMonth;)Lcom/kizitonwose/calendar/core/YearMonth; + public static final fun getPrevious (Lcom/kizitonwose/calendar/core/YearMonth;)Lcom/kizitonwose/calendar/core/YearMonth; public static final fun lengthOfMonth (Lcom/kizitonwose/calendar/core/YearMonth;)I public static final fun minus (Lcom/kizitonwose/calendar/core/YearMonth;ILkotlinx/datetime/DateTimeUnit$MonthBased;)Lcom/kizitonwose/calendar/core/YearMonth; public static final fun monthsUntil (Lcom/kizitonwose/calendar/core/YearMonth;Lcom/kizitonwose/calendar/core/YearMonth;)I diff --git a/compose-multiplatform/library/api/desktop/library.api b/compose-multiplatform/library/api/desktop/library.api index 87fdf312..362a1914 100644 --- a/compose-multiplatform/library/api/desktop/library.api +++ b/compose-multiplatform/library/api/desktop/library.api @@ -302,6 +302,8 @@ public final class com/kizitonwose/calendar/core/YearMonthKt { public static final fun atDay (Lcom/kizitonwose/calendar/core/YearMonth;I)Lkotlinx/datetime/LocalDate; public static final fun atEndOfMonth (Lcom/kizitonwose/calendar/core/YearMonth;)Lkotlinx/datetime/LocalDate; public static final fun atStartOfMonth (Lcom/kizitonwose/calendar/core/YearMonth;)Lkotlinx/datetime/LocalDate; + public static final fun getNext (Lcom/kizitonwose/calendar/core/YearMonth;)Lcom/kizitonwose/calendar/core/YearMonth; + public static final fun getPrevious (Lcom/kizitonwose/calendar/core/YearMonth;)Lcom/kizitonwose/calendar/core/YearMonth; public static final fun lengthOfMonth (Lcom/kizitonwose/calendar/core/YearMonth;)I public static final fun minus (Lcom/kizitonwose/calendar/core/YearMonth;ILkotlinx/datetime/DateTimeUnit$MonthBased;)Lcom/kizitonwose/calendar/core/YearMonth; public static final fun monthsUntil (Lcom/kizitonwose/calendar/core/YearMonth;Lcom/kizitonwose/calendar/core/YearMonth;)I diff --git a/compose-multiplatform/library/src/commonMain/kotlin/com/kizitonwose/calendar/core/YearMonth.kt b/compose-multiplatform/library/src/commonMain/kotlin/com/kizitonwose/calendar/core/YearMonth.kt index 61e2af50..a1ca4ba9 100644 --- a/compose-multiplatform/library/src/commonMain/kotlin/com/kizitonwose/calendar/core/YearMonth.kt +++ b/compose-multiplatform/library/src/commonMain/kotlin/com/kizitonwose/calendar/core/YearMonth.kt @@ -118,3 +118,25 @@ public fun YearMonth.plus(value: Int, unit: DateTimeUnit.MonthBased): YearMonth */ public fun YearMonth.minus(value: Int, unit: DateTimeUnit.MonthBased): YearMonth = atStartOfMonth().minus(value, unit).yearMonth + +/** + * Returns a [YearMonth] that results from adding the 1 month this year-month. + * + * @throws DateTimeArithmeticException if the result exceeds the boundaries + * of [YearMonth] which is essentially the [LocalDate] boundaries. + * + * @see YearMonth.plus + */ +public val YearMonth.next: YearMonth + get() = this.plus(1, DateTimeUnit.MONTH) + +/** + * Returns a [YearMonth] that results from subtracting the 1 month this year-month. + * + * @throws DateTimeArithmeticException if the result exceeds the boundaries + * of [YearMonth] which is essentially the [LocalDate] boundaries. + * + * @see YearMonth.minus + */ +public val YearMonth.previous: YearMonth + get() = this.minus(1, DateTimeUnit.MONTH) diff --git a/compose-multiplatform/sample/src/commonMain/kotlin/ContinuousSelectionHelper.kt b/compose-multiplatform/sample/src/commonMain/kotlin/ContinuousSelectionHelper.kt index e8fdaabb..4604cd06 100644 --- a/compose-multiplatform/sample/src/commonMain/kotlin/ContinuousSelectionHelper.kt +++ b/compose-multiplatform/sample/src/commonMain/kotlin/ContinuousSelectionHelper.kt @@ -1,5 +1,7 @@ import com.kizitonwose.calendar.core.atEndOfMonth import com.kizitonwose.calendar.core.atStartOfMonth +import com.kizitonwose.calendar.core.next +import com.kizitonwose.calendar.core.previous import com.kizitonwose.calendar.core.yearMonth import kotlinx.datetime.LocalDate import kotlinx.datetime.daysUntil diff --git a/compose-multiplatform/sample/src/commonMain/kotlin/Example1Page.kt b/compose-multiplatform/sample/src/commonMain/kotlin/Example1Page.kt index 23a58cb0..316baaa2 100644 --- a/compose-multiplatform/sample/src/commonMain/kotlin/Example1Page.kt +++ b/compose-multiplatform/sample/src/commonMain/kotlin/Example1Page.kt @@ -27,6 +27,8 @@ import com.kizitonwose.calendar.core.CalendarDay import com.kizitonwose.calendar.core.DayPosition import com.kizitonwose.calendar.core.YearMonth import com.kizitonwose.calendar.core.daysOfWeek +import com.kizitonwose.calendar.core.next +import com.kizitonwose.calendar.core.previous import kotlinx.coroutines.launch import kotlinx.datetime.DayOfWeek import org.jetbrains.compose.ui.tooling.preview.Preview diff --git a/compose-multiplatform/sample/src/commonMain/kotlin/Example3Page.kt b/compose-multiplatform/sample/src/commonMain/kotlin/Example3Page.kt index 492f5685..351205df 100644 --- a/compose-multiplatform/sample/src/commonMain/kotlin/Example3Page.kt +++ b/compose-multiplatform/sample/src/commonMain/kotlin/Example3Page.kt @@ -48,6 +48,8 @@ import com.kizitonwose.calendar.core.DayPosition import com.kizitonwose.calendar.core.OutDateStyle import com.kizitonwose.calendar.core.YearMonth import com.kizitonwose.calendar.core.daysOfWeek +import com.kizitonwose.calendar.core.next +import com.kizitonwose.calendar.core.previous import kotlinx.coroutines.launch import kotlinx.datetime.DayOfWeek import org.jetbrains.compose.ui.tooling.preview.Preview diff --git a/compose-multiplatform/sample/src/commonMain/kotlin/Example9Page.kt b/compose-multiplatform/sample/src/commonMain/kotlin/Example9Page.kt index 8a36a605..d548d25b 100644 --- a/compose-multiplatform/sample/src/commonMain/kotlin/Example9Page.kt +++ b/compose-multiplatform/sample/src/commonMain/kotlin/Example9Page.kt @@ -55,7 +55,9 @@ import com.kizitonwose.calendar.core.YearMonth import com.kizitonwose.calendar.core.atEndOfMonth import com.kizitonwose.calendar.core.atStartOfMonth import com.kizitonwose.calendar.core.daysOfWeek +import com.kizitonwose.calendar.core.next import com.kizitonwose.calendar.core.now +import com.kizitonwose.calendar.core.previous import com.kizitonwose.calendar.core.yearMonth import kotlinx.coroutines.launch import kotlinx.datetime.DayOfWeek diff --git a/compose-multiplatform/sample/src/commonMain/kotlin/Utils.kt b/compose-multiplatform/sample/src/commonMain/kotlin/Utils.kt index 45cc3c0b..11e85a6f 100644 --- a/compose-multiplatform/sample/src/commonMain/kotlin/Utils.kt +++ b/compose-multiplatform/sample/src/commonMain/kotlin/Utils.kt @@ -185,7 +185,3 @@ internal fun LocalDate.plusDays(value: Int): LocalDate = plus(value, DateTimeUni internal fun LocalDate.minusDays(value: Int): LocalDate = minus(value, DateTimeUnit.DAY) internal fun YearMonth.plusMonths(value: Int): YearMonth = plus(value, DateTimeUnit.MONTH) internal fun YearMonth.minusMonths(value: Int): YearMonth = minus(value, DateTimeUnit.MONTH) -internal val YearMonth.next: YearMonth - get() = this.minusMonths(1) -internal val YearMonth.previous: YearMonth - get() = this.minusMonths(1)