small fixes and add styles

This commit is contained in:
AnastasiyaK97 2022-08-30 16:10:53 +03:00 committed by Anastasiya97
parent d7cf93a471
commit 0dbbe3f6f0
9 changed files with 216 additions and 150 deletions

View File

@ -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
```
```

View File

@ -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'
}
}
}
}

View File

@ -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<Boolean>,
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)
}
}
}
}
}
)
}
}
}

View File

@ -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<Boolean>,
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)
}
}
}
}
}
)
}
}
}

View File

@ -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<TextView>(R.id.alert_title)?.setTextOrGone(title)
dialog.findViewById<TextView>(R.id.alert_message)?.setTextOrGone(message)
dialog.findViewById<TextView>(R.id.alert_positive_button)?.let { buttonView ->
setupButton(dialog, buttonView, positiveButtonText, onPositiveClick)
}
dialog.findViewById<TextView>(R.id.alert_negative_button)?.let { buttonView ->
setupButton(dialog, buttonView, negativeButtonText, onNegativeClick)
}
}
}

View File

@ -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()

View File

@ -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<TextView>(R.id.alert_title)?.setTextOrGone(title)
dialog.findViewById<TextView>(R.id.alert_message)?.setTextOrGone(message)
dialog.findViewById<TextView>(R.id.alert_positive_button)?.let { buttonView ->
setupButton(dialog, buttonView, positiveButtonText, onPositiveClick)
}
dialog.findViewById<TextView>(R.id.alert_negative_button)?.let { buttonView ->
setupButton(dialog, buttonView, negativeButtonText, onNegativeClick)
}
}
}

View File

@ -10,13 +10,9 @@
<TextView
android:id="@+id/alert_title"
style="?attr/materialAlertDialogTitleTextStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:paddingBottom="16dp"
android:textColor="@android:color/black"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
@ -24,14 +20,10 @@
<TextView
android:id="@+id/alert_message"
style="?attr/materialAlertDialogBodyTextStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/alert_title"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:lineSpacingExtra="8dp"
android:paddingBottom="28dp"
android:textSize="16sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/alert_title"
@ -39,27 +31,23 @@
<TextView
android:id="@+id/alert_positive_button"
style="@style/DialogButton"
style="?attr/buttonBarPositiveButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/alert_message"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginTop="8dp"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/alert_message"
tools:text="OK" />
<TextView
android:id="@+id/alert_negative_button"
style="@style/DialogButton"
style="?attr/buttonBarNegativeButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/alert_message"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_toStartOf="@id/alert_positive_button"
android:layout_toLeftOf="@id/alert_positive_button"
app:layout_constraintRight_toLeftOf="@id/alert_positive_button"

View File

@ -1,11 +1,39 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="DialogButton">
<style name="AlertDialogDefault" parent="ThemeOverlay.MaterialComponents.MaterialAlertDialog">
<item name="colorSurface">#FFFFFF</item>
<item name="materialAlertDialogTitleTextStyle">@style/MaterialAlertDialog.MaterialComponents.Title.Text.Default</item>
<item name="buttonBarPositiveButtonStyle">@style/MaterialAlertDialog.MaterialComponents.Button.Default</item>
<item name="buttonBarNegativeButtonStyle">@style/MaterialAlertDialog.MaterialComponents.Button.Default</item>
<item name="materialAlertDialogBodyTextStyle">@style/MaterialAlertDialog.MaterialComponents.Body.Text.Default</item>
</style>
<style name="MaterialAlertDialog.MaterialComponents.Title.Text.Default" parent="MaterialAlertDialog.MaterialComponents.Title.Text">
<item name="android:textColor">#383838</item>
<item name="android:textSize">15sp</item>
<item name="android:layout_marginLeft">24dp</item>
<item name="android:layout_marginRight">24dp</item>
<item name="android:paddingBottom">16dp</item>
</style>
<style name="MaterialAlertDialog.MaterialComponents.Body.Text.Default" parent="MaterialAlertDialog.MaterialComponents.Body.Text">
<item name="android:textColor">#383838</item>
<item name="android:textSize">12sp</item>
<item name="android:layout_marginLeft">24dp</item>
<item name="android:layout_marginRight">24dp</item>
<item name="android:paddingBottom">28dp</item>
<item name="android:lineSpacingExtra">8dp</item>
</style>
<style name="MaterialAlertDialog.MaterialComponents.Button.Default" parent="Widget.MaterialComponents.Button.TextButton.Dialog">
<item name="android:textColor">#383838</item>
<item name="android:minWidth">56dp</item>
<item name="android:gravity">center</item>
<item name="android:textAllCaps">true</item>
<item name="android:textSize">14sp</item>
<item name="android:paddingTop">11dp</item>
<item name="android:paddingBottom">9dp</item>
<item name="android:layout_marginTop">8dp</item>
<item name="android:layout_marginRight">8dp</item>
</style>
</resources>