Add input dialogs to the KPM module to optimize control logic and display Snackbar prompts for operation results.

This commit is contained in:
ShirkNeko
2025-04-01 17:04:00 +08:00
parent f71de1742a
commit e5f5b8f831
5 changed files with 88 additions and 26 deletions

View File

@@ -51,6 +51,7 @@ import androidx.compose.animation.shrinkVertically
import androidx.compose.runtime.saveable.rememberSaveable
import shirkneko.zako.sukisu.ui.theme.CardConfig
import androidx.core.content.edit
import kotlin.random.Random
@OptIn(ExperimentalMaterial3Api::class)
@Destination<RootGraph>(start = true)
@@ -406,6 +407,8 @@ fun WarningCard(
}
@Composable
fun ContributionCard() {
val uriHandler = LocalUriHandler.current
val links = listOf("https://github.com/ShirkNeko", "https://github.com/udochina")
ElevatedCard(
colors = getCardColors(MaterialTheme.colorScheme.secondaryContainer),
elevation = CardDefaults.cardElevation(defaultElevation = getCardElevation())
@@ -413,6 +416,10 @@ fun ContributionCard() {
Row(
modifier = Modifier
.fillMaxWidth()
.clickable {
val randomIndex = Random.nextInt(links.size)
uriHandler.openUri(links[randomIndex])
}
.padding(24.dp),
verticalAlignment = Alignment.CenterVertically
) {
@@ -548,19 +555,15 @@ private fun InfoCard() {
InfoCardItem(stringResource(R.string.home_selinux_status), getSELinuxStatus())
if (!isSimpleMode) {
var showKpmVersion by remember { mutableStateOf(true) }
LaunchedEffect(Unit) {
try {
getKpmVersion()
} catch (e: Exception) {
showKpmVersion = false
}
}
AnimatedVisibility(visible = showKpmVersion) {
Spacer(Modifier.height(16.dp))
InfoCardItem(stringResource(R.string.home_kpm_version), getKpmVersion())
if (!isSimpleMode){
Spacer(Modifier.height(16.dp))
val kpmVersion = getKpmVersion()
val displayVersion = if (kpmVersion.isEmpty() || kpmVersion.startsWith("Error")) {
stringResource(R.string.not_supported)
} else {
kpmVersion
}
InfoCardItem(stringResource(R.string.home_kpm_version), displayVersion)
}

View File

@@ -276,10 +276,46 @@ private fun KpmModuleItem(
val viewModel: KpmViewModel = viewModel()
val scope = rememberCoroutineScope()
val snackBarHost = remember { SnackbarHostState() }
val successMessage = stringResource(R.string.kpm_control_success)
val failureMessage = stringResource(R.string.kpm_control_failed)
if (viewModel.showInputDialog && viewModel.selectedModuleId == module.id) {
AlertDialog(
onDismissRequest = { viewModel.hideInputDialog() },
title = { Text(stringResource(R.string.kpm_control)) },
text = {
OutlinedTextField(
value = viewModel.inputArgs,
onValueChange = { viewModel.updateInputArgs(it) },
label = { Text(stringResource(R.string.kpm_args)) },
placeholder = { Text(module.args) }
)
},
confirmButton = {
TextButton(
onClick = {
scope.launch {
val result = viewModel.executeControl()
val message = when (result) {
0 -> successMessage
else -> failureMessage
}
snackBarHost.showSnackbar(message)
onControl()
}
}
) {
Text(stringResource(R.string.confirm))
}
},
dismissButton = {
TextButton(onClick = { viewModel.hideInputDialog() }) {
Text(stringResource(R.string.cancel))
}
}
)
}
ElevatedCard(
colors = getCardColors(MaterialTheme.colorScheme.secondaryContainer),
elevation = CardDefaults.cardElevation(defaultElevation = getCardElevation())
@@ -326,17 +362,7 @@ private fun KpmModuleItem(
horizontalArrangement = Arrangement.spacedBy(8.dp)
) {
FilledTonalButton(
onClick = {
scope.launch {
val result = viewModel.controlModule(module.id, module.args)
val message = when (result) {
0 -> successMessage
else -> failureMessage
}
snackBarHost.showSnackbar(message)
onControl()
}
},
onClick = { viewModel.showInputDialog(module.id) },
enabled = module.hasAction
) {
Icon(

View File

@@ -111,6 +111,38 @@ class KpmViewModel : ViewModel() {
}
}
}
var showInputDialog by mutableStateOf(false)
private set
var selectedModuleId by mutableStateOf<String?>(null)
private set
var inputArgs by mutableStateOf("")
private set
fun showInputDialog(moduleId: String) {
selectedModuleId = moduleId
showInputDialog = true
}
fun hideInputDialog() {
showInputDialog = false
selectedModuleId = null
inputArgs = ""
}
fun updateInputArgs(args: String) {
inputArgs = args
}
fun executeControl(): Int {
val moduleId = selectedModuleId ?: return -1
val result = controlKpmModule(moduleId, inputArgs)
hideInputDialog()
return result
}
fun controlModule(moduleId: String, args: String? = null): Int {
return try {
val result = controlKpmModule(moduleId, args)