Skip to content

Commit 705a6cc

Browse files
committed
Add comments
1 parent d751d51 commit 705a6cc

File tree

1 file changed

+25
-15
lines changed
  • compose/snippets/src/main/java/com/example/compose/snippets/components

1 file changed

+25
-15
lines changed

compose/snippets/src/main/java/com/example/compose/snippets/components/SearchBar.kt

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import androidx.compose.foundation.layout.fillMaxWidth
2626
import androidx.compose.foundation.layout.padding
2727
import androidx.compose.foundation.lazy.LazyColumn
2828
import androidx.compose.foundation.rememberScrollState
29+
import androidx.compose.foundation.text.input.TextFieldState
30+
import androidx.compose.foundation.text.input.rememberTextFieldState
2931
import androidx.compose.foundation.verticalScroll
3032
import androidx.compose.material.icons.Icons
3133
import androidx.compose.material.icons.filled.MoreVert
@@ -86,13 +88,12 @@ fun SearchBarExamples() {
8688
@OptIn(ExperimentalMaterial3Api::class)
8789
@Composable
8890
fun SimpleSearchBar(
89-
query: String,
90-
onQueryChange: (String) -> Unit,
91+
textFieldState: TextFieldState,
9192
onSearch: (String) -> Unit,
9293
searchResults: List<String>,
93-
onResultClick: (String) -> Unit,
9494
modifier: Modifier = Modifier
9595
) {
96+
// Controls expansion state of the search bar
9697
var expanded by rememberSaveable { mutableStateOf(false) }
9798

9899
Box(
@@ -106,10 +107,10 @@ fun SimpleSearchBar(
106107
.semantics { traversalIndex = 0f },
107108
inputField = {
108109
SearchBarDefaults.InputField(
109-
query = query,
110-
onQueryChange = onQueryChange,
110+
query = textFieldState.text.toString(),
111+
onQueryChange = { textFieldState.edit { replace(0, length, it) } },
111112
onSearch = {
112-
onSearch(query)
113+
onSearch(textFieldState.text.toString())
113114
expanded = false
114115
},
115116
expanded = expanded,
@@ -120,13 +121,14 @@ fun SimpleSearchBar(
120121
expanded = expanded,
121122
onExpandedChange = { expanded = it },
122123
) {
124+
// Display search results in a scrollable column
123125
Column(Modifier.verticalScroll(rememberScrollState())) {
124126
searchResults.forEach { result ->
125127
ListItem(
126128
headlineContent = { Text(result) },
127129
modifier = Modifier
128130
.clickable {
129-
onResultClick(result)
131+
textFieldState.edit { replace(0, length, result) }
130132
expanded = false
131133
}
132134
.fillMaxWidth()
@@ -141,28 +143,29 @@ fun SimpleSearchBar(
141143
@Preview(showBackground = true)
142144
@Composable
143145
private fun SimpleSearchBarExample() {
144-
var query by rememberSaveable { mutableStateOf("") }
146+
// Create and remember the text field state
147+
val textFieldState = rememberTextFieldState()
145148
val items = listOf(
146149
"Cupcake", "Donut", "Eclair", "Froyo", "Gingerbread", "Honeycomb",
147150
"Ice Cream Sandwich", "Jelly Bean", "KitKat", "Lollipop"
148151
)
149152

153+
// Filter items based on the current search text
150154
val filteredItems by remember {
151155
derivedStateOf {
152-
if (query.isEmpty()) {
156+
val searchText = textFieldState.text.toString()
157+
if (searchText.isEmpty()) {
153158
emptyList()
154159
} else {
155-
items.filter { it.contains(query, ignoreCase = true) }
160+
items.filter { it.contains(searchText, ignoreCase = true) }
156161
}
157162
}
158163
}
159164

160165
SimpleSearchBar(
161-
query = query,
162-
onQueryChange = { query = it },
166+
textFieldState = textFieldState,
163167
onSearch = { /* Handle search submission */ },
164-
searchResults = filteredItems,
165-
onResultClick = { query = it }
168+
searchResults = filteredItems
166169
)
167170
}
168171

@@ -175,13 +178,15 @@ fun CustomizableSearchBar(
175178
onSearch: (String) -> Unit,
176179
searchResults: List<String>,
177180
onResultClick: (String) -> Unit,
181+
// Customization options
178182
placeholder: @Composable () -> Unit = { Text("Search") },
179183
leadingIcon: @Composable (() -> Unit)? = { Icon(Icons.Default.Search, contentDescription = "Search") },
180184
trailingIcon: @Composable (() -> Unit)? = null,
181185
supportingContent: (@Composable (String) -> Unit)? = null,
182186
leadingContent: (@Composable () -> Unit)? = null,
183187
modifier: Modifier = Modifier
184188
) {
189+
// Track expanded state of search bar
185190
var expanded by rememberSaveable { mutableStateOf(false) }
186191

187192
Box(
@@ -194,6 +199,7 @@ fun CustomizableSearchBar(
194199
.align(Alignment.TopCenter)
195200
.semantics { traversalIndex = 0f },
196201
inputField = {
202+
// Customizable input field implementation
197203
SearchBarDefaults.InputField(
198204
query = query,
199205
onQueryChange = onQueryChange,
@@ -211,6 +217,7 @@ fun CustomizableSearchBar(
211217
expanded = expanded,
212218
onExpandedChange = { expanded = it },
213219
) {
220+
// Show search results in a lazy column for better performance
214221
LazyColumn {
215222
items(count = searchResults.size) { index ->
216223
val resultText = searchResults[index]
@@ -237,13 +244,15 @@ fun CustomizableSearchBar(
237244
@Preview(showBackground = true)
238245
@Composable
239246
fun CustomizableSearchBarExample() {
247+
// Manage query state
240248
var query by rememberSaveable { mutableStateOf("") }
241249
val items = listOf(
242250
"Cupcake", "Donut", "Eclair", "Froyo", "Gingerbread", "Honeycomb",
243251
"Ice Cream Sandwich", "Jelly Bean", "KitKat", "Lollipop", "Marshmallow",
244252
"Nougat", "Oreo", "Pie"
245253
)
246254

255+
// Filter items based on query
247256
val filteredItems by remember {
248257
derivedStateOf {
249258
if (query.isEmpty()) {
@@ -261,6 +270,7 @@ fun CustomizableSearchBarExample() {
261270
onSearch = { /* Handle search submission */ },
262271
searchResults = filteredItems,
263272
onResultClick = { query = it },
273+
// Customize appearance with optional parameters
264274
placeholder = { Text("Search desserts") },
265275
leadingIcon = { Icon(Icons.Default.Search, contentDescription = "Search") },
266276
trailingIcon = { Icon(Icons.Default.MoreVert, contentDescription = "More options") },
@@ -272,7 +282,7 @@ fun CustomizableSearchBarExample() {
272282
LazyColumn(
273283
contentPadding = PaddingValues(
274284
start = 16.dp,
275-
top = 72.dp,
285+
top = 72.dp, // Provides space for the search bar
276286
end = 16.dp,
277287
bottom = 16.dp
278288
),

0 commit comments

Comments
 (0)