mirror of
https://github.com/meshtastic/Meshtastic-Android.git
synced 2026-04-20 22:23:37 +00:00
refactor: Migrate Node dropdown menu to Compose (#1386)
This commit is contained in:
parent
3f9b56a97d
commit
2d2d94924b
6 changed files with 292 additions and 180 deletions
149
app/src/main/java/com/geeksville/mesh/ui/components/NodeMenu.kt
Normal file
149
app/src/main/java/com/geeksville/mesh/ui/components/NodeMenu.kt
Normal file
|
|
@ -0,0 +1,149 @@
|
|||
package com.geeksville.mesh.ui.components
|
||||
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.material.Checkbox
|
||||
import androidx.compose.material.CheckboxDefaults
|
||||
import androidx.compose.material.Divider
|
||||
import androidx.compose.material.DropdownMenu
|
||||
import androidx.compose.material.DropdownMenuItem
|
||||
import androidx.compose.material.MaterialTheme
|
||||
import androidx.compose.material.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import com.geeksville.mesh.R
|
||||
import com.geeksville.mesh.database.entity.NodeEntity
|
||||
|
||||
@Suppress("LongMethod")
|
||||
@Composable
|
||||
fun NodeMenu(
|
||||
node: NodeEntity,
|
||||
ignoreIncomingList: List<Int>,
|
||||
isThisNode: Boolean = false,
|
||||
onMenuItemAction: (MenuItemAction) -> Unit,
|
||||
onDismissRequest: () -> Unit,
|
||||
expanded: Boolean = false,
|
||||
isConnected: Boolean = false,
|
||||
) {
|
||||
val isIgnored = ignoreIncomingList.contains(node.num)
|
||||
var displayIgnoreDialog by remember { mutableStateOf(false) }
|
||||
var displayRemoveDialog by remember { mutableStateOf(false) }
|
||||
if (displayIgnoreDialog) {
|
||||
SimpleAlertDialog(
|
||||
title = R.string.ignore,
|
||||
text = stringResource(
|
||||
id = if (isIgnored) R.string.ignore_remove else R.string.ignore_add,
|
||||
node.user.longName
|
||||
),
|
||||
onConfirm = {
|
||||
displayIgnoreDialog = false
|
||||
onMenuItemAction(MenuItemAction.Ignore)
|
||||
},
|
||||
onDismiss = {
|
||||
displayIgnoreDialog = false
|
||||
}
|
||||
)
|
||||
}
|
||||
if (displayRemoveDialog) {
|
||||
SimpleAlertDialog(
|
||||
title = R.string.remove,
|
||||
text = R.string.remove_node_text,
|
||||
onConfirm = {
|
||||
displayRemoveDialog = false
|
||||
onMenuItemAction(MenuItemAction.Remove)
|
||||
},
|
||||
onDismiss = {
|
||||
displayRemoveDialog = false
|
||||
}
|
||||
)
|
||||
}
|
||||
DropdownMenu(
|
||||
modifier = Modifier.background(MaterialTheme.colors.background.copy(alpha = 1f)),
|
||||
expanded = expanded,
|
||||
onDismissRequest = onDismissRequest,
|
||||
) {
|
||||
|
||||
if (!isThisNode && isConnected) {
|
||||
DropdownMenuItem(
|
||||
onClick = {
|
||||
onDismissRequest()
|
||||
onMenuItemAction(MenuItemAction.DirectMessage)
|
||||
},
|
||||
content = { Text(stringResource(R.string.direct_message)) }
|
||||
)
|
||||
Divider()
|
||||
DropdownMenuItem(
|
||||
onClick = {
|
||||
onDismissRequest()
|
||||
onMenuItemAction(MenuItemAction.RequestUserInfo)
|
||||
},
|
||||
content = { Text(stringResource(R.string.request_userinfo)) }
|
||||
)
|
||||
Divider()
|
||||
DropdownMenuItem(
|
||||
onClick = {
|
||||
onDismissRequest()
|
||||
onMenuItemAction(MenuItemAction.RequestPosition)
|
||||
},
|
||||
content = { Text(stringResource(R.string.request_position)) }
|
||||
)
|
||||
Divider()
|
||||
DropdownMenuItem(
|
||||
onClick = {
|
||||
onDismissRequest()
|
||||
onMenuItemAction(MenuItemAction.TraceRoute)
|
||||
},
|
||||
content = { Text(stringResource(R.string.traceroute)) }
|
||||
)
|
||||
Divider()
|
||||
DropdownMenuItem(
|
||||
onClick = {
|
||||
onDismissRequest()
|
||||
displayRemoveDialog = true
|
||||
},
|
||||
content = { Text(stringResource(R.string.remove)) },
|
||||
)
|
||||
Divider()
|
||||
DropdownMenuItem(
|
||||
onClick = {
|
||||
onDismissRequest()
|
||||
displayIgnoreDialog = true
|
||||
},
|
||||
content = {
|
||||
Text(stringResource(R.string.ignore))
|
||||
Checkbox(
|
||||
colors = CheckboxDefaults.colors(checkedColor = MaterialTheme.colors.primary),
|
||||
checked = isIgnored,
|
||||
onCheckedChange = {
|
||||
onDismissRequest()
|
||||
displayIgnoreDialog = true
|
||||
},
|
||||
)
|
||||
},
|
||||
enabled = ignoreIncomingList.size < 3 || isIgnored
|
||||
)
|
||||
Divider()
|
||||
}
|
||||
DropdownMenuItem(
|
||||
onClick = {
|
||||
onDismissRequest()
|
||||
onMenuItemAction(MenuItemAction.MoreDetails)
|
||||
},
|
||||
content = { Text(stringResource(R.string.more_details)) }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
enum class MenuItemAction {
|
||||
Remove,
|
||||
Ignore,
|
||||
DirectMessage,
|
||||
RequestUserInfo,
|
||||
RequestPosition,
|
||||
TraceRoute,
|
||||
MoreDetails
|
||||
}
|
||||
|
|
@ -1,7 +1,6 @@
|
|||
package com.geeksville.mesh.ui.components
|
||||
|
||||
import androidx.annotation.StringRes
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
|
|
@ -23,10 +22,11 @@ import com.geeksville.mesh.ui.theme.AppTheme
|
|||
fun SimpleAlertDialog(
|
||||
@StringRes title: Int,
|
||||
text: @Composable (() -> Unit)? = null,
|
||||
onConfirm: (() -> Unit)? = null,
|
||||
onDismiss: () -> Unit = {},
|
||||
) = AlertDialog(
|
||||
onDismissRequest = onDismiss,
|
||||
confirmButton = {
|
||||
dismissButton = {
|
||||
TextButton(
|
||||
onClick = onDismiss,
|
||||
modifier = Modifier
|
||||
|
|
@ -36,6 +36,18 @@ fun SimpleAlertDialog(
|
|||
),
|
||||
) { Text(text = stringResource(id = R.string.close)) }
|
||||
},
|
||||
confirmButton = {
|
||||
onConfirm?.let {
|
||||
TextButton(
|
||||
onClick = onConfirm,
|
||||
modifier = Modifier
|
||||
.padding(horizontal = 16.dp),
|
||||
colors = ButtonDefaults.textButtonColors(
|
||||
contentColor = MaterialTheme.colors.onSurface,
|
||||
),
|
||||
) { Text(text = stringResource(id = R.string.okay)) }
|
||||
}
|
||||
},
|
||||
title = {
|
||||
Text(
|
||||
text = stringResource(id = title),
|
||||
|
|
@ -52,8 +64,10 @@ fun SimpleAlertDialog(
|
|||
fun SimpleAlertDialog(
|
||||
@StringRes title: Int,
|
||||
@StringRes text: Int,
|
||||
onConfirm: (() -> Unit)? = null,
|
||||
onDismiss: () -> Unit = {},
|
||||
) = SimpleAlertDialog(
|
||||
onConfirm = onConfirm,
|
||||
onDismiss = onDismiss,
|
||||
title = title,
|
||||
text = {
|
||||
|
|
@ -65,6 +79,25 @@ fun SimpleAlertDialog(
|
|||
},
|
||||
)
|
||||
|
||||
@Composable
|
||||
fun SimpleAlertDialog(
|
||||
@StringRes title: Int,
|
||||
text: String,
|
||||
onConfirm: (() -> Unit)? = null,
|
||||
onDismiss: () -> Unit = {},
|
||||
) = SimpleAlertDialog(
|
||||
onConfirm = onConfirm,
|
||||
onDismiss = onDismiss,
|
||||
title = title,
|
||||
text = {
|
||||
Text(
|
||||
text = text,
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
textAlign = TextAlign.Center,
|
||||
)
|
||||
},
|
||||
)
|
||||
|
||||
@PreviewLightDark
|
||||
@Composable
|
||||
private fun SimpleAlertDialogPreview() {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue