@@ -2,14 +2,20 @@ package com.swordfish.lemuroid.app.mobile.feature.main
2
2
3
3
import android.content.Context
4
4
import androidx.compose.animation.AnimatedVisibility
5
- import androidx.compose.animation.Crossfade
6
5
import androidx.compose.animation.fadeIn
7
6
import androidx.compose.animation.fadeOut
7
+ import androidx.compose.foundation.layout.Box
8
8
import androidx.compose.foundation.layout.Column
9
9
import androidx.compose.foundation.layout.Row
10
+ import androidx.compose.foundation.layout.fillMaxSize
10
11
import androidx.compose.foundation.layout.fillMaxWidth
12
+ import androidx.compose.foundation.layout.height
13
+ import androidx.compose.foundation.layout.padding
14
+ import androidx.compose.foundation.shape.RoundedCornerShape
15
+ import androidx.compose.foundation.text.KeyboardActions
11
16
import androidx.compose.material.icons.Icons
12
17
import androidx.compose.material.icons.automirrored.filled.ArrowBack
18
+ import androidx.compose.material.icons.filled.Search
13
19
import androidx.compose.material.icons.outlined.CloudSync
14
20
import androidx.compose.material.icons.outlined.Info
15
21
import androidx.compose.material.icons.outlined.Settings
@@ -21,13 +27,22 @@ import androidx.compose.material3.LinearProgressIndicator
21
27
import androidx.compose.material3.MaterialTheme
22
28
import androidx.compose.material3.Surface
23
29
import androidx.compose.material3.Text
30
+ import androidx.compose.material3.TextField
31
+ import androidx.compose.material3.TextFieldDefaults
24
32
import androidx.compose.material3.TopAppBar
25
33
import androidx.compose.material3.TopAppBarDefaults
26
34
import androidx.compose.material3.surfaceColorAtElevation
27
35
import androidx.compose.runtime.Composable
36
+ import androidx.compose.runtime.LaunchedEffect
37
+ import androidx.compose.runtime.remember
28
38
import androidx.compose.ui.Modifier
39
+ import androidx.compose.ui.focus.FocusRequester
40
+ import androidx.compose.ui.focus.focusRequester
41
+ import androidx.compose.ui.graphics.Color
29
42
import androidx.compose.ui.platform.LocalContext
43
+ import androidx.compose.ui.platform.LocalFocusManager
30
44
import androidx.compose.ui.res.stringResource
45
+ import androidx.compose.ui.unit.dp
31
46
import androidx.navigation.NavController
32
47
import androidx.navigation.NavHostController
33
48
import com.swordfish.lemuroid.R
@@ -42,22 +57,13 @@ fun MainTopBar(
42
57
mainUIState : MainViewModel .UiState ,
43
58
) {
44
59
Column {
45
- Surface (tonalElevation = BottomAppBarDefaults .ContainerElevation ) {
46
- Crossfade (
47
- targetState = currentRoute,
48
- label = " MainTopBar"
49
- ) { route ->
50
- when (route) {
51
- MainRoute .SEARCH -> SearchTopBar (
52
- navController = navController,
53
- onHelpPressed = onHelpPressed,
54
- mainUIState = mainUIState,
55
- onUpdateQueryString = onUpdateQueryString,
56
- )
57
- else -> LemuroidTopAppBar (route, navController, mainUIState, onHelpPressed)
58
- }
59
- }
60
- }
60
+ LemuroidTopAppBar (
61
+ route = currentRoute,
62
+ navController = navController,
63
+ mainUIState = mainUIState,
64
+ onHelpPressed = onHelpPressed,
65
+ onUpdateQueryString = onUpdateQueryString,
66
+ )
61
67
62
68
AnimatedVisibility (mainUIState.operationInProgress) {
63
69
LinearProgressIndicator (modifier = Modifier .fillMaxWidth())
@@ -72,6 +78,7 @@ fun LemuroidTopAppBar(
72
78
navController : NavController ,
73
79
mainUIState : MainViewModel .UiState ,
74
80
onHelpPressed : () -> Unit ,
81
+ onUpdateQueryString : (String ) -> Unit ,
75
82
) {
76
83
val context = LocalContext .current
77
84
val topBarColor =
@@ -80,15 +87,24 @@ fun LemuroidTopAppBar(
80
87
)
81
88
82
89
TopAppBar (
83
- title = { Text (text = stringResource(route.titleId)) },
90
+ title = {
91
+ if (route == MainRoute .SEARCH ) {
92
+ LemuroidSearchView (
93
+ mainUIState = mainUIState,
94
+ onUpdateQueryString = onUpdateQueryString,
95
+ )
96
+ } else {
97
+ Text (text = stringResource(route.titleId))
98
+ }
99
+ },
84
100
colors =
85
- TopAppBarDefaults .topAppBarColors(
86
- scrolledContainerColor = topBarColor,
87
- containerColor = topBarColor,
88
- ),
101
+ TopAppBarDefaults .topAppBarColors(
102
+ scrolledContainerColor = topBarColor,
103
+ containerColor = topBarColor,
104
+ ),
89
105
navigationIcon = {
90
106
AnimatedVisibility (
91
- visible = route? .parent != null ,
107
+ visible = route.parent != null ,
92
108
enter = fadeIn(),
93
109
exit = fadeOut(),
94
110
) {
@@ -154,3 +170,55 @@ fun LemuroidTopBarActions(
154
170
}
155
171
}
156
172
}
173
+
174
+ @Composable
175
+ private fun LemuroidSearchView (
176
+ mainUIState : MainViewModel .UiState ,
177
+ onUpdateQueryString : (String ) -> Unit ,
178
+ ) {
179
+ val focusRequester = remember { FocusRequester () }
180
+ val focusManager = LocalFocusManager .current
181
+
182
+ LaunchedEffect (Unit ) {
183
+ focusRequester.requestFocus()
184
+ }
185
+
186
+ Box (
187
+ modifier =
188
+ Modifier
189
+ .fillMaxWidth()
190
+ .height(56 .dp),
191
+ ) {
192
+ Surface (
193
+ modifier =
194
+ Modifier
195
+ .fillMaxSize()
196
+ .padding(top = 8 .dp, bottom = 8 .dp, end = 8 .dp),
197
+ shape = RoundedCornerShape (100 ),
198
+ tonalElevation = 16 .dp,
199
+ ) { }
200
+
201
+ TextField (
202
+ value = mainUIState.searchQuery,
203
+ modifier =
204
+ Modifier
205
+ .fillMaxSize()
206
+ .focusRequester(focusRequester),
207
+ textStyle = MaterialTheme .typography.bodyMedium,
208
+ leadingIcon = { Icon (Icons .Default .Search , contentDescription = null ) },
209
+ onValueChange = { onUpdateQueryString(it) },
210
+ singleLine = true ,
211
+ keyboardActions =
212
+ KeyboardActions (
213
+ onDone = { focusManager.clearFocus(true ) },
214
+ ),
215
+ colors =
216
+ TextFieldDefaults .colors(
217
+ focusedContainerColor = Color .Transparent ,
218
+ unfocusedContainerColor = Color .Transparent ,
219
+ focusedIndicatorColor = Color .Transparent ,
220
+ unfocusedIndicatorColor = Color .Transparent ,
221
+ ),
222
+ )
223
+ }
224
+ }
0 commit comments