forked from melod1n/fast-messenger
remove old material dialog
This commit is contained in:
@@ -26,18 +26,13 @@ import androidx.compose.runtime.derivedStateOf
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.saveable.rememberSaveable
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.window.DialogProperties
|
||||
import com.meloda.app.fast.common.model.UiText
|
||||
import com.meloda.app.fast.common.model.parseString
|
||||
import com.meloda.app.fast.ui.util.ImmutableList
|
||||
import com.meloda.app.fast.ui.util.ImmutableList.Companion.toImmutableList
|
||||
import com.meloda.app.fast.ui.util.getString
|
||||
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
@Composable
|
||||
@@ -241,220 +236,6 @@ fun MaterialDialog(
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: 08.04.2023, Danil Nikolaev: remove this
|
||||
@Deprecated("need refactoring")
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
@Composable
|
||||
fun MaterialDialog(
|
||||
onDismissAction: (() -> Unit),
|
||||
title: UiText? = null,
|
||||
text: UiText? = null,
|
||||
confirmText: UiText? = null,
|
||||
confirmAction: (() -> Unit)? = null,
|
||||
cancelText: UiText? = null,
|
||||
cancelAction: (() -> Unit)? = null,
|
||||
neutralText: UiText? = null,
|
||||
neutralAction: (() -> Unit)? = null,
|
||||
selectionType: SelectionType = SelectionType.None,
|
||||
preSelectedItems: ImmutableList<Int> = ImmutableList.empty(),
|
||||
items: ImmutableList<UiText> = ImmutableList.empty(),
|
||||
onItemClick: ((index: Int) -> Unit)? = null,
|
||||
buttonsInvokeDismiss: Boolean = true,
|
||||
properties: DialogProperties = DialogProperties(),
|
||||
customContent: (@Composable ColumnScope.() -> Unit)? = null,
|
||||
) {
|
||||
var isVisible by rememberSaveable {
|
||||
mutableStateOf(true)
|
||||
}
|
||||
val onDismissRequest = remember {
|
||||
{
|
||||
onDismissAction.invoke()
|
||||
isVisible = false
|
||||
}
|
||||
}
|
||||
|
||||
val context = LocalContext.current
|
||||
|
||||
val stringTitles = items.mapNotNull { it.parseString(context.resources) }
|
||||
|
||||
var alertItems by remember {
|
||||
mutableStateOf(
|
||||
stringTitles.mapIndexed { index, title ->
|
||||
DialogItem(
|
||||
title,
|
||||
preSelectedItems.contains(index)
|
||||
)
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
if (isVisible) {
|
||||
BasicAlertDialog(
|
||||
onDismissRequest = onDismissRequest,
|
||||
properties = properties
|
||||
) {
|
||||
val scrollState = rememberScrollState()
|
||||
val canScrollBackward by remember { derivedStateOf { scrollState.value > 0 } }
|
||||
val canScrollForward by remember { derivedStateOf { scrollState.value < scrollState.maxValue } }
|
||||
|
||||
Surface(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
color = AlertDialogDefaults.containerColor,
|
||||
shape = AlertDialogDefaults.shape,
|
||||
tonalElevation = AlertDialogDefaults.TonalElevation
|
||||
) {
|
||||
Column(modifier = Modifier.padding(bottom = 10.dp)) {
|
||||
val stringTitle = title?.getString()
|
||||
if (stringTitle != null) {
|
||||
Spacer(modifier = Modifier.height(20.dp))
|
||||
}
|
||||
|
||||
Row {
|
||||
stringTitle?.let { title ->
|
||||
Spacer(modifier = Modifier.width(24.dp))
|
||||
Text(
|
||||
modifier = Modifier.weight(1f),
|
||||
text = title,
|
||||
style = MaterialTheme.typography.headlineSmall
|
||||
)
|
||||
Spacer(modifier = Modifier.width(20.dp))
|
||||
}
|
||||
}
|
||||
|
||||
if (canScrollBackward) {
|
||||
HorizontalDivider(modifier = Modifier.fillMaxWidth())
|
||||
}
|
||||
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.weight(1f, fill = false)
|
||||
.verticalScroll(scrollState)
|
||||
) {
|
||||
Spacer(modifier = Modifier.height(8.dp))
|
||||
|
||||
val stringMessage = text?.getString()
|
||||
if (stringMessage != null && stringTitle == null) {
|
||||
Spacer(modifier = Modifier.height(20.dp))
|
||||
}
|
||||
|
||||
Row {
|
||||
stringMessage?.let { message ->
|
||||
Spacer(modifier = Modifier.width(24.dp))
|
||||
Text(
|
||||
modifier = Modifier.weight(1f),
|
||||
text = message,
|
||||
style = MaterialTheme.typography.bodyMedium,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant
|
||||
)
|
||||
Spacer(modifier = Modifier.width(20.dp))
|
||||
}
|
||||
}
|
||||
|
||||
Spacer(modifier = Modifier.height(8.dp))
|
||||
|
||||
if (alertItems.isNotEmpty()) {
|
||||
Spacer(modifier = Modifier.height(4.dp))
|
||||
AlertItems(
|
||||
selectionType = selectionType,
|
||||
items = alertItems,
|
||||
onItemClick = { index ->
|
||||
onItemClick?.invoke(index)
|
||||
|
||||
if (selectionType == SelectionType.None) {
|
||||
onDismissRequest.invoke()
|
||||
} else {
|
||||
val newItems =
|
||||
alertItems.mapIndexed { itemIndex, item ->
|
||||
item.copy(isSelected = itemIndex == index)
|
||||
}
|
||||
|
||||
alertItems = newItems
|
||||
}
|
||||
},
|
||||
onItemCheckedChanged = { index ->
|
||||
val newItems = alertItems.toMutableList()
|
||||
val oldItem = newItems[index]
|
||||
newItems[index] =
|
||||
oldItem.copy(isSelected = !oldItem.isSelected)
|
||||
|
||||
alertItems = newItems.toImmutableList()
|
||||
}
|
||||
)
|
||||
Spacer(modifier = Modifier.height(10.dp))
|
||||
} else {
|
||||
customContent?.let { content ->
|
||||
Spacer(modifier = Modifier.height(4.dp))
|
||||
content.invoke(this)
|
||||
Spacer(modifier = Modifier.height(10.dp))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (canScrollForward) {
|
||||
HorizontalDivider(modifier = Modifier.fillMaxWidth())
|
||||
}
|
||||
|
||||
Row {
|
||||
Spacer(modifier = Modifier.width(20.dp))
|
||||
neutralText?.getString()?.let { text ->
|
||||
TextButton(
|
||||
onClick = {
|
||||
if (buttonsInvokeDismiss) {
|
||||
onDismissRequest.invoke()
|
||||
} else {
|
||||
isVisible = false
|
||||
}
|
||||
neutralAction?.invoke()
|
||||
}
|
||||
) {
|
||||
Text(text = text)
|
||||
}
|
||||
}
|
||||
|
||||
Spacer(modifier = Modifier.weight(1f))
|
||||
|
||||
cancelText?.getString()?.let { text ->
|
||||
TextButton(
|
||||
onClick = {
|
||||
if (buttonsInvokeDismiss) {
|
||||
onDismissRequest.invoke()
|
||||
} else {
|
||||
isVisible = false
|
||||
}
|
||||
cancelAction?.invoke()
|
||||
}
|
||||
) {
|
||||
Text(text = text)
|
||||
}
|
||||
}
|
||||
|
||||
Spacer(modifier = Modifier.width(2.dp))
|
||||
|
||||
confirmText?.getString()?.let { text ->
|
||||
TextButton(
|
||||
onClick = {
|
||||
if (buttonsInvokeDismiss) {
|
||||
onDismissRequest.invoke()
|
||||
} else {
|
||||
isVisible = false
|
||||
}
|
||||
confirmAction?.invoke()
|
||||
}
|
||||
) {
|
||||
Text(text = text)
|
||||
}
|
||||
}
|
||||
|
||||
Spacer(modifier = Modifier.width(20.dp))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Composable
|
||||
fun AlertItems(
|
||||
selectionType: SelectionType,
|
||||
|
||||
Reference in New Issue
Block a user