Skip to content

Commit 015a776

Browse files
committed
textFieldValue stored in viewModel
- add systemBarsPadding. - change try-catch to runCatching for context. - minor changes in word Tags visuals.
1 parent e75fe31 commit 015a776

File tree

2 files changed

+34
-44
lines changed

2 files changed

+34
-44
lines changed

app/build.gradle.kts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ android {
1414
minSdk = 21
1515
targetSdk = 36
1616

17-
versionCode = 50
18-
versionName = "1.0-50"
17+
versionCode = 51
18+
versionName = "1.0-51"
1919

2020
}
2121

app/src/main/java/jisho/MainActivity.kt

Lines changed: 32 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,10 @@ import androidx.compose.foundation.layout.Arrangement
1111
import androidx.compose.foundation.layout.Box
1212
import androidx.compose.foundation.layout.Column
1313
import androidx.compose.foundation.layout.Row
14-
import androidx.compose.foundation.layout.WindowInsets
15-
import androidx.compose.foundation.layout.asPaddingValues
1614
import androidx.compose.foundation.layout.fillMaxSize
1715
import androidx.compose.foundation.layout.fillMaxWidth
1816
import androidx.compose.foundation.layout.padding
19-
import androidx.compose.foundation.layout.systemBars
17+
import androidx.compose.foundation.layout.systemBarsPadding
2018
import androidx.compose.foundation.lazy.LazyColumn
2119
import androidx.compose.foundation.lazy.LazyListState
2220
import androidx.compose.foundation.lazy.items
@@ -76,11 +74,11 @@ class MainActivity : ComponentActivity() {
7674
LazyColumn(
7775
Modifier
7876
.fillMaxSize()
79-
.padding(WindowInsets.systemBars.asPaddingValues()),
77+
.systemBarsPadding(),
8078
listState
8179
) {
8280
item {
83-
SearchBar(searchModel)
81+
SearchBar(searchModel, listState)
8482
}
8583
items(results) {
8684
ItemColumn(it, searchModel, listState)
@@ -93,18 +91,16 @@ class MainActivity : ComponentActivity() {
9391

9492
@Composable
9593
private fun SearchBar(
96-
searchModel: SearchModel
94+
searchModel: SearchModel,
95+
listState: LazyListState
9796
) {
98-
val search by searchModel.search.collectAsState("")
99-
val iPos by searchModel.indicatorPos.collectAsState(0)
97+
val searchState by searchModel.searchState.collectAsState(TextFieldValue())
10098
TextField(
101-
value = TextFieldValue(text = search, selection = TextRange(iPos)),
102-
onValueChange = {
103-
searchModel.search(it.text)
104-
},
99+
value = searchState,
100+
onValueChange = { searchModel.search(it.text, listState) },
105101
modifier = Modifier
106102
.fillMaxWidth()
107-
.border(3.dp, Color(0xFF6E6E6E), RoundedCornerShape(6.dp)),
103+
.border(3.dp, Color(0xFF6E6E6E)),
108104
placeholder = { Text("Search") },
109105
leadingIcon = {
110106
// @todo add keyword filter
@@ -114,9 +110,9 @@ class MainActivity : ComponentActivity() {
114110
horizontalArrangement = Arrangement.spacedBy((-6).dp),
115111
verticalAlignment = Alignment.CenterVertically // @note adapt with ic_search padding
116112
) {
117-
if (search.isNotEmpty()) {
113+
if (searchState.text.isNotEmpty()) {
118114
IconButton(
119-
onClick = { searchModel.search("") }
115+
onClick = { searchModel.search("", listState) }
120116
) {
121117
Icon(
122118
painter = painterResource(R.drawable.ic_clear),
@@ -178,7 +174,7 @@ class MainActivity : ComponentActivity() {
178174
}
179175
if (jishoData.tags.isNotEmpty()) {
180176
wordTag(
181-
"Wanikani level ${jishoData.tags.firstOrNull()?.lastOrNull().toString()}",
177+
"wanikani level ${jishoData.tags.firstOrNull()?.lastOrNull().toString()}",
182178
Color(0xFF909dc0)
183179
)
184180
}
@@ -245,15 +241,19 @@ class MainActivity : ComponentActivity() {
245241
modifier = Modifier
246242
.background(
247243
color = backgroundColor,
248-
shape = RoundedCornerShape(4.dp)
244+
shape = RoundedCornerShape(2.dp)
249245
)
250-
.padding(start = 5.dp, end = 3.dp)
246+
.padding(start = 6.dp, end = 4.dp)
251247
) {
252248
Text(
253249
text = label,
254-
color = Color(0xFF222222),
255-
fontSize = 10.sp,
256-
fontWeight = FontWeight.Bold
250+
maxLines = 1,
251+
style = MaterialTheme.typography.labelLarge.copy(
252+
color = Color(0xFF222222),
253+
fontWeight = FontWeight.Bold,
254+
letterSpacing = 0.6.sp,
255+
fontSize = 12.sp
256+
)
257257
)
258258
}
259259
}
@@ -273,49 +273,39 @@ class MainActivity : ComponentActivity() {
273273
offsetPosition
274274
).firstOrNull()?.let {
275275
searchModel.apply {
276-
search(it.item)
277-
updateIndicator(it.item.length)
278-
viewModelScope.launch {
279-
listState.scrollToItem(0)
280-
}
276+
search(it.item, listState)
281277
}
282278
}
283279
}
284280
}
285281

286282
class SearchModel : ViewModel() {
287-
private val _search = MutableStateFlow("")
288-
val search: StateFlow<String> = _search
283+
private val _searchState = MutableStateFlow(TextFieldValue())
284+
val searchState: StateFlow<TextFieldValue> = _searchState
289285

290286
private val _results = MutableStateFlow<List<JishoData>>(emptyList())
291287
val results: StateFlow<List<JishoData>> = _results
292288

293289
private var job: Job? = null
294-
fun search(query: String, page: Int = 1) {
295-
_search.value = query
296-
_indicatorPos.value = query.length
290+
fun search(query: String, listState: LazyListState, page: Int = 1) {
291+
_searchState.value = TextFieldValue(query, TextRange(query.length))
297292
job?.takeIf { it.isActive }?.cancel()
298293
if (query.isEmpty()) {
299294
_results.value = emptyList()
300295
return
301296
}
302297
job = viewModelScope.launch {
303-
try {
304-
val thisQuery = _search.value
298+
runCatching {
299+
val thisQuery = _searchState.value.text
305300
search(thisQuery, page, { word ->
306-
if (thisQuery != _search.value) throw CancellationException()
301+
if (thisQuery != _searchState.value.text) throw CancellationException()
307302
_results.value = word.data
308303
})
309-
} catch (_: CancellationException) {
310-
_results.value = emptyList()
304+
listState.scrollToItem(0)
305+
}.onFailure {
306+
if (it is CancellationException) _results.value = emptyList()
311307
}
312308
}
313309
}
314-
315-
private val _indicatorPos = MutableStateFlow(0)
316-
val indicatorPos: StateFlow<Int> = _indicatorPos
317-
fun updateIndicator(pos: Int) {
318-
_indicatorPos.value = pos
319-
}
320310
}
321311
}

0 commit comments

Comments
 (0)