|
| 1 | +/* |
| 2 | + * Copyright 2024 The Android Open Source Project |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.example.compose.snippets.text |
| 18 | + |
| 19 | +import androidx.compose.foundation.layout.Column |
| 20 | +import androidx.compose.foundation.layout.fillMaxSize |
| 21 | +import androidx.compose.foundation.layout.fillMaxWidth |
| 22 | +import androidx.compose.foundation.layout.padding |
| 23 | +import androidx.compose.foundation.lazy.LazyColumn |
| 24 | +import androidx.compose.material3.ListItem |
| 25 | +import androidx.compose.material3.OutlinedTextField |
| 26 | +import androidx.compose.material3.Text |
| 27 | +import androidx.compose.runtime.Composable |
| 28 | +import androidx.compose.runtime.getValue |
| 29 | +import androidx.compose.runtime.mutableStateOf |
| 30 | +import androidx.compose.runtime.saveable.rememberSaveable |
| 31 | +import androidx.compose.runtime.setValue |
| 32 | +import androidx.compose.ui.Modifier |
| 33 | +import androidx.compose.ui.tooling.preview.Preview |
| 34 | +import androidx.compose.ui.unit.dp |
| 35 | +import androidx.lifecycle.ViewModel |
| 36 | +import androidx.lifecycle.compose.collectAsStateWithLifecycle |
| 37 | +import androidx.lifecycle.viewmodel.compose.viewModel |
| 38 | +import kotlinx.coroutines.flow.MutableStateFlow |
| 39 | +import kotlinx.coroutines.flow.StateFlow |
| 40 | + |
| 41 | +// [START android_compose_text_filtertextviewmodel] |
| 42 | +class FilterTextViewModel : ViewModel() { |
| 43 | + private val items = listOf( |
| 44 | + "Cupcake", |
| 45 | + "Donut", |
| 46 | + "Eclair", |
| 47 | + "Froyo", |
| 48 | + "Gingerbread", |
| 49 | + "Honeycomb", |
| 50 | + "Ice Cream Sandwich" |
| 51 | + ) |
| 52 | + |
| 53 | + private val _filteredItems = MutableStateFlow(items) |
| 54 | + var filteredItems: StateFlow<List<String>> = _filteredItems |
| 55 | + |
| 56 | + fun filterText(input: String) { |
| 57 | + // This filter returns the full items list when input is an empty string. |
| 58 | + _filteredItems.value = items.filter { it.contains(input, ignoreCase = true) } |
| 59 | + } |
| 60 | +} |
| 61 | +// [END android_compose_text_filtertextviewmodel] |
| 62 | + |
| 63 | +// [START android_compose_text_filtertextview] |
| 64 | +@Composable |
| 65 | +fun FilterTextView(modifier: Modifier = Modifier, viewModel: FilterTextViewModel = viewModel()) { |
| 66 | + val filteredItems by viewModel.filteredItems.collectAsStateWithLifecycle() |
| 67 | + var text by rememberSaveable { mutableStateOf("") } |
| 68 | + |
| 69 | + Column( |
| 70 | + modifier = Modifier |
| 71 | + .fillMaxSize() |
| 72 | + .padding(all = 10.dp) |
| 73 | + ) { |
| 74 | + OutlinedTextField( |
| 75 | + value = text, |
| 76 | + onValueChange = { |
| 77 | + text = it |
| 78 | + viewModel.filterText(text) |
| 79 | + }, |
| 80 | + label = { Text("Filter Text") }, |
| 81 | + modifier = Modifier.fillMaxWidth() |
| 82 | + ) |
| 83 | + |
| 84 | + LazyColumn { |
| 85 | + items( |
| 86 | + count = filteredItems.size, |
| 87 | + key = { index -> filteredItems[index] } |
| 88 | + ) { |
| 89 | + ListItem( |
| 90 | + headlineContent = { Text(filteredItems[it]) }, |
| 91 | + modifier = Modifier |
| 92 | + .fillParentMaxWidth() |
| 93 | + .padding(10.dp) |
| 94 | + ) |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | +} |
| 99 | +// [END android_compose_text_filtertextview] |
| 100 | + |
| 101 | +@Preview(showBackground = true) |
| 102 | +@Composable |
| 103 | +private fun FilterTextViewPreview() { |
| 104 | + FilterTextView() |
| 105 | +} |
0 commit comments