Skip to content

Commit

Permalink
[app] process manage v2 app filter
Browse files Browse the repository at this point in the history
  • Loading branch information
Tornaco committed Apr 8, 2022
1 parent de24fa8 commit 80ff497
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,34 @@

package github.tornaco.android.thanos.process.v2

import android.content.Context
import github.tornaco.android.thanos.core.app.ThanosManager
import github.tornaco.android.thanos.core.pm.PackageSet
import github.tornaco.android.thanos.module.compose.common.widget.FilterItem
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

data class AppSetFilterItem(override val label: String, override val isSelected: Boolean) :
FilterItem
FilterItem


object Loader {
suspend fun loadAllFromAppSet(
context: Context,
selection: ((PackageSet) -> Boolean)? = null
): List<AppSetFilterItem> {
val thanos = ThanosManager.from(context)
return withContext(Dispatchers.IO) {
if (!thanos.isServiceInstalled) {
emptyList()
} else {
thanos.pkgManager.getAllPackageSets(false).mapIndexed { index, packageSet ->
AppSetFilterItem(
packageSet.label,
if (selection == null) index == 0 else selection(packageSet)
)
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.FilterAlt
import androidx.compose.material.ripple.rememberRipple
import androidx.compose.material3.*
import androidx.compose.runtime.*
Expand Down Expand Up @@ -93,44 +95,61 @@ fun ProcessManageScreen(
)
}
) {
RunningAppList(state, contentPadding)
RunningAppList(state, contentPadding) {
viewModel.onFilterItemSelected(it)
}
}
}
}

@Composable
fun AppFilterDropDown() {
fun AppFilterDropDown(state: ProcessManageState, onFilterItemSelected: (AppSetFilterItem) -> Unit) {
FilterDropDown(
allItems = listOf(
AppSetFilterItem("System", false),
AppSetFilterItem("Installed", true),
AppSetFilterItem("Media provider", false),
)
icon = Icons.Filled.FilterAlt,
allItems = state.appFilterItems,
onItemSelected = onFilterItemSelected
)
}

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun RunningAppList(state: ProcessManageState, contentPadding: PaddingValues) {
fun RunningAppList(
state: ProcessManageState,
contentPadding: PaddingValues,
onFilterItemSelected: (AppSetFilterItem) -> Unit
) {
LazyColumn(
contentPadding = contentPadding,
modifier = Modifier
.background(color = MaterialTheme.colorScheme.surface)
.fillMaxSize()
) {
items(state.runningAppStates) {
RunningAppItem(it)
}

item {
Spacer(
Row(
modifier = Modifier
.size(24.dp)
)
.fillMaxWidth()
.padding(horizontal = 16.dp, vertical = 8.dp)
) {
AppFilterDropDown(state, onFilterItemSelected)
Spacer(modifier = Modifier.size(16.dp))
}
}

if (state.runningAppStates.isNotEmpty()) {
item { GroupHeader(false, state.runningAppStates.size) }
items(state.runningAppStates) {
RunningAppItem(it)
}
}

items(state.runningAppStatesBg) {
RunningAppItem(it)
if (state.runningAppStatesBg.isNotEmpty()) {
item {
Spacer(modifier = Modifier.size(24.dp))
}
item { GroupHeader(true, state.runningAppStatesBg.size) }
items(state.runningAppStatesBg) {
RunningAppItem(it)
}
}
}
}
Expand All @@ -149,7 +168,7 @@ fun GroupHeader(isTotallyCached: Boolean, itemCount: Int) {
else stringResource(id = R.string.running_process_running)
Text(
text = "$text $itemCount",
style = MaterialTheme.typography.headlineSmall
style = MaterialTheme.typography.titleMedium
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ package github.tornaco.android.thanos.process.v2
data class ProcessManageState(
val isLoading: Boolean,
val runningAppStates: List<RunningAppState>,
val runningAppStatesBg: List<RunningAppState>
val runningAppStatesBg: List<RunningAppState>,
val appFilterItems: List<AppSetFilterItem>
)
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ import javax.inject.Inject
@HiltViewModel
class ProcessManageViewModel @Inject constructor(@ApplicationContext private val context: Context) :
ViewModel() {
private val _state = MutableStateFlow(ProcessManageState(true, emptyList(), emptyList()))
private val _state =
MutableStateFlow(ProcessManageState(true, emptyList(), emptyList(), emptyList()))
val state = _state.asStateFlow()

private val thanox by lazy { ThanosManager.from(context) }
Expand Down Expand Up @@ -61,14 +62,21 @@ class ProcessManageViewModel @Inject constructor(@ApplicationContext private val
val runningAppStatesGroupByCached = runningAppStates.groupBy { it.allProcessIsCached }
XLog.d("startLoading: %s", runningAppStatesGroupByCached)

val appFilterListItems = Loader.loadAllFromAppSet(context)

_state.value = _state.value.copy(
isLoading = false,
runningAppStates = runningAppStatesGroupByCached[false] ?: emptyList(),
runningAppStatesBg = runningAppStatesGroupByCached[true] ?: emptyList()
runningAppStatesBg = runningAppStatesGroupByCached[true] ?: emptyList(),
appFilterItems = appFilterListItems
)
}

private fun updateLoadingState(isLoading: Boolean) {
_state.value = _state.value.copy(isLoading = isLoading)
}

fun onFilterItemSelected(it: AppSetFilterItem) {

}
}
2 changes: 1 addition & 1 deletion android/internal/Thanox-Internal
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,17 @@ package github.tornaco.android.thanos.module.compose.common.widget

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.material.DropdownMenu
import androidx.compose.material.DropdownMenuItem
import androidx.compose.material3.FilledTonalButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.unit.dp
import github.tornaco.android.thanos.module.compose.common.theme.ColorDefaults

interface FilterItem {
val label: String
Expand All @@ -41,33 +39,44 @@ interface FilterItem {

@Composable
fun <T : FilterItem> FilterDropDown(
icon: ImageVector? = null,
allItems: List<T>,
onItemSelected: (T) -> Unit = {},
) {
var expanded by remember { mutableStateOf(false) }
val selectedItem = allItems.find { it.isSelected }
requireNotNull(selectedItem) { "At least 1 selected item required." }
Box(
modifier = Modifier
.background(ColorDefaults.backgroundSurfaceColor())
.fillMaxWidth()
.padding(8.dp)
.wrapContentSize(Alignment.TopStart)
) {
FilledTonalButton(onClick = { expanded = true }) {
Text(text = selectedItem.label)
}
DropdownMenu(
modifier = Modifier.background(MaterialTheme.colorScheme.surface),
expanded = expanded,
onDismissRequest = { expanded = false }
if (allItems.isNotEmpty()) {
Box(
modifier = Modifier.wrapContentSize(Alignment.TopStart)
) {
allItems.forEach { item ->
DropdownMenuItem(onClick = {
onItemSelected(item)
expanded = false
}) {
Text(text = item.label)
var expanded by remember { mutableStateOf(false) }
val selectedItem = allItems.find { it.isSelected }
requireNotNull(selectedItem) { "At least 1 selected item required." }
FilledTonalButton(
onClick = { expanded = true }) {
icon?.let {
Icon(
it,
contentDescription = "FilterDropDown",
modifier = Modifier.size(ButtonDefaults.IconSize)
)
Spacer(Modifier.size(4.dp))
}
Text(
text = selectedItem.label,
style = MaterialTheme.typography.labelLarge
)
}
DropdownMenu(
modifier = Modifier.background(MaterialTheme.colorScheme.surface),
expanded = expanded,
onDismissRequest = { expanded = false }
) {
allItems.forEach { item ->
DropdownMenuItem(onClick = {
onItemSelected(item)
expanded = false
}) {
Text(text = item.label)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ private fun ThanoxSmallTopAppBar(
scrolledContainerColor = Color.Transparent
)
Surface(color = backgroundColor) {
SmallTopAppBar(
MediumTopAppBar(
colors = foregroundColors,
modifier = Modifier
.padding(
Expand Down

0 comments on commit 80ff497

Please sign in to comment.