11package org.nsh07.wikireader.ui.homeScreen
22
3+ import android.os.Build
4+ import androidx.activity.compose.BackHandler
35import androidx.compose.animation.AnimatedContent
46import androidx.compose.animation.core.AnimationSpec
57import androidx.compose.animation.core.animateFloatAsState
@@ -68,6 +70,8 @@ import androidx.compose.runtime.derivedStateOf
6870import androidx.compose.runtime.getValue
6971import androidx.compose.runtime.mutableStateOf
7072import androidx.compose.runtime.remember
73+ import androidx.compose.runtime.rememberCoroutineScope
74+ import androidx.compose.runtime.setValue
7175import androidx.compose.ui.Alignment
7276import androidx.compose.ui.BiasAlignment
7377import androidx.compose.ui.Modifier
@@ -85,6 +89,8 @@ import androidx.compose.ui.unit.dp
8589import androidx.compose.ui.unit.max
8690import androidx.window.core.layout.WindowSizeClass
8791import coil3.ImageLoader
92+ import kotlinx.coroutines.delay
93+ import kotlinx.coroutines.launch
8894import org.nsh07.wikireader.R
8995import org.nsh07.wikireader.data.WikiPrefixSearchResult
9096import org.nsh07.wikireader.data.WikiSearchResult
@@ -130,6 +136,7 @@ fun AppSearchBar(
130136) {
131137 val focusRequester = appSearchBarState.focusRequester
132138 val haptic = LocalHapticFeedback .current
139+ val scope = rememberCoroutineScope()
133140 val colorScheme = colorScheme
134141 val history = appSearchBarState.history.toList()
135142 val size = history.size
@@ -138,6 +145,17 @@ fun AppSearchBar(
138145
139146 val (showLanguageSheet, setShowLanguageSheet) = remember { mutableStateOf(false ) }
140147
148+ var enabled by remember { mutableStateOf(Build .VERSION .SDK_INT >= 29 ) }
149+
150+ if (Build .VERSION .SDK_INT < 29 ) {
151+ LaunchedEffect (Unit ) {
152+ scope.launch {
153+ delay(1000 )
154+ enabled = true
155+ }
156+ }
157+ }
158+
141159 LaunchedEffect (textFieldState.text) {
142160 loadSearchDebounced(textFieldState.text.toString())
143161 }
@@ -174,7 +192,7 @@ fun AppSearchBar(
174192 }
175193 }
176194 } else null ,
177- enabled = searchBarEnabled,
195+ enabled = searchBarEnabled && enabled ,
178196 modifier = Modifier
179197 .fillMaxWidth()
180198 .focusRequester(focusRequester)
@@ -290,6 +308,18 @@ fun AppSearchBar(
290308 },
291309 setSearchStr = updateLanguageSearchStr
292310 )
311+
312+ if (Build .VERSION .SDK_INT < 29 ) { // Workaround to fix the infinite search bar loop bug
313+ BackHandler (true ) {
314+ scope.launch {
315+ enabled = false
316+ searchBarState.animateToCollapsed()
317+ delay(1000 )
318+ enabled = true
319+ }
320+ }
321+ }
322+
293323 AnimatedContent (textFieldState.text.trim().isEmpty()) {
294324 when (it) {
295325 true ->
0 commit comments