remove duplcate code

This commit is contained in:
Helium314 2025-02-06 16:51:12 +01:00
parent edc883adf3
commit d8b829f4f4
3 changed files with 31 additions and 90 deletions

View file

@ -45,101 +45,31 @@ import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import helium314.keyboard.latin.R import helium314.keyboard.latin.R
@OptIn(ExperimentalMaterial3Api::class)
@Composable @Composable
fun SearchPrefScreen( fun SearchPrefScreen(
onClickBack: () -> Unit, onClickBack: () -> Unit,
title: String, title: String,
content: @Composable ColumnScope.() -> Unit content: @Composable ColumnScope.() -> Unit
) { ) {
var searchText by remember { mutableStateOf(TextFieldValue()) } // must be outside th column to work without messing up cursor position SearchScreen(
Column(Modifier.fillMaxSize()) { onClickBack = onClickBack,
// rememberSaveable would be better, but does not work with TextFieldValue title = title,
// if we just store the string, the cursor is messed up content = content,
// hmm... no, sth else is messing up that thing, and I just didn't notice filteredItems = { SettingsActivity2.allPrefs.filter(it) },
var showSearch by remember { mutableStateOf(false) } itemContent = { it.Preference() }
)
fun setShowSearch(value: Boolean) {
showSearch = value
if (!value) searchText = TextFieldValue()
}
BackHandler {
if (showSearch) setShowSearch(false)
else onClickBack()
}
Surface(
color = MaterialTheme.colorScheme.surfaceContainer,
) {
Column {
TopAppBar(
title = { Text(title) },
windowInsets = TopAppBarDefaults.windowInsets,
navigationIcon = {
IconButton(onClick = {
if (showSearch) setShowSearch(false)
else onClickBack()
}) {
Icon(
painterResource(R.drawable.ic_arrow_left), // todo: "old" arrow icon existed, so must be somewhere in resources (maybe androidx?)
stringResource(R.string.spoken_description_action_previous)
)
}
},
actions = {
IconButton(onClick = { setShowSearch(!showSearch) }) { Icon(painterResource(R.drawable.sym_keyboard_search_lxx), stringResource(R.string.label_search_key)) }
},
//elevation = 0.dp
)
ExpandableSearchField(
expanded = showSearch,
onDismiss = { setShowSearch(false) },
search = searchText,
onSearchChange = { searchText = it },
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 16.dp, vertical = 8.dp),
colors = TextFieldDefaults.colors(
focusedContainerColor = MaterialTheme.colorScheme.surface
)
)
}
}
if (searchText.text.isBlank())
CompositionLocalProvider(LocalTextStyle provides MaterialTheme.typography.bodyLarge) {
Column(
Modifier
.verticalScroll(rememberScrollState())
.windowInsetsPadding(
WindowInsets.safeDrawing.only(
WindowInsetsSides.Horizontal + WindowInsetsSides.Bottom
))
) {
content()
}
}
else
CompositionLocalProvider(LocalTextStyle provides MaterialTheme.typography.bodyLarge) {
val filteredPrefs = SettingsActivity2.allPrefs.filter(searchText.text)
LazyColumn {
items(filteredPrefs) {
it.Preference()
}
}
}
}
} }
// todo: this is just copy paste from above, could probably share more code
@OptIn(ExperimentalMaterial3Api::class) @OptIn(ExperimentalMaterial3Api::class)
@Composable @Composable
fun <T: Any> SearchScreen( fun <T: Any> SearchScreen(
onClickBack: () -> Unit, onClickBack: () -> Unit,
title: String, title: String,
items: List<T>, filteredItems: (String) -> List<T>,
filter: (String, T) -> Boolean,
itemContent: @Composable (T) -> Unit, itemContent: @Composable (T) -> Unit,
content: @Composable (ColumnScope.() -> Unit)? = null,
) { ) {
var searchText by remember { mutableStateOf(TextFieldValue()) } // must be outside th column to work without messing up cursor position var searchText by remember { mutableStateOf(TextFieldValue()) } // must be outside the column to work without messing up cursor position
Column(Modifier.fillMaxSize()) { Column(Modifier.fillMaxSize()) {
// rememberSaveable would be better, but does not work with TextFieldValue // rememberSaveable would be better, but does not work with TextFieldValue
// if we just store the string, the cursor is messed up // if we just store the string, the cursor is messed up
@ -156,7 +86,6 @@ fun <T: Any> SearchScreen(
} }
Surface( Surface(
color = MaterialTheme.colorScheme.surfaceContainer, color = MaterialTheme.colorScheme.surfaceContainer,
//shadowElevation = TopAppBarDefaults.??
) { ) {
Column { Column {
TopAppBar( TopAppBar(
@ -176,7 +105,6 @@ fun <T: Any> SearchScreen(
actions = { actions = {
IconButton(onClick = { setShowSearch(!showSearch) }) { Icon(painterResource(R.drawable.sym_keyboard_search_lxx), stringResource(R.string.label_search_key)) } IconButton(onClick = { setShowSearch(!showSearch) }) { Icon(painterResource(R.drawable.sym_keyboard_search_lxx), stringResource(R.string.label_search_key)) }
}, },
//elevation = 0.dp
) )
ExpandableSearchField( ExpandableSearchField(
expanded = showSearch, expanded = showSearch,
@ -193,10 +121,24 @@ fun <T: Any> SearchScreen(
} }
} }
CompositionLocalProvider(LocalTextStyle provides MaterialTheme.typography.bodyLarge) { CompositionLocalProvider(LocalTextStyle provides MaterialTheme.typography.bodyLarge) {
val filteredItems = items.filter { filter(searchText.text, it) } if (searchText.text.isBlank() && content != null) {
LazyColumn { Column(
items(filteredItems) { Modifier
itemContent(it) .verticalScroll(rememberScrollState())
.windowInsetsPadding(
WindowInsets.safeDrawing.only(
WindowInsetsSides.Horizontal + WindowInsetsSides.Bottom
)
)
) {
content()
}
} else {
val items = filteredItems(searchText.text)
LazyColumn {
items(items) {
itemContent(it)
}
} }
} }
} }

View file

@ -15,7 +15,6 @@ import kotlinx.coroutines.flow.MutableStateFlow
// todo (roughly in order) // todo (roughly in order)
// try making a dialog with reduced padding // try making a dialog with reduced padding
// avoid duplicate code in SearchScreen
// work on todos in other files // work on todos in other files
// check dark and light theme (don't have dynamic) // check dark and light theme (don't have dynamic)
// rename both settingsActivities // rename both settingsActivities

View file

@ -20,9 +20,9 @@ fun ColorsScreen(
SearchScreen( SearchScreen(
title = stringResource(if (night) R.string.select_user_colors_night else R.string.select_user_colors), title = stringResource(if (night) R.string.select_user_colors_night else R.string.select_user_colors),
onClickBack = onClickBack, onClickBack = onClickBack,
items = availableColors, filteredItems = { search -> availableColors.filter { it.displayName.contains(search, true) } },
filter = { search, color -> color.displayName.contains(search, true) } itemContent = { }
) { } )
} }
private class ColorSetting( private class ColorSetting(