diff --git a/CHANGELOG.md b/CHANGELOG.md
index 624ec534..bd140294 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,12 @@
# Changelog
+### 0.6.0 - 2021-09-10
+
+ - [BREAKING CHANGE] Replace MaterialDialog class structure with composable function with state. See [docs](https://vanpra.github.io/compose-material-dialogs) and the [sample app](https://github.com/vanpra/compose-material-dialogs/tree/main/app/src/main/java/com/vanpra/composematerialdialogdemos/demos) for examples
+ - Update compose to 1.1.0-alpha03
+- Update kotlin to 1.5.30
+- Fix bug which caused the year picker to fill the whole dialog
+
### 0.5.1 - 2021-07-29
- Update compose to 1.0.0
diff --git a/README.md b/README.md
index 2daa656f..59819c6a 100644
--- a/README.md
+++ b/README.md
@@ -19,7 +19,7 @@
```gradle
dependencies {
...
- implementation "io.github.vanpra.compose-material-dialogs:core:0.5.1"
+ implementation "io.github.vanpra.compose-material-dialogs:core:0.6.0"
...
}
```
@@ -35,7 +35,7 @@ dependencies {
```gradle
dependencies {
...
- implementation "io.github.vanpra.compose-material-dialogs:datetime:0.5.1"
+ implementation "io.github.vanpra.compose-material-dialogs:datetime:0.6.0"
...
}
```
@@ -51,7 +51,7 @@ dependencies {
```gradle
dependencies {
...
- implementation "io.github.vanpra.compose-material-dialogs:color:0.5.1"
+ implementation "io.github.vanpra.compose-material-dialogs:color:0.6.0"
...
}
```
diff --git a/app/build.gradle.kts b/app/build.gradle.kts
index 71b0d69e..180803b2 100644
--- a/app/build.gradle.kts
+++ b/app/build.gradle.kts
@@ -5,7 +5,7 @@ plugins {
}
android {
- compileSdk = 30
+ compileSdk = 31
buildToolsVersion = "30.0.3"
defaultConfig {
@@ -52,6 +52,7 @@ dependencies {
// implementation(Dependencies.ComposeMaterialDialogs.color)
implementation(Dependencies.Kotlin.stdlib)
+ implementation(Dependencies.Google.material)
implementation(Dependencies.AndroidX.Compose.ui)
implementation(Dependencies.AndroidX.Compose.material)
@@ -59,13 +60,6 @@ dependencies {
implementation(Dependencies.AndroidX.Compose.activity)
implementation(Dependencies.AndroidX.Compose.navigation)
- implementation(Dependencies.AndroidX.coreKtx)
- implementation(Dependencies.AndroidX.appcompat)
-
- implementation(Dependencies.material)
-
implementation(kotlin("stdlib-jdk8"))
-
- androidTestImplementation(Dependencies.AndroidX.Compose.testing)
coreLibraryDesugaring(Dependencies.desugar)
}
diff --git a/app/mirakle_build_file_stub b/app/mirakle_build_file_stub
deleted file mode 100644
index e69de29b..00000000
diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index 13bd46b9..2521959d 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -1,6 +1,5 @@
@@ -10,15 +9,12 @@
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
- android:theme="@style/Theme.Composematerialdialogs"
- tools:replace="android:theme">
+ android:theme="@style/Theme.MaterialComponents.DayNight.NoActionBar">
+ android:exported="true">
-
diff --git a/app/src/main/java/com/vanpra/composematerialdialogdemos/MainActivity.kt b/app/src/main/java/com/vanpra/composematerialdialogdemos/MainActivity.kt
index bd1a4431..e1a1a469 100644
--- a/app/src/main/java/com/vanpra/composematerialdialogdemos/MainActivity.kt
+++ b/app/src/main/java/com/vanpra/composematerialdialogdemos/MainActivity.kt
@@ -1,8 +1,8 @@
package com.vanpra.composematerialdialogdemos
import android.os.Bundle
+import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
-import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
@@ -31,7 +31,7 @@ import com.vanpra.composematerialdialogdemos.ui.ComposeMaterialDialogsTheme
/**
* @brief MainActivity with material dialog samples
*/
-class MainActivity : AppCompatActivity() {
+class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
diff --git a/app/src/main/java/com/vanpra/composematerialdialogdemos/Util.kt b/app/src/main/java/com/vanpra/composematerialdialogdemos/Util.kt
index 6206e7a2..adb1b3cc 100644
--- a/app/src/main/java/com/vanpra/composematerialdialogdemos/Util.kt
+++ b/app/src/main/java/com/vanpra/composematerialdialogdemos/Util.kt
@@ -8,12 +8,13 @@ import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.material.TextButton
import androidx.compose.runtime.Composable
-import androidx.compose.runtime.remember
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.rememberMaterialDialogState
/**
* @brief Builds a dialog and adds button to the layout which shows the dialog on click
@@ -22,14 +23,16 @@ import com.vanpra.composematerialdialogs.MaterialDialogButtons
fun DialogAndShowButton(
buttonText: String,
buttons: @Composable MaterialDialogButtons.() -> Unit = {},
- content: @Composable MaterialDialog.() -> Unit
+ content: @Composable MaterialDialogScope.() -> Unit
) {
- val dialog = remember { MaterialDialog() }
+ val dialogState = rememberMaterialDialogState()
- dialog.build(content = content, buttons = buttons)
+ MaterialDialog(dialogState = dialogState, buttons = buttons) {
+ content()
+ }
TextButton(
- onClick = { dialog.show() },
+ onClick = { dialogState.show() },
modifier = Modifier
.fillMaxWidth()
.padding(8.dp)
diff --git a/app/src/main/java/com/vanpra/composematerialdialogdemos/demos/BasicDialog.kt b/app/src/main/java/com/vanpra/composematerialdialogdemos/demos/BasicDialog.kt
index 425c2f96..6676062d 100644
--- a/app/src/main/java/com/vanpra/composematerialdialogdemos/demos/BasicDialog.kt
+++ b/app/src/main/java/com/vanpra/composematerialdialogdemos/demos/BasicDialog.kt
@@ -115,7 +115,7 @@ fun BasicDialogDemo() {
input(
label = "Name", hint = "Jon Smith",
keyboardActions = KeyboardActions(
- onDone = { this@DialogAndShowButton.submit() }
+ onDone = { submit() }
),
keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done)
) {
diff --git a/app/src/main/java/com/vanpra/composematerialdialogdemos/demos/ListDialog.kt b/app/src/main/java/com/vanpra/composematerialdialogdemos/demos/ListDialog.kt
index 263d3d76..71ac0fc5 100644
--- a/app/src/main/java/com/vanpra/composematerialdialogdemos/demos/ListDialog.kt
+++ b/app/src/main/java/com/vanpra/composematerialdialogdemos/demos/ListDialog.kt
@@ -106,12 +106,15 @@ fun BasicListDialogDemo() {
fun MultiSelectionDemo() {
var initialSelection by remember { mutableStateOf(setOf(3, 5)) }
- DialogAndShowButton(buttonText = "Multi-Selection Dialog", buttons = { defaultListDialogButtons() }) {
- title(res = R.string.labels_dialog_title)
- listItemsMultiChoice(labels) {
- println(it)
+ DialogAndShowButton(
+ buttonText = "Multi-Selection Dialog",
+ buttons = { defaultListDialogButtons() }
+ ) {
+ title(res = R.string.labels_dialog_title)
+ listItemsMultiChoice(labels) {
+ println(it)
+ }
}
-}
DialogAndShowButton(
buttonText = "Multi-Selection Dialog with disabled items",
diff --git a/app/src/main/res/values/themes.xml b/app/src/main/res/values/themes.xml
index 07fc90a3..17a54dde 100644
--- a/app/src/main/res/values/themes.xml
+++ b/app/src/main/res/values/themes.xml
@@ -1,25 +1,2 @@
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/build.gradle.kts b/build.gradle.kts
index 1aaeedce..f0e99515 100644
--- a/build.gradle.kts
+++ b/build.gradle.kts
@@ -1,7 +1,7 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
- id("com.diffplug.spotless") version "5.14.1"
+ id("com.diffplug.spotless") version "5.14.3"
id("org.jetbrains.dokka") version "1.5.0"
}
@@ -15,10 +15,10 @@ buildscript {
dependencies {
classpath(Dependencies.Kotlin.gradlePlugin)
- classpath("com.android.tools.build:gradle:7.1.0-alpha05")
+ classpath("com.android.tools.build:gradle:7.1.0-alpha11")
classpath("com.vanniktech:gradle-maven-publish-plugin:0.17.0")
classpath("org.jetbrains.dokka:dokka-gradle-plugin:1.5.0")
- classpath("com.karumi:shot:5.10.6")
+ classpath(Dependencies.Shot.core)
}
}
diff --git a/buildSrc/src/main/kotlin/CommonModulePlugin.kt b/buildSrc/src/main/kotlin/CommonModulePlugin.kt
index ede2ab96..114c8e6e 100644
--- a/buildSrc/src/main/kotlin/CommonModulePlugin.kt
+++ b/buildSrc/src/main/kotlin/CommonModulePlugin.kt
@@ -24,8 +24,6 @@ class CommonModulePlugin: Plugin {
dependencies.apply {
implementation(Dependencies.Kotlin.stdlib)
implementation(Dependencies.AndroidX.coreKtx)
- implementation(Dependencies.AndroidX.appcompat)
- implementation(Dependencies.material)
implementation(Dependencies.AndroidX.Compose.ui)
implementation(Dependencies.AndroidX.Compose.material)
diff --git a/buildSrc/src/main/kotlin/Dependencies.kt b/buildSrc/src/main/kotlin/Dependencies.kt
index b9b5ce34..b0d8699f 100644
--- a/buildSrc/src/main/kotlin/Dependencies.kt
+++ b/buildSrc/src/main/kotlin/Dependencies.kt
@@ -1,5 +1,4 @@
object Dependencies {
- const val material = "com.google.android.material:material:1.5.0-alpha01"
const val desugar = "com.android.tools:desugar_jdk_libs:1.1.5"
object ComposeMaterialDialogs {
@@ -11,47 +10,51 @@ object Dependencies {
}
object Ktlint {
- const val version = "0.42.0"
+ const val version = "0.42.1"
}
object Accompanist {
- private const val version = "0.15.0"
+ private const val version = "0.18.0"
const val pager = "com.google.accompanist:accompanist-pager:$version"
}
object Kotlin {
- private const val version = "1.5.10"
+ private const val version = "1.5.30"
const val stdlib = "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$version"
const val gradlePlugin = "org.jetbrains.kotlin:kotlin-gradle-plugin:$version"
}
object Shot {
- private const val version = "5.10.6"
- const val shot = "com.karumi:shot-android:$version"
+ private const val version = "5.11.2"
+ const val core = "com.karumi:shot:$version"
+ const val android = "com.karumi:shot-android:$version"
+ }
+
+ object Google {
+ const val material = "com.google.android.material:material:1.5.0-alpha02"
}
object AndroidX {
- const val appcompat = "androidx.appcompat:appcompat:1.4.0-alpha03"
- const val coreKtx = "androidx.core:core-ktx:1.7.0-alpha01"
+ const val coreKtx = "androidx.core:core-ktx:1.7.0-alpha02"
object Testing {
- const val version = "1.4.0"
+ const val version = "1.4.1-alpha01"
const val core = "androidx.test:core:$version"
const val rules = "androidx.test:rules:$version"
const val runner = "androidx.test:runner:$version"
}
object Compose {
- const val version = "1.0.0"
+ const val version = "1.1.0-alpha03"
const val ui = "androidx.compose.ui:ui:$version"
const val material = "androidx.compose.material:material:$version"
const val materialIconsExtended =
"androidx.compose.material:material-icons-extended:$version"
- const val testing = "androidx.compose.ui:ui-test-junit4:1.0.0-rc02"
- const val activity = "androidx.activity:activity-compose:1.3.0-rc02"
- const val navigation = "androidx.navigation:navigation-compose:2.4.0-alpha05"
+ const val testing = "androidx.compose.ui:ui-test-junit4:1.1.0-alpha03"
+ const val activity = "androidx.activity:activity-compose:1.4.0-alpha01"
+ const val navigation = "androidx.navigation:navigation-compose:2.4.0-alpha08"
}
}
-}
+}
\ No newline at end of file
diff --git a/buildSrc/src/main/kotlin/update_deps.py b/buildSrc/src/main/kotlin/update_deps.py
new file mode 100644
index 00000000..45f5f94d
--- /dev/null
+++ b/buildSrc/src/main/kotlin/update_deps.py
@@ -0,0 +1,80 @@
+import re
+from typing import List
+import bs4 as beautifulsoup
+import requests
+from tqdm import tqdm
+
+from selenium import webdriver
+from selenium.webdriver.chrome.options import Options
+from selenium.webdriver.common.by import By
+from selenium.webdriver.support.ui import WebDriverWait
+from selenium.webdriver.support import expected_conditions as ec
+
+main_search_url = 'https://search.maven.org/solrsearch/select?q=g:"{}"+AND+a:"{}"&core=gav&rows=1&wt=json'
+google_search_url = "https://maven.google.com/web/index.html"
+
+
+chrome_options = Options()
+# chrome_options.add_argument("--headless")
+chrome_options.add_argument("--window-size=1920x1080")
+driver = webdriver.Chrome(options=chrome_options)
+
+dep_file = "Dependencies.kt"
+
+with open(dep_file, "r") as readfile:
+ current_deps = readfile.read().splitlines()
+
+version_patten = re.compile(r"version = \"(.*)\"")
+dep_pattern = re.compile(r"const val ([^=]*) = \"(.*)\"")
+current_version = ("", -1, "")
+
+updated_deps: List[str] = []
+
+for index, item in enumerate(tqdm(current_deps)):
+ version_match = version_patten.search(item)
+ dep_match = dep_pattern.search(item)
+
+ if version_match:
+ if current_version[1] != -1:
+ updated_deps[current_version[1]] = re.sub(version_patten, f'version = "{current_version[0]}"', current_version[2])
+ current_version = (version_match.group(1), index, item)
+ updated_deps.append(item)
+ elif dep_match:
+ (dep, version) = dep_match.group(2).rsplit(":", 1)
+ (group, artifact) = dep.split(":")
+ main_dep_url = main_search_url.format(group, artifact)
+ google_dep_url = f"{google_search_url}#{group}:{artifact}"
+
+ res = requests.get(main_dep_url).json()
+ latest_version = version
+
+ if res["response"]["docs"]:
+ latest_version = res["response"]["docs"][0]["v"]
+ else:
+ print(google_dep_url)
+ driver.get(google_dep_url)
+ elems = WebDriverWait(driver, 10).until(
+ ec.visibility_of_element_located(
+ (By.XPATH, "//div[@class='content-header ng-binding ng-scope']")
+ )
+ )
+
+ soup = beautifulsoup.BeautifulSoup(driver.page_source, "html.parser")
+ latest_version = soup.find("span", {"class": "ng-binding"}).text
+
+ if version == "$version":
+ current_version = (latest_version, current_version[1], current_version[2])
+ updated_deps.append(item)
+ else:
+ updated_deps.append(
+ f"const val {dep_match.group(1)} = \"{group}:{artifact}:{latest_version}\""
+ )
+ else:
+ updated_deps.append(item)
+
+
+if current_version[1] != -1:
+ updated_deps[current_version[1]] = re.sub(version_patten, f'version = "{current_version[0]}"', current_version[2])
+
+with open(dep_file, "w") as outfile:
+ outfile.write("\n".join(updated_deps))
\ No newline at end of file
diff --git a/color/build.gradle.kts b/color/build.gradle.kts
index fac87bf8..62e356dd 100644
--- a/color/build.gradle.kts
+++ b/color/build.gradle.kts
@@ -5,8 +5,8 @@ plugins {
android {
defaultConfig {
minSdk = 21
- compileSdk = 30
- targetSdk = 30
+ compileSdk = 31
+ targetSdk = 31
testInstrumentationRunner = "com.karumi.shot.ShotTestRunner"
testApplicationId = "com.vanpra.composematerialdialogs.color.test"
@@ -48,6 +48,7 @@ android {
dependencies {
api(project(":core"))
+ implementation(Dependencies.AndroidX.coreKtx)
}
shot {
diff --git a/color/src/androidTest/AndroidManifest.xml b/color/src/androidTest/AndroidManifest.xml
index 867298a8..3009fdae 100644
--- a/color/src/androidTest/AndroidManifest.xml
+++ b/color/src/androidTest/AndroidManifest.xml
@@ -4,6 +4,7 @@
+ android:exported="true"
+ />
\ No newline at end of file
diff --git a/color/src/androidTest/java/com/vanpra/composematerialdialogs/color/test/functional/ColorPickerDialogTests.kt b/color/src/androidTest/java/com/vanpra/composematerialdialogs/color/test/functional/ColorPickerDialogTests.kt
index 24e18725..28674c13 100644
--- a/color/src/androidTest/java/com/vanpra/composematerialdialogs/color/test/functional/ColorPickerDialogTests.kt
+++ b/color/src/androidTest/java/com/vanpra/composematerialdialogs/color/test/functional/ColorPickerDialogTests.kt
@@ -37,11 +37,11 @@ class ColorPickerDialogTests {
composeTestRule.setContent {
DialogWithContent(buttons = { defaultButtons() }) {
- colorChooser(colors = ColorPalette.Primary, waitForPositiveButton = true) {
- selectedColor = it
+ colorChooser(colors = ColorPalette.Primary, waitForPositiveButton = true) {
+ selectedColor = it
+ }
}
}
- }
composeTestRule.onDialogColorSelector(2).performClick()
assertEquals(null, selectedColor)
@@ -57,11 +57,11 @@ class ColorPickerDialogTests {
composeTestRule.setContent {
DialogWithContent(buttons = { defaultButtons() }) {
- colorChooser(colors = ColorPalette.Primary, waitForPositiveButton = false) {
- selectedColor = it
+ colorChooser(colors = ColorPalette.Primary, waitForPositiveButton = false) {
+ selectedColor = it
+ }
}
}
- }
composeTestRule.onDialogColorSelector(2).performClick()
composeTestRule.waitForIdle()
diff --git a/color/src/main/java/com/vanpra/composematerialdialogs/color/ColorPicker.kt b/color/src/main/java/com/vanpra/composematerialdialogs/color/ColorPicker.kt
index 00339ec3..52b33a2a 100644
--- a/color/src/main/java/com/vanpra/composematerialdialogs/color/ColorPicker.kt
+++ b/color/src/main/java/com/vanpra/composematerialdialogs/color/ColorPicker.kt
@@ -58,7 +58,7 @@ import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.viewinterop.AndroidView
import androidx.core.graphics.ColorUtils
-import com.vanpra.composematerialdialogs.MaterialDialog
+import com.vanpra.composematerialdialogs.MaterialDialogScope
import java.util.Locale
import android.graphics.Color as AndroidColor
@@ -101,7 +101,7 @@ data class ARGBPickerState internal constructor(
* dictated by [waitForPositiveButton]
*/
@Composable
-fun MaterialDialog.colorChooser(
+fun MaterialDialogScope.colorChooser(
colors: List,
subColors: List> = listOf(),
initialSelection: Int = 0,
diff --git a/core/build.gradle.kts b/core/build.gradle.kts
index f058e687..58adf680 100644
--- a/core/build.gradle.kts
+++ b/core/build.gradle.kts
@@ -5,8 +5,8 @@ plugins {
android {
defaultConfig {
minSdk = 21
- targetSdk = 30
- compileSdk = 30
+ compileSdk = 31
+ targetSdk = 31
testInstrumentationRunner = "com.karumi.shot.ShotTestRunner"
testApplicationId = "com.vanpra.composematerialdialogs.test"
diff --git a/core/src/androidTest/AndroidManifest.xml b/core/src/androidTest/AndroidManifest.xml
index 96085361..e6090891 100644
--- a/core/src/androidTest/AndroidManifest.xml
+++ b/core/src/androidTest/AndroidManifest.xml
@@ -5,6 +5,7 @@
+ android:exported="true"
+ />
\ No newline at end of file
diff --git a/core/src/androidTest/java/com/vanpra/composematerialdialogs/test/functional/DialogButtonsTest.kt b/core/src/androidTest/java/com/vanpra/composematerialdialogs/test/functional/DialogButtonsTest.kt
index 1409d43d..2e9a4a43 100644
--- a/core/src/androidTest/java/com/vanpra/composematerialdialogs/test/functional/DialogButtonsTest.kt
+++ b/core/src/androidTest/java/com/vanpra/composematerialdialogs/test/functional/DialogButtonsTest.kt
@@ -5,7 +5,6 @@ import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.performClick
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.vanpra.composematerialdialogs.MaterialDialogButtons
-import com.vanpra.composematerialdialogs.buttons
import com.vanpra.composematerialdialogs.test.utils.DialogWithContent
import com.vanpra.composematerialdialogs.test.utils.extensions.assertDialogDoesNotExist
import com.vanpra.composematerialdialogs.test.utils.extensions.assertDialogExists
diff --git a/core/src/androidTest/java/com/vanpra/composematerialdialogs/test/functional/InputDialogTest.kt b/core/src/androidTest/java/com/vanpra/composematerialdialogs/test/functional/InputDialogTest.kt
index cc9cd51f..009f66a2 100644
--- a/core/src/androidTest/java/com/vanpra/composematerialdialogs/test/functional/InputDialogTest.kt
+++ b/core/src/androidTest/java/com/vanpra/composematerialdialogs/test/functional/InputDialogTest.kt
@@ -11,9 +11,9 @@ import androidx.compose.ui.test.performClick
import androidx.compose.ui.test.performTextClearance
import androidx.compose.ui.test.performTextInput
import androidx.test.ext.junit.runners.AndroidJUnit4
-import com.vanpra.composematerialdialogs.MaterialDialog
-import com.vanpra.composematerialdialogs.buttons
+import com.vanpra.composematerialdialogs.MaterialDialogScope
import com.vanpra.composematerialdialogs.input
+import com.vanpra.composematerialdialogs.rememberMaterialDialogState
import com.vanpra.composematerialdialogs.test.R
import com.vanpra.composematerialdialogs.test.utils.DialogWithContent
import com.vanpra.composematerialdialogs.test.utils.defaultButtons
@@ -177,14 +177,15 @@ class InputDialogTest {
}
private fun setupBasicInputDialog(
- dialog: MaterialDialog = MaterialDialog(),
- testInputDialog: @Composable MaterialDialog.() -> Unit = { input(label = "Test") }
+ testInputDialog: @Composable MaterialDialogScope.() -> Unit = { input(label = "Test") }
) {
composeTestRule.setContent {
- DialogWithContent(dialog = dialog, buttons = { defaultButtons() }) {
- title(res = R.string.input_dialog_title)
- testInputDialog()
- }
+ val dialogState = rememberMaterialDialogState(true)
+
+ DialogWithContent(dialogState = dialogState, buttons = { defaultButtons() }) {
+ title(res = R.string.input_dialog_title)
+ testInputDialog()
+ }
}
}
}
diff --git a/core/src/androidTest/java/com/vanpra/composematerialdialogs/test/functional/ListDialogTest.kt b/core/src/androidTest/java/com/vanpra/composematerialdialogs/test/functional/ListDialogTest.kt
index 2502e46a..5dc3d667 100644
--- a/core/src/androidTest/java/com/vanpra/composematerialdialogs/test/functional/ListDialogTest.kt
+++ b/core/src/androidTest/java/com/vanpra/composematerialdialogs/test/functional/ListDialogTest.kt
@@ -20,8 +20,7 @@ import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.performClick
import androidx.compose.ui.unit.dp
import androidx.test.ext.junit.runners.AndroidJUnit4
-import com.vanpra.composematerialdialogs.MaterialDialog
-import com.vanpra.composematerialdialogs.buttons
+import com.vanpra.composematerialdialogs.MaterialDialogState
import com.vanpra.composematerialdialogs.listItems
import com.vanpra.composematerialdialogs.listItemsMultiChoice
import com.vanpra.composematerialdialogs.listItemsSingleChoice
@@ -71,11 +70,11 @@ class ListDialog {
@Test
fun simpleListSelectionDialog() {
- val dialog = MaterialDialog()
+ val dialogState = MaterialDialogState(true)
var selectedItem: Pair? = null
composeTestRule.setContent {
- DialogWithContent(dialog = dialog) {
+ DialogWithContent(dialogState = dialogState) {
title(res = R.string.backup_dialog_title)
listItems(emails) { index, item ->
selectedItem = Pair(index, item)
@@ -88,17 +87,17 @@ class ListDialog {
/* Need this line or else tests don't wait for dialog to close */
composeTestRule.assertDialogDoesNotExist()
assertThat(selectedItem, equalTo(Pair(index, email)))
- dialog.show()
+ dialogState.show()
}
}
@Test
fun customListSelectionDialog() {
var selectedItem: Pair? = null
- val dialog = MaterialDialog()
+ val dialogState = MaterialDialogState(true)
composeTestRule.setContent {
- DialogWithContent(dialog = dialog) {
+ DialogWithContent(dialogState = dialogState) {
title(res = R.string.backup_dialog_title)
listItems(
emails,
@@ -132,15 +131,14 @@ class ListDialog {
/* Need this line or else tests don't wait for dialog to close */
composeTestRule.assertDialogDoesNotExist()
assertThat(selectedItem, equalTo(Pair(index, email)))
- dialog.show()
+ dialogState.show()
}
}
@Test
fun multiSelectionDialogWaitForPositiveButton() {
val selectedItem = mutableStateOf?>(null)
- val dialog = MaterialDialog()
- setupMultiSelectionDialog(dialog, selectedItem)
+ setupMultiSelectionDialog(selectedItem = selectedItem)
composeTestRule.onDialogListItem(0).performClick()
assertEquals(null, selectedItem.value)
@@ -153,8 +151,7 @@ class ListDialog {
@Test
fun multiSelectionDialogDontWaitForPositiveButton() {
val selectedItem = mutableStateOf?>(null)
- val dialog = MaterialDialog()
- setupMultiSelectionDialog(dialog, selectedItem, waitForPositiveButton = false)
+ setupMultiSelectionDialog(selectedItem = selectedItem, waitForPositiveButton = false)
composeTestRule.onDialogListItem(0).performClick()
assertEquals(setOf(0), selectedItem.value)
@@ -169,8 +166,8 @@ class ListDialog {
@Test
fun multiSelectionDialogItems() {
val selectedItem = mutableStateOf?>(null)
- val dialog = MaterialDialog()
- setupMultiSelectionDialog(dialog, selectedItem)
+ val dialogState = MaterialDialogState(true)
+ setupMultiSelectionDialog(dialogState, selectedItem)
IntRange(0, labels.size - 1).toList().powerSet().forEachIndexed { _, indexes ->
/* Tests all combinations of of input items */
@@ -181,15 +178,15 @@ class ListDialog {
/* Need this line or else tests don't wait for dialog to close */
composeTestRule.assertDialogDoesNotExist()
assertEquals(indexes, selectedItem.value)
- dialog.show()
+ dialogState.show()
}
}
@Test
fun multiSelectionDialogItemRemovedWhenClickedDeselected() {
val selectedItem = mutableStateOf?>(null)
- val dialog = MaterialDialog()
- setupMultiSelectionDialog(dialog, selectedItem, waitForPositiveButton = false)
+ val dialogState = MaterialDialogState(true)
+ setupMultiSelectionDialog(dialogState, selectedItem, waitForPositiveButton = false)
labels.forEachIndexed { index, _ ->
composeTestRule.onDialogListItem(index).performClick()
@@ -198,16 +195,16 @@ class ListDialog {
/* Need this line or else tests don't wait for dialog to close */
composeTestRule.assertDialogDoesNotExist()
assertEquals(setOf(), selectedItem.value)
- dialog.show()
+ dialogState.show()
}
}
@Test
fun singleSelectionDialogAllItems() {
val selectedItem = mutableStateOf(null)
- val dialog = MaterialDialog()
+ val dialogState = MaterialDialogState(true)
- setupSingleSelectionDialog(dialog, selectedItem)
+ setupSingleSelectionDialog(dialogState, selectedItem)
ringtones.forEachIndexed { index, _ ->
composeTestRule.onDialogListItem(index).performClick()
@@ -216,16 +213,15 @@ class ListDialog {
/* Need this line or else tests don't wait for dialog to close */
composeTestRule.assertDialogDoesNotExist()
assertEquals(index, selectedItem.value)
- dialog.show()
+ dialogState.show()
}
}
@Test
fun singleSelectionDialogWaitForPositiveButton() {
val selectedItem = mutableStateOf(null)
- val dialog = MaterialDialog()
- setupSingleSelectionDialog(dialog, selectedItem)
+ setupSingleSelectionDialog(selectedItem = selectedItem)
composeTestRule.onDialogListItem(0).performClick()
assertEquals(null, selectedItem.value)
@@ -238,7 +234,7 @@ class ListDialog {
@Test
fun singleSelectionDialogDontWaitForPositiveButton() {
val selectedItem = mutableStateOf(null)
- val dialog = MaterialDialog()
+ val dialog = MaterialDialogState(true)
setupSingleSelectionDialog(dialog, selectedItem, waitForPositiveButton = false)
@@ -273,32 +269,32 @@ class ListDialog {
}
private fun setupMultiSelectionDialog(
- dialog: MaterialDialog,
+ dialogState: MaterialDialogState = MaterialDialogState(true),
selectedItem: MutableState?>,
waitForPositiveButton: Boolean = true
) {
composeTestRule.setContent {
- DialogWithContent(dialog = dialog, buttons = { defaultButtons() }) {
- title(res = R.string.labels_dialog_title)
- listItemsMultiChoice(labels, waitForPositiveButton = waitForPositiveButton) {
- selectedItem.value = it
+ DialogWithContent(dialogState = dialogState, buttons = { defaultButtons() }) {
+ title(res = R.string.labels_dialog_title)
+ listItemsMultiChoice(labels, waitForPositiveButton = waitForPositiveButton) {
+ selectedItem.value = it
+ }
}
}
- }
}
private fun setupSingleSelectionDialog(
- dialog: MaterialDialog = MaterialDialog(),
+ dialogState: MaterialDialogState = MaterialDialogState(true),
selectedItem: MutableState = mutableStateOf(null),
waitForPositiveButton: Boolean = true
) {
composeTestRule.setContent {
- DialogWithContent(dialog = dialog, buttons = { defaultButtons() }) {
- title(res = R.string.ringtone_dialog_title)
- listItemsSingleChoice(ringtones, waitForPositiveButton = waitForPositiveButton) {
- selectedItem.value = it
+ DialogWithContent(dialogState = dialogState, buttons = { defaultButtons() }) {
+ title(res = R.string.ringtone_dialog_title)
+ listItemsSingleChoice(ringtones, waitForPositiveButton = waitForPositiveButton) {
+ selectedItem.value = it
+ }
}
}
- }
}
}
diff --git a/core/src/androidTest/java/com/vanpra/composematerialdialogs/test/screenshot/BasicDialogTest.kt b/core/src/androidTest/java/com/vanpra/composematerialdialogs/test/screenshot/BasicDialogTest.kt
index 7194f386..a1fc77de 100644
--- a/core/src/androidTest/java/com/vanpra/composematerialdialogs/test/screenshot/BasicDialogTest.kt
+++ b/core/src/androidTest/java/com/vanpra/composematerialdialogs/test/screenshot/BasicDialogTest.kt
@@ -8,7 +8,6 @@ import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.karumi.shot.ScreenshotTest
-import com.vanpra.composematerialdialogs.buttons
import com.vanpra.composematerialdialogs.iconTitle
import com.vanpra.composematerialdialogs.input
import com.vanpra.composematerialdialogs.message
diff --git a/core/src/androidTest/java/com/vanpra/composematerialdialogs/test/screenshot/ListDialogTest.kt b/core/src/androidTest/java/com/vanpra/composematerialdialogs/test/screenshot/ListDialogTest.kt
index a578fd36..443ecf6e 100644
--- a/core/src/androidTest/java/com/vanpra/composematerialdialogs/test/screenshot/ListDialogTest.kt
+++ b/core/src/androidTest/java/com/vanpra/composematerialdialogs/test/screenshot/ListDialogTest.kt
@@ -111,9 +111,9 @@ class ListDialog : ScreenshotTest {
fun multiSelectionDialog() {
composeTestRule.setContentAndWaitForIdle {
DialogWithContent(buttons = { defaultButtons() }) {
- title(res = R.string.labels_dialog_title)
- listItemsMultiChoice(labels)
- }
+ title(res = R.string.labels_dialog_title)
+ listItemsMultiChoice(labels)
+ }
}
compareScreenshot(composeTestRule.onDialog())
}
@@ -122,9 +122,9 @@ class ListDialog : ScreenshotTest {
fun singleSelectionDialog() {
composeTestRule.setContentAndWaitForIdle {
DialogWithContent(buttons = { defaultButtons() }) {
- title(res = R.string.ringtone_dialog_title)
- listItemsSingleChoice(ringtones)
- }
+ title(res = R.string.ringtone_dialog_title)
+ listItemsSingleChoice(ringtones)
+ }
}
compareScreenshot(composeTestRule.onDialog())
}
diff --git a/core/src/main/java/com/vanpra/composematerialdialogs/MaterialDialog.kt b/core/src/main/java/com/vanpra/composematerialdialogs/MaterialDialog.kt
index 95591632..3988a438 100644
--- a/core/src/main/java/com/vanpra/composematerialdialogs/MaterialDialog.kt
+++ b/core/src/main/java/com/vanpra/composematerialdialogs/MaterialDialog.kt
@@ -12,13 +12,14 @@ import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
-import androidx.compose.runtime.MutableState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateMapOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
+import androidx.compose.runtime.saveable.Saver
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
+import androidx.compose.runtime.snapshots.SnapshotStateMap
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clipToBounds
import androidx.compose.ui.focus.FocusManager
@@ -36,79 +37,99 @@ import java.util.concurrent.atomic.AtomicInteger
import kotlin.math.min
/**
- * The MaterialDialog class is used to build and display a dialog using both pre-made and
- * custom views
- *
- * @param autoDismiss when true the dialog will be automatically dismissed when a positive or
- * negative button is pressed
- * @param onCloseRequest a callback for when the user tries to exit the dialog by clicking outside
- * the dialog. This callback takes the current MaterialDialog as
- * a parameter to allow for the hide method of the dialog to be called if required. By default
- * this callback hides the dialog.
+ * Interface defining values and functions which are available to any code
+ * within a [MaterialDialog]'s content parameter
*/
-class MaterialDialog(
- private val autoDismiss: Boolean = true,
- private val onCloseRequest: (MaterialDialog) -> Unit = { it.hide() },
-) {
- private val showing: MutableState = mutableStateOf(false)
-
- val dialogButtons = MaterialDialogButtons(this)
+interface MaterialDialogScope {
+ val dialogState: MaterialDialogState
+ val dialogButtons: MaterialDialogButtons
- val callbacks = mutableMapOf Unit>()
- private val callbackCounter = AtomicInteger(0)
+ val callbacks: SnapshotStateMap Unit>
+ val positiveButtonEnabled: SnapshotStateMap
- var positiveEnabled = mutableStateMapOf()
- private val positiveEnabledCounter = AtomicInteger(0)
- var positiveButtonEnabledOverride by mutableStateOf(true)
+ val autoDismiss: Boolean
/**
- * Dialog background color with elevation overlay
+ * Hides the dialog and calls any callbacks from components in the dialog
*/
- var dialogBackgroundColor by mutableStateOf(null)
+ fun submit()
- internal fun setPositiveEnabled(index: Int, value: Boolean) {
- positiveEnabled[index] = value
- }
+ /**
+ * Clears the dialog's state
+ */
+ fun reset()
/**
- * Shows the dialog
+ * Adds a value to the [positiveButtonEnabled] map and updates the value in the map when
+ * [valid] changes
+ *
+ * @param valid boolean value to initialise the index in the list
+ * @param onDispose cleanup callback when component calling this gets destroyed
*/
- fun show() {
- showing.value = true
- }
+ @Composable
+ fun PositiveButtonEnabled(valid: Boolean, onDispose: () -> Unit)
/**
- * Clears focus with a given [FocusManager] and then hides the dialog
+ * Adds a callback to the dialog which is called on positive button press
*
- * @param focusManager the focus manager of the dialog view
+ * @param callback called when positive button is pressed
*/
- fun hide(focusManager: FocusManager? = null) {
- focusManager?.clearFocus()
- showing.value = false
- }
+ @Composable
+ fun DialogCallback(callback: () -> Unit)
+}
+
+internal class MaterialDialogScopeImpl(
+ override val dialogState: MaterialDialogState,
+ override val autoDismiss: Boolean = true
+) : MaterialDialogScope {
+ override val dialogButtons = MaterialDialogButtons(this)
+
+ override val callbacks = mutableStateMapOf Unit>()
+ private val callbackCounter = AtomicInteger(0)
+
+ override val positiveButtonEnabled = mutableStateMapOf()
+ private val positiveEnabledCounter = AtomicInteger(0)
/**
* Hides the dialog and calls any callbacks from components in the dialog
*/
- fun submit() {
- hide()
+ override fun submit() {
+ dialogState.hide()
callbacks.values.forEach {
it()
}
}
/**
- * Disables the positive dialog button if present
+ * Clears the dialog callbacks and positive button enables values along with their
+ * respective counters
*/
- fun disablePositiveButton() {
- positiveButtonEnabledOverride = false
+ override fun reset() {
+ positiveButtonEnabled.clear()
+ callbacks.clear()
+
+ positiveEnabledCounter.set(0)
+ callbackCounter.set(0)
}
/**
- * Enables the positive dialog button if present
+ * Adds a value to the [positiveButtonEnabled] map and updates the value in the map when
+ * [valid] changes
+ *
+ * @param valid boolean value to initialise the index in the list
+ * @param onDispose cleanup callback when component calling this gets destroyed
*/
- fun enablePositiveButton() {
- positiveButtonEnabledOverride = true
+ @Composable
+ override fun PositiveButtonEnabled(valid: Boolean, onDispose: () -> Unit) {
+ val positiveEnabledIndex = remember { positiveEnabledCounter.getAndIncrement() }
+
+ DisposableEffect(valid) {
+ positiveButtonEnabled[positiveEnabledIndex] = valid
+ onDispose {
+ positiveButtonEnabled[positiveEnabledIndex] = true
+ onDispose()
+ }
+ }
}
/**
@@ -117,7 +138,7 @@ class MaterialDialog(
* @param callback called when positive button is pressed
*/
@Composable
- fun DialogCallback(callback: () -> Unit) {
+ override fun DialogCallback(callback: () -> Unit) {
val callbackIndex = rememberSaveable { callbackCounter.getAndIncrement() }
DisposableEffect(Unit) {
@@ -125,83 +146,109 @@ class MaterialDialog(
onDispose { callbacks[callbackIndex] = {} }
}
}
+}
+
+/**
+ * The [MaterialDialogState] class is used to store the state for a [MaterialDialog]
+ *
+ * @param initialValue the initial showing state of the dialog
+ */
+class MaterialDialogState(initialValue: Boolean = false) {
+ var showing by mutableStateOf(initialValue)
/**
- * Adds a value to the [positiveEnabled] list and returns the index used to store the boolean
- *
- * @param valid boolean value to initialise the index in the list
- * @param onDispose cleanup callback when component calling this gets destroyed
+ * Dialog background color with elevation overlay
*/
- @Composable
- fun addPositiveButtonEnabled(valid: Boolean, onDispose: () -> Unit = {}): Int {
- val positiveEnabledIndex = remember { positiveEnabledCounter.getAndIncrement() }
-
- DisposableEffect(Unit) {
- positiveEnabled[positiveEnabledIndex] = valid
-
- onDispose {
- setPositiveEnabled(positiveEnabledIndex, true)
- onDispose()
- }
- }
+ var dialogBackgroundColor by mutableStateOf(null)
- return positiveEnabledIndex
+ /**
+ * Shows the dialog
+ */
+ fun show() {
+ showing = true
}
/**
- * Checks if autoDismiss is set
+ * Clears focus with a given [FocusManager] and then hides the dialog
*
- * @return true if autoDismiss is set to true and false otherwise
+ * @param focusManager the focus manager of the dialog view
*/
- fun isAutoDismiss() = autoDismiss
+ fun hide(focusManager: FocusManager? = null) {
+ focusManager?.clearFocus()
+ showing = false
+ }
- private fun resetDialog() {
- positiveEnabled.clear()
- callbacks.clear()
+ companion object {
+ /**
+ * The default [Saver] implementation for [MaterialDialogState].
+ */
+ fun Saver(): Saver = Saver(
+ save = { it.showing },
+ restore = { MaterialDialogState(it) }
+ )
+ }
+}
- positiveEnabledCounter.set(0)
- callbackCounter.set(0)
+/**
+ * Create and [remember] a [MaterialDialogState].
+ *
+ * @param initialValue the initial showing state of the dialog
+ */
+@Composable
+fun rememberMaterialDialogState(initialValue: Boolean = false): MaterialDialogState {
+ return rememberSaveable(saver = MaterialDialogState.Saver()) {
+ MaterialDialogState(initialValue)
}
+}
- /**
- * Builds a dialog with the given content
- * @param backgroundColor background color of the dialog
- * @param shape shape of the dialog and components used in the dialog
- * @param border border stoke of the dialog
- * @param elevation elevation of the dialog
- * @param content the body content of the dialog
- */
- @Composable
- fun build(
- backgroundColor: Color = MaterialTheme.colors.surface,
- shape: Shape = MaterialTheme.shapes.medium,
- border: BorderStroke? = null,
- elevation: Dp = 24.dp,
- buttons: @Composable MaterialDialogButtons.() -> Unit = {},
- content: @Composable MaterialDialog.() -> Unit
- ) {
- BoxWithConstraints {
- val maxHeight = if (isLargeDevice()) {
- LocalConfiguration.current.screenHeightDp.dp - 90.dp
- } else {
- 560.dp
- }
+/**
+ * Builds a dialog with the given content
+ * @param dialogState state of the dialog
+ * @param backgroundColor background color of the dialog
+ * @param shape shape of the dialog and components used in the dialog
+ * @param border border stoke of the dialog
+ * @param elevation elevation of the dialog
+ * @param autoDismiss when true the dialog is hidden on any button press
+ * @param onCloseRequest function to be executed when user clicks outside dialog
+ * @param buttons the buttons layout of the dialog
+ * @param content the body content of the dialog
+ */
+@Composable
+fun MaterialDialog(
+ dialogState: MaterialDialogState = rememberMaterialDialogState(),
+ backgroundColor: Color = MaterialTheme.colors.surface,
+ shape: Shape = MaterialTheme.shapes.medium,
+ border: BorderStroke? = null,
+ elevation: Dp = 24.dp,
+ autoDismiss: Boolean = true,
+ onCloseRequest: (MaterialDialogState) -> Unit = { it.hide() },
+ buttons: @Composable MaterialDialogButtons.() -> Unit = {},
+ content: @Composable MaterialDialogScope.() -> Unit
+) {
+ val dialogScope = remember { MaterialDialogScopeImpl(dialogState, autoDismiss) }
+ DisposableEffect(dialogState.showing) {
+ if (!dialogState.showing) dialogScope.reset()
+ onDispose { }
+ }
- val maxHeightPx = with(LocalDensity.current) { maxHeight.toPx().toInt() }
- val isDialogFullWidth = LocalConfiguration.current.screenWidthDp.dp == maxWidth
- val padding = if (isDialogFullWidth) 16.dp else 0.dp
+ BoxWithConstraints {
+ val maxHeight = if (isLargeDevice()) {
+ LocalConfiguration.current.screenHeightDp.dp - 90.dp
+ } else {
+ 560.dp
+ }
- if (showing.value) {
- dialogBackgroundColor = LocalElevationOverlay.current?.apply(
- color = backgroundColor,
- elevation = elevation
- ) ?: MaterialTheme.colors.surface
+ val maxHeightPx = with(LocalDensity.current) { maxHeight.toPx().toInt() }
+ val isDialogFullWidth = LocalConfiguration.current.screenWidthDp.dp == maxWidth
+ val padding = if (isDialogFullWidth) 16.dp else 0.dp
- Dialog(onDismissRequest = { onCloseRequest(this@MaterialDialog) }) {
- DisposableEffect(Unit) {
- onDispose { resetDialog() }
- }
+ if (dialogState.showing) {
+ dialogState.dialogBackgroundColor = LocalElevationOverlay.current?.apply(
+ color = backgroundColor,
+ elevation = elevation
+ ) ?: MaterialTheme.colors.surface
+ Dialog(onDismissRequest = { onCloseRequest(dialogState) }) {
Surface(
modifier = Modifier
.fillMaxWidth()
@@ -217,13 +264,11 @@ class MaterialDialog(
) {
Layout(
content = {
- DialogButtons(
+ dialogScope.DialogButtonsLayout(
modifier = Modifier.layoutId("buttons"),
- dialogButtons = dialogButtons,
- dialog = this@MaterialDialog,
content = buttons
)
- Column(Modifier.layoutId("content")) { content() }
+ Column(Modifier.layoutId("content")) { content(dialogScope) }
}
) { measurables, constraints ->
val buttonsHeight =
@@ -235,7 +280,7 @@ class MaterialDialog(
val contentPlaceable = measurables[1].measure(
constraints.copy(
maxHeight = maxHeightPx - buttonsPlaceable.height,
- minHeight = 0
+ minHeight = 0,
)
)
@@ -249,7 +294,6 @@ class MaterialDialog(
}
}
}
- }
}
}
}
diff --git a/core/src/main/java/com/vanpra/composematerialdialogs/MaterialDialogButtons.kt b/core/src/main/java/com/vanpra/composematerialdialogs/MaterialDialogButtons.kt
index 06aae9c9..35479e37 100644
--- a/core/src/main/java/com/vanpra/composematerialdialogs/MaterialDialogButtons.kt
+++ b/core/src/main/java/com/vanpra/composematerialdialogs/MaterialDialogButtons.kt
@@ -38,10 +38,8 @@ internal enum class MaterialDialogButtonTypes(val testTag: String) {
* See [MaterialDialogButtons] for more information about the content
*/
@Composable
-internal fun DialogButtons(
+internal fun MaterialDialogScope.DialogButtonsLayout(
modifier: Modifier = Modifier,
- dialogButtons: MaterialDialogButtons,
- dialog: MaterialDialog,
content: @Composable MaterialDialogButtons.() -> Unit
) {
val interButtonPadding = with(LocalDensity.current) { 12.dp.toPx().toInt() }
@@ -54,7 +52,7 @@ internal fun DialogButtons(
modifier
.fillMaxWidth()
.padding(horizontal = 8.dp)
- .background(dialog.dialogBackgroundColor!!),
+ .background(dialogState.dialogBackgroundColor!!),
{ measurables, constraints ->
if (measurables.isEmpty()) {
@@ -112,7 +110,7 @@ internal fun DialogButtons(
* A class used to build a buttons layout for a MaterialDialog. This should be used in conjunction
* with the [com.vanpra.composematerialdialogs.MaterialDialog.dialogButtons] function
*/
-class MaterialDialogButtons(private val dialog: MaterialDialog) {
+class MaterialDialogButtons(private val scope: MaterialDialogScope) {
/**
* Adds a button which is always enabled to the bottom of the dialog. This should
* only be used for neutral actions.
@@ -159,16 +157,16 @@ class MaterialDialogButtons(private val dialog: MaterialDialog) {
onClick: () -> Unit = {}
) {
val buttonText = getString(res, text).uppercase(Locale.ROOT)
- val buttonEnabled = dialog.positiveEnabled.values.all { it }
+ val buttonEnabled = scope.positiveButtonEnabled.values.all { it }
val focusManager = LocalFocusManager.current
TextButton(
onClick = {
- if (dialog.isAutoDismiss() && !disableDismiss) {
- dialog.hide(focusManager)
+ if (scope.autoDismiss && !disableDismiss) {
+ scope.dialogState.hide(focusManager)
}
- dialog.callbacks.values.forEach {
+ scope.callbacks.values.forEach {
it()
}
@@ -176,7 +174,7 @@ class MaterialDialogButtons(private val dialog: MaterialDialog) {
},
modifier = Modifier.layoutId(MaterialDialogButtonTypes.Positive)
.testTag(MaterialDialogButtonTypes.Positive.testTag),
- enabled = buttonEnabled && dialog.positiveButtonEnabledOverride
+ enabled = buttonEnabled
) {
Text(text = buttonText, style = textStyle)
}
@@ -202,8 +200,8 @@ class MaterialDialogButtons(private val dialog: MaterialDialog) {
TextButton(
onClick = {
- if (dialog.isAutoDismiss()) {
- dialog.hide(focusManager)
+ if (scope.autoDismiss) {
+ scope.dialogState.hide(focusManager)
}
onClick()
},
diff --git a/core/src/main/java/com/vanpra/composematerialdialogs/MaterialDialogCore.kt b/core/src/main/java/com/vanpra/composematerialdialogs/MaterialDialogCore.kt
index 2f75a1d2..c01a2387 100644
--- a/core/src/main/java/com/vanpra/composematerialdialogs/MaterialDialogCore.kt
+++ b/core/src/main/java/com/vanpra/composematerialdialogs/MaterialDialogCore.kt
@@ -40,7 +40,7 @@ import androidx.compose.ui.unit.sp
* @param center text is aligned to center when true
*/
@Composable
-fun MaterialDialog.title(
+fun MaterialDialogScope.title(
text: String? = null,
@StringRes res: Int? = null,
center: Boolean = false
@@ -77,7 +77,7 @@ fun MaterialDialog.title(
* @param icon optional icon displayed at the start of the title
*/
@Composable
-fun MaterialDialog.iconTitle(
+fun MaterialDialogScope.iconTitle(
text: String? = null,
@StringRes textRes: Int? = null,
icon: @Composable () -> Unit = {},
@@ -105,7 +105,7 @@ fun MaterialDialog.iconTitle(
* @param res message text from a string resource
*/
@Composable
-fun MaterialDialog.message(text: String? = null, @StringRes res: Int? = null) {
+fun MaterialDialogScope.message(text: String? = null, @StringRes res: Int? = null) {
val messageText = getString(res, text)
Text(
@@ -137,7 +137,7 @@ fun MaterialDialog.message(text: String? = null, @StringRes res: Int? = null) {
* dictated by [waitForPositiveButton]
*/
@Composable
-fun MaterialDialog.input(
+fun MaterialDialogScope.input(
label: String,
hint: String = "",
prefill: String = "",
@@ -155,15 +155,10 @@ fun MaterialDialog.input(
val valid = remember(text) { isTextValid(text) }
val focusManager = LocalFocusManager.current
- val positiveEnabledIndex = addPositiveButtonEnabled(valid = valid) {
+ PositiveButtonEnabled(valid = valid) {
focusManager.clearFocus()
}
- DisposableEffect(valid) {
- setPositiveEnabled(positiveEnabledIndex, valid)
- onDispose { }
- }
-
if (waitForPositiveButton) {
DialogCallback { onInput(text) }
}
@@ -213,7 +208,7 @@ fun MaterialDialog.input(
* @param content the content of the custom view
*/
@Composable
-fun MaterialDialog.customView(content: @Composable () -> Unit) {
+fun MaterialDialogScope.customView(content: @Composable () -> Unit) {
Box(modifier = Modifier.padding(bottom = 28.dp, start = 24.dp, end = 24.dp)) {
content()
}
diff --git a/core/src/main/java/com/vanpra/composematerialdialogs/MaterialDialogLists.kt b/core/src/main/java/com/vanpra/composematerialdialogs/MaterialDialogLists.kt
index f5e97498..03e66c2a 100644
--- a/core/src/main/java/com/vanpra/composematerialdialogs/MaterialDialogLists.kt
+++ b/core/src/main/java/com/vanpra/composematerialdialogs/MaterialDialogLists.kt
@@ -39,7 +39,7 @@ private val bottomPadding = Modifier.padding(bottom = 8.dp)
* @param item a composable function which takes an object of given generic type
*/
@Composable
-fun MaterialDialog.listItems(
+fun MaterialDialogScope.listItems(
list: List,
closeOnClick: Boolean = true,
onClick: (index: Int, item: T) -> Unit = { _, _ -> },
@@ -56,7 +56,7 @@ fun MaterialDialog.listItems(
.clickable(
onClick = {
if (closeOnClick) {
- hide()
+ dialogState.hide()
}
onClick(index, it)
},
@@ -78,7 +78,7 @@ fun MaterialDialog.listItems(
* @param onClick callback with the index and string of an item when it is clicked
*/
@Composable
-fun MaterialDialog.listItems(
+fun MaterialDialogScope.listItems(
list: List,
closeOnClick: Boolean = true,
onClick: (index: Int, item: String) -> Unit = { _, _ -> }
@@ -107,7 +107,7 @@ fun MaterialDialog.listItems(
* this call is dictated by [waitForPositiveButton]
*/
@Composable
-fun MaterialDialog.listItemsMultiChoice(
+fun MaterialDialogScope.listItemsMultiChoice(
list: List,
disabledIndices: Set = setOf(),
initialSelection: Set = setOf(),
@@ -169,7 +169,7 @@ fun MaterialDialog.listItemsMultiChoice(
* The timing of this call is dictated by [waitForPositiveButton]
*/
@Composable
-fun MaterialDialog.listItemsSingleChoice(
+fun MaterialDialogScope.listItemsSingleChoice(
list: List,
disabledIndices: Set = setOf(),
initialSelection: Int? = null,
@@ -177,7 +177,7 @@ fun MaterialDialog.listItemsSingleChoice(
onChoiceChange: (selected: Int) -> Unit = {}
) {
var selectedItem by remember { mutableStateOf(initialSelection) }
- val positiveEnabledIndex = addPositiveButtonEnabled(valid = selectedItem != null)
+ PositiveButtonEnabled(valid = selectedItem != null) {}
if (waitForPositiveButton) {
DialogCallback { onChoiceChange(selectedItem!!) }
@@ -185,7 +185,6 @@ fun MaterialDialog.listItemsSingleChoice(
val onSelect = { index: Int ->
if (index !in disabledIndices) {
- setPositiveEnabled(positiveEnabledIndex, true)
selectedItem = index
if (!waitForPositiveButton) {
diff --git a/datetime/build.gradle.kts b/datetime/build.gradle.kts
index ac40f38a..3675ad78 100644
--- a/datetime/build.gradle.kts
+++ b/datetime/build.gradle.kts
@@ -8,8 +8,8 @@ android {
}
defaultConfig {
minSdk = 21
- targetSdk = 30
- compileSdk = 30
+ compileSdk = 31
+ targetSdk = 31
testInstrumentationRunner = "com.karumi.shot.ShotTestRunner"
testApplicationId = "com.vanpra.composematerialdialogs.test"
diff --git a/datetime/screenshots/debug/com.vanpra.composematerialdialogs.datetime.test.screenshot.DateTimePickerTest_datePickerWithCustomTitle.png b/datetime/screenshots/debug/com.vanpra.composematerialdialogs.datetime.test.screenshot.DateTimePickerTest_datePickerWithCustomTitle.png
index e3bee5fb..5bd5fb86 100644
Binary files a/datetime/screenshots/debug/com.vanpra.composematerialdialogs.datetime.test.screenshot.DateTimePickerTest_datePickerWithCustomTitle.png and b/datetime/screenshots/debug/com.vanpra.composematerialdialogs.datetime.test.screenshot.DateTimePickerTest_datePickerWithCustomTitle.png differ
diff --git a/datetime/src/androidTest/AndroidManifest.xml b/datetime/src/androidTest/AndroidManifest.xml
index d6eb7b9c..e9de7dc3 100644
--- a/datetime/src/androidTest/AndroidManifest.xml
+++ b/datetime/src/androidTest/AndroidManifest.xml
@@ -4,6 +4,7 @@
+ android:exported="true"
+ />
\ No newline at end of file
diff --git a/datetime/src/androidTest/java/com/vanpra/composematerialdialogs/datetime/test/functional/DatePickerTest.kt b/datetime/src/androidTest/java/com/vanpra/composematerialdialogs/datetime/test/functional/DatePickerTest.kt
index 62169b50..69b97fa2 100644
--- a/datetime/src/androidTest/java/com/vanpra/composematerialdialogs/datetime/test/functional/DatePickerTest.kt
+++ b/datetime/src/androidTest/java/com/vanpra/composematerialdialogs/datetime/test/functional/DatePickerTest.kt
@@ -10,7 +10,6 @@ import com.vanpra.composematerialdialogs.test.utils.defaultButtons
import com.vanpra.composematerialdialogs.test.utils.extensions.assertDialogDoesNotExist
import com.vanpra.composematerialdialogs.test.utils.extensions.onDialogDateSelector
import com.vanpra.composematerialdialogs.test.utils.extensions.onPositiveButton
-import com.vanpra.composematerialdialogs.title
import org.junit.Assert.assertEquals
import org.junit.Rule
import org.junit.Test
@@ -30,11 +29,11 @@ class DatePickerTest {
composeTestRule.setContent {
DialogWithContent(buttons = { defaultButtons() }) {
- datepicker(initialDate = testDate, waitForPositiveButton = true) {
- selectedDate = it
+ datepicker(initialDate = testDate, waitForPositiveButton = true) {
+ selectedDate = it
+ }
}
}
- }
composeTestRule.onDialogDateSelector(20).performClick()
assertEquals(null, selectedDate)
@@ -50,11 +49,11 @@ class DatePickerTest {
composeTestRule.setContent {
DialogWithContent(buttons = { defaultButtons() }) {
- datepicker(initialDate = testDate, waitForPositiveButton = false) {
- selectedDate = it
+ datepicker(initialDate = testDate, waitForPositiveButton = false) {
+ selectedDate = it
+ }
}
}
- }
composeTestRule.onDialogDateSelector(20).performClick()
composeTestRule.waitForIdle()
@@ -71,8 +70,8 @@ class DatePickerTest {
val title = "Custom Title"
composeTestRule.setContent {
DialogWithContent(buttons = { defaultButtons() }) {
- datepicker(title = title)
- }
+ datepicker(title = title)
+ }
}
composeTestRule.onNodeWithText(title).assertExists()
diff --git a/datetime/src/androidTest/java/com/vanpra/composematerialdialogs/datetime/test/functional/TimePickerTest.kt b/datetime/src/androidTest/java/com/vanpra/composematerialdialogs/datetime/test/functional/TimePickerTest.kt
index d14984a6..fca9c303 100644
--- a/datetime/src/androidTest/java/com/vanpra/composematerialdialogs/datetime/test/functional/TimePickerTest.kt
+++ b/datetime/src/androidTest/java/com/vanpra/composematerialdialogs/datetime/test/functional/TimePickerTest.kt
@@ -17,8 +17,8 @@ class TimePickerTest {
val title = "Custom Title"
composeTestRule.setContent {
DialogWithContent(buttons = { defaultButtons() }) {
- timepicker(title = title)
- }
+ timepicker(title = title)
+ }
}
composeTestRule.onNodeWithText(title).assertExists()
diff --git a/datetime/src/androidTest/java/com/vanpra/composematerialdialogs/datetime/test/screenshot/DateTimePickerTest.kt b/datetime/src/androidTest/java/com/vanpra/composematerialdialogs/datetime/test/screenshot/DateTimePickerTest.kt
index 50872d3a..d71344e8 100644
--- a/datetime/src/androidTest/java/com/vanpra/composematerialdialogs/datetime/test/screenshot/DateTimePickerTest.kt
+++ b/datetime/src/androidTest/java/com/vanpra/composematerialdialogs/datetime/test/screenshot/DateTimePickerTest.kt
@@ -16,7 +16,7 @@ class DateTimePickerTest : ScreenshotTest {
@get:Rule
val composeTestRule = createComposeRule()
- val testTitle = "Custom Title"
+ private val testTitle = "Custom Title"
@Test
fun datePickerBasic() {
@@ -55,7 +55,7 @@ class DateTimePickerTest : ScreenshotTest {
fun datePickerWithCustomTitle() {
composeTestRule.setContentAndWaitForIdle {
DialogWithContent {
- datepicker(title = testTitle)
+ datepicker(title = testTitle, initialDate = LocalDate.of(2021, 7, 27))
}
}
diff --git a/datetime/src/main/java/com/vanpra/composematerialdialogs/datetime/date/DatePicker.kt b/datetime/src/main/java/com/vanpra/composematerialdialogs/datetime/date/DatePicker.kt
index abc760cf..1f75aceb 100644
--- a/datetime/src/main/java/com/vanpra/composematerialdialogs/datetime/date/DatePicker.kt
+++ b/datetime/src/main/java/com/vanpra/composematerialdialogs/datetime/date/DatePicker.kt
@@ -55,7 +55,7 @@ import com.google.accompanist.pager.HorizontalPager
import com.google.accompanist.pager.PagerDefaults
import com.google.accompanist.pager.PagerState
import com.google.accompanist.pager.rememberPagerState
-import com.vanpra.composematerialdialogs.MaterialDialog
+import com.vanpra.composematerialdialogs.MaterialDialogScope
import com.vanpra.composematerialdialogs.datetime.util.isSmallDevice
import com.vanpra.composematerialdialogs.datetime.util.shortLocalName
import kotlinx.coroutines.launch
@@ -74,7 +74,7 @@ import java.util.Locale
* @param onDateChange callback with a LocalDateTime object when the user completes their input
*/
@Composable
-fun MaterialDialog.datepicker(
+fun MaterialDialogScope.datepicker(
initialDate: LocalDate = LocalDate.now(),
title: String = "SELECT DATE",
colors: DatePickerColors = DatePickerDefaults.colors(),
@@ -83,7 +83,7 @@ fun MaterialDialog.datepicker(
onDateChange: (LocalDate) -> Unit = {}
) {
val datePickerState = remember {
- DatePickerState(initialDate, colors, yearRange, dialogBackgroundColor!!)
+ DatePickerState(initialDate, colors, yearRange, dialogState.dialogBackgroundColor!!)
}
DatePickerImpl(title = title, state = datePickerState)
@@ -110,7 +110,7 @@ internal fun DatePickerImpl(title: String, state: DatePickerState) {
HorizontalPager(
state = pagerState,
verticalAlignment = Alignment.Top,
- flingBehavior = PagerDefaults.defaultPagerFlingConfig(
+ flingBehavior = PagerDefaults.rememberPagerFlingConfig(
state = pagerState,
snapAnimationSpec = spring(stiffness = 1000f)
)
@@ -129,7 +129,6 @@ internal fun DatePickerImpl(title: String, state: DatePickerState) {
androidx.compose.animation.AnimatedVisibility(
state.yearPickerShowing,
modifier = Modifier
- .fillMaxSize()
.zIndex(0.7f)
.clipToBounds(),
enter = slideInVertically({ -it }),
@@ -157,7 +156,9 @@ private fun YearPicker(
LazyVerticalGrid(
cells = GridCells.Fixed(3),
state = gridState,
- modifier = Modifier.background(state.dialogBackground)
+ modifier = Modifier
+ .background(state.dialogBackground)
+ .height(280.dp)
) {
itemsIndexed(state.yearRange.toList()) { _, item ->
val selected = remember { item == viewDate.year }
@@ -295,7 +296,11 @@ private fun CalendarViewHeader(
@Composable
private fun CalendarView(viewDate: LocalDate, state: DatePickerState) {
- Column(Modifier.padding(start = 12.dp, end = 12.dp).testTag("dialog_date_calendar")) {
+ Column(
+ Modifier
+ .padding(start = 12.dp, end = 12.dp)
+ .testTag("dialog_date_calendar")
+ ) {
DayOfWeekHeader()
val calendarDatesData = remember { getDates(viewDate) }
val datesList = remember { IntRange(1, calendarDatesData.second).toList() }
@@ -303,7 +308,7 @@ private fun CalendarView(viewDate: LocalDate, state: DatePickerState) {
viewDate.year == state.selected.year && viewDate.month == state.selected.month
}
- LazyVerticalGrid(cells = GridCells.Fixed(7)) {
+ LazyVerticalGrid(cells = GridCells.Fixed(7), modifier = Modifier.height(240.dp)) {
for (x in 0 until calendarDatesData.first) {
item { Box(Modifier.size(40.dp)) }
}
@@ -322,7 +327,12 @@ private fun CalendarView(viewDate: LocalDate, state: DatePickerState) {
}
@Composable
-private fun DateSelectionBox(date: Int, selected: Boolean, colors: DatePickerColors, onClick: () -> Unit) {
+private fun DateSelectionBox(
+ date: Int,
+ selected: Boolean,
+ colors: DatePickerColors,
+ onClick: () -> Unit
+) {
Box(
Modifier
.testTag("dialog_date_selection_$date")
diff --git a/datetime/src/main/java/com/vanpra/composematerialdialogs/datetime/time/TimePicker.kt b/datetime/src/main/java/com/vanpra/composematerialdialogs/datetime/time/TimePicker.kt
index a488902b..5b581515 100644
--- a/datetime/src/main/java/com/vanpra/composematerialdialogs/datetime/time/TimePicker.kt
+++ b/datetime/src/main/java/com/vanpra/composematerialdialogs/datetime/time/TimePicker.kt
@@ -48,7 +48,7 @@ import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
-import com.vanpra.composematerialdialogs.MaterialDialog
+import com.vanpra.composematerialdialogs.MaterialDialogScope
import com.vanpra.composematerialdialogs.datetime.util.getOffset
import com.vanpra.composematerialdialogs.datetime.util.isAM
import com.vanpra.composematerialdialogs.datetime.util.isSmallDevice
@@ -85,7 +85,7 @@ private data class SelectedOffset(
* @param onTimeChange callback with a LocalTime object when the user completes their input
*/
@Composable
-fun MaterialDialog.timepicker(
+fun MaterialDialogScope.timepicker(
initialTime: LocalTime = LocalTime.now().noSeconds(),
title: String = "SELECT TIME",
colors: TimePickerColors = TimePickerDefaults.colors(),
diff --git a/docs/ColorPicker.md b/docs/ColorPicker.md
index 94c182db..80454a4c 100644
--- a/docs/ColorPicker.md
+++ b/docs/ColorPicker.md
@@ -7,16 +7,15 @@
Here is an example of how to add a color picker to a dialog:
``` kotlin
-val dialog = remember { MaterialDialog() }
-
-dialog.build {
+val dialogState = rememberMaterialDialogState()
+MaterialDialog(dialogState = dialogState) {
...
colorPicker(colors = ColorPalette.Primary)
...
}
/* This should be called in an onClick or an Effect */
-dialog.show()
+dialogState.show()
```
`ColorPalette.Primary` is a predefined list of colors and can be replaced with a list of custom colors.
@@ -26,7 +25,7 @@ dialog.show()
Here is an example of how to add a color picker with sub colors to a dialog:
``` kotlin
-dialog.build {
+MaterialDialog(dialogState = dialogState) {
...
colorPicker(colors = ColorPalette.Primary, subColors = ColorPalette.PrimarySub)
...
@@ -40,7 +39,7 @@ The `subColors` parameter is passed in a list of list of colors which are show t
Here is an example of how to add a color picker with custom argb selector to a dialog:
``` kotlin
-dialog.build {
+MaterialDialog(dialogState = dialogState) {
...
colorPicker(colors = ColorPalette.Primary, allowCustomArgb = true)
...
diff --git a/docs/Core.md b/docs/Core.md
index 7eb4ea83..683ba920 100644
--- a/docs/Core.md
+++ b/docs/Core.md
@@ -7,9 +7,8 @@
Here is an example to make a basic dialog with a title and text:
```kotlin
-val dialog = remember { MaterialDialog() }
-
-dialog.build {
+val dialogState = rememberMaterialDialogState()
+MaterialDialog(dialogState = dialogState) {
title(text = "Use Location Services?")
message(res = R.string.location_dialog_message)
}
@@ -20,26 +19,16 @@ dialog.show()
The dialog is shown when the `dialog.show()` is called. The hard coded strings for all the components in the example above can be replaced with a string resource id.
-If the `autoDismiss` parameter of a `MaterialDialog` is set to false then the dialog will not close when the positive or negative buttons are clicked, allowing for flexibility in custom views.
-
-The `MaterialDialog` class also has a `onCancelRequest` parameter which is a callback which is used when the user clicks outside the dialog area. By default this parameter is set to hide the dialog.
-
Buttons can be added to the dialog by adding the `buttons` parameter:
```kotlin
-dialog.build(buttons = {
- positiveButton("Ok", onClick = {
- //Do Something
- })
-
- button("Later", onClick = {
- //Do Something
- })
-
- negativeButton("Cancel", onClick = {
- //Do Something
- })
-}) {
+MaterialDialog(
+ dialogState = dialogState,
+ buttons = {
+ positiveButton("Ok")
+ negativeButton("Cancel")
+ }
+) {
...
}
```
@@ -51,7 +40,7 @@ If the text of the buttons is too long to fit in one row the buttons will be aut
```kotlin
-dialog.build(buttons = { ... }) {
+MaterialDialog(dialogState = dialogState, buttons = { ... }) {
...
input(label = "Name", hint = "Jon Smith") { inputString ->
/* Do something */
@@ -69,7 +58,7 @@ In the code snippet above creates the dialog seen in the image (without the titl
Below is an example of a plain list dialog:
```kotlin
-dialog.build {
+MaterialDialog(dialogState = dialogState) {
...
listItems(listOf("Item 1", "Item 2", "Item 3"), onClick { index, item ->
//Do Something
@@ -88,7 +77,9 @@ val items = listOf(
TextIcon("Item 3", imageFromResource(R.drawable.item_three))
)
-dialog.build {
+...
+
+MaterialDialog(dialogState = dialogState) {
...
listItems(items, onClick { index, item ->
//Do Something
@@ -109,7 +100,7 @@ dialog.build {

```kotlin
-dialog.build {
+MaterialDialog(dialogState = dialogState, buttons = { ... }) {
listItemsSingleChoice(
list = listOf("Item 1", "Item 2", "Item 3"),
disabledIndices = listOf(1),
@@ -127,7 +118,7 @@ As seen in the code snippet above you can pass in a list of indices of items whi

```kotlin
-dialog.build {
+MaterialDialog(dialogState = dialogState, buttons = { ... }) {
listItemsSingleChoice(
list = listOf("Item 1", "Item 2", "Item 3"),
disabledIndices = listOf(1),
@@ -143,7 +134,7 @@ As seen in the code snippet above you can select which items will be disabled (i
### Custom View
```kotlin
-dialog.build {
+MaterialDialog(dialogState = dialogState, buttons = { ... }) {
customView {
//Define your view here
}
@@ -157,10 +148,10 @@ The custom view provides a box with the appropriate material design padding's a
The background colour of the dialog can be changed by using the `backgroundColor` parameter of the build function:
```kotlin
-dialog.build(backgroundColor = Color.Red) {
+MaterialDialog(dialogState = dialogState, buttons = { ... }, backgroundColor = Color.Red) {
title(text = "Use Location Services?")
message(res = R.string.location_dialog_message)
}
```
-The shape of the dialog is dictated by the `MaterialTheme.shapes.medium` parameter.
+The shape of the dialog is defaulted to the `MaterialTheme.shapes.medium` property but can also be set using the `shape` parameter in `MaterialDialog` .
diff --git a/docs/DateTimePicker.md b/docs/DateTimePicker.md
index ee1026d3..c8d0ce26 100644
--- a/docs/DateTimePicker.md
+++ b/docs/DateTimePicker.md
@@ -33,11 +33,14 @@ Note, this only has to be done if you intend to target an Android API level < 26

```kotlin
-val dialog = remember { MaterialDialog() }
-dialog.build(buttons = {
- positiveButton("Ok")
- negativeButton("Cancel")
-}) {
+val dialogState = rememberMaterialDialogState()
+MaterialDialog(
+ dialogState = dialogState,
+ buttons = {
+ positiveButton("Ok")
+ negativeButton("Cancel")
+ }
+) {
...
datepicker { date ->
// Do stuff with java.time.LocalDate object which is passed in
@@ -53,10 +56,14 @@ dialog.show()

```kotlin
-dialog.build(buttons = {
- positiveButton("Ok")
- negativeButton("Cancel")
-}) {
+val dialogState = rememberMaterialDialogState()
+MaterialDialog(
+ dialogState = dialogState,
+ buttons = {
+ positiveButton("Ok")
+ negativeButton("Cancel")
+ }
+) {
...
timepicker { time ->
// Do stuff with java.time.LocalTime object which is passed in
diff --git a/docs/index.md b/docs/index.md
index 00b9c0e8..df4a652c 100644
--- a/docs/index.md
+++ b/docs/index.md
@@ -11,7 +11,7 @@
```gradle
dependencies {
...
- implementation "io.github.vanpra.compose-material-dialogs:core:0.5.1"
+ implementation "io.github.vanpra.compose-material-dialogs:core:0.5.2"
...
}
```
@@ -27,7 +27,7 @@ dependencies {
```gradle
dependencies {
...
- implementation "io.github.vanpra.compose-material-dialogs:datetime:0.5.1"
+ implementation "io.github.vanpra.compose-material-dialogs:datetime:0.5.2"
...
}
```
@@ -43,7 +43,7 @@ dependencies {
```gradle
dependencies {
...
- implementation "io.github.vanpra.compose-material-dialogs:color:0.5.1"
+ implementation "io.github.vanpra.compose-material-dialogs:color:0.5.2"
...
}
```
diff --git a/gradle.properties b/gradle.properties
index 79990d48..8e095b84 100644
--- a/gradle.properties
+++ b/gradle.properties
@@ -1,7 +1,7 @@
org.gradle.jvmargs=-Xmx4096m -Dfile.encoding=UTF-8
org.gradle.configureondemand=true
org.gradle.caching=true
-org.gradle.parallel=true
+org.gradle.parallel=false
org.gradle.daemon=true
android.useAndroidX=true
@@ -10,7 +10,7 @@ android.enableJetifier=false
kotlin.code.style=official
GROUP=io.github.vanpra.compose-material-dialogs
-VERSION_NAME=0.5.1
+VERSION_NAME=0.6.0
POM_DESCRIPTION=A Material Dialog Builder for Jetpack Compose
POM_INCEPTION_YEAR=2020
diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties
index 0159e263..c4910396 100644
--- a/gradle/wrapper/gradle-wrapper.properties
+++ b/gradle/wrapper/gradle-wrapper.properties
@@ -1,6 +1,6 @@
#Fri Feb 05 10:11:42 GMT 2021
distributionBase=GRADLE_USER_HOME
-distributionUrl=https\://services.gradle.org/distributions/gradle-7.1-bin.zip
+distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-bin.zip
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
diff --git a/test-utils/build.gradle.kts b/test-utils/build.gradle.kts
index f463e179..ef7f3961 100644
--- a/test-utils/build.gradle.kts
+++ b/test-utils/build.gradle.kts
@@ -6,8 +6,8 @@ plugins {
android {
defaultConfig {
minSdk = 21
+ compileSdk = 31
targetSdk = 30
- compileSdk = 30
testInstrumentationRunner = "com.karumi.shot.ShotTestRunner"
testApplicationId = "com.vanpra.composematerialdialogs.test"
@@ -51,9 +51,6 @@ android {
dependencies {
api(project(":core"))
implementation(Dependencies.Kotlin.stdlib)
- implementation(Dependencies.AndroidX.coreKtx)
- implementation(Dependencies.AndroidX.appcompat)
- implementation(Dependencies.material)
implementation(Dependencies.AndroidX.Compose.ui)
implementation(Dependencies.AndroidX.Compose.material)
@@ -61,5 +58,5 @@ dependencies {
implementation(Dependencies.AndroidX.Compose.activity)
implementation(Dependencies.AndroidX.Compose.testing)
- implementation(Dependencies.Shot.shot)
+ implementation(Dependencies.Shot.android)
}
diff --git a/test-utils/src/main/java/com/vanpra/composematerialdialogs/test/utils/DialogTestUtil.kt b/test-utils/src/main/java/com/vanpra/composematerialdialogs/test/utils/DialogTestUtil.kt
index be484362..794b9afc 100644
--- a/test-utils/src/main/java/com/vanpra/composematerialdialogs/test/utils/DialogTestUtil.kt
+++ b/test-utils/src/main/java/com/vanpra/composematerialdialogs/test/utils/DialogTestUtil.kt
@@ -1,27 +1,24 @@
package com.vanpra.composematerialdialogs.test.utils
-import androidx.compose.foundation.layout.Box
-import androidx.compose.foundation.layout.fillMaxSize
-import androidx.compose.material.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.SideEffect
-import androidx.compose.ui.Modifier
import com.vanpra.composematerialdialogs.MaterialDialog
import com.vanpra.composematerialdialogs.MaterialDialogButtons
+import com.vanpra.composematerialdialogs.MaterialDialogScope
+import com.vanpra.composematerialdialogs.MaterialDialogState
+import com.vanpra.composematerialdialogs.rememberMaterialDialogState
@Composable
fun DialogWithContent(
autoDismiss: Boolean = true,
- dialog: MaterialDialog = MaterialDialog(autoDismiss),
+ dialogState: MaterialDialogState = rememberMaterialDialogState(true),
buttons: @Composable MaterialDialogButtons.() -> Unit = {},
- content: @Composable MaterialDialog.() -> Unit = {}
+ content: @Composable MaterialDialogScope.() -> Unit = {}
) {
- MaterialTheme {
- Box(Modifier.fillMaxSize()) {
- dialog.build(buttons = buttons) { content() }
- SideEffect { dialog.show() }
- }
+ MaterialDialog(dialogState = dialogState, buttons = buttons, autoDismiss = autoDismiss) {
+ content()
}
+ SideEffect { dialogState.show() }
}
@Composable