Skip to content

Commit 63c22ea

Browse files
committed
dynamic divider
- add better updating during search change - rename wordTag to categoryTag since that made more sense - add padding (4.dp) to categoryTag so it's not on edge of screen
1 parent 4cb2efa commit 63c22ea

File tree

2 files changed

+23
-35
lines changed

2 files changed

+23
-35
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 = 57
18-
versionName = "1.0-57"
17+
versionCode = 58
18+
versionName = "1.0-58"
1919

2020
}
2121

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

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ import androidx.lifecycle.viewModelScope
5858
import kotlinx.coroutines.Job
5959
import kotlinx.coroutines.flow.MutableStateFlow
6060
import kotlinx.coroutines.flow.StateFlow
61+
import kotlinx.coroutines.flow.update
6162
import kotlinx.coroutines.launch
6263
import kotlin.coroutines.cancellation.CancellationException
6364

@@ -87,6 +88,7 @@ class MainActivity : ComponentActivity() {
8788
}
8889
items(results) {
8990
ItemColumn(it, searchModel, listState)
91+
HorizontalDivider()
9092
}
9193
}
9294
}
@@ -111,29 +113,13 @@ class MainActivity : ComponentActivity() {
111113
// @todo add keyword filter
112114
},
113115
trailingIcon = {
114-
Row(
115-
horizontalArrangement = Arrangement.spacedBy((-6).dp),
116-
verticalAlignment = Alignment.CenterVertically // @note adapt with ic_search padding
117-
) {
118-
if (searchState.text.isNotEmpty()) {
119-
IconButton(
120-
onClick = { searchModel.search("", listState) }
121-
) {
122-
Icon(
123-
painter = painterResource(R.drawable.ic_clear),
124-
contentDescription = "Clear"
125-
)
126-
}
127-
}
116+
if (searchState.text.isNotEmpty()) {
128117
IconButton(
129-
onClick = { }, // @todo
130-
modifier = Modifier
131-
.padding(6.dp)
132-
.background(color = Color(0xFF6A6A6A))
118+
onClick = { searchModel.resetField() }
133119
) {
134120
Icon(
135-
painter = painterResource(R.drawable.ic_search),
136-
contentDescription = "Search"
121+
painter = painterResource(R.drawable.ic_clear),
122+
contentDescription = "Clear"
137123
)
138124
}
139125
}
@@ -167,19 +153,20 @@ class MainActivity : ComponentActivity() {
167153
)
168154
}
169155
Row(
170-
horizontalArrangement = Arrangement.spacedBy(6.dp)
156+
horizontalArrangement = Arrangement.spacedBy(6.dp),
157+
modifier = Modifier.padding(4.dp)
171158
) {
172159
if (jishoData.isCommon) {
173-
wordTag("common word", Color(0xFF8abc83))
160+
categoryTag("common word", Color(0xFF8abc83))
174161
}
175162
if (jishoData.jlpt.isNotEmpty()) {
176-
wordTag(
163+
categoryTag(
177164
jishoData.jlpt.firstOrNull().orEmpty().replace("-", " "),
178165
Color(0xFF909dc0)
179166
)
180167
}
181168
if (jishoData.tags.isNotEmpty()) {
182-
wordTag(
169+
categoryTag(
183170
"wanikani level ${jishoData.tags.firstOrNull()?.lastOrNull().toString()}",
184171
Color(0xFF909dc0)
185172
)
@@ -235,11 +222,10 @@ class MainActivity : ComponentActivity() {
235222
style = TextStyle(fontSize = 20.sp)
236223
)
237224
}
238-
HorizontalDivider()
239225
}
240226

241227
@Composable
242-
private fun wordTag(
228+
private fun categoryTag(
243229
label: String,
244230
backgroundColor: Color
245231
) {
@@ -253,7 +239,6 @@ class MainActivity : ComponentActivity() {
253239
) {
254240
Text(
255241
text = label,
256-
maxLines = 1,
257242
style = MaterialTheme.typography.labelLarge.copy(
258243
color = Color(0xFF222222),
259244
fontWeight = FontWeight.Bold,
@@ -294,24 +279,27 @@ class MainActivity : ComponentActivity() {
294279

295280
private var job: Job? = null
296281
fun search(query: String, listState: LazyListState, page: Int = 1) {
297-
_searchState.value = TextFieldValue(query, TextRange(query.length))
298-
job?.takeIf { it.isActive }?.cancel()
299-
if (query.isEmpty()) {
300-
_results.value = emptyList()
282+
_searchState.update { it.copy(text = query, selection = TextRange(query.length)) }
283+
job?.cancel()
284+
if (query.isBlank()) {
285+
_results.update { emptyList() }
301286
return
302287
}
303288
job = viewModelScope.launch {
304289
runCatching {
305290
val thisQuery = _searchState.value.text
306291
search(thisQuery, page, { word ->
307292
if (thisQuery != _searchState.value.text) throw CancellationException()
308-
_results.value = word.data
293+
_results.update { word.data }
309294
})
310295
listState.scrollToItem(0)
311296
}.onFailure {
312-
if (it is CancellationException) _results.value = emptyList()
297+
if (it is CancellationException) _results.update { emptyList() }
313298
}
314299
}
315300
}
301+
fun resetField() {
302+
_searchState.update { TextFieldValue() }
303+
}
316304
}
317305
}

0 commit comments

Comments
 (0)