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

Removed Oreo requirement for components #15

Closed
wants to merge 3 commits into from
Closed
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
Next Next commit
Removed constraint of needing to have Oreo to use this component (bec…
…ause of LocalDate/LocalDateTime etc)

- Added CompatDate/CompatTime/CompatDateTime models which act as a layer that can handle both the new java.time API and the legacy API
- Edited Pickers in the core folder accordingly, in order to use CompatDate
- Created new *PickerCompat components, which the user will use in case they don't support Oreo
  • Loading branch information
Alessandro Sperotti committed Dec 15, 2022
commit 3fe4b1ac0fcfe82673837131c6907c12e8c5b6cd
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ android {

defaultConfig {
applicationId "com.commandiron.wheelpickercompose"
minSdk 23
minSdk 21
targetSdk 33
versionCode 1
versionName "1.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,14 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.DpSize
import androidx.compose.ui.unit.dp
import com.commandiron.wheel_picker_compose.WheelDatePicker
import com.commandiron.wheel_picker_compose.WheelDateTimePicker
import com.commandiron.wheel_picker_compose.WheelTimePicker
import com.commandiron.wheel_picker_compose.*
import com.commandiron.wheel_picker_compose.core.TimeFormat
import com.commandiron.wheel_picker_compose.core.WheelPickerDefaults
import com.commandiron.wheelpickercompose.ui.theme.WheelPickerComposeTheme
import java.sql.Time
import java.time.LocalDateTime

class MainActivity : ComponentActivity() {
@RequiresApi(Build.VERSION_CODES.O)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
Expand All @@ -40,35 +37,51 @@ class MainActivity : ComponentActivity() {
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center
) {
WheelTimePicker { snappedTime ->
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
WheelTimePicker { snappedTime ->
println(snappedTime)
}
} else WheelTimePickerCompat { snappedTime ->
println(snappedTime)
}
WheelDatePicker { snappedDate ->
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
WheelDatePicker { snappedDate ->
println(snappedDate)
}
} else WheelDatePickerCompat { snappedDate ->
println(snappedDate)
}
WheelDateTimePicker(
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
WheelDateTimePicker(
timeFormat = TimeFormat.AM_PM
) { snappedDateTime ->
println(snappedDateTime)
}
} else WheelDateTimePickerCompat(
timeFormat = TimeFormat.AM_PM
) { snappedDateTime ->
println(snappedDateTime)
}
WheelDateTimePicker(
startDateTime = LocalDateTime.of(
2025, 10, 30, 5, 0
),
yearsRange = null,
backwardsDisabled = true,
timeFormat = TimeFormat.AM_PM,
size = DpSize(200.dp, 100.dp),
textStyle = MaterialTheme.typography.titleSmall,
textColor = Color(0xFFffc300),
selectorProperties = WheelPickerDefaults.selectorProperties(
enabled = true,
shape = RoundedCornerShape(0.dp),
color = Color(0xFFf1faee).copy(alpha = 0.2f),
border = BorderStroke(2.dp, Color(0xFFf1faee))
)
){ snappedDateTime ->
println(snappedDateTime)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
WheelDateTimePicker(
startDateTime = LocalDateTime.of(
2025, 10, 30, 5, 0
),
yearsRange = null,
backwardsDisabled = true,
timeFormat = TimeFormat.AM_PM,
size = DpSize(200.dp, 100.dp),
textStyle = MaterialTheme.typography.titleSmall,
textColor = Color(0xFFffc300),
selectorProperties = WheelPickerDefaults.selectorProperties(
enabled = true,
shape = RoundedCornerShape(0.dp),
color = Color(0xFFf1faee).copy(alpha = 0.2f),
border = BorderStroke(2.dp, Color(0xFFf1faee))
)
){ snappedDateTime ->
println(snappedDateTime)
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion wheel-picker-compose/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ android {
compileSdk 33

defaultConfig {
minSdk 23
minSdk 21
targetSdk 33

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.unit.DpSize
import androidx.compose.ui.unit.dp
import com.commandiron.wheel_picker_compose.core.CompatDate
import com.commandiron.wheel_picker_compose.core.DefaultWheelDatePicker
import com.commandiron.wheel_picker_compose.core.SelectorProperties
import com.commandiron.wheel_picker_compose.core.WheelPickerDefaults
import java.time.LocalDate
import java.util.*

@RequiresApi(Build.VERSION_CODES.O)
@Composable
Expand All @@ -26,11 +28,43 @@ fun WheelDatePicker(
textStyle: TextStyle = MaterialTheme.typography.titleMedium,
textColor: Color = LocalContentColor.current,
selectorProperties: SelectorProperties = WheelPickerDefaults.selectorProperties(),
onSnappedDate : (snappedDate: LocalDate) -> Unit = {}
onSnappedDate: (snappedDate: CompatDate) -> Unit = {}
) {
DefaultWheelDatePicker(
modifier,
startDate,
CompatDate(startDate.dayOfMonth, startDate.monthValue, startDate.year),
yearsRange,
backwardsDisabled,
size,
textStyle,
textColor,
selectorProperties,
onSnappedDate = { snappedDate ->
onSnappedDate(snappedDate.snappedLocalDate)
snappedDate.snappedIndex
}
)
}

@Composable
fun WheelDatePickerCompat(
modifier: Modifier = Modifier,
startDate: Calendar = Calendar.getInstance(),
yearsRange: IntRange? = IntRange(1922, 2122),
backwardsDisabled: Boolean = false,
size: DpSize = DpSize(256.dp, 128.dp),
textStyle: TextStyle = MaterialTheme.typography.titleMedium,
textColor: Color = LocalContentColor.current,
selectorProperties: SelectorProperties = WheelPickerDefaults.selectorProperties(),
onSnappedDate: (snappedDate: CompatDate) -> Unit = {}
) {
DefaultWheelDatePicker(
modifier,
CompatDate(
startDate.get(Calendar.DAY_OF_MONTH),
startDate.get(Calendar.MONTH),
startDate.get(Calendar.YEAR)
),
yearsRange,
backwardsDisabled,
size,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.unit.DpSize
import androidx.compose.ui.unit.dp
import com.commandiron.wheel_picker_compose.core.*
import com.commandiron.wheel_picker_compose.core.DefaultWheelDateTimePicker
import com.commandiron.wheel_picker_compose.core.SelectorProperties
import com.commandiron.wheel_picker_compose.core.TimeFormat
import com.commandiron.wheel_picker_compose.core.WheelPickerDefaults
import java.time.LocalDateTime
import java.util.*

@RequiresApi(Build.VERSION_CODES.O)
@Composable
Expand All @@ -28,11 +27,51 @@ fun WheelDateTimePicker(
textStyle: TextStyle = MaterialTheme.typography.titleMedium,
textColor: Color = LocalContentColor.current,
selectorProperties: SelectorProperties = WheelPickerDefaults.selectorProperties(),
onSnappedDateTime : (snappedDateTime: LocalDateTime) -> Unit = {}
onSnappedDateTime: (snappedDateTime: CompatDateTime) -> Unit = {}
) {
DefaultWheelDateTimePicker(
modifier,
startDateTime,
CompatDateTime(
CompatDate(startDateTime.dayOfMonth, startDateTime.monthValue, startDateTime.year),
CompatTime(startDateTime.hour, startDateTime.minute)
),
yearsRange,
timeFormat,
backwardsDisabled,
size,
textStyle,
textColor,
selectorProperties,
onSnappedDateTime = { snappedDateTime ->
onSnappedDateTime(snappedDateTime.snappedLocalDateTime)
snappedDateTime.snappedIndex
}
)
}

@Composable
fun WheelDateTimePickerCompat(
modifier: Modifier = Modifier,
startDateTime: Calendar = Calendar.getInstance(),
yearsRange: IntRange? = IntRange(1922, 2122),
timeFormat: TimeFormat = TimeFormat.HOUR_24,
backwardsDisabled: Boolean = false,
size: DpSize = DpSize(256.dp, 128.dp),
textStyle: TextStyle = MaterialTheme.typography.titleMedium,
textColor: Color = LocalContentColor.current,
selectorProperties: SelectorProperties = WheelPickerDefaults.selectorProperties(),
onSnappedDateTime: (snappedDateTime: CompatDateTime) -> Unit = {}
) {
DefaultWheelDateTimePicker(
modifier,
CompatDateTime(
CompatDate(
startDateTime.get(Calendar.DAY_OF_MONTH),
startDateTime.get(Calendar.MONTH),
startDateTime.get(Calendar.YEAR)
),
CompatTime(startDateTime.get(Calendar.HOUR), startDateTime.get(Calendar.MINUTE))
),
yearsRange,
timeFormat,
backwardsDisabled,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.unit.DpSize
import androidx.compose.ui.unit.dp
import com.commandiron.wheel_picker_compose.core.*
import com.commandiron.wheel_picker_compose.core.DefaultWheelTimePicker
import com.commandiron.wheel_picker_compose.core.SelectorProperties
import com.commandiron.wheel_picker_compose.core.TimeFormat
import com.commandiron.wheel_picker_compose.core.WheelPickerDefaults
import java.time.LocalTime
import java.util.*

@RequiresApi(Build.VERSION_CODES.O)
@Composable
@RequiresApi(Build.VERSION_CODES.O)
fun WheelTimePicker(
modifier: Modifier = Modifier,
startTime: LocalTime = LocalTime.now(),
Expand All @@ -27,11 +26,39 @@ fun WheelTimePicker(
textStyle: TextStyle = MaterialTheme.typography.titleMedium,
textColor: Color = LocalContentColor.current,
selectorProperties: SelectorProperties = WheelPickerDefaults.selectorProperties(),
onSnappedTime : (snappedTime: LocalTime) -> Unit = {},
onSnappedTime : (snappedTime: CompatTime) -> Unit = {},
) {
DefaultWheelTimePicker(
modifier,
CompatTime(startTime.hour, startTime.minute),
timeFormat,
backwardsDisabled,
size,
textStyle,
textColor,
selectorProperties,
onSnappedTime = { snappedTime, _ ->
onSnappedTime(snappedTime.snappedLocalTime)
snappedTime.snappedIndex
}
)
}

@Composable
fun WheelTimePickerCompat(
modifier: Modifier = Modifier,
startTime: Calendar = Calendar.getInstance(),
timeFormat: TimeFormat = TimeFormat.HOUR_24,
backwardsDisabled: Boolean = false,
size: DpSize = DpSize(128.dp, 128.dp),
textStyle: TextStyle = MaterialTheme.typography.titleMedium,
textColor: Color = LocalContentColor.current,
selectorProperties: SelectorProperties = WheelPickerDefaults.selectorProperties(),
onSnappedTime : (snappedTime: CompatTime) -> Unit = {},
) {
DefaultWheelTimePicker(
modifier,
startTime,
CompatTime(startTime.get(Calendar.HOUR_OF_DAY), startTime.get(Calendar.MINUTE)),
timeFormat,
backwardsDisabled,
size,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.commandiron.wheel_picker_compose.core

import android.os.Build
import androidx.annotation.RequiresApi
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.LocalTime
import java.util.Calendar

data class CompatTime(val hour: Int, val minute: Int) {
@RequiresApi(Build.VERSION_CODES.O)
fun toLocalTime(): LocalTime =
LocalTime.of(hour, minute)

fun toCalendarTime(): Calendar =
Calendar.getInstance().apply {
set(Calendar.HOUR_OF_DAY, hour)
set(Calendar.MINUTE, minute)
}
}

data class CompatDate(val dayOfMonth: Int, val month: Int, val year: Int) {
@RequiresApi(Build.VERSION_CODES.O)
fun toLocalDate(): LocalDate =
LocalDate.of(dayOfMonth, month, year)

fun toCalendarDate(): Calendar =
Calendar.getInstance().apply {
set(Calendar.DAY_OF_MONTH, dayOfMonth)
set(Calendar.MONTH, month)
set(Calendar.YEAR, year)
}
}

data class CompatDateTime(val compatDate: CompatDate, val compatTime: CompatTime) {

val minute: Int get() = compatTime.minute
val hour: Int get() = compatTime.hour
val dayOfMonth: Int get() = compatDate.dayOfMonth
val month: Int get() = compatDate.month
val year: Int get() = compatDate.year



@RequiresApi(Build.VERSION_CODES.O)
fun toLocalDateTime(): LocalDateTime =
LocalDateTime.of(
compatDate.year,
compatDate.month,
compatDate.dayOfMonth,
compatTime.hour,
compatTime.minute
)

fun toCalendarDateTime(): Calendar =
Calendar.getInstance().apply {
set(Calendar.DAY_OF_MONTH, compatDate.dayOfMonth)
set(Calendar.MONTH, compatDate.month)
set(Calendar.YEAR, compatDate.year)
set(Calendar.HOUR, compatTime.hour)
set(Calendar.MINUTE, compatTime.minute)
}
}
Loading