|
| 1 | +package day.vitayuzu.neodb.ui.page.search |
| 2 | + |
| 3 | +import androidx.compose.foundation.layout.Column |
| 4 | +import androidx.compose.foundation.layout.fillMaxSize |
| 5 | +import androidx.compose.foundation.layout.fillMaxWidth |
| 6 | +import androidx.compose.foundation.layout.safeDrawingPadding |
| 7 | +import androidx.compose.foundation.lazy.LazyColumn |
| 8 | +import androidx.compose.foundation.lazy.items |
| 9 | +import androidx.compose.foundation.text.input.rememberTextFieldState |
| 10 | +import androidx.compose.material.icons.Icons |
| 11 | +import androidx.compose.material.icons.automirrored.filled.ArrowBack |
| 12 | +import androidx.compose.material.icons.filled.Search |
| 13 | +import androidx.compose.material3.ExperimentalMaterial3Api |
| 14 | +import androidx.compose.material3.Icon |
| 15 | +import androidx.compose.material3.IconButton |
| 16 | +import androidx.compose.material3.SearchBarDefaults |
| 17 | +import androidx.compose.material3.SearchBarValue |
| 18 | +import androidx.compose.material3.Surface |
| 19 | +import androidx.compose.material3.Text |
| 20 | +import androidx.compose.material3.rememberSearchBarState |
| 21 | +import androidx.compose.runtime.Composable |
| 22 | +import androidx.compose.runtime.LaunchedEffect |
| 23 | +import androidx.compose.runtime.rememberCoroutineScope |
| 24 | +import androidx.compose.runtime.snapshotFlow |
| 25 | +import androidx.compose.ui.Alignment |
| 26 | +import androidx.compose.ui.Modifier |
| 27 | +import androidx.compose.ui.res.stringResource |
| 28 | +import androidx.hilt.lifecycle.viewmodel.compose.hiltViewModel |
| 29 | +import day.vitayuzu.neodb.R |
| 30 | +import day.vitayuzu.neodb.ui.component.EntryMarkCard |
| 31 | +import kotlinx.coroutines.FlowPreview |
| 32 | +import kotlinx.coroutines.flow.collectLatest |
| 33 | +import kotlinx.coroutines.flow.debounce |
| 34 | +import kotlinx.coroutines.flow.filterNot |
| 35 | +import kotlinx.coroutines.launch |
| 36 | + |
| 37 | +@OptIn(ExperimentalMaterial3Api::class, FlowPreview::class) |
| 38 | +@Composable |
| 39 | +fun SearchPage(modifier: Modifier = Modifier, viewModel: SearchViewModel = hiltViewModel()) { |
| 40 | + val state = rememberSearchBarState(SearchBarValue.Expanded) |
| 41 | + val textFieldState = rememberTextFieldState() |
| 42 | + val scope = rememberCoroutineScope() |
| 43 | + |
| 44 | + Surface(modifier = modifier.fillMaxSize().safeDrawingPadding()) { |
| 45 | + Column(horizontalAlignment = Alignment.CenterHorizontally) { |
| 46 | + SearchBarDefaults.InputField( |
| 47 | + modifier = Modifier.safeDrawingPadding(), |
| 48 | + textFieldState = textFieldState, |
| 49 | + searchBarState = state, |
| 50 | + onSearch = {}, |
| 51 | + placeholder = { Text(stringResource(R.string.textfield_search)) }, |
| 52 | + leadingIcon = { |
| 53 | + if (state.targetValue == SearchBarValue.Collapsed) { |
| 54 | + Icon( |
| 55 | + imageVector = Icons.Default.Search, |
| 56 | + contentDescription = "Search", |
| 57 | + ) |
| 58 | + } else { |
| 59 | + IconButton( |
| 60 | + onClick = { scope.launch { state.animateToCollapsed() } }, |
| 61 | + ) { |
| 62 | + Icon( |
| 63 | + imageVector = Icons.AutoMirrored.Filled.ArrowBack, |
| 64 | + contentDescription = "Back", |
| 65 | + ) |
| 66 | + } |
| 67 | + } |
| 68 | + }, |
| 69 | + ) |
| 70 | + |
| 71 | + // Perform search request |
| 72 | + LaunchedEffect(textFieldState.text) { |
| 73 | + snapshotFlow { textFieldState.text } |
| 74 | + .debounce(500) |
| 75 | + .filterNot { it.isEmpty() } |
| 76 | + .collectLatest { query -> |
| 77 | + viewModel.onSearch(query.toString()) |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + LazyColumn( |
| 82 | + horizontalAlignment = Alignment.CenterHorizontally, |
| 83 | + modifier = Modifier.fillMaxWidth(), |
| 84 | + ) { |
| 85 | + items(viewModel.searchResult.toList(), key = { it.uuid }) { |
| 86 | + EntryMarkCard(entry = it, mark = null) |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | +} |
0 commit comments