Skip to content
Draft
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ local.properties
secret.properties
debug.keystore
/docs/api
*.log
12 changes: 8 additions & 4 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@ android {

packagingOptions.excludes.addAll(
listOf(
"META-INF/DEPENDENCIES.txt",
"META-INF/LICENSE",
"META-INF/LICENSE.txt",
"META-INF/NOTICE",
"META-INF/NOTICE.txt",
"META-INF/AL2.0",
"META-INF/**",
"META-INF/LGPL2.1"
)
)

Expand All @@ -31,8 +35,8 @@ android {

compileOptions {
isCoreLibraryDesugaringEnabled = true
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}

composeOptions {
Expand All @@ -54,7 +58,7 @@ dependencies {
implementation(Dependencies.AndroidX.coreKtx)

implementation(Dependencies.AndroidX.Compose.ui)
implementation(Dependencies.AndroidX.Compose.material)
implementation(Dependencies.AndroidX.Compose.material3)
implementation(Dependencies.AndroidX.Compose.materialIconsExtended)
implementation(Dependencies.AndroidX.Compose.animation)
implementation(Dependencies.AndroidX.Compose.foundationLayout)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.material.TextButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
Expand Down Expand Up @@ -46,9 +46,11 @@ data class DialogSectionData(val title: String, val content: @Composable () -> U

val sections = listOf(
DialogSectionData("Basic Dialogs") { BasicDialogDemo() },
DialogSectionData("Basic List Dialogs") { BasicListDialogDemo() },
DialogSectionData("Single Selection List Dialogs") { SingleSelectionDemo() },
DialogSectionData("Multi-Selection List Dialogs") { MultiSelectionDemo() },
DialogSectionData("List Dialogs") {
BasicListDialogDemo()
SingleSelectionDemo()
MultiSelectionDemo()
},
DialogSectionData("Date and Time Picker Dialogs") { DateTimeDialogDemo() },
DialogSectionData("Color Picker Dialogs") { ColorDialogDemo() }
)
Expand All @@ -69,14 +71,14 @@ fun DialogDemos() {
modifier = Modifier
.fillMaxWidth()
.padding(8.dp)
.background(MaterialTheme.colors.primaryVariant)
.background(MaterialTheme.colorScheme.primary)
) {
Text(
it.title,
modifier = Modifier
.fillMaxWidth()
.wrapContentSize(Alignment.Center),
color = MaterialTheme.colors.onPrimary
color = MaterialTheme.colorScheme.onPrimary
)
}
}
Expand Down
91 changes: 82 additions & 9 deletions app/src/main/java/com/vanpra/composematerialdialogdemos/Util.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,29 @@ import androidx.compose.foundation.background
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.material.TextButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.vanpra.composematerialdialogs.MaterialDialog
import com.vanpra.composematerialdialogs.MaterialDialogButtons
import com.vanpra.composematerialdialogs.MaterialDialogScope
import com.vanpra.composematerialdialogs.MaterialDialogState
import com.vanpra.composematerialdialogs.datetime.date.DatePickerColors
import com.vanpra.composematerialdialogs.datetime.date.DatePickerDefaults
import com.vanpra.composematerialdialogs.datetime.date.MaterialDatePickerDialog
import com.vanpra.composematerialdialogs.datetime.date.rememberDatePickerOptions
import com.vanpra.composematerialdialogs.datetime.time.MaterialTimePickerDialog
import com.vanpra.composematerialdialogs.datetime.time.TimePickerColors
import com.vanpra.composematerialdialogs.datetime.time.TimePickerDefaults
import com.vanpra.composematerialdialogs.datetime.time.rememberTimePickerOptions
import com.vanpra.composematerialdialogs.rememberMaterialDialogState
import java.time.LocalDate
import java.time.LocalTime
import java.util.Locale

/**
* @brief Builds a dialog and adds button to the layout which shows the dialog on click
Expand All @@ -27,23 +39,84 @@ fun DialogAndShowButton(
) {
val dialogState = rememberMaterialDialogState()

MaterialDialog(dialogState = dialogState, buttons = buttons) {
MaterialDialog(state = dialogState, buttons = buttons) {
content()
}

DialogDemoButton(state = dialogState, buttonText = buttonText)
}

@Composable
fun TimePickerDialogAndShowButton(
buttonText: String,
buttons: @Composable MaterialDialogButtons.() -> Unit = {},
initialTime: LocalTime = LocalTime.now(),
colors: TimePickerColors = TimePickerDefaults.colors(),
timeRange: ClosedRange<LocalTime> = LocalTime.MIN..LocalTime.MAX,
is24HourClock: Boolean = false,
onTimeChange: (LocalTime) -> Unit = {}
) {
val dialogState = rememberMaterialDialogState()

MaterialTimePickerDialog(
state = dialogState,
buttons = buttons,
timePickerOptions = rememberTimePickerOptions(
initialTime = initialTime,
colors = colors,
timeRange = timeRange,
is24HourClock = is24HourClock,
onTimeChange = onTimeChange
)
)
DialogDemoButton(state = dialogState, buttonText = buttonText)
}

@Composable
fun DatePickerDialogAndShowButton(
buttonText: String,
buttons: @Composable MaterialDialogButtons.() -> Unit = {},
initialDate: LocalDate = LocalDate.now(),
colors: DatePickerColors = DatePickerDefaults.colors(),
yearRange: IntRange = IntRange(1900, 2100),
waitForPositiveButton: Boolean = true,
allowedDateValidator: (LocalDate) -> Boolean = { true },
locale: Locale = Locale.getDefault(),
onDateChange: (LocalDate) -> Unit = {}
) {
val dialogState = rememberMaterialDialogState()

MaterialDatePickerDialog(
state = dialogState,
buttons = buttons,
datePickerOptions = rememberDatePickerOptions(
initialDate = initialDate,
colors = colors,
yearRange = yearRange,
waitForPositiveButton = waitForPositiveButton,
allowedDateValidator = allowedDateValidator,
locale = locale,
onDateChange = onDateChange
)
)
DialogDemoButton(state = dialogState, buttonText = buttonText)
}

@Composable
fun DialogDemoButton(state: MaterialDialogState, buttonText: String) {
TextButton(
onClick = { dialogState.show() },
onClick = { state.show() },
modifier = Modifier
.fillMaxWidth()
.padding(8.dp)
.background(MaterialTheme.colors.primaryVariant)
.background(MaterialTheme.colorScheme.primary)
) {
Text(
buttonText,
modifier = Modifier
.fillMaxWidth()
.wrapContentSize(Alignment.Center),
color = MaterialTheme.colors.onPrimary
color = MaterialTheme.colorScheme.onPrimary
)
}
}
Expand All @@ -55,8 +128,8 @@ fun DialogAndShowButton(
fun DialogSection(title: String, content: @Composable () -> Unit) {
Text(
title,
color = MaterialTheme.colors.onSurface,
style = MaterialTheme.typography.subtitle1,
color = MaterialTheme.colorScheme.onSurface,
style = MaterialTheme.typography.headlineSmall,
modifier = Modifier.padding(start = 8.dp, top = 8.dp)
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@ package com.vanpra.composematerialdialogdemos.demos

import android.util.Log
import android.util.Patterns
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.MaterialTheme
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.LocationOn
import androidx.compose.material3.Icon
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.unit.dp
import com.vanpra.composematerialdialogdemos.DialogAndShowButton
import com.vanpra.composematerialdialogdemos.R
import com.vanpra.composematerialdialogs.TextFieldStyle
Expand All @@ -31,81 +32,63 @@ fun BasicDialogDemo() {
message(res = R.string.location_dialog_message)
}

DialogAndShowButton(
buttonText = "Basic Dialog With Buttons",
buttons = {
negativeButton("Disagree")
positiveButton("Agree")
}
) {
DialogAndShowButton(buttonText = "Basic Dialog With Buttons", buttons = {
negativeButton("Disagree")
positiveButton("Agree")
}) {
title(res = R.string.location_dialog_title)
message(res = R.string.location_dialog_message)
}

DialogAndShowButton(
buttonText = "Basic Dialog With Buttons and Icon Title",
buttons = {
negativeButton("Disagree")
positiveButton("Agree")
}
) {
DialogAndShowButton(buttonText = "Basic Dialog With Buttons and Icon Title", buttons = {
negativeButton("Disagree")
positiveButton("Agree")
}) {
iconTitle(
icon = {
Image(
Icon(
Icons.Default.LocationOn,
contentDescription = "Location Icon",
colorFilter = ColorFilter.tint(MaterialTheme.colors.onBackground)
modifier = Modifier.size(24.dp),
contentDescription = "Location Icon"
)
},
textRes = R.string.location_dialog_title
)
message(res = R.string.location_dialog_message)
}

DialogAndShowButton(
buttonText = "Basic Dialog With Stacked Buttons",
buttons = {
negativeButton("No Thanks")
positiveButton("Turn On Speed Boost")
}
) {
DialogAndShowButton(buttonText = "Basic Dialog With Stacked Buttons", buttons = {
negativeButton("No Thanks")
positiveButton("Turn On Speed Boost")
}) {
title(res = R.string.location_dialog_title)
message(res = R.string.location_dialog_message)
}

DialogAndShowButton(
buttonText = "Basic Input Dialog",
buttons = {
negativeButton("Cancel")
positiveButton("Ok")
}
) {
DialogAndShowButton(buttonText = "Basic Input Dialog", buttons = {
negativeButton("Cancel")
positiveButton("Ok")
}) {
title(res = R.string.input_dialog_title)
input(label = "Name", placeholder = "Jon Smith") {
Log.d("SELECTION:", it)
}
}

DialogAndShowButton(
buttonText = "Outlined Input Dialog",
buttons = {
negativeButton("Cancel")
positiveButton("Ok")
}
) {
DialogAndShowButton(buttonText = "Outlined Input Dialog", buttons = {
negativeButton("Cancel")
positiveButton("Ok")
}) {
title(res = R.string.input_dialog_title)
input(label = "Name", placeholder = "Jon Smith", textFieldStyle = TextFieldStyle.Outlined) {
Log.d("SELECTION:", it)
}
}

DialogAndShowButton(
buttonText = "Basic Input Dialog With Immediate Focus",
buttons = {
negativeButton("Cancel")
positiveButton("Ok")
}
) {
DialogAndShowButton(buttonText = "Basic Input Dialog With Immediate Focus", buttons = {
negativeButton("Cancel")
positiveButton("Ok")
}) {
val focusRequester = remember { FocusRequester() }
title(res = R.string.input_dialog_title)
input(
Expand All @@ -118,33 +101,25 @@ fun BasicDialogDemo() {
}
}

DialogAndShowButton(
buttonText = "Input Dialog with submit on IME Action",
buttons = {
negativeButton("Cancel")
positiveButton("Ok")
}
) {
DialogAndShowButton(buttonText = "Input Dialog with submit on IME Action", buttons = {
negativeButton("Cancel")
positiveButton("Ok")
}) {
title(res = R.string.input_dialog_title)
input(
label = "Name",
placeholder = "Jon Smith",
keyboardActions = KeyboardActions(
onDone = { submit() }
),
keyboardActions = KeyboardActions(onDone = { submit() }),
keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done)
) {
Log.d("SELECTION:", it)
}
}

DialogAndShowButton(
buttonText = "Input Dialog with input validation",
buttons = {
negativeButton("Cancel")
positiveButton("Ok")
}
) {
DialogAndShowButton(buttonText = "Input Dialog with input validation", buttons = {
negativeButton("Cancel")
positiveButton("Ok")
}) {
title("Please enter your email")
input(
label = "Email",
Expand Down
Loading