Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove core library desugaring in library #120

Merged
merged 1 commit into from
May 13, 2024
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
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,23 @@ Library is available on Maven Central repository.
```
Snapshots are available on [Sonatype’s snapshots repository](https://s01.oss.sonatype.org/content/repositories/snapshots/io/github/boguszpawlowski/composecalendar/).

### 1.3.0 Changes
From version 1.3.0 library no longer uses `coreLibraryDesugaring` itself. If you are using it in project supporting min SDK < 26, you need to add it to prevent runtime crashes.
```kotlin
// app-level build.gradle
android {
compileOptions {
coreLibraryDesugaringEnabled true
}
kotlinOptions {
jvmTarget = "1.8"
}
}
dependencies {
coreLibraryDesugaring "com.android.tools:desugar_jdk_libs:2.0.4"
}
```

## Supported features
- Selection (single, multiple or a range of days)
- Chose day as first day of week
Expand Down Expand Up @@ -213,7 +230,7 @@ This implementation of SelectionState also allows for handling side-effects and
Apart from the default calendar, there is also a week calendar, which shows a single week at a time. It can be used in the same way as the default calendar, and has the same customization options.

## KotlinX DateTime
As the core of the library is built on `java.time` library, on Android it requires to use [core libary desugaring](https://developer.android.com/studio/write/java8-support) to be able to access it's API.
As the core of the library is built on `java.time` library, on Android SDK < 26 it requires to use [core libary desugaring](https://developer.android.com/studio/write/java8-support) to access it's API.
As a result it's features may be unavailable to some project built around different date-time libraries (e.g. kotlinx-datetime). Although the project wont be migrating from `java.time`, as it's the best suited for it, there is a separate `kotlinx-datetime` artifact for those who need to use the library from a codebase based on it. It doesn't consist of a separate version of `ComposeCalendar` features, but offers a small bunch of utilities, that will enable you to create your own wrapper, as briefly presented in `KotlinDateTimeSample`. If the provided functionality, doesn't match your use-case, please submit an issue.

## License
Expand Down
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ allprojects {
mavenCentral()
gradlePluginPortal()
google()
mavenLocal()
}

tasks.withType<KotlinCompile> {
Expand Down
3 changes: 0 additions & 3 deletions buildSrc/src/main/kotlin/CommonAndroidPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,13 @@ class CommonAndroidPlugin : Plugin<Project> {
compileOptions {
sourceCompatibility = VERSION_17
targetCompatibility = VERSION_17
isCoreLibraryDesugaringEnabled = true
}

buildFeatures.compose = true

composeOptions {
kotlinCompilerExtensionVersion = Compose.CompilerVersion
}

target.dependencies.add("coreLibraryDesugaring", Kotlin.DesugarJdkLibs)
}
}
}
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/Dependencies.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ object Kotlin {
const val CompatibilityPlugin = "org.jetbrains.kotlinx.binary-compatibility-validator"
const val CompatibilityPluginId = "binary-compatibility-validator"

const val DesugarJdkLibs = "com.android.tools:desugar_jdk_libs:1.1.5"
const val DesugarJdkLibs = "com.android.tools:desugar_jdk_libs:2.0.4"
const val DateTime = "org.jetbrains.kotlinx:kotlinx-datetime:0.4.0"
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

package io.github.boguszpawlowski.composecalendar

import android.annotation.SuppressLint
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxScope
import androidx.compose.foundation.layout.Column
Expand Down Expand Up @@ -68,6 +69,7 @@ public class CalendarState<T : SelectionState>(
* @param monthContainer container composable for all the days in current month
*/
@Composable
@SuppressLint("NewApi")
public fun SelectableCalendar(
modifier: Modifier = Modifier,
firstDayOfWeek: DayOfWeek = WeekFields.of(Locale.getDefault()).firstDayOfWeek,
Expand Down Expand Up @@ -122,6 +124,7 @@ public fun SelectableCalendar(
* @param monthContainer container composable for all the days in current month
*/
@Composable
@SuppressLint("NewApi")
public fun StaticCalendar(
modifier: Modifier = Modifier,
firstDayOfWeek: DayOfWeek = WeekFields.of(Locale.getDefault()).firstDayOfWeek,
Expand Down Expand Up @@ -170,6 +173,7 @@ public fun StaticCalendar(
* @param monthContainer container composable for all the days in current month
*/
@Composable
@SuppressLint("NewApi")
public fun <T : SelectionState> Calendar(
calendarState: CalendarState<T>,
modifier: Modifier = Modifier,
Expand Down Expand Up @@ -235,6 +239,7 @@ public fun <T : SelectionState> Calendar(
* @param maxMonth last month that can be shown
*/
@Composable
@SuppressLint("NewApi")
public fun rememberSelectableCalendarState(
initialMonth: YearMonth = YearMonth.now(),
minMonth: YearMonth = initialMonth.minusMonths(DefaultCalendarPagerRange),
Expand Down Expand Up @@ -264,6 +269,7 @@ public fun rememberSelectableCalendarState(
* @param maxMonth last month that can be shown
*/
@Composable
@SuppressLint("NewApi")
public fun rememberCalendarState(
initialMonth: YearMonth = YearMonth.now(),
minMonth: YearMonth = initialMonth.minusMonths(DefaultCalendarPagerRange),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

package io.github.boguszpawlowski.composecalendar

import android.annotation.SuppressLint
import androidx.compose.foundation.layout.BoxScope
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ColumnScope
Expand Down Expand Up @@ -111,6 +112,7 @@ public fun SelectableWeekCalendar(
* @param daysOfWeekHeader header for showing captions for each day of week
*/
@Composable
@SuppressLint("NewApi")
public fun StaticWeekCalendar(
modifier: Modifier = Modifier,
firstDayOfWeek: DayOfWeek = WeekFields.of(Locale.getDefault()).firstDayOfWeek,
Expand Down Expand Up @@ -153,6 +155,7 @@ public fun StaticWeekCalendar(
* @param daysOfWeekHeader header for showing captions for each day of week
*/
@Composable
@SuppressLint("NewApi")
public fun <T : SelectionState> WeekCalendar(
calendarState: WeekCalendarState<T>,
modifier: Modifier = Modifier,
Expand Down Expand Up @@ -211,6 +214,7 @@ public fun <T : SelectionState> WeekCalendar(
* @param maxWeek last week that can be shown
*/
@Composable
@SuppressLint("NewApi")
public fun rememberSelectableWeekCalendarState(
firstDayOfWeek: DayOfWeek = WeekFields.of(Locale.getDefault()).firstDayOfWeek,
initialWeek: Week = Week.now(firstDayOfWeek),
Expand Down Expand Up @@ -243,6 +247,7 @@ public fun rememberSelectableWeekCalendarState(
* @param maxWeek last week that can be shown
*/
@Composable
@SuppressLint("NewApi")
public fun rememberWeekCalendarState(
firstDayOfWeek: DayOfWeek = WeekFields.of(Locale.getDefault()).firstDayOfWeek,
initialWeek: Week = Week.now(firstDayOfWeek),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.boguszpawlowski.composecalendar.day

import android.annotation.SuppressLint
import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
Expand All @@ -26,6 +27,7 @@ import java.time.LocalDate
* @param onClick callback for interacting with day clicks
*/
@Composable
@SuppressLint("NewApi")
public fun <T : SelectionState> DefaultDay(
state: DayState<T>,
modifier: Modifier = Modifier,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.boguszpawlowski.composecalendar.header

import android.annotation.SuppressLint
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Row
Expand All @@ -26,6 +27,7 @@ import java.util.Locale
* 2 arrows for changing currently showed month
*/
@Composable
@SuppressLint("NewApi")
public fun DefaultMonthHeader(
monthState: MonthState,
modifier: Modifier = Modifier,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.boguszpawlowski.composecalendar.header

import android.annotation.SuppressLint
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Row
Expand Down Expand Up @@ -27,6 +28,7 @@ import java.util.Locale
*/
@Composable
@Suppress("LongMethod")
@SuppressLint("NewApi")
public fun DefaultWeekHeader(
weekState: WeekState,
modifier: Modifier = Modifier,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.boguszpawlowski.composecalendar.header

import android.annotation.SuppressLint
import androidx.compose.runtime.Stable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
Expand Down Expand Up @@ -42,6 +43,7 @@ public interface MonthState {
}

@Stable
@SuppressLint("NewApi")
private class MonthStateImpl(
initialMonth: YearMonth,
minMonth: YearMonth,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.boguszpawlowski.composecalendar.header

import android.annotation.SuppressLint
import androidx.compose.runtime.Stable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
Expand All @@ -21,6 +22,7 @@ public fun WeekState(
)

@Stable
@SuppressLint("NewApi")
public interface WeekState {
public var currentWeek: Week
public var minWeek: Week
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import java.time.temporal.ChronoUnit

@OptIn(ExperimentalSnapperApi::class)
@Composable
@Suppress("LongMethod")
@Suppress("LongMethod", "NewApi")
internal fun <T : SelectionState> MonthPager(
initialMonth: YearMonth,
showAdjacentMonths: Boolean,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.boguszpawlowski.composecalendar.month

import android.annotation.SuppressLint
import androidx.compose.foundation.lazy.LazyListState
import androidx.compose.runtime.Stable
import androidx.compose.runtime.derivedStateOf
Expand All @@ -15,6 +16,7 @@ import java.time.YearMonth
import java.time.temporal.ChronoUnit

@Stable
@SuppressLint("NewApi")
internal class MonthListState(
private val coroutineScope: CoroutineScope,
private val monthState: MonthState,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.boguszpawlowski.composecalendar.selection

import android.annotation.SuppressLint
import io.github.boguszpawlowski.composecalendar.util.addOrRemoveIfExists
import java.time.LocalDate

Expand All @@ -10,6 +11,7 @@ import java.time.LocalDate
* @param selectionMode current selection mode
* @returns new selection in a form of a list of local dates.
*/
@SuppressLint("NewApi")
public object DynamicSelectionHandler {
public fun calculateNewSelection(
date: LocalDate,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package io.github.boguszpawlowski.composecalendar.selection

import android.annotation.SuppressLint
import java.time.LocalDate

@SuppressLint("NewApi")
internal fun Collection<LocalDate>.startOrMax() = firstOrNull() ?: LocalDate.MAX

internal fun Collection<LocalDate>.endOrNull() = drop(1).lastOrNull()

@SuppressLint("NewApi")
internal fun Collection<LocalDate>.fillUpTo(date: LocalDate) =
(0..date.toEpochDay() - first().toEpochDay()).map {
first().plusDays(it)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.boguszpawlowski.composecalendar.selection

import android.annotation.SuppressLint
import androidx.compose.runtime.Immutable
import androidx.compose.runtime.Stable
import androidx.compose.runtime.getValue
Expand All @@ -21,6 +22,7 @@ public interface SelectionState {
* @param confirmSelectionChange return false from this callback to veto the selection change
*/
@Stable
@SuppressLint("NewApi")
public class DynamicSelectionState(
private val confirmSelectionChange: (newValue: List<LocalDate>) -> Boolean = { true },
selection: List<LocalDate>,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.boguszpawlowski.composecalendar.week

import android.annotation.SuppressLint
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.material.Text
Expand All @@ -20,6 +21,7 @@ import kotlin.DeprecationLevel.WARNING
),
message = "Replace with DefaultDaysOfWeekHeader, DefaultWeekHeader will be removed in future versions"
)
@SuppressLint("NewApi")
public fun DefaultWeekHeader(
daysOfWeek: List<DayOfWeek>,
modifier: Modifier = Modifier,
Expand All @@ -38,6 +40,7 @@ public fun DefaultWeekHeader(
}

@Composable
@SuppressLint("NewApi")
public fun DefaultDaysOfWeekHeader(
daysOfWeek: List<DayOfWeek>,
modifier: Modifier = Modifier,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.boguszpawlowski.composecalendar.week

import android.annotation.SuppressLint
import io.github.boguszpawlowski.composecalendar.selection.fillUpTo
import io.github.boguszpawlowski.composecalendar.util.daysUntil
import java.time.DayOfWeek
Expand All @@ -9,6 +10,7 @@ import java.time.temporal.ChronoUnit
import java.time.temporal.WeekFields
import java.util.Locale

@SuppressLint("NewApi")
public data class Week(
val days: List<LocalDate>,
) {
Expand Down Expand Up @@ -48,5 +50,6 @@ public data class Week(
}
}

@SuppressLint("NewApi")
public fun ChronoUnit.between(first: Week, other: Week): Int =
between(first.start, other.start).toInt()
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.boguszpawlowski.composecalendar.week

import android.annotation.SuppressLint
import androidx.compose.foundation.lazy.LazyListState
import androidx.compose.runtime.Stable
import androidx.compose.runtime.derivedStateOf
Expand All @@ -14,6 +15,7 @@ import kotlinx.coroutines.launch
import java.time.temporal.ChronoUnit

@Stable
@SuppressLint("NewApi")
internal class WeekListState(
private val coroutineScope: CoroutineScope,
private val weekState: WeekState,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.boguszpawlowski.composecalendar.week

import android.annotation.SuppressLint
import androidx.compose.animation.rememberSplineBasedDecay
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxScope
Expand Down Expand Up @@ -30,6 +31,7 @@ import java.time.temporal.ChronoUnit
@OptIn(ExperimentalSnapperApi::class)
@Composable
@Suppress("LongMethod")
@SuppressLint("NewApi")
internal fun <T : SelectionState> WeekPager(
initialWeek: Week,
selectionState: T,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.boguszpawlowski.composecalendar.week

import android.annotation.SuppressLint
import io.github.boguszpawlowski.composecalendar.day.WeekDay
import io.github.boguszpawlowski.composecalendar.util.daysUntil
import java.time.DayOfWeek
Expand All @@ -8,10 +9,11 @@ import java.time.YearMonth

internal const val DaysInAWeek = 7

@SuppressLint("NewApi")
internal fun YearMonth.getWeeks(
includeAdjacentMonths: Boolean,
firstDayOfTheWeek: DayOfWeek,
today: LocalDate = LocalDate.now(),
today: LocalDate,
): List<WeekDays> {
val daysLength = lengthOfMonth()

Expand Down
Loading
Loading