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,14 +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(
onClickBack = onClickBack,
title = title,
content = content,
filteredItems = { SettingsActivity2.allPrefs.filter(it) },
itemContent = { it.Preference() }
)
}
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun <T: Any> SearchScreen(
onClickBack: () -> Unit,
title: String,
filteredItems: (String) -> List<T>,
itemContent: @Composable (T) -> Unit,
content: @Composable (ColumnScope.() -> Unit)? = null,
) {
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
@ -88,7 +105,6 @@ fun SearchPrefScreen(
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,
@ -104,104 +120,30 @@ fun SearchPrefScreen(
) )
} }
} }
if (searchText.text.isBlank())
CompositionLocalProvider(LocalTextStyle provides MaterialTheme.typography.bodyLarge) { CompositionLocalProvider(LocalTextStyle provides MaterialTheme.typography.bodyLarge) {
if (searchText.text.isBlank() && content != null) {
Column( Column(
Modifier Modifier
.verticalScroll(rememberScrollState()) .verticalScroll(rememberScrollState())
.windowInsetsPadding( .windowInsetsPadding(
WindowInsets.safeDrawing.only( WindowInsets.safeDrawing.only(
WindowInsetsSides.Horizontal + WindowInsetsSides.Bottom WindowInsetsSides.Horizontal + WindowInsetsSides.Bottom
)) )
)
) { ) {
content() content()
} }
} } else {
else val items = filteredItems(searchText.text)
CompositionLocalProvider(LocalTextStyle provides MaterialTheme.typography.bodyLarge) {
val filteredPrefs = SettingsActivity2.allPrefs.filter(searchText.text)
LazyColumn { LazyColumn {
items(filteredPrefs) { items(items) {
it.Preference()
}
}
}
}
}
// todo: this is just copy paste from above, could probably share more code
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun <T: Any> SearchScreen(
onClickBack: () -> Unit,
title: String,
items: List<T>,
filter: (String, T) -> Boolean,
itemContent: @Composable (T) -> Unit,
) {
var searchText by remember { mutableStateOf(TextFieldValue()) } // must be outside th column to work without messing up cursor position
Column(Modifier.fillMaxSize()) {
// rememberSaveable would be better, but does not work with TextFieldValue
// if we just store the string, the cursor is messed up
// hmm... no, sth else is messing up that thing, and I just didn't notice
var showSearch by remember { mutableStateOf(false) }
fun setShowSearch(value: Boolean) {
showSearch = value
if (!value) searchText = TextFieldValue()
}
BackHandler {
if (showSearch) setShowSearch(false)
else onClickBack()
}
Surface(
color = MaterialTheme.colorScheme.surfaceContainer,
//shadowElevation = TopAppBarDefaults.??
) {
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
)
)
}
}
CompositionLocalProvider(LocalTextStyle provides MaterialTheme.typography.bodyLarge) {
val filteredItems = items.filter { filter(searchText.text, it) }
LazyColumn {
items(filteredItems) {
itemContent(it) itemContent(it)
} }
} }
} }
} }
} }
}
// from StreetComplete // from StreetComplete
/** Expandable text field that can be dismissed and requests focus when it is expanded */ /** Expandable text field that can be dismissed and requests focus when it is expanded */

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(