Skip to content

Commit

Permalink
Remove dup code
Browse files Browse the repository at this point in the history
  • Loading branch information
Tornaco committed Jun 16, 2024
1 parent 5d53da9 commit da692e0
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion android/internal/Thanox-Internal
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -40,8 +40,7 @@ object Loader {
thanos.pkgManager.getAllPackageSets(false).mapIndexed { _, packageSet ->
AppSetFilterItem(
packageSet.id,
packageSet.label,
)
) { packageSet.label }
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -29,31 +31,36 @@ 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(
popMenuExpend: MutableState<Boolean>,
items: List<MenuItem>,
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)
}
})
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -43,6 +53,7 @@ fun <T : FilterItem> FilterDropDown(
allItems: List<T>,
onItemSelected: (T) -> Unit = {},
) {
val context = LocalContext.current
if (selectedItem != null && allItems.isNotEmpty()) {
Box(
modifier = Modifier.wrapContentSize(Alignment.TopStart)
Expand All @@ -59,24 +70,29 @@ fun <T : FilterItem> 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))
}
}
}
}

}
}
}

0 comments on commit da692e0

Please sign in to comment.