Skip to content

Commit 24541d3

Browse files
committed
Refactor datepicker dialog and update to material 3 spec
1 parent 2a024a9 commit 24541d3

File tree

21 files changed

+707
-535
lines changed

21 files changed

+707
-535
lines changed

app/build.gradle.kts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ android {
3535

3636
compileOptions {
3737
isCoreLibraryDesugaringEnabled = true
38-
sourceCompatibility = JavaVersion.VERSION_1_8
39-
targetCompatibility = JavaVersion.VERSION_1_8
38+
sourceCompatibility = JavaVersion.VERSION_11
39+
targetCompatibility = JavaVersion.VERSION_11
4040
}
4141

4242
composeOptions {

app/src/main/java/com/vanpra/composematerialdialogdemos/Util.kt

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,18 @@ import com.vanpra.composematerialdialogs.MaterialDialog
1515
import com.vanpra.composematerialdialogs.MaterialDialogButtons
1616
import com.vanpra.composematerialdialogs.MaterialDialogScope
1717
import com.vanpra.composematerialdialogs.MaterialDialogState
18+
import com.vanpra.composematerialdialogs.datetime.date.DatePickerColors
19+
import com.vanpra.composematerialdialogs.datetime.date.DatePickerDefaults
20+
import com.vanpra.composematerialdialogs.datetime.date.MaterialDatePickerDialog
21+
import com.vanpra.composematerialdialogs.datetime.date.rememberDatePickerOptions
1822
import com.vanpra.composematerialdialogs.datetime.time.MaterialTimePickerDialog
1923
import com.vanpra.composematerialdialogs.datetime.time.TimePickerColors
2024
import com.vanpra.composematerialdialogs.datetime.time.TimePickerDefaults
2125
import com.vanpra.composematerialdialogs.datetime.time.rememberTimePickerOptions
2226
import com.vanpra.composematerialdialogs.rememberMaterialDialogState
27+
import java.time.LocalDate
2328
import java.time.LocalTime
29+
import java.util.Locale
2430

2531
/**
2632
* @brief Builds a dialog and adds button to the layout which shows the dialog on click
@@ -66,6 +72,37 @@ fun TimePickerDialogAndShowButton(
6672
DialogDemoButton(state = dialogState, buttonText = buttonText)
6773
}
6874

75+
@Composable
76+
fun DatePickerDialogAndShowButton(
77+
buttonText: String,
78+
buttons: @Composable MaterialDialogButtons.() -> Unit = {},
79+
initialDate: LocalDate = LocalDate.now(),
80+
colors: DatePickerColors = DatePickerDefaults.colors(),
81+
yearRange: IntRange = IntRange(1900, 2100),
82+
waitForPositiveButton: Boolean = true,
83+
allowedDateValidator: (LocalDate) -> Boolean = { true },
84+
locale: Locale = Locale.getDefault(),
85+
onDateChange: (LocalDate) -> Unit = {}
86+
) {
87+
val dialogState = rememberMaterialDialogState()
88+
89+
MaterialDatePickerDialog(
90+
state = dialogState,
91+
buttons = buttons,
92+
datePickerOptions = rememberDatePickerOptions(
93+
initialDate = initialDate,
94+
colors = colors,
95+
yearRange = yearRange,
96+
waitForPositiveButton = waitForPositiveButton,
97+
allowedDateValidator = allowedDateValidator,
98+
locale = locale,
99+
onDateChange = onDateChange
100+
)
101+
)
102+
DialogDemoButton(state = dialogState, buttonText = buttonText)
103+
}
104+
105+
69106
@Composable
70107
fun DialogDemoButton(state: MaterialDialogState, buttonText: String) {
71108
TextButton(

app/src/main/java/com/vanpra/composematerialdialogdemos/demos/DateTimeDialog.kt

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@ package com.vanpra.composematerialdialogdemos.demos
33
import android.widget.Toast
44
import androidx.compose.runtime.Composable
55
import androidx.compose.ui.platform.LocalContext
6-
import com.vanpra.composematerialdialogdemos.DialogAndShowButton
6+
import com.vanpra.composematerialdialogdemos.DatePickerDialogAndShowButton
77
import com.vanpra.composematerialdialogdemos.TimePickerDialogAndShowButton
88
import com.vanpra.composematerialdialogs.MaterialDialogButtons
99
import com.vanpra.composematerialdialogs.datetime.date.DatePickerDefaults
10-
import com.vanpra.composematerialdialogs.datetime.date.datepicker
1110
import java.time.DayOfWeek
1211
import java.time.LocalTime
1312

@@ -18,66 +17,55 @@ import java.time.LocalTime
1817
fun DateTimeDialogDemo() {
1918
val context = LocalContext.current
2019

21-
TimePickerDialogAndShowButton(
22-
buttonText = "Time Picker Dialog",
20+
TimePickerDialogAndShowButton(buttonText = "Time Picker Dialog",
2321
buttons = { defaultDateTimeDialogButtons() },
2422
onTimeChange = {
2523
println(it.toString())
2624
Toast.makeText(context, it.toString(), Toast.LENGTH_LONG).show()
27-
}
28-
)
25+
})
2926

30-
TimePickerDialogAndShowButton(
31-
buttonText = "Time Picker Dialog With Min/Max",
27+
TimePickerDialogAndShowButton(buttonText = "Time Picker Dialog With Min/Max",
3228
buttons = { defaultDateTimeDialogButtons() },
3329
timeRange = LocalTime.of(9, 35)..LocalTime.of(21, 13),
3430
is24HourClock = false,
3531
onTimeChange = {
3632
println(it.toString())
3733
Toast.makeText(context, it.toString(), Toast.LENGTH_LONG).show()
38-
}
39-
)
34+
})
4035

41-
TimePickerDialogAndShowButton(
42-
buttonText = "Time Picker Dialog 24H",
36+
TimePickerDialogAndShowButton(buttonText = "Time Picker Dialog 24H",
4337
buttons = { defaultDateTimeDialogButtons() },
4438
is24HourClock = true,
4539
onTimeChange = {
4640
println(it.toString())
4741
Toast.makeText(context, it.toString(), Toast.LENGTH_LONG).show()
48-
}
49-
)
42+
})
5043

51-
TimePickerDialogAndShowButton(
52-
buttonText = "Time Picker Dialog 24H With Min/Max",
44+
TimePickerDialogAndShowButton(buttonText = "Time Picker Dialog 24H With Min/Max",
5345
buttons = { defaultDateTimeDialogButtons() },
5446
timeRange = LocalTime.of(9, 35)..LocalTime.of(21, 13),
5547
is24HourClock = true,
5648
onTimeChange = {
5749
println(it.toString())
5850
Toast.makeText(context, it.toString(), Toast.LENGTH_LONG).show()
59-
}
60-
)
51+
})
6152

62-
DialogAndShowButton(
63-
buttonText = "Date Picker Dialog",
64-
buttons = { defaultDateTimeDialogButtons() }
65-
) {
66-
datepicker(colors = DatePickerDefaults.colors()) {
53+
DatePickerDialogAndShowButton(buttonText = "Date Picker Dialog",
54+
buttons = { defaultDateTimeDialogButtons() },
55+
colors = DatePickerDefaults.colors(),
56+
onDateChange = {
6757
println(it.toString())
68-
}
69-
}
58+
})
7059

71-
DialogAndShowButton(
72-
buttonText = "Date Picker Dialog with date restrictions",
73-
buttons = { defaultDateTimeDialogButtons() }
74-
) {
75-
datepicker(allowedDateValidator = {
60+
DatePickerDialogAndShowButton(buttonText = "Date Picker Dialog with date restrictions",
61+
buttons = { defaultDateTimeDialogButtons() },
62+
allowedDateValidator = {
7663
it.dayOfWeek !== DayOfWeek.SATURDAY && it.dayOfWeek !== DayOfWeek.SUNDAY
77-
}) {
64+
},
65+
colors = DatePickerDefaults.colors(),
66+
onDateChange = {
7867
println(it.toString())
79-
}
80-
}
68+
})
8169
}
8270

8371
@Composable

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ buildscript {
1414

1515
dependencies {
1616
classpath(Dependencies.Kotlin.gradlePlugin)
17-
classpath("com.android.tools.build:gradle:8.0.0-alpha09")
17+
classpath("com.android.tools.build:gradle:8.0.0-alpha10")
1818
classpath("com.vanniktech:gradle-maven-publish-plugin:0.22.0")
1919
classpath("org.jetbrains.dokka:dokka-gradle-plugin:1.7.20")
2020
classpath(Dependencies.Shot.core)

buildSrc/src/main/kotlin/CommonModulePlugin.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,16 @@ class CommonModulePlugin : Plugin<Project> {
3131
implementation(Dependencies.AndroidX.Compose.foundationLayout)
3232
implementation(Dependencies.AndroidX.Compose.animation)
3333
implementation(Dependencies.AndroidX.Compose.viewmodel)
34+
implementation(Dependencies.AndroidX.Compose.uiToolingPreview)
3435

3536
androidTestImplementation(Dependencies.AndroidX.Compose.activity)
3637
androidTestImplementation(Dependencies.AndroidX.Compose.testing)
3738
androidTestImplementation(Dependencies.AndroidX.Testing.core)
3839
androidTestImplementation(Dependencies.AndroidX.Testing.rules)
3940
androidTestImplementation(Dependencies.AndroidX.Testing.runner)
4041

42+
debugImplementation(Dependencies.AndroidX.Compose.uiTooling)
43+
4144
add("androidTestImplementation", project(":test-utils"))
4245
}
4346
}
@@ -49,4 +52,8 @@ class CommonModulePlugin : Plugin<Project> {
4952
private fun DependencyHandler.androidTestImplementation(dependency: String) {
5053
add("androidTestImplementation", dependency)
5154
}
55+
56+
private fun DependencyHandler.debugImplementation(dependency: String) {
57+
add("debugImplementation", dependency)
58+
}
5259
}

buildSrc/src/main/kotlin/Dependencies.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ object Dependencies {
4747
const val version = "1.3.2"
4848
const val compilerVersion = "1.3.2"
4949

50+
const val uiTooling = "androidx.compose.ui:ui-tooling:1.3.0"
51+
const val uiToolingPreview = "androidx.compose.ui:ui-tooling-preview:1.3.0"
52+
5053
const val ui = "androidx.compose.ui:ui:$version"
5154
const val material = "androidx.compose.material:material:1.3.1"
5255
const val material3 = "androidx.compose.material3:material3:1.0.1"

color/build.gradle.kts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ android {
3737
)
3838

3939
compileOptions {
40-
sourceCompatibility = JavaVersion.VERSION_1_8
41-
targetCompatibility = JavaVersion.VERSION_1_8
40+
sourceCompatibility = JavaVersion.VERSION_11
41+
targetCompatibility = JavaVersion.VERSION_11
4242
}
4343

4444
composeOptions {

core/build.gradle.kts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ android {
4040
}
4141

4242
compileOptions {
43-
sourceCompatibility = JavaVersion.VERSION_1_8
44-
targetCompatibility = JavaVersion.VERSION_1_8
43+
sourceCompatibility = JavaVersion.VERSION_11
44+
targetCompatibility = JavaVersion.VERSION_11
4545
}
4646

4747
composeOptions {

datetime/build.gradle.kts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ android {
4040
)
4141

4242
compileOptions {
43-
sourceCompatibility = JavaVersion.VERSION_1_8
44-
targetCompatibility = JavaVersion.VERSION_1_8
43+
sourceCompatibility = JavaVersion.VERSION_11
44+
targetCompatibility = JavaVersion.VERSION_11
4545
}
4646

4747
composeOptions {
@@ -59,7 +59,9 @@ android {
5959
dependencies {
6060
api(project(":core"))
6161
implementation(Dependencies.Accompanist.pager)
62+
implementation("androidx.compose.ui:ui-tooling-preview:1.1.1")
6263
coreLibraryDesugaring(Dependencies.desugar)
64+
debugImplementation("androidx.compose.ui:ui-tooling:1.1.1")
6365
}
6466

6567
shot {

0 commit comments

Comments
 (0)