Skip to content

Commit

Permalink
Fix: show list app dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
BryanGIG committed Dec 24, 2023
1 parent c372a19 commit 00ab28e
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 21 deletions.
76 changes: 61 additions & 15 deletions app/src/main/java/com/dumper/android/ui/memory/MemoryScreen.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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(
Expand Down Expand Up @@ -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())
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Array<ProcessData>>()

fun showProcess(ctx: MainActivity, list: List<ProcessData>) {
Expand All @@ -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) {
Expand All @@ -38,6 +40,10 @@ class MemoryViewModel : ViewModel() {
)
}

fun closeProcessListDialog() {
isDialogProcessList.value = false
}

fun getProcessList(context: Context) {
context.asMainActivity()?.sendRequestAllProcess()
}
Expand All @@ -57,9 +63,4 @@ class MemoryViewModel : ViewModel() {
fun changeDumpMetadata(isDumpMetadata: Boolean) {
this.isDumpMetadata.value = isDumpMetadata
}

override fun onCleared() {
super.onCleared()

}
}
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@
<string name="check_flag_address">Check flag address</string>
<string name="arch_abi_32_bit_or_64_bit">ELF Arch (32 Bit or 64 Bit)</string>
<string name="copy_console">Copy Console</string>
<string name="select">Select</string>
<string name="cancel">Cancel</string>
</resources>

0 comments on commit 00ab28e

Please sign in to comment.