TrackerSearchDialog.kt
composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/ui/screens/detail/TrackerSearchDialog.kt
1package eu.friedcutlet.yokan.ui.screens.detail23import androidx.compose.foundation.layout.Arrangement4import androidx.compose.foundation.layout.Column5import androidx.compose.foundation.layout.FlowRow6import androidx.compose.foundation.layout.fillMaxWidth7import androidx.compose.foundation.layout.heightIn8import androidx.compose.foundation.layout.padding9import androidx.compose.foundation.rememberScrollState10import androidx.compose.foundation.shape.RoundedCornerShape11import androidx.compose.foundation.text.KeyboardActions12import androidx.compose.foundation.text.KeyboardOptions13import androidx.compose.foundation.verticalScroll14import androidx.compose.material3.AlertDialog15import androidx.compose.material3.FilledTonalButton16import androidx.compose.material3.FilterChip17import androidx.compose.material3.MaterialTheme18import androidx.compose.material3.OutlinedButton19import androidx.compose.material3.OutlinedTextField20import androidx.compose.material3.Surface21import androidx.compose.material3.Text22import androidx.compose.material3.TextButton23import androidx.compose.runtime.Composable24import androidx.compose.runtime.getValue25import androidx.compose.runtime.mutableStateOf26import androidx.compose.runtime.remember27import androidx.compose.runtime.setValue28import androidx.compose.ui.Modifier29import androidx.compose.ui.text.font.FontWeight30import androidx.compose.ui.text.input.ImeAction31import androidx.compose.ui.unit.dp32import eu.friedcutlet.yokan.database.DbTrackSearch33import eu.friedcutlet.yokan.domain.Manga34import org.jetbrains.compose.resources.stringResource35import yokan.composeapp.generated.resources.Res36import yokan.composeapp.generated.resources.action_search37import yokan.composeapp.generated.resources.ui_close38import yokan.composeapp.generated.resources.ui_search_tracker39import yokan.composeapp.generated.resources.ui_title_field40import eu.friedcutlet.yokan.ui.components.ScrollbarBox41import eu.friedcutlet.yokan.ui.components.autoFocusOnMount42import eu.friedcutlet.yokan.ui.components.compactDialogWidth43import eu.friedcutlet.yokan.ui.components.compactSearchField44import eu.friedcutlet.yokan.ui.components.searchOnEnter4546@Composable47internal fun TrackerSearchDialog(48 manga: Manga,49 trackerNames: List<String>,50 search: TrackerSearchState,51 onSearch: (trackerIndex: Int, query: String) -> Unit,52 onBind: (trackerIndex: Int, result: DbTrackSearch) -> Unit,53 onDismiss: () -> Unit,54) {55 var serviceIndex by remember { mutableStateOf(0) }56 var query by remember { mutableStateOf(manga.name) }57 val loading = search.loading58 val error = search.error59 val results = search.results6061 fun submitTrackerSearch() {62 if (loading || query.isBlank() || trackerNames.isEmpty()) return63 onSearch(serviceIndex, query)64 }6566 androidx.compose.material3.AlertDialog(67 onDismissRequest = onDismiss,68 modifier = Modifier.compactDialogWidth(),69 shape = MaterialTheme.shapes.extraLarge,70 confirmButton = {71 TextButton(onClick = onDismiss) { Text(stringResource(Res.string.ui_close)) }72 },73 title = {74 Text(75 stringResource(Res.string.ui_search_tracker),76 style = MaterialTheme.typography.titleMedium,77 fontWeight = FontWeight.SemiBold,78 )79 },80 text = {81 // Tracker search can return a long result list; without a scroll82 // wrapper the AlertDialog body clips at the bottom and the83 // "Add" buttons for lower results aren't reachable. Cap height84 // so short result lists still hug their content.85 val dialogScroll = androidx.compose.foundation.rememberScrollState()86 eu.friedcutlet.yokan.ui.components.ScrollbarBox(87 state = dialogScroll,88 modifier = Modifier.fillMaxWidth().heightIn(max = 520.dp),89 ) {90 Column(91 modifier = Modifier92 .fillMaxWidth()93 .verticalScroll(dialogScroll),94 verticalArrangement = Arrangement.spacedBy(8.dp),95 ) {96 if (trackerNames.isEmpty()) {97 Text(98 "No trackers configured.",99 style = MaterialTheme.typography.bodySmall,100 color = MaterialTheme.colorScheme.onSurfaceVariant,101 )102 } else {103 FlowRow(104 horizontalArrangement = Arrangement.spacedBy(6.dp),105 verticalArrangement = Arrangement.spacedBy(6.dp),106 ) {107 trackerNames.forEachIndexed { index, trackerName ->108 FilterChip(109 selected = serviceIndex == index,110 onClick = { serviceIndex = index },111 label = { Text(trackerName) },112 )113 }114 }115 OutlinedTextField(116 value = query,117 onValueChange = { query = it },118 placeholder = { Text(stringResource(Res.string.ui_title_field)) },119 singleLine = true,120 keyboardOptions = KeyboardOptions(imeAction = ImeAction.Search),121 keyboardActions = KeyboardActions(onSearch = { submitTrackerSearch() }),122 modifier = Modifier123 .fillMaxWidth()124 .autoFocusOnMount()125 .compactSearchField()126 .searchOnEnter { submitTrackerSearch() },127 )128 FilledTonalButton(129 onClick = { submitTrackerSearch() },130 enabled = !loading && query.isNotBlank(),131 ) { Text(if (loading) "Searching…" else stringResource(Res.string.action_search)) }132 error?.let {133 Text(134 it,135 color = MaterialTheme.colorScheme.error,136 style = MaterialTheme.typography.bodySmall,137 )138 }139 if (results.isEmpty() && !loading && error == null) {140 Text(141 "Type a title and tap Search.",142 style = MaterialTheme.typography.bodySmall,143 color = MaterialTheme.colorScheme.onSurfaceVariant,144 )145 }146 results.forEach { result ->147 Surface(148 modifier = Modifier.fillMaxWidth(),149 tonalElevation = 1.dp,150 shape = RoundedCornerShape(8.dp),151 ) {152 Column(modifier = Modifier.padding(10.dp)) {153 Text(result.title.orEmpty(), fontWeight = FontWeight.SemiBold)154 Text(155 "Chapters: ${result.totalChapter ?: "?"} • ${result.status}",156 style = MaterialTheme.typography.bodySmall,157 color = MaterialTheme.colorScheme.onSurfaceVariant,158 )159 OutlinedButton(160 onClick = {161 onBind(serviceIndex, result)162 onDismiss()163 },164 modifier = Modifier.padding(top = 6.dp),165 ) { Text("Bind") }166 }167 }168 }169 }170 }171 } // end ScrollbarBox172 },173 )174}175