From da692e0190678f571f5435754d9fc198a9fe269a Mon Sep 17 00:00:00 2001 From: Tornaco Date: Sun, 16 Jun 2024 21:08:21 +0800 Subject: [PATCH] Remove dup code --- .../fortuitous/thanos/main/ChooserActivity.kt | 2 +- .../thanos/settings/access/FilterItems.kt | 7 +-- .../SettingsAccessRecordViewerScreen.kt | 3 +- .../SettingsAccessRecordViewerViewModel.kt | 2 +- android/internal/Thanox-Internal | 2 +- .../compose/common/loader/AppSetFilterItem.kt | 5 +- .../module/compose/common/theme/Colors.kt | 4 +- .../compose/common/widget/DropDownExt.kt | 39 +++++++++------- .../compose/common/widget/FilterDropDown.kt | 46 +++++++++++++------ 9 files changed, 67 insertions(+), 43 deletions(-) diff --git a/android/app/src/main/java/now/fortuitous/thanos/main/ChooserActivity.kt b/android/app/src/main/java/now/fortuitous/thanos/main/ChooserActivity.kt index 003eb4268..61b4b2eb3 100644 --- a/android/app/src/main/java/now/fortuitous/thanos/main/ChooserActivity.kt +++ b/android/app/src/main/java/now/fortuitous/thanos/main/ChooserActivity.kt @@ -35,9 +35,9 @@ import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.unit.dp import dagger.hilt.android.AndroidEntryPoint import github.tornaco.android.thanos.module.compose.common.ComposeThemeActivity +import github.tornaco.android.thanos.module.compose.common.widget.LargeSpacer import github.tornaco.android.thanos.util.ActivityUtils import now.fortuitous.thanos.pref.AppPreference -import tornaco.apps.thanox.base.ui.LargeSpacer import tornaco.apps.thanox.base.ui.NiaGradientBackground import tornaco.apps.thanox.base.ui.theme.ThanosTheme diff --git a/android/app/src/main/java/now/fortuitous/thanos/settings/access/FilterItems.kt b/android/app/src/main/java/now/fortuitous/thanos/settings/access/FilterItems.kt index e66561db1..2a32e1e7d 100644 --- a/android/app/src/main/java/now/fortuitous/thanos/settings/access/FilterItems.kt +++ b/android/app/src/main/java/now/fortuitous/thanos/settings/access/FilterItems.kt @@ -17,19 +17,20 @@ package now.fortuitous.thanos.settings.access +import android.content.Context import github.tornaco.android.thanos.module.compose.common.widget.FilterItem data class PackageFilterItem( override val id: String, - override val label: String + override val label: (Context) -> String ) : FilterItem data class ReadFilterItem( override val id: String = "read", - override val label: String = "Read" + override val label: (Context) -> String = { "Read" } ) : FilterItem data class WriteFilterItem( override val id: String = "write", - override val label: String = "Write" + override val label: (Context) -> String = { "Write" } ) : FilterItem \ No newline at end of file diff --git a/android/app/src/main/java/now/fortuitous/thanos/settings/access/SettingsAccessRecordViewerScreen.kt b/android/app/src/main/java/now/fortuitous/thanos/settings/access/SettingsAccessRecordViewerScreen.kt index cf5afe560..e2c61f91a 100644 --- a/android/app/src/main/java/now/fortuitous/thanos/settings/access/SettingsAccessRecordViewerScreen.kt +++ b/android/app/src/main/java/now/fortuitous/thanos/settings/access/SettingsAccessRecordViewerScreen.kt @@ -194,8 +194,9 @@ private fun AppFilterDropDown( } ) ) { + val context = LocalContext.current Text( - text = item.filterItem.label, + text = item.filterItem.label(context), style = MaterialTheme.typography.labelLarge ) } diff --git a/android/app/src/main/java/now/fortuitous/thanos/settings/access/SettingsAccessRecordViewerViewModel.kt b/android/app/src/main/java/now/fortuitous/thanos/settings/access/SettingsAccessRecordViewerViewModel.kt index 1bb4e609e..bc2b50134 100644 --- a/android/app/src/main/java/now/fortuitous/thanos/settings/access/SettingsAccessRecordViewerViewModel.kt +++ b/android/app/src/main/java/now/fortuitous/thanos/settings/access/SettingsAccessRecordViewerViewModel.kt @@ -76,7 +76,7 @@ class SettingsAccessRecordViewerViewModel @Inject constructor(@ApplicationContex .mapNotNull { pkgName -> val appInfo = thanox.pkgManager.getAppInfo(pkgName) appInfo?.let { app -> - PackageFilterItem(pkgName, app.appLabel) + PackageFilterItem(pkgName) { app.appLabel } } } val selectableFilterItems = (rwFilterItems + packageFilterItems).map { diff --git a/android/internal/Thanox-Internal b/android/internal/Thanox-Internal index 835bc444c..9aef6286e 160000 --- a/android/internal/Thanox-Internal +++ b/android/internal/Thanox-Internal @@ -1 +1 @@ -Subproject commit 835bc444c8442cdea7a977a0d3d0b418cd14d9bf +Subproject commit 9aef6286e0d2ed88aa4b5eef4897c73a21755226 diff --git a/android/modules/module_common/src/main/java/github/tornaco/android/thanos/module/compose/common/loader/AppSetFilterItem.kt b/android/modules/module_common/src/main/java/github/tornaco/android/thanos/module/compose/common/loader/AppSetFilterItem.kt index 526a402d2..4fd9a89a4 100644 --- a/android/modules/module_common/src/main/java/github/tornaco/android/thanos/module/compose/common/loader/AppSetFilterItem.kt +++ b/android/modules/module_common/src/main/java/github/tornaco/android/thanos/module/compose/common/loader/AppSetFilterItem.kt @@ -25,7 +25,7 @@ import kotlinx.coroutines.withContext data class AppSetFilterItem( override val id: String, - override val label: String + override val label: (Context) -> String ) : FilterItem object Loader { @@ -40,8 +40,7 @@ object Loader { thanos.pkgManager.getAllPackageSets(false).mapIndexed { _, packageSet -> AppSetFilterItem( packageSet.id, - packageSet.label, - ) + ) { packageSet.label } } } } diff --git a/android/modules/module_common/src/main/java/github/tornaco/android/thanos/module/compose/common/theme/Colors.kt b/android/modules/module_common/src/main/java/github/tornaco/android/thanos/module/compose/common/theme/Colors.kt index b79d0d94b..06c872ca8 100644 --- a/android/modules/module_common/src/main/java/github/tornaco/android/thanos/module/compose/common/theme/Colors.kt +++ b/android/modules/module_common/src/main/java/github/tornaco/android/thanos/module/compose/common/theme/Colors.kt @@ -28,9 +28,9 @@ import kotlin.math.ln object ColorDefaults { @Composable - fun backgroundSurfaceColor() = MaterialTheme.colorScheme.applyTonalElevation( + fun backgroundSurfaceColor(size: Dp = 2.dp) = MaterialTheme.colorScheme.applyTonalElevation( MaterialTheme.colorScheme.surface, - 2.dp + size ) private fun ColorScheme.applyTonalElevation(backgroundColor: Color, elevation: Dp): Color { diff --git a/android/modules/module_common/src/main/java/github/tornaco/android/thanos/module/compose/common/widget/DropDownExt.kt b/android/modules/module_common/src/main/java/github/tornaco/android/thanos/module/compose/common/widget/DropDownExt.kt index be46c4d85..66a030577 100644 --- a/android/modules/module_common/src/main/java/github/tornaco/android/thanos/module/compose/common/widget/DropDownExt.kt +++ b/android/modules/module_common/src/main/java/github/tornaco/android/thanos/module/compose/common/widget/DropDownExt.kt @@ -20,6 +20,8 @@ package github.tornaco.android.thanos.module.compose.common.widget import androidx.annotation.DrawableRes import androidx.compose.foundation.background import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.shape.RoundedCornerShape +import androidx.compose.material3.DropdownMenu import androidx.compose.material3.DropdownMenuItem import androidx.compose.material3.Icon import androidx.compose.material3.MaterialTheme @@ -29,8 +31,9 @@ import androidx.compose.runtime.MutableState import androidx.compose.ui.Alignment.Companion.CenterVertically import androidx.compose.ui.Modifier import androidx.compose.ui.res.painterResource +import androidx.compose.ui.unit.dp -data class MenuItem(val id: String, val title: String, @DrawableRes val iconRes: Int = 0) +data class MenuItem(val id: String, val title: String, @DrawableRes val iconRes: Int) @Composable fun DropdownPopUpMenu( @@ -38,22 +41,26 @@ fun DropdownPopUpMenu( items: List, onItemClick: (MenuItem) -> Unit ) { - androidx.compose.material.DropdownMenu( - modifier = Modifier.background(MaterialTheme.colorScheme.surface), - expanded = popMenuExpend.value, - onDismissRequest = { popMenuExpend.value = false } + MaterialTheme( + shapes = MaterialTheme.shapes.copy(extraSmall = RoundedCornerShape(18.dp)) ) { - items.forEach { - DropdownMenuItem(onClick = { - popMenuExpend.value = false - onItemClick(it) - }, text = { - Row(verticalAlignment = CenterVertically) { - Icon(painterResource(id = it.iconRes), it.title) - SmallSpacer() - Text(text = it.title) - } - }) + DropdownMenu( + modifier = Modifier.background(MaterialTheme.colorScheme.surface), + expanded = popMenuExpend.value, + onDismissRequest = { popMenuExpend.value = false } + ) { + items.forEach { + DropdownMenuItem(onClick = { + popMenuExpend.value = false + onItemClick(it) + }, text = { + Row(verticalAlignment = CenterVertically) { + Icon(painterResource(id = it.iconRes), it.title) + SmallSpacer() + Text(text = it.title) + } + }) + } } } } \ No newline at end of file diff --git a/android/modules/module_common/src/main/java/github/tornaco/android/thanos/module/compose/common/widget/FilterDropDown.kt b/android/modules/module_common/src/main/java/github/tornaco/android/thanos/module/compose/common/widget/FilterDropDown.kt index 0d0756cdd..c19063068 100644 --- a/android/modules/module_common/src/main/java/github/tornaco/android/thanos/module/compose/common/widget/FilterDropDown.kt +++ b/android/modules/module_common/src/main/java/github/tornaco/android/thanos/module/compose/common/widget/FilterDropDown.kt @@ -17,23 +17,33 @@ package github.tornaco.android.thanos.module.compose.common.widget +import android.content.Context import androidx.compose.foundation.background import androidx.compose.foundation.layout.Box 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.foundation.shape.RoundedCornerShape import androidx.compose.material.DropdownMenuItem -import androidx.compose.material3.* -import androidx.compose.runtime.* +import androidx.compose.material3.ButtonDefaults +import androidx.compose.material3.FilledTonalButton +import androidx.compose.material3.Icon +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.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.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.vector.ImageVector +import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.unit.dp interface FilterItem { val id: String - val label: String + val label: (Context) -> String } @Composable @@ -43,6 +53,7 @@ fun FilterDropDown( allItems: List, onItemSelected: (T) -> Unit = {}, ) { + val context = LocalContext.current if (selectedItem != null && allItems.isNotEmpty()) { Box( modifier = Modifier.wrapContentSize(Alignment.TopStart) @@ -59,24 +70,29 @@ fun FilterDropDown( Spacer(Modifier.size(4.dp)) } Text( - text = selectedItem.label, + text = selectedItem.label(context), style = MaterialTheme.typography.labelLarge ) } - DropdownMenu( - modifier = Modifier.background(MaterialTheme.colorScheme.surface), - expanded = expanded, - onDismissRequest = { expanded = false } + MaterialTheme( + shapes = MaterialTheme.shapes.copy(extraSmall = RoundedCornerShape(18.dp)) ) { - allItems.forEach { item -> - DropdownMenuItem(onClick = { - onItemSelected(item) - expanded = false - }) { - Text(text = item.label) + androidx.compose.material3.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(context)) + } } } } + } } }