|
| 1 | +package com.github.damontecres.wholphin.ui.detail.search |
| 2 | + |
| 3 | +import androidx.activity.compose.BackHandler |
| 4 | +import androidx.compose.foundation.focusGroup |
| 5 | +import androidx.compose.foundation.layout.Arrangement |
| 6 | +import androidx.compose.foundation.layout.Box |
| 7 | +import androidx.compose.foundation.layout.Column |
| 8 | +import androidx.compose.foundation.layout.Row |
| 9 | +import androidx.compose.foundation.layout.fillMaxHeight |
| 10 | +import androidx.compose.foundation.layout.fillMaxWidth |
| 11 | +import androidx.compose.foundation.layout.padding |
| 12 | +import androidx.compose.runtime.Composable |
| 13 | +import androidx.compose.runtime.LaunchedEffect |
| 14 | +import androidx.compose.runtime.collectAsState |
| 15 | +import androidx.compose.runtime.getValue |
| 16 | +import androidx.compose.runtime.mutableStateOf |
| 17 | +import androidx.compose.runtime.remember |
| 18 | +import androidx.compose.runtime.saveable.rememberSaveable |
| 19 | +import androidx.compose.runtime.setValue |
| 20 | +import androidx.compose.ui.Alignment |
| 21 | +import androidx.compose.ui.Modifier |
| 22 | +import androidx.compose.ui.focus.FocusDirection |
| 23 | +import androidx.compose.ui.focus.FocusRequester |
| 24 | +import androidx.compose.ui.focus.focusRequester |
| 25 | +import androidx.compose.ui.focus.focusRestorer |
| 26 | +import androidx.compose.ui.focus.onFocusChanged |
| 27 | +import androidx.compose.ui.input.key.Key |
| 28 | +import androidx.compose.ui.input.key.KeyEventType |
| 29 | +import androidx.compose.ui.input.key.key |
| 30 | +import androidx.compose.ui.input.key.onPreviewKeyEvent |
| 31 | +import androidx.compose.ui.input.key.type |
| 32 | +import androidx.compose.ui.platform.LocalFocusManager |
| 33 | +import androidx.compose.ui.platform.LocalSoftwareKeyboardController |
| 34 | +import androidx.compose.ui.res.stringResource |
| 35 | +import androidx.compose.ui.unit.dp |
| 36 | +import androidx.compose.ui.window.DialogProperties |
| 37 | +import androidx.hilt.lifecycle.viewmodel.compose.hiltViewModel |
| 38 | +import androidx.lifecycle.compose.LifecycleResumeEffect |
| 39 | +import androidx.tv.material3.MaterialTheme |
| 40 | +import androidx.tv.material3.Text |
| 41 | +import com.github.damontecres.wholphin.R |
| 42 | +import com.github.damontecres.wholphin.data.model.BaseItem |
| 43 | +import com.github.damontecres.wholphin.ui.Cards |
| 44 | +import com.github.damontecres.wholphin.ui.cards.ItemRow |
| 45 | +import com.github.damontecres.wholphin.ui.cards.SeasonCard |
| 46 | +import com.github.damontecres.wholphin.ui.components.BasicDialog |
| 47 | +import com.github.damontecres.wholphin.ui.components.ErrorMessage |
| 48 | +import com.github.damontecres.wholphin.ui.components.SearchEditTextBox |
| 49 | +import com.github.damontecres.wholphin.ui.components.VoiceSearchButton |
| 50 | +import com.github.damontecres.wholphin.ui.main.SearchResult |
| 51 | +import kotlinx.coroutines.delay |
| 52 | +import org.jellyfin.sdk.model.api.BaseItemKind |
| 53 | + |
| 54 | +@Composable |
| 55 | +fun SearchForContent( |
| 56 | + searchType: BaseItemKind, |
| 57 | + onClick: (BaseItem) -> Unit, |
| 58 | + modifier: Modifier = Modifier, |
| 59 | + viewModel: SearchForViewModel = hiltViewModel(key = searchType.serialName), |
| 60 | +) { |
| 61 | + val focusManager = LocalFocusManager.current |
| 62 | + val keyboardController = LocalSoftwareKeyboardController.current |
| 63 | + val state by viewModel.state.collectAsState() |
| 64 | + |
| 65 | + var query by rememberSaveable { mutableStateOf("") } |
| 66 | + val searchFocusRequester = remember { FocusRequester() } |
| 67 | + val focusRequester = remember { FocusRequester() } |
| 68 | + |
| 69 | + var immediateSearchQuery by rememberSaveable { mutableStateOf<String?>(null) } |
| 70 | + |
| 71 | + LifecycleResumeEffect(Unit) { |
| 72 | + onPauseOrDispose { |
| 73 | + viewModel.voiceInputManager.stopListening() |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + fun triggerImmediateSearch(searchQuery: String) { |
| 78 | + immediateSearchQuery = searchQuery |
| 79 | + viewModel.search(searchType, searchQuery) |
| 80 | + } |
| 81 | + |
| 82 | + LaunchedEffect(query) { |
| 83 | + when { |
| 84 | + immediateSearchQuery == query -> { |
| 85 | + immediateSearchQuery = null |
| 86 | + } |
| 87 | + |
| 88 | + else -> { |
| 89 | + delay(750L) |
| 90 | + viewModel.search(searchType, query) |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + Column( |
| 95 | + verticalArrangement = Arrangement.spacedBy(8.dp), |
| 96 | + modifier = modifier, |
| 97 | + ) { |
| 98 | + Box( |
| 99 | + contentAlignment = Alignment.Center, |
| 100 | + modifier = Modifier.fillMaxWidth(), |
| 101 | + ) { |
| 102 | + var isSearchActive by remember { mutableStateOf(false) } |
| 103 | + var isTextFieldFocused by remember { mutableStateOf(false) } |
| 104 | + val textFieldFocusRequester = remember { FocusRequester() } |
| 105 | + |
| 106 | + BackHandler(isTextFieldFocused) { |
| 107 | + when { |
| 108 | + isSearchActive -> { |
| 109 | + isSearchActive = false |
| 110 | + keyboardController?.hide() |
| 111 | + } |
| 112 | + |
| 113 | + else -> { |
| 114 | + focusManager.moveFocus(FocusDirection.Next) |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + Row( |
| 120 | + horizontalArrangement = Arrangement.spacedBy(12.dp), |
| 121 | + verticalAlignment = Alignment.CenterVertically, |
| 122 | + modifier = |
| 123 | + Modifier |
| 124 | + .focusGroup() |
| 125 | + .focusRestorer(textFieldFocusRequester) |
| 126 | + .focusRequester(searchFocusRequester), |
| 127 | + ) { |
| 128 | + VoiceSearchButton( |
| 129 | + onSpeechResult = { spokenText -> |
| 130 | + query = spokenText |
| 131 | + triggerImmediateSearch(spokenText) |
| 132 | + }, |
| 133 | + voiceInputManager = viewModel.voiceInputManager, |
| 134 | + ) |
| 135 | + |
| 136 | + SearchEditTextBox( |
| 137 | + value = query, |
| 138 | + onValueChange = { |
| 139 | + isSearchActive = true |
| 140 | + query = it |
| 141 | + }, |
| 142 | + onSearchClick = { triggerImmediateSearch(query) }, |
| 143 | + readOnly = !isSearchActive, |
| 144 | + modifier = |
| 145 | + Modifier |
| 146 | + .focusRequester(textFieldFocusRequester) |
| 147 | + .onFocusChanged { state -> |
| 148 | + isTextFieldFocused = state.isFocused |
| 149 | + if (!state.isFocused) isSearchActive = false |
| 150 | + }.onPreviewKeyEvent { event -> |
| 151 | + val isActivationKey = |
| 152 | + event.key in listOf(Key.DirectionCenter, Key.Enter) |
| 153 | + if (event.type == KeyEventType.KeyUp && isActivationKey && !isSearchActive) { |
| 154 | + isSearchActive = true |
| 155 | + keyboardController?.show() |
| 156 | + true |
| 157 | + } else { |
| 158 | + false |
| 159 | + } |
| 160 | + }, |
| 161 | + ) |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + when (val st = state.results) { |
| 166 | + is SearchResult.Error -> { |
| 167 | + ErrorMessage("Error", st.ex) |
| 168 | + } |
| 169 | + |
| 170 | + SearchResult.NoQuery -> { |
| 171 | + // no-op |
| 172 | + } |
| 173 | + |
| 174 | + SearchResult.Searching -> { |
| 175 | + Text( |
| 176 | + text = stringResource(R.string.searching), |
| 177 | + ) |
| 178 | + } |
| 179 | + |
| 180 | + is SearchResult.SuccessSeerr -> { |
| 181 | + Text( |
| 182 | + text = "Not supported", |
| 183 | + color = MaterialTheme.colorScheme.error, |
| 184 | + ) |
| 185 | + } |
| 186 | + |
| 187 | + is SearchResult.Success -> { |
| 188 | + if (st.items.isEmpty()) { |
| 189 | + Text( |
| 190 | + text = stringResource(R.string.no_results), |
| 191 | + ) |
| 192 | + } else { |
| 193 | + val titleRes = |
| 194 | + remember { |
| 195 | + when (searchType) { |
| 196 | + BaseItemKind.BOX_SET -> R.string.collections |
| 197 | + BaseItemKind.PLAYLIST -> R.string.playlists |
| 198 | + else -> null |
| 199 | + } |
| 200 | + } |
| 201 | + ItemRow( |
| 202 | + title = titleRes?.let { stringResource(it) } ?: "", |
| 203 | + items = st.items, |
| 204 | + onClickItem = { _, item -> onClick.invoke(item) }, |
| 205 | + onLongClickItem = { _, _ -> }, |
| 206 | + modifier = Modifier.focusRequester(focusRequester), |
| 207 | + cardContent = { index, item, mod, onClick, onLongClick -> |
| 208 | + SeasonCard( |
| 209 | + item = item, |
| 210 | + onClick = { |
| 211 | + onClick.invoke() |
| 212 | + }, |
| 213 | + onLongClick = onLongClick, |
| 214 | + imageHeight = Cards.height2x3, |
| 215 | + modifier = mod, |
| 216 | + ) |
| 217 | + }, |
| 218 | + ) |
| 219 | + } |
| 220 | + } |
| 221 | + } |
| 222 | + } |
| 223 | +} |
| 224 | + |
| 225 | +@Composable |
| 226 | +fun SearchForDialog( |
| 227 | + onDismissRequest: () -> Unit, |
| 228 | + searchType: BaseItemKind, |
| 229 | + onClick: (BaseItem) -> Unit, |
| 230 | +) { |
| 231 | + BasicDialog( |
| 232 | + onDismissRequest = onDismissRequest, |
| 233 | + properties = |
| 234 | + DialogProperties( |
| 235 | + usePlatformDefaultWidth = false, |
| 236 | + ), |
| 237 | + ) { |
| 238 | + SearchForContent( |
| 239 | + searchType = searchType, |
| 240 | + onClick = onClick, |
| 241 | + modifier = |
| 242 | + Modifier |
| 243 | + .padding(8.dp) |
| 244 | + .fillMaxWidth(.8f) |
| 245 | + .fillMaxHeight(.66f), |
| 246 | + ) |
| 247 | + } |
| 248 | +} |
0 commit comments