Skip to content

Commit dfd78ec

Browse files
committed
fix(search): search restored query, fix ui
1 parent ff2a33d commit dfd78ec

File tree

4 files changed

+39
-5
lines changed

4 files changed

+39
-5
lines changed

core-ui/src/main/java/com/hoc/flowmvi/core_ui/AppBarTextField.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import androidx.compose.ui.focus.focusRequester
2727
import androidx.compose.ui.graphics.Color
2828
import androidx.compose.ui.graphics.SolidColor
2929
import androidx.compose.ui.graphics.takeOrElse
30+
import androidx.compose.ui.text.TextRange
3031
import androidx.compose.ui.text.TextStyle
3132
import androidx.compose.ui.text.input.TextFieldValue
3233
import androidx.compose.ui.text.input.VisualTransformation
@@ -55,7 +56,14 @@ fun AppBarTextField(
5556
// Holds the latest internal TextFieldValue state. We need to keep it to have the correct value
5657
// of the composition.
5758
// Set the correct cursor position when this composable is first initialized
58-
var textFieldValueState by remember { mutableStateOf(TextFieldValue(text = value)) }
59+
var textFieldValueState by remember {
60+
mutableStateOf(
61+
TextFieldValue(
62+
text = value,
63+
selection = TextRange(value.length),
64+
),
65+
)
66+
}
5967

6068
// Holds the latest TextFieldValue that BasicTextField was recomposed with. We couldn't simply
6169
// pass `TextFieldValue(text = value)` to the CoreTextField because we need to preserve the

feature-search/src/main/java/com/hoc/flowmvi/ui/search/SearchScreen.kt

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import androidx.compose.foundation.layout.Arrangement
88
import androidx.compose.foundation.layout.PaddingValues
99
import androidx.compose.foundation.layout.fillMaxSize
1010
import androidx.compose.foundation.layout.fillMaxWidth
11+
import androidx.compose.foundation.layout.padding
1112
import androidx.compose.foundation.lazy.grid.GridCells
13+
import androidx.compose.foundation.lazy.grid.GridItemSpan
1214
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
1315
import androidx.compose.foundation.lazy.grid.items
1416
import androidx.compose.foundation.text.KeyboardActions
@@ -19,6 +21,8 @@ import androidx.compose.material.icons.filled.Search
1921
import androidx.compose.material3.ExperimentalMaterial3Api
2022
import androidx.compose.material3.Icon
2123
import androidx.compose.material3.IconButton
24+
import androidx.compose.material3.MaterialTheme
25+
import androidx.compose.material3.Text
2226
import androidx.compose.material3.TopAppBarDefaults
2327
import androidx.compose.runtime.Composable
2428
import androidx.compose.runtime.LaunchedEffect
@@ -36,6 +40,7 @@ import androidx.compose.ui.res.stringResource
3640
import androidx.compose.ui.text.input.ImeAction
3741
import androidx.compose.ui.tooling.preview.Preview
3842
import androidx.compose.ui.unit.dp
43+
import androidx.compose.ui.unit.sp
3944
import androidx.hilt.navigation.compose.hiltViewModel
4045
import androidx.lifecycle.Lifecycle
4146
import androidx.lifecycle.compose.ExperimentalLifecycleComposeApi
@@ -190,7 +195,10 @@ private fun SearchContent(
190195
) {
191196
UsersGrid(
192197
modifier = modifier,
193-
userItems = viewState.users
198+
userItems = viewState.users,
199+
submittedQuery = viewState
200+
.submittedQuery
201+
.takeUnless { viewState.isLoading || viewState.submittedQuery.isBlank() }
194202
)
195203

196204
AnimatedVisibility(
@@ -223,6 +231,7 @@ private fun SearchContent(
223231
@Composable
224232
private fun UsersGrid(
225233
userItems: ImmutableList<UserItem>,
234+
submittedQuery: String?,
226235
modifier: Modifier = Modifier,
227236
) {
228237
LazyVerticalGrid(
@@ -234,6 +243,20 @@ private fun UsersGrid(
234243
verticalArrangement = Arrangement.spacedBy(8.dp),
235244
horizontalArrangement = Arrangement.spacedBy(8.dp),
236245
) {
246+
if (submittedQuery != null) {
247+
item(
248+
span = { GridItemSpan(maxCurrentLineSpan) },
249+
) {
250+
Text(
251+
modifier = Modifier.padding(vertical = 8.dp),
252+
text = "Search result for '$submittedQuery'",
253+
style = MaterialTheme.typography
254+
.titleLarge
255+
.copy(fontSize = 20.sp),
256+
)
257+
}
258+
}
259+
237260
items(userItems, key = { it.id }) { userItem ->
238261
UserItemCell(
239262
userItem = userItem
@@ -260,7 +283,8 @@ fun PreviewUsersGrid() {
260283
avatar = "avatar/$id",
261284
).valueOr { throw IllegalArgumentException() }
262285
)
263-
}.toImmutableList()
286+
}.toImmutableList(),
287+
submittedQuery = "hoc081098",
264288
)
265289
}
266290
}

feature-search/src/main/java/com/hoc/flowmvi/ui/search/SearchVM.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class SearchVM @Inject constructor(
4949

5050
viewState = intentSharedFlow
5151
.debugLog("ViewIntent")
52-
.toPartialStateChangeFlow()
52+
.toPartialStateChangeFlow(initialVS)
5353
.debugLog("PartialStateChange")
5454
.onEach { sendEvent(it.toSingleEventOrNull() ?: return@onEach) }
5555
.scan(initialVS) { state, change -> change.reduce(state) }
@@ -61,15 +61,17 @@ class SearchVM @Inject constructor(
6161
}
6262
}
6363

64-
private fun SharedFlow<ViewIntent>.toPartialStateChangeFlow(): Flow<PartialStateChange> {
64+
private fun SharedFlow<ViewIntent>.toPartialStateChangeFlow(initialVS: ViewState): Flow<PartialStateChange> {
6565
val queryFlow = filterIsInstance<ViewIntent.Search>()
6666
.map { it.query }
6767
.shareWhileSubscribed()
6868

6969
val searchableQueryFlow = queryFlow
7070
.debounce(SEARCH_DEBOUNCE_DURATION)
71+
.startWith(initialVS.originalQuery)
7172
.filter { it.isNotBlank() }
7273
.distinctUntilChanged()
74+
.debugLog(">>> SearchableQuery")
7375
.shareWhileSubscribed()
7476

7577
return merge(

gradlew

100644100755
File mode changed.

0 commit comments

Comments
 (0)