@@ -58,6 +58,7 @@ import androidx.lifecycle.viewModelScope
58
58
import kotlinx.coroutines.Job
59
59
import kotlinx.coroutines.flow.MutableStateFlow
60
60
import kotlinx.coroutines.flow.StateFlow
61
+ import kotlinx.coroutines.flow.update
61
62
import kotlinx.coroutines.launch
62
63
import kotlin.coroutines.cancellation.CancellationException
63
64
@@ -87,6 +88,7 @@ class MainActivity : ComponentActivity() {
87
88
}
88
89
items(results) {
89
90
ItemColumn (it, searchModel, listState)
91
+ HorizontalDivider ()
90
92
}
91
93
}
92
94
}
@@ -111,29 +113,13 @@ class MainActivity : ComponentActivity() {
111
113
// @todo add keyword filter
112
114
},
113
115
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()) {
128
117
IconButton (
129
- onClick = { }, // @todo
130
- modifier = Modifier
131
- .padding(6 .dp)
132
- .background(color = Color (0xFF6A6A6A ))
118
+ onClick = { searchModel.resetField() }
133
119
) {
134
120
Icon (
135
- painter = painterResource(R .drawable.ic_search ),
136
- contentDescription = " Search "
121
+ painter = painterResource(R .drawable.ic_clear ),
122
+ contentDescription = " Clear "
137
123
)
138
124
}
139
125
}
@@ -167,19 +153,20 @@ class MainActivity : ComponentActivity() {
167
153
)
168
154
}
169
155
Row (
170
- horizontalArrangement = Arrangement .spacedBy(6 .dp)
156
+ horizontalArrangement = Arrangement .spacedBy(6 .dp),
157
+ modifier = Modifier .padding(4 .dp)
171
158
) {
172
159
if (jishoData.isCommon) {
173
- wordTag (" common word" , Color (0xFF8abc83 ))
160
+ categoryTag (" common word" , Color (0xFF8abc83 ))
174
161
}
175
162
if (jishoData.jlpt.isNotEmpty()) {
176
- wordTag (
163
+ categoryTag (
177
164
jishoData.jlpt.firstOrNull().orEmpty().replace(" -" , " " ),
178
165
Color (0xFF909dc0 )
179
166
)
180
167
}
181
168
if (jishoData.tags.isNotEmpty()) {
182
- wordTag (
169
+ categoryTag (
183
170
" wanikani level ${jishoData.tags.firstOrNull()?.lastOrNull().toString()} " ,
184
171
Color (0xFF909dc0 )
185
172
)
@@ -235,11 +222,10 @@ class MainActivity : ComponentActivity() {
235
222
style = TextStyle (fontSize = 20 .sp)
236
223
)
237
224
}
238
- HorizontalDivider ()
239
225
}
240
226
241
227
@Composable
242
- private fun wordTag (
228
+ private fun categoryTag (
243
229
label : String ,
244
230
backgroundColor : Color
245
231
) {
@@ -253,7 +239,6 @@ class MainActivity : ComponentActivity() {
253
239
) {
254
240
Text (
255
241
text = label,
256
- maxLines = 1 ,
257
242
style = MaterialTheme .typography.labelLarge.copy(
258
243
color = Color (0xFF222222 ),
259
244
fontWeight = FontWeight .Bold ,
@@ -294,24 +279,27 @@ class MainActivity : ComponentActivity() {
294
279
295
280
private var job: Job ? = null
296
281
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() }
301
286
return
302
287
}
303
288
job = viewModelScope.launch {
304
289
runCatching {
305
290
val thisQuery = _searchState .value.text
306
291
search(thisQuery, page, { word ->
307
292
if (thisQuery != _searchState .value.text) throw CancellationException ()
308
- _results .value = word.data
293
+ _results .update { word.data }
309
294
})
310
295
listState.scrollToItem(0 )
311
296
}.onFailure {
312
- if (it is CancellationException ) _results .value = emptyList()
297
+ if (it is CancellationException ) _results .update { emptyList() }
313
298
}
314
299
}
315
300
}
301
+ fun resetField () {
302
+ _searchState .update { TextFieldValue () }
303
+ }
316
304
}
317
305
}
0 commit comments