Add alerts module
This commit is contained in:
parent
b2b9caa4d3
commit
2b0fd79f88
|
|
@ -0,0 +1 @@
|
|||
/build
|
||||
|
|
@ -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
|
||||
```
|
||||
|
|
@ -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'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
<manifest
|
||||
package="ru.touchin.roboswag.alerts"/>
|
||||
|
|
@ -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<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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
@ -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<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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,69 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<androidx.constraintlayout.widget.ConstraintLayout
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:paddingTop="22dp"
|
||||
android:paddingBottom="8dp">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/alert_title"
|
||||
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"
|
||||
tools:text="Header" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/alert_message"
|
||||
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"
|
||||
tools:text="Text" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/alert_positive_button"
|
||||
style="@style/DialogButton"
|
||||
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"
|
||||
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"
|
||||
app:layout_constraintTop_toBottomOf="@id/alert_message"
|
||||
tools:text="Cancel" />
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<string name="positive_btn">OK</string>
|
||||
<string name="negative_btn">Cancel</string>
|
||||
</resources>
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<style name="DialogButton">
|
||||
<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>
|
||||
</style>
|
||||
</resources>
|
||||
Loading…
Reference in New Issue