From 2b0fd79f8826b94356c9cd7e24bda4c8784f197d Mon Sep 17 00:00:00 2001 From: tonlirise Date: Wed, 10 Aug 2022 12:13:50 +0700 Subject: [PATCH 01/11] Add alerts module --- alerts/.gitignore | 1 + alerts/README.md | 55 +++++++++++++++ alerts/build.gradle | 40 +++++++++++ alerts/src/main/AndroidManifest.xml | 2 + .../ComposableAlertDialog.kt | 63 +++++++++++++++++ .../viewable_dialog/AlertDialogUtils.kt | 24 +++++++ .../viewable_dialog/ViewableAlertDialog.kt | 63 +++++++++++++++++ alerts/src/main/res/layout/dialog_alert.xml | 69 +++++++++++++++++++ alerts/src/main/res/values/strings.xml | 5 ++ alerts/src/main/res/values/styles.xml | 11 +++ 10 files changed, 333 insertions(+) create mode 100644 alerts/.gitignore create mode 100644 alerts/README.md create mode 100644 alerts/build.gradle create mode 100644 alerts/src/main/AndroidManifest.xml create mode 100644 alerts/src/main/java/ru/touchin/roboswag/composable dialog/ComposableAlertDialog.kt create mode 100644 alerts/src/main/java/ru/touchin/roboswag/viewable_dialog/AlertDialogUtils.kt create mode 100644 alerts/src/main/java/ru/touchin/roboswag/viewable_dialog/ViewableAlertDialog.kt create mode 100644 alerts/src/main/res/layout/dialog_alert.xml create mode 100644 alerts/src/main/res/values/strings.xml create mode 100644 alerts/src/main/res/values/styles.xml diff --git a/alerts/.gitignore b/alerts/.gitignore new file mode 100644 index 0000000..796b96d --- /dev/null +++ b/alerts/.gitignore @@ -0,0 +1 @@ +/build diff --git a/alerts/README.md b/alerts/README.md new file mode 100644 index 0000000..b4d5aa5 --- /dev/null +++ b/alerts/README.md @@ -0,0 +1,55 @@ +alerts +===== + +### Общее описание + +Модуль содержит: +`ViewableAlertDialog` - служит для демонстрации AlertDialog с использованием View, необходимо вызвать метод `showAlertDialog`, который +в качестве агруметов может принимать: +* title - Заголовок диалога +* message - дополнительное сообщение +* positiveButtonText - текст правой кнопки (по умолчанию "ОК") +* onPositiveAction - колбэк при нажатии на правую кнопку +* negativeBtnTitle - текст левой кнопки (по умолчаннию null - в этом случаи не отображается) +* onNegativeAction - колбэк при нажатии на левую кнопку, +* dialogLayout - id кастомного layout (по умолчанию R.layout.dialog_alert) + +--- +`ComposableAlertDialog` - служит для демонстрации AlertDialog с использованием Jetpack Compose, необходимо вызвать метод `ShowAlertDialog`, который +в качестве агруметов может принимать: +* isDialogOpen - индикатор состояния диалога +* title - Заголовок диалога +* message - дополнительное сообщение +* positiveButtonText - текст правой кнопки +* onPositiveAction - колбэк при нажатии на правую кнопку +* negativeBtnTitle - текст левой кнопки (по умолчаннию null - в этом случаи не отображается) +* onNegativeAction - колбэк при нажатии на левую кнопку, + +Кастомизация Compose версии происходит по средствам инициализации полей: customTitle, customMessage, customConfirmBtn, customNegativeBtn + +### Примеры + +View версия (ViewableAlertDialog): +```kotlin +ViewableAlertDialog.showAlertDialog( + activity, + title = "Ой, что-то пошло не так", + message = "Попробуйте ещё раз", + positiveButtonText = "Ещё раз", + onPositiveAction = { retryConnection() }, + negativeBtnTitle = "Отмена" + ) +``` + +Compose версия (ComposableAlertDialog): +```kotlin +val isDialogOpen = remember { mutableStateOf(false)} +.... +//Создание диалога +ComposableAlertDialog + .apply { customTitle = { Text(text = "Ой, что-то пошло не так", color = Color.Blue) } } + .ShowAlertDialog(isDialogOpen, message = "Проблемы с сетью", positiveButtonText = "ОК") +.... +//Отображение диалога +isDialogOpen.value = true +``` \ No newline at end of file diff --git a/alerts/build.gradle b/alerts/build.gradle new file mode 100644 index 0000000..dba3f27 --- /dev/null +++ b/alerts/build.gradle @@ -0,0 +1,40 @@ +apply from: "../android-configs/lib-config.gradle" + +ext { + composeVersion = '1.0.5' +} + +android { + buildFeatures { + viewBinding true + } +} + +dependencies { + implementation("androidx.core:core-ktx") + implementation("androidx.constraintlayout:constraintlayout") + implementation project(":kotlin-extensions") + + implementation "androidx.compose.runtime:runtime:1.1.1" + implementation "androidx.compose.ui:ui:1.1.1" + implementation "androidx.compose.foundation:foundation:1.1.1" + implementation "androidx.compose.foundation:foundation-layout:1.1.1" + implementation "androidx.compose.material:material:1.1.1" + implementation "androidx.compose.runtime:runtime-livedata:1.1.1" + implementation "androidx.compose.ui:ui-tooling:1.1.1" + implementation "com.google.android.material:compose-theme-adapter:1.1.9" + + constraints { + implementation("androidx.core:core-ktx") { + version { + require '1.0.0' + } + } + + implementation("androidx.constraintlayout:constraintlayout") { + version { + require '2.2.0-alpha03' + } + } + } +} diff --git a/alerts/src/main/AndroidManifest.xml b/alerts/src/main/AndroidManifest.xml new file mode 100644 index 0000000..80ea08a --- /dev/null +++ b/alerts/src/main/AndroidManifest.xml @@ -0,0 +1,2 @@ + diff --git a/alerts/src/main/java/ru/touchin/roboswag/composable dialog/ComposableAlertDialog.kt b/alerts/src/main/java/ru/touchin/roboswag/composable dialog/ComposableAlertDialog.kt new file mode 100644 index 0000000..7ae6011 --- /dev/null +++ b/alerts/src/main/java/ru/touchin/roboswag/composable dialog/ComposableAlertDialog.kt @@ -0,0 +1,63 @@ +package com.ptut.statehandlingcompose + +import androidx.compose.material.AlertDialog +import androidx.compose.material.Text +import androidx.compose.material.TextButton +import androidx.compose.runtime.Composable +import androidx.compose.runtime.MutableState + +object ComposableAlertDialog { + var customTitle: @Composable (() -> Unit)? = null + var customMessage: @Composable (() -> Unit)? = null + var customConfirmBtn: @Composable (() -> Unit)? = null + var customNegativeBtn: @Composable (() -> Unit)? = null + + @Composable + fun ShowAlertDialog( + isDialogOpen: MutableState, + title: String? = null, + message: String? = null, + positiveButtonText: String? = null, + onPositiveAction: (() -> Unit)? = null, + negativeBtnTitle: String? = null, + onNegativeAction: (() -> Unit)? = null + ) { + if (isDialogOpen.value) { + AlertDialog( + onDismissRequest = { isDialogOpen.value = false }, + + title = customTitle ?: { Text(title.orEmpty()) }, + text = customMessage ?: { Text(message.orEmpty()) }, + + confirmButton = customConfirmBtn ?: { + TextButton( + onClick = { + onPositiveAction?.invoke() + isDialogOpen.value = false + } + ) { + Text(positiveButtonText.orEmpty()) + } + }, + + dismissButton = when { + customNegativeBtn != null -> customNegativeBtn + else -> { + negativeBtnTitle?.let { positiveText -> + { + TextButton( + onClick = { + onNegativeAction?.invoke() + isDialogOpen.value = false + } + ) { + Text(positiveText) + } + } + } + } + } + ) + } + } +} diff --git a/alerts/src/main/java/ru/touchin/roboswag/viewable_dialog/AlertDialogUtils.kt b/alerts/src/main/java/ru/touchin/roboswag/viewable_dialog/AlertDialogUtils.kt new file mode 100644 index 0000000..6664c1a --- /dev/null +++ b/alerts/src/main/java/ru/touchin/roboswag/viewable_dialog/AlertDialogUtils.kt @@ -0,0 +1,24 @@ +package ru.touchin.roboswag.alerts.dialog_view + +import android.app.AlertDialog +import android.widget.TextView +import androidx.core.view.isVisible +import ru.touchin.extensions.setOnRippleClickListener + +fun setupButton(alertDialog: AlertDialog, buttonView: TextView, text: String?, onButtonClick: (() -> Unit)?) { + buttonView.setTextOrGone(text) + buttonView.setOnRippleClickListener { + onButtonClick?.invoke() + alertDialog.dismiss() + } +} + +fun TextView.setTextOrGone(text: CharSequence?) { + if (!text.isNullOrEmpty()) { + isVisible = true + setText(text) + } else { + isVisible = false + setText(null) + } +} diff --git a/alerts/src/main/java/ru/touchin/roboswag/viewable_dialog/ViewableAlertDialog.kt b/alerts/src/main/java/ru/touchin/roboswag/viewable_dialog/ViewableAlertDialog.kt new file mode 100644 index 0000000..0ff2d76 --- /dev/null +++ b/alerts/src/main/java/ru/touchin/roboswag/viewable_dialog/ViewableAlertDialog.kt @@ -0,0 +1,63 @@ +package ru.touchin.roboswag.alerts + +import android.app.AlertDialog +import android.content.Context +import android.view.LayoutInflater +import android.widget.TextView +import ru.touchin.roboswag.alerts.dialog_view.setTextOrGone +import ru.touchin.roboswag.alerts.dialog_view.setupButton + +object ViewableAlertDialog { + + fun showAlertDialog( + context: Context, + title: String? = null, + message: String? = null, + positiveButtonText: String = context.getString(R.string.positive_btn), + onPositiveAction: (() -> Unit)? = null, + negativeBtnTitle: String? = null, + onNegativeAction: (() -> Unit)? = null, + dialogLayout: Int = R.layout.dialog_alert, + cancelable: Boolean = true, + onCancelAction: () -> Unit = {} + ) { + AlertDialog.Builder(context) + .setView(LayoutInflater.from(context).inflate(dialogLayout, null)) + .show() + .apply { + setupAlertDialog( + dialog = this, + title = title, + message = message, + positiveButtonText = positiveButtonText, + onPositiveClick = onPositiveAction, + negativeButtonText = negativeBtnTitle, + onNegativeClick = onNegativeAction, + cancelable = cancelable, + onCancelAction = onCancelAction) + } + } + + private fun setupAlertDialog( + dialog: AlertDialog, + title: String? = null, + message: String? = null, + positiveButtonText: String, + onPositiveClick: (() -> Unit)? = null, + negativeButtonText: String? = null, + onNegativeClick: (() -> Unit)? = null, + cancelable: Boolean = true, + onCancelAction: () -> Unit = {} + ) { + dialog.setCancelable(cancelable) + dialog.setOnDismissListener { onCancelAction() } + dialog.findViewById(R.id.alert_title)?.setTextOrGone(title) + dialog.findViewById(R.id.alert_message)?.setTextOrGone(message) + dialog.findViewById(R.id.alert_positive_button)?.let { buttonView -> + setupButton(dialog, buttonView, positiveButtonText, onPositiveClick) + } + dialog.findViewById(R.id.alert_negative_button)?.let { buttonView -> + setupButton(dialog, buttonView, negativeButtonText, onNegativeClick) + } + } +} diff --git a/alerts/src/main/res/layout/dialog_alert.xml b/alerts/src/main/res/layout/dialog_alert.xml new file mode 100644 index 0000000..2d8797e --- /dev/null +++ b/alerts/src/main/res/layout/dialog_alert.xml @@ -0,0 +1,69 @@ + + + + + + + + + + + + diff --git a/alerts/src/main/res/values/strings.xml b/alerts/src/main/res/values/strings.xml new file mode 100644 index 0000000..319d46c --- /dev/null +++ b/alerts/src/main/res/values/strings.xml @@ -0,0 +1,5 @@ + + + OK + Cancel + diff --git a/alerts/src/main/res/values/styles.xml b/alerts/src/main/res/values/styles.xml new file mode 100644 index 0000000..6999012 --- /dev/null +++ b/alerts/src/main/res/values/styles.xml @@ -0,0 +1,11 @@ + + + + From d7cf93a47143f26ae037dbb63787837d105f6895 Mon Sep 17 00:00:00 2001 From: tonlirise Date: Wed, 10 Aug 2022 14:42:57 +0700 Subject: [PATCH 02/11] Update compose version init --- alerts/build.gradle | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/alerts/build.gradle b/alerts/build.gradle index dba3f27..9c438b5 100644 --- a/alerts/build.gradle +++ b/alerts/build.gradle @@ -1,7 +1,7 @@ apply from: "../android-configs/lib-config.gradle" ext { - composeVersion = '1.0.5' + composeVersion = '1.1.1' } android { @@ -15,13 +15,13 @@ dependencies { implementation("androidx.constraintlayout:constraintlayout") implementation project(":kotlin-extensions") - implementation "androidx.compose.runtime:runtime:1.1.1" - implementation "androidx.compose.ui:ui:1.1.1" - implementation "androidx.compose.foundation:foundation:1.1.1" - implementation "androidx.compose.foundation:foundation-layout:1.1.1" - implementation "androidx.compose.material:material:1.1.1" - implementation "androidx.compose.runtime:runtime-livedata:1.1.1" - implementation "androidx.compose.ui:ui-tooling:1.1.1" + implementation "androidx.compose.runtime:runtime:$composeVersion" + implementation "androidx.compose.ui:ui:$composeVersion" + implementation "androidx.compose.foundation:foundation:$composeVersion" + implementation "androidx.compose.foundation:foundation-layout:$composeVersion" + implementation "androidx.compose.material:material:$composeVersion" + implementation "androidx.compose.runtime:runtime-livedata:$composeVersion" + implementation "androidx.compose.ui:ui-tooling:$composeVersion" implementation "com.google.android.material:compose-theme-adapter:1.1.9" constraints { From 0dbbe3f6f0d2de544b7864ed54dc436510f10341 Mon Sep 17 00:00:00 2001 From: AnastasiyaK97 Date: Tue, 30 Aug 2022 16:10:53 +0300 Subject: [PATCH 03/11] small fixes and add styles --- alerts/README.md | 28 +++++- alerts/build.gradle | 7 ++ .../ComposableAlertDialog.kt | 63 -------------- .../ComposableAlertDialog.kt | 63 ++++++++++++++ .../viewable_dialog/AlertDialogManager.kt | 87 +++++++++++++++++++ .../viewable_dialog/AlertDialogUtils.kt | 5 +- .../viewable_dialog/ViewableAlertDialog.kt | 63 -------------- alerts/src/main/res/layout/dialog_alert.xml | 20 +---- alerts/src/main/res/values/styles.xml | 30 ++++++- 9 files changed, 216 insertions(+), 150 deletions(-) delete mode 100644 alerts/src/main/java/ru/touchin/roboswag/composable dialog/ComposableAlertDialog.kt create mode 100644 alerts/src/main/java/ru/touchin/roboswag/composable_dialog/ComposableAlertDialog.kt create mode 100644 alerts/src/main/java/ru/touchin/roboswag/viewable_dialog/AlertDialogManager.kt delete mode 100644 alerts/src/main/java/ru/touchin/roboswag/viewable_dialog/ViewableAlertDialog.kt diff --git a/alerts/README.md b/alerts/README.md index b4d5aa5..edb2100 100644 --- a/alerts/README.md +++ b/alerts/README.md @@ -4,8 +4,10 @@ alerts ### Общее описание Модуль содержит: -`ViewableAlertDialog` - служит для демонстрации AlertDialog с использованием View, необходимо вызвать метод `showAlertDialog`, который +`AlertDialogManager` - служит для демонстрации AlertDialog с использованием View, необходимо вызвать метод `showAlertDialog`, который в качестве агруметов может принимать: +* context +* style - стиль для элементов дефолтного диалога (по умолчанию R.style.AlertDialogDefault) * title - Заголовок диалога * message - дополнительное сообщение * positiveButtonText - текст правой кнопки (по умолчанию "ОК") @@ -29,9 +31,9 @@ alerts ### Примеры -View версия (ViewableAlertDialog): +View версия (ViewableAlertDialog) ok/cancel диалога: ```kotlin -ViewableAlertDialog.showAlertDialog( +alertDialogManager.showAlertDialog( activity, title = "Ой, что-то пошло не так", message = "Попробуйте ещё раз", @@ -41,6 +43,24 @@ ViewableAlertDialog.showAlertDialog( ) ``` +View версия (ViewableAlertDialog) ok диалога: +```kotlin +alertDialogManager.showOkDialog( + dialog?.window?.decorView?.context ?: throw Exception(), + title = "Необходимо изменить настройки", + okButtonText = "Ок", + onOkAction = { + viewModel.dispatchAction(ItemAction.ChangeSettings) + } +) +``` + +Для катомизации стилей элементов в дефолтной разметке диалога необходимо создать стиль - наследника от `ThemeOverlay.MaterialComponents.MaterialAlertDialog` и переопределить стили: +* materialAlertDialogTitleTextStyle - стиль для заголока (наследник от `MaterialAlertDialog.MaterialComponents.Title.Text`) +* materialAlertDialogBodyTextStyle - стиль для подзаголовка (наследник от `MaterialAlertDialog.MaterialComponents.Body.Text`) +* buttonBarPositiveButtonStyle - стиль для позитивной кнопки (наследник от `Widget.MaterialComponents.Button.TextButton.Dialog`) +* buttonBarNegativeButtonStyle - стиль для негативной кнопки (наследник от `Widget.MaterialComponents.Button.TextButton.Dialog`) + Compose версия (ComposableAlertDialog): ```kotlin val isDialogOpen = remember { mutableStateOf(false)} @@ -52,4 +72,4 @@ ComposableAlertDialog .... //Отображение диалога isDialogOpen.value = true -``` \ No newline at end of file +``` diff --git a/alerts/build.gradle b/alerts/build.gradle index 9c438b5..6c08de1 100644 --- a/alerts/build.gradle +++ b/alerts/build.gradle @@ -13,6 +13,7 @@ android { dependencies { implementation("androidx.core:core-ktx") implementation("androidx.constraintlayout:constraintlayout") + implementation("com.google.android.material:material") implementation project(":kotlin-extensions") implementation "androidx.compose.runtime:runtime:$composeVersion" @@ -36,5 +37,11 @@ dependencies { require '2.2.0-alpha03' } } + + implementation("com.google.android.material:material") { + version { + require '1.1.0' + } + } } } diff --git a/alerts/src/main/java/ru/touchin/roboswag/composable dialog/ComposableAlertDialog.kt b/alerts/src/main/java/ru/touchin/roboswag/composable dialog/ComposableAlertDialog.kt deleted file mode 100644 index 7ae6011..0000000 --- a/alerts/src/main/java/ru/touchin/roboswag/composable dialog/ComposableAlertDialog.kt +++ /dev/null @@ -1,63 +0,0 @@ -package com.ptut.statehandlingcompose - -import androidx.compose.material.AlertDialog -import androidx.compose.material.Text -import androidx.compose.material.TextButton -import androidx.compose.runtime.Composable -import androidx.compose.runtime.MutableState - -object ComposableAlertDialog { - var customTitle: @Composable (() -> Unit)? = null - var customMessage: @Composable (() -> Unit)? = null - var customConfirmBtn: @Composable (() -> Unit)? = null - var customNegativeBtn: @Composable (() -> Unit)? = null - - @Composable - fun ShowAlertDialog( - isDialogOpen: MutableState, - title: String? = null, - message: String? = null, - positiveButtonText: String? = null, - onPositiveAction: (() -> Unit)? = null, - negativeBtnTitle: String? = null, - onNegativeAction: (() -> Unit)? = null - ) { - if (isDialogOpen.value) { - AlertDialog( - onDismissRequest = { isDialogOpen.value = false }, - - title = customTitle ?: { Text(title.orEmpty()) }, - text = customMessage ?: { Text(message.orEmpty()) }, - - confirmButton = customConfirmBtn ?: { - TextButton( - onClick = { - onPositiveAction?.invoke() - isDialogOpen.value = false - } - ) { - Text(positiveButtonText.orEmpty()) - } - }, - - dismissButton = when { - customNegativeBtn != null -> customNegativeBtn - else -> { - negativeBtnTitle?.let { positiveText -> - { - TextButton( - onClick = { - onNegativeAction?.invoke() - isDialogOpen.value = false - } - ) { - Text(positiveText) - } - } - } - } - } - ) - } - } -} diff --git a/alerts/src/main/java/ru/touchin/roboswag/composable_dialog/ComposableAlertDialog.kt b/alerts/src/main/java/ru/touchin/roboswag/composable_dialog/ComposableAlertDialog.kt new file mode 100644 index 0000000..64ff4c6 --- /dev/null +++ b/alerts/src/main/java/ru/touchin/roboswag/composable_dialog/ComposableAlertDialog.kt @@ -0,0 +1,63 @@ +package ru.touchin.roboswag.composable_dialog + +import androidx.compose.material.AlertDialog +import androidx.compose.material.Text +import androidx.compose.material.TextButton +import androidx.compose.runtime.Composable +import androidx.compose.runtime.MutableState + +object ComposableAlertDialog { + var customTitle: @Composable (() -> Unit)? = null + var customMessage: @Composable (() -> Unit)? = null + var customConfirmBtn: @Composable (() -> Unit)? = null + var customNegativeBtn: @Composable (() -> Unit)? = null + + @Composable + fun ShowAlertDialog( + isDialogOpen: MutableState, + title: String? = null, + message: String? = null, + positiveButtonText: String? = null, + onPositiveAction: (() -> Unit)? = null, + negativeBtnTitle: String? = null, + onNegativeAction: (() -> Unit)? = null + ) { + if (isDialogOpen.value) { + AlertDialog( + onDismissRequest = { isDialogOpen.value = false }, + + title = customTitle ?: { Text(title.orEmpty()) }, + text = customMessage ?: { Text(message.orEmpty()) }, + + confirmButton = customConfirmBtn ?: { + TextButton( + onClick = { + onPositiveAction?.invoke() + isDialogOpen.value = false + } + ) { + Text(positiveButtonText.orEmpty()) + } + }, + + dismissButton = when { + customNegativeBtn != null -> customNegativeBtn + else -> { + negativeBtnTitle?.let { positiveText -> + { + TextButton( + onClick = { + onNegativeAction?.invoke() + isDialogOpen.value = false + } + ) { + Text(positiveText) + } + } + } + } + } + ) + } + } +} diff --git a/alerts/src/main/java/ru/touchin/roboswag/viewable_dialog/AlertDialogManager.kt b/alerts/src/main/java/ru/touchin/roboswag/viewable_dialog/AlertDialogManager.kt new file mode 100644 index 0000000..e2f847c --- /dev/null +++ b/alerts/src/main/java/ru/touchin/roboswag/viewable_dialog/AlertDialogManager.kt @@ -0,0 +1,87 @@ +package ru.touchin.roboswag.viewable_dialog + +import android.content.Context +import android.view.LayoutInflater +import android.widget.TextView +import androidx.appcompat.view.ContextThemeWrapper +import com.google.android.material.dialog.MaterialAlertDialogBuilder +import ru.touchin.roboswag.alerts.R + +class AlertDialogManager { + + fun showAlertDialog( + context: Context, + style: Int = R.style.AlertDialogDefault, + title: String? = null, + message: String? = null, + positiveButtonText: String = context.getString(R.string.positive_btn), + onPositiveAction: (() -> Unit)? = null, + negativeBtnTitle: String? = null, + onNegativeAction: (() -> Unit)? = null, + dialogLayout: Int = R.layout.dialog_alert, + cancelable: Boolean = true, + onCancelAction: () -> Unit = {} + ) { + val styledContext = ContextThemeWrapper(context, style) + MaterialAlertDialogBuilder(styledContext) + .setView(LayoutInflater.from(styledContext).inflate(dialogLayout, null)) + .show() + .apply { + setupAlertDialog( + dialog = this, + title = title, + message = message, + positiveButtonText = positiveButtonText, + onPositiveClick = onPositiveAction, + negativeButtonText = negativeBtnTitle, + onNegativeClick = onNegativeAction, + cancelable = cancelable, + onCancelAction = onCancelAction + ) + } + } + + fun showOkDialog( + context: Context, + style: Int = R.style.AlertDialogDefault, + title: String? = null, + message: String? = null, + okButtonText: String = context.getString(R.string.positive_btn), + onOkAction: (() -> Unit)? = null, + cancelable: Boolean = true, + onCancelAction: () -> Unit = {} + ) = showAlertDialog( + context = context, + style = style, + title = title, + message = message, + positiveButtonText = okButtonText, + onPositiveAction = onOkAction, + cancelable = cancelable, + onCancelAction = onCancelAction + ) + + + private fun setupAlertDialog( + dialog: androidx.appcompat.app.AlertDialog, + title: String? = null, + message: String? = null, + positiveButtonText: String, + onPositiveClick: (() -> Unit)? = null, + negativeButtonText: String? = null, + onNegativeClick: (() -> Unit)? = null, + cancelable: Boolean = true, + onCancelAction: () -> Unit = {} + ) { + dialog.setCancelable(cancelable) + dialog.setOnDismissListener { onCancelAction() } + dialog.findViewById(R.id.alert_title)?.setTextOrGone(title) + dialog.findViewById(R.id.alert_message)?.setTextOrGone(message) + dialog.findViewById(R.id.alert_positive_button)?.let { buttonView -> + setupButton(dialog, buttonView, positiveButtonText, onPositiveClick) + } + dialog.findViewById(R.id.alert_negative_button)?.let { buttonView -> + setupButton(dialog, buttonView, negativeButtonText, onNegativeClick) + } + } +} diff --git a/alerts/src/main/java/ru/touchin/roboswag/viewable_dialog/AlertDialogUtils.kt b/alerts/src/main/java/ru/touchin/roboswag/viewable_dialog/AlertDialogUtils.kt index 6664c1a..340b3da 100644 --- a/alerts/src/main/java/ru/touchin/roboswag/viewable_dialog/AlertDialogUtils.kt +++ b/alerts/src/main/java/ru/touchin/roboswag/viewable_dialog/AlertDialogUtils.kt @@ -1,11 +1,10 @@ -package ru.touchin.roboswag.alerts.dialog_view +package ru.touchin.roboswag.viewable_dialog -import android.app.AlertDialog import android.widget.TextView import androidx.core.view.isVisible import ru.touchin.extensions.setOnRippleClickListener -fun setupButton(alertDialog: AlertDialog, buttonView: TextView, text: String?, onButtonClick: (() -> Unit)?) { +fun setupButton(alertDialog: androidx.appcompat.app.AlertDialog, buttonView: TextView, text: String?, onButtonClick: (() -> Unit)?) { buttonView.setTextOrGone(text) buttonView.setOnRippleClickListener { onButtonClick?.invoke() diff --git a/alerts/src/main/java/ru/touchin/roboswag/viewable_dialog/ViewableAlertDialog.kt b/alerts/src/main/java/ru/touchin/roboswag/viewable_dialog/ViewableAlertDialog.kt deleted file mode 100644 index 0ff2d76..0000000 --- a/alerts/src/main/java/ru/touchin/roboswag/viewable_dialog/ViewableAlertDialog.kt +++ /dev/null @@ -1,63 +0,0 @@ -package ru.touchin.roboswag.alerts - -import android.app.AlertDialog -import android.content.Context -import android.view.LayoutInflater -import android.widget.TextView -import ru.touchin.roboswag.alerts.dialog_view.setTextOrGone -import ru.touchin.roboswag.alerts.dialog_view.setupButton - -object ViewableAlertDialog { - - fun showAlertDialog( - context: Context, - title: String? = null, - message: String? = null, - positiveButtonText: String = context.getString(R.string.positive_btn), - onPositiveAction: (() -> Unit)? = null, - negativeBtnTitle: String? = null, - onNegativeAction: (() -> Unit)? = null, - dialogLayout: Int = R.layout.dialog_alert, - cancelable: Boolean = true, - onCancelAction: () -> Unit = {} - ) { - AlertDialog.Builder(context) - .setView(LayoutInflater.from(context).inflate(dialogLayout, null)) - .show() - .apply { - setupAlertDialog( - dialog = this, - title = title, - message = message, - positiveButtonText = positiveButtonText, - onPositiveClick = onPositiveAction, - negativeButtonText = negativeBtnTitle, - onNegativeClick = onNegativeAction, - cancelable = cancelable, - onCancelAction = onCancelAction) - } - } - - private fun setupAlertDialog( - dialog: AlertDialog, - title: String? = null, - message: String? = null, - positiveButtonText: String, - onPositiveClick: (() -> Unit)? = null, - negativeButtonText: String? = null, - onNegativeClick: (() -> Unit)? = null, - cancelable: Boolean = true, - onCancelAction: () -> Unit = {} - ) { - dialog.setCancelable(cancelable) - dialog.setOnDismissListener { onCancelAction() } - dialog.findViewById(R.id.alert_title)?.setTextOrGone(title) - dialog.findViewById(R.id.alert_message)?.setTextOrGone(message) - dialog.findViewById(R.id.alert_positive_button)?.let { buttonView -> - setupButton(dialog, buttonView, positiveButtonText, onPositiveClick) - } - dialog.findViewById(R.id.alert_negative_button)?.let { buttonView -> - setupButton(dialog, buttonView, negativeButtonText, onNegativeClick) - } - } -} diff --git a/alerts/src/main/res/layout/dialog_alert.xml b/alerts/src/main/res/layout/dialog_alert.xml index 2d8797e..6aec6ce 100644 --- a/alerts/src/main/res/layout/dialog_alert.xml +++ b/alerts/src/main/res/layout/dialog_alert.xml @@ -10,13 +10,9 @@ - + + + + + + From 613d7241f86a4d2d29131853b5afe10929860bdc Mon Sep 17 00:00:00 2001 From: AnastasiyaK97 Date: Thu, 15 Sep 2022 16:59:41 +0300 Subject: [PATCH 04/11] update README --- alerts/README.md | 56 ++++++++++++++++++++++++------------------------ 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/alerts/README.md b/alerts/README.md index edb2100..6f17075 100644 --- a/alerts/README.md +++ b/alerts/README.md @@ -6,26 +6,26 @@ alerts Модуль содержит: `AlertDialogManager` - служит для демонстрации AlertDialog с использованием View, необходимо вызвать метод `showAlertDialog`, который в качестве агруметов может принимать: -* context -* style - стиль для элементов дефолтного диалога (по умолчанию R.style.AlertDialogDefault) -* title - Заголовок диалога -* message - дополнительное сообщение -* positiveButtonText - текст правой кнопки (по умолчанию "ОК") -* onPositiveAction - колбэк при нажатии на правую кнопку -* negativeBtnTitle - текст левой кнопки (по умолчаннию null - в этом случаи не отображается) -* onNegativeAction - колбэк при нажатии на левую кнопку, -* dialogLayout - id кастомного layout (по умолчанию R.layout.dialog_alert) +* `context`, +* `style` - стиль для элементов дефолтного диалога (по умолчанию R.style.AlertDialogDefault), +* `title` - Заголовок диалога, +* `message` - дополнительное сообщение, +* `positiveButtonText` - текст правой кнопки (по умолчанию "ОК") , +* `onPositiveAction` - колбэк при нажатии на правую кнопку, +* `negativeBtnTitle` - текст левой кнопки (по умолчаннию null - в этом случаи не отображается), +* `onNegativeAction` - колбэк при нажатии на левую кнопку, +* `dialogLayout` - id кастомного layout (по умолчанию R.layout.dialog_alert). --- `ComposableAlertDialog` - служит для демонстрации AlertDialog с использованием Jetpack Compose, необходимо вызвать метод `ShowAlertDialog`, который в качестве агруметов может принимать: -* isDialogOpen - индикатор состояния диалога -* title - Заголовок диалога -* message - дополнительное сообщение -* positiveButtonText - текст правой кнопки -* onPositiveAction - колбэк при нажатии на правую кнопку -* negativeBtnTitle - текст левой кнопки (по умолчаннию null - в этом случаи не отображается) -* onNegativeAction - колбэк при нажатии на левую кнопку, +* `isDialogOpen` - индикатор состояния диалога, +* `title` - Заголовок диалога, +* `message` - дополнительное сообщение, +* `positiveButtonText` - текст правой кнопки, +* `onPositiveAction` - колбэк при нажатии на правую кнопку, +* `negativeBtnTitle` - текст левой кнопки (по умолчаннию null - в этом случаи не отображается), +* `onNegativeAction` - колбэк при нажатии на левую кнопку. Кастомизация Compose версии происходит по средствам инициализации полей: customTitle, customMessage, customConfirmBtn, customNegativeBtn @@ -34,19 +34,19 @@ alerts View версия (ViewableAlertDialog) ok/cancel диалога: ```kotlin alertDialogManager.showAlertDialog( - activity, - title = "Ой, что-то пошло не так", - message = "Попробуйте ещё раз", - positiveButtonText = "Ещё раз", - onPositiveAction = { retryConnection() }, - negativeBtnTitle = "Отмена" - ) + context = activity, + title = "Ой, что-то пошло не так", + message = "Попробуйте ещё раз", + positiveButtonText = "Ещё раз", + onPositiveAction = { retryConnection() }, + negativeBtnTitle = "Отмена" +) ``` View версия (ViewableAlertDialog) ok диалога: ```kotlin alertDialogManager.showOkDialog( - dialog?.window?.decorView?.context ?: throw Exception(), + context = dialog?.window?.decorView?.context ?: throw Exception(), title = "Необходимо изменить настройки", okButtonText = "Ок", onOkAction = { @@ -56,10 +56,10 @@ alertDialogManager.showOkDialog( ``` Для катомизации стилей элементов в дефолтной разметке диалога необходимо создать стиль - наследника от `ThemeOverlay.MaterialComponents.MaterialAlertDialog` и переопределить стили: -* materialAlertDialogTitleTextStyle - стиль для заголока (наследник от `MaterialAlertDialog.MaterialComponents.Title.Text`) -* materialAlertDialogBodyTextStyle - стиль для подзаголовка (наследник от `MaterialAlertDialog.MaterialComponents.Body.Text`) -* buttonBarPositiveButtonStyle - стиль для позитивной кнопки (наследник от `Widget.MaterialComponents.Button.TextButton.Dialog`) -* buttonBarNegativeButtonStyle - стиль для негативной кнопки (наследник от `Widget.MaterialComponents.Button.TextButton.Dialog`) +* `materialAlertDialogTitleTextStyle` - стиль для заголока (наследник от `MaterialAlertDialog.MaterialComponents.Title.Text`), +* `materialAlertDialogBodyTextStyle` - стиль для подзаголовка (наследник от `MaterialAlertDialog.MaterialComponents.Body.Text`), +* `buttonBarPositiveButtonStyle` - стиль для позитивной кнопки (наследник от `Widget.MaterialComponents.Button.TextButton.Dialog`), +* `buttonBarNegativeButtonStyle` - стиль для негативной кнопки (наследник от `Widget.MaterialComponents.Button.TextButton.Dialog`). Compose версия (ComposableAlertDialog): ```kotlin From c1474b546cc7b6ce66d65ff538eb1d1d24ed2fda Mon Sep 17 00:00:00 2001 From: AnastasiyaK97 Date: Thu, 15 Sep 2022 18:14:29 +0300 Subject: [PATCH 05/11] update AlertDialogManagers --- .../ComposableAlertDialog.kt | 61 ++++++++----------- .../viewable_dialog/AlertDialogManager.kt | 45 +++++++------- 2 files changed, 47 insertions(+), 59 deletions(-) diff --git a/alerts/src/main/java/ru/touchin/roboswag/composable_dialog/ComposableAlertDialog.kt b/alerts/src/main/java/ru/touchin/roboswag/composable_dialog/ComposableAlertDialog.kt index 64ff4c6..e8d6c3f 100644 --- a/alerts/src/main/java/ru/touchin/roboswag/composable_dialog/ComposableAlertDialog.kt +++ b/alerts/src/main/java/ru/touchin/roboswag/composable_dialog/ComposableAlertDialog.kt @@ -22,42 +22,33 @@ object ComposableAlertDialog { negativeBtnTitle: String? = null, onNegativeAction: (() -> Unit)? = null ) { - if (isDialogOpen.value) { - AlertDialog( - onDismissRequest = { isDialogOpen.value = false }, + if (!isDialogOpen.value) return - title = customTitle ?: { Text(title.orEmpty()) }, - text = customMessage ?: { Text(message.orEmpty()) }, - - confirmButton = customConfirmBtn ?: { - TextButton( - onClick = { - onPositiveAction?.invoke() - isDialogOpen.value = false - } - ) { - Text(positiveButtonText.orEmpty()) - } - }, - - dismissButton = when { - customNegativeBtn != null -> customNegativeBtn - else -> { - negativeBtnTitle?.let { positiveText -> - { - TextButton( - onClick = { - onNegativeAction?.invoke() - isDialogOpen.value = false - } - ) { - Text(positiveText) - } - } - } - } + AlertDialog( + onDismissRequest = { isDialogOpen.value = false }, + title = customTitle ?: { Text(title.orEmpty()) }, + text = customMessage ?: { Text(message.orEmpty()) }, + confirmButton = customConfirmBtn ?: createButton(positiveButtonText.orEmpty()) { + onPositiveAction?.invoke() + isDialogOpen.value = false + }, + dismissButton = when { + customNegativeBtn != null -> customNegativeBtn + negativeBtnTitle != null -> createButton(negativeBtnTitle) { + onNegativeAction?.invoke() + isDialogOpen.value = false } - ) - } + else -> null + } + ) } + + @Composable + private fun createButton(text: String, onClickAction: () -> Unit): @Composable (() -> Unit) = + { + TextButton(onClick = onClickAction) { + Text(text) + } + } + } diff --git a/alerts/src/main/java/ru/touchin/roboswag/viewable_dialog/AlertDialogManager.kt b/alerts/src/main/java/ru/touchin/roboswag/viewable_dialog/AlertDialogManager.kt index e2f847c..8111d1d 100644 --- a/alerts/src/main/java/ru/touchin/roboswag/viewable_dialog/AlertDialogManager.kt +++ b/alerts/src/main/java/ru/touchin/roboswag/viewable_dialog/AlertDialogManager.kt @@ -3,6 +3,7 @@ package ru.touchin.roboswag.viewable_dialog import android.content.Context import android.view.LayoutInflater import android.widget.TextView +import androidx.appcompat.app.AlertDialog import androidx.appcompat.view.ContextThemeWrapper import com.google.android.material.dialog.MaterialAlertDialogBuilder import ru.touchin.roboswag.alerts.R @@ -26,19 +27,17 @@ class AlertDialogManager { MaterialAlertDialogBuilder(styledContext) .setView(LayoutInflater.from(styledContext).inflate(dialogLayout, null)) .show() - .apply { - setupAlertDialog( - dialog = this, - title = title, - message = message, - positiveButtonText = positiveButtonText, - onPositiveClick = onPositiveAction, - negativeButtonText = negativeBtnTitle, - onNegativeClick = onNegativeAction, - cancelable = cancelable, - onCancelAction = onCancelAction - ) - } + .setupAlertDialog( + title = title, + message = message, + positiveButtonText = positiveButtonText, + onPositiveClick = onPositiveAction, + negativeButtonText = negativeBtnTitle, + onNegativeClick = onNegativeAction, + cancelable = cancelable, + onCancelAction = onCancelAction + ) + } fun showOkDialog( @@ -61,9 +60,7 @@ class AlertDialogManager { onCancelAction = onCancelAction ) - - private fun setupAlertDialog( - dialog: androidx.appcompat.app.AlertDialog, + private fun AlertDialog.setupAlertDialog( title: String? = null, message: String? = null, positiveButtonText: String, @@ -73,15 +70,15 @@ class AlertDialogManager { cancelable: Boolean = true, onCancelAction: () -> Unit = {} ) { - dialog.setCancelable(cancelable) - dialog.setOnDismissListener { onCancelAction() } - dialog.findViewById(R.id.alert_title)?.setTextOrGone(title) - dialog.findViewById(R.id.alert_message)?.setTextOrGone(message) - dialog.findViewById(R.id.alert_positive_button)?.let { buttonView -> - setupButton(dialog, buttonView, positiveButtonText, onPositiveClick) + setCancelable(cancelable) + setOnDismissListener { onCancelAction() } + findViewById(R.id.alert_title)?.setTextOrGone(title) + findViewById(R.id.alert_message)?.setTextOrGone(message) + findViewById(R.id.alert_positive_button)?.let { buttonView -> + setupButton(this, buttonView, positiveButtonText, onPositiveClick) } - dialog.findViewById(R.id.alert_negative_button)?.let { buttonView -> - setupButton(dialog, buttonView, negativeButtonText, onNegativeClick) + findViewById(R.id.alert_negative_button)?.let { buttonView -> + setupButton(this, buttonView, negativeButtonText, onNegativeClick) } } } From c905682e78ccc5fc676e30432dd49c7182329682 Mon Sep 17 00:00:00 2001 From: AnastasiyaK97 Date: Thu, 15 Sep 2022 18:17:40 +0300 Subject: [PATCH 06/11] update AlertDialogUtils --- .../touchin/roboswag/viewable_dialog/AlertDialogUtils.kt | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/alerts/src/main/java/ru/touchin/roboswag/viewable_dialog/AlertDialogUtils.kt b/alerts/src/main/java/ru/touchin/roboswag/viewable_dialog/AlertDialogUtils.kt index 340b3da..d6e9a8d 100644 --- a/alerts/src/main/java/ru/touchin/roboswag/viewable_dialog/AlertDialogUtils.kt +++ b/alerts/src/main/java/ru/touchin/roboswag/viewable_dialog/AlertDialogUtils.kt @@ -13,11 +13,6 @@ fun setupButton(alertDialog: androidx.appcompat.app.AlertDialog, buttonView: Tex } fun TextView.setTextOrGone(text: CharSequence?) { - if (!text.isNullOrEmpty()) { - isVisible = true - setText(text) - } else { - isVisible = false - setText(null) - } + isVisible = !text.isNullOrEmpty() + setText(text) } From 67fa1ed8c63a54bf0d85fb837c9ceb474f289f46 Mon Sep 17 00:00:00 2001 From: Anadol <74777850+AnadolStudio@users.noreply.github.com> Date: Thu, 15 Sep 2022 19:30:01 +0300 Subject: [PATCH 07/11] Update README.md --- alerts/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/alerts/README.md b/alerts/README.md index 6f17075..b967056 100644 --- a/alerts/README.md +++ b/alerts/README.md @@ -1,4 +1,4 @@ -alerts +Alerts ===== ### Общее описание From 5766535891b86776bb9c2a7f69c2125b2e4863d6 Mon Sep 17 00:00:00 2001 From: Anadol <74777850+AnadolStudio@users.noreply.github.com> Date: Thu, 15 Sep 2022 19:30:39 +0300 Subject: [PATCH 08/11] Update AlertDialogManager.kt --- .../ru/touchin/roboswag/viewable_dialog/AlertDialogManager.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/alerts/src/main/java/ru/touchin/roboswag/viewable_dialog/AlertDialogManager.kt b/alerts/src/main/java/ru/touchin/roboswag/viewable_dialog/AlertDialogManager.kt index 8111d1d..01ee58a 100644 --- a/alerts/src/main/java/ru/touchin/roboswag/viewable_dialog/AlertDialogManager.kt +++ b/alerts/src/main/java/ru/touchin/roboswag/viewable_dialog/AlertDialogManager.kt @@ -24,6 +24,7 @@ class AlertDialogManager { onCancelAction: () -> Unit = {} ) { val styledContext = ContextThemeWrapper(context, style) + MaterialAlertDialogBuilder(styledContext) .setView(LayoutInflater.from(styledContext).inflate(dialogLayout, null)) .show() @@ -37,7 +38,6 @@ class AlertDialogManager { cancelable = cancelable, onCancelAction = onCancelAction ) - } fun showOkDialog( From b1f7580d6923eca6744b98da856b5f919eb698cb Mon Sep 17 00:00:00 2001 From: Anadol <74777850+AnadolStudio@users.noreply.github.com> Date: Thu, 15 Sep 2022 19:31:59 +0300 Subject: [PATCH 09/11] Update README.md --- alerts/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/alerts/README.md b/alerts/README.md index b967056..61cca56 100644 --- a/alerts/README.md +++ b/alerts/README.md @@ -10,7 +10,7 @@ Alerts * `style` - стиль для элементов дефолтного диалога (по умолчанию R.style.AlertDialogDefault), * `title` - Заголовок диалога, * `message` - дополнительное сообщение, -* `positiveButtonText` - текст правой кнопки (по умолчанию "ОК") , +* `positiveButtonText` - текст правой кнопки (по умолчанию "ОК"), * `onPositiveAction` - колбэк при нажатии на правую кнопку, * `negativeBtnTitle` - текст левой кнопки (по умолчаннию null - в этом случаи не отображается), * `onNegativeAction` - колбэк при нажатии на левую кнопку, From 5e3636d5e7780033535ea76296a97f99f0fa29af Mon Sep 17 00:00:00 2001 From: AnastasiyaK97 Date: Fri, 16 Sep 2022 10:56:48 +0300 Subject: [PATCH 10/11] small fixes --- .../ru/touchin/roboswag/viewable_dialog/AlertDialogManager.kt | 1 + .../ru/touchin/roboswag/viewable_dialog/AlertDialogUtils.kt | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/alerts/src/main/java/ru/touchin/roboswag/viewable_dialog/AlertDialogManager.kt b/alerts/src/main/java/ru/touchin/roboswag/viewable_dialog/AlertDialogManager.kt index 01ee58a..b78824f 100644 --- a/alerts/src/main/java/ru/touchin/roboswag/viewable_dialog/AlertDialogManager.kt +++ b/alerts/src/main/java/ru/touchin/roboswag/viewable_dialog/AlertDialogManager.kt @@ -10,6 +10,7 @@ import ru.touchin.roboswag.alerts.R class AlertDialogManager { + @SuppressWarnings("detekt.LongParameterList") fun showAlertDialog( context: Context, style: Int = R.style.AlertDialogDefault, diff --git a/alerts/src/main/java/ru/touchin/roboswag/viewable_dialog/AlertDialogUtils.kt b/alerts/src/main/java/ru/touchin/roboswag/viewable_dialog/AlertDialogUtils.kt index d6e9a8d..07cd8d4 100644 --- a/alerts/src/main/java/ru/touchin/roboswag/viewable_dialog/AlertDialogUtils.kt +++ b/alerts/src/main/java/ru/touchin/roboswag/viewable_dialog/AlertDialogUtils.kt @@ -1,10 +1,11 @@ package ru.touchin.roboswag.viewable_dialog import android.widget.TextView +import androidx.appcompat.app.AlertDialog import androidx.core.view.isVisible import ru.touchin.extensions.setOnRippleClickListener -fun setupButton(alertDialog: androidx.appcompat.app.AlertDialog, buttonView: TextView, text: String?, onButtonClick: (() -> Unit)?) { +fun setupButton(alertDialog: AlertDialog, buttonView: TextView, text: String?, onButtonClick: (() -> Unit)?) { buttonView.setTextOrGone(text) buttonView.setOnRippleClickListener { onButtonClick?.invoke() From dc0c902e836a89e9089500fe7eeb9546f2c75a6f Mon Sep 17 00:00:00 2001 From: AnastasiyaK97 Date: Fri, 16 Sep 2022 11:41:07 +0300 Subject: [PATCH 11/11] fix detekt --- .../ru/touchin/roboswag/viewable_dialog/AlertDialogManager.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/alerts/src/main/java/ru/touchin/roboswag/viewable_dialog/AlertDialogManager.kt b/alerts/src/main/java/ru/touchin/roboswag/viewable_dialog/AlertDialogManager.kt index b78824f..b6df3fd 100644 --- a/alerts/src/main/java/ru/touchin/roboswag/viewable_dialog/AlertDialogManager.kt +++ b/alerts/src/main/java/ru/touchin/roboswag/viewable_dialog/AlertDialogManager.kt @@ -25,7 +25,7 @@ class AlertDialogManager { onCancelAction: () -> Unit = {} ) { val styledContext = ContextThemeWrapper(context, style) - + MaterialAlertDialogBuilder(styledContext) .setView(LayoutInflater.from(styledContext).inflate(dialogLayout, null)) .show()