From 00ab28e29517e433228815dbab33b5b32e96075b Mon Sep 17 00:00:00 2001 From: BryanGIG Date: Sun, 24 Dec 2023 09:14:26 +0700 Subject: [PATCH] Fix: show list app dialog --- .../dumper/android/ui/memory/MemoryScreen.kt | 76 +++++++++++++++---- .../android/ui/memory/MemoryViewModel.kt | 13 ++-- app/src/main/res/values/strings.xml | 2 + 3 files changed, 70 insertions(+), 21 deletions(-) diff --git a/app/src/main/java/com/dumper/android/ui/memory/MemoryScreen.kt b/app/src/main/java/com/dumper/android/ui/memory/MemoryScreen.kt index 90df44f..fbdb2de 100644 --- a/app/src/main/java/com/dumper/android/ui/memory/MemoryScreen.kt +++ b/app/src/main/java/com/dumper/android/ui/memory/MemoryScreen.kt @@ -1,21 +1,27 @@ package com.dumper.android.ui.memory +import androidx.compose.foundation.clickable import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.lazy.LazyColumn +import androidx.compose.foundation.lazy.itemsIndexed import androidx.compose.foundation.rememberScrollState import androidx.compose.foundation.verticalScroll +import androidx.compose.material3.AlertDialog import androidx.compose.material3.Button import androidx.compose.material3.Checkbox import androidx.compose.material3.OutlinedTextField import androidx.compose.material3.Text import androidx.compose.runtime.Composable -import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue import androidx.compose.runtime.livedata.observeAsState +import androidx.compose.runtime.mutableIntStateOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.platform.LocalContext @@ -26,26 +32,46 @@ import androidx.lifecycle.viewmodel.compose.viewModel import androidx.navigation.NavController import androidx.navigation.NavGraph.Companion.findStartDestination import com.dumper.android.R -import com.google.android.material.dialog.MaterialAlertDialogBuilder +import com.dumper.android.dumper.process.ProcessData @Composable -fun MemoryScreen(navController: NavController, viewModel: MemoryViewModel = viewModel()) { +fun MemoryScreen(navController: NavController, viewModel: MemoryViewModel) { val scrollState = rememberScrollState() val context = LocalContext.current - val processList by viewModel.processList.observeAsState() + val isDialogProcessList by viewModel.isDialogProcessList.collectAsState() + val processList by viewModel.processList.observeAsState(emptyArray()) - LaunchedEffect(processList) { - if (processList?.isNotEmpty() == true) { - val displayNames = processList!!.map { it.getDisplayName() }.toTypedArray() + if (isDialogProcessList && processList.isNotEmpty()) { + var selectedProcessIndex by remember { mutableIntStateOf(0) } - MaterialAlertDialogBuilder(context) - .setTitle(context.getString(R.string.select_process)) - .setSingleChoiceItems(displayNames, -1) { dialog, idx -> - viewModel.changePackageName(processList!![idx].processName) - dialog.dismiss() + AlertDialog( + onDismissRequest = { viewModel.closeProcessListDialog() }, + title = { Text(stringResource(R.string.select_process)) }, + text = { + LazyColumn(modifier = Modifier.fillMaxWidth()) { + itemsIndexed(items = processList) { idx, process -> + ItemApp(selectedProcessIndex, idx, process) { + selectedProcessIndex = idx + } + } } - .create() - } + }, + confirmButton = { + Button( + onClick = { + viewModel.changePackageName(processList!![selectedProcessIndex].processName) + viewModel.closeProcessListDialog() + } + ) { + Text(stringResource(R.string.select)) + } + }, + dismissButton = { + Button(onClick = { viewModel.closeProcessListDialog() }) { + Text(stringResource(R.string.cancel)) + } + } + ) } Column( @@ -122,8 +148,28 @@ fun MemoryScreen(navController: NavController, viewModel: MemoryViewModel = view } } +@Composable +private fun ItemApp( + selectedProcessIndex: Int, + idx: Int, + process: ProcessData, + onClick: () -> Unit = {} +) { + Row( + modifier = Modifier.fillMaxWidth().clickable { onClick() }, + verticalAlignment = Alignment.CenterVertically + ) { + Checkbox( + checked = idx == selectedProcessIndex, + onCheckedChange = {} + ) + + Text(text = process.getDisplayName()) + } +} + @Preview @Composable private fun MemoryScreenPreview() { - MemoryScreen(navController = NavController(LocalContext.current)) + MemoryScreen(NavController(LocalContext.current), viewModel()) } \ No newline at end of file diff --git a/app/src/main/java/com/dumper/android/ui/memory/MemoryViewModel.kt b/app/src/main/java/com/dumper/android/ui/memory/MemoryViewModel.kt index 8e519d0..1cd19c1 100644 --- a/app/src/main/java/com/dumper/android/ui/memory/MemoryViewModel.kt +++ b/app/src/main/java/com/dumper/android/ui/memory/MemoryViewModel.kt @@ -14,6 +14,7 @@ class MemoryViewModel : ViewModel() { val libName = MutableStateFlow("libil2cpp.so") val isFixELF = MutableStateFlow(false) val isDumpMetadata = MutableStateFlow(false) + val isDialogProcessList = MutableStateFlow(false) val processList = MutableLiveData>() fun showProcess(ctx: MainActivity, list: List) { @@ -22,7 +23,8 @@ class MemoryViewModel : ViewModel() { return } - processList.value = list.sortedBy { it.processName }.toTypedArray() + processList.value = list.sortedBy { it.getDisplayName() }.toTypedArray() + isDialogProcessList.value = true } fun beginDump(context: Context) { @@ -38,6 +40,10 @@ class MemoryViewModel : ViewModel() { ) } + fun closeProcessListDialog() { + isDialogProcessList.value = false + } + fun getProcessList(context: Context) { context.asMainActivity()?.sendRequestAllProcess() } @@ -57,9 +63,4 @@ class MemoryViewModel : ViewModel() { fun changeDumpMetadata(isDumpMetadata: Boolean) { this.isDumpMetadata.value = isDumpMetadata } - - override fun onCleared() { - super.onCleared() - - } } \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 057adb8..27b5d53 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -13,4 +13,6 @@ Check flag address ELF Arch (32 Bit or 64 Bit) Copy Console + Select + Cancel \ No newline at end of file