@@ -3,6 +3,7 @@ package jisho
3
3
import android.os.Bundle
4
4
import androidx.activity.ComponentActivity
5
5
import androidx.activity.compose.setContent
6
+ import androidx.activity.viewModels
6
7
import androidx.compose.foundation.background
7
8
import androidx.compose.foundation.border
8
9
import androidx.compose.foundation.gestures.detectTapGestures
@@ -60,7 +61,7 @@ class MainActivity : ComponentActivity() {
60
61
override fun onCreate (savedInstanceState : Bundle ? ) {
61
62
getActionBar()?.hide()
62
63
super .onCreate(savedInstanceState)
63
- val viewModel = Model ()
64
+ val searchModel : SearchModel by viewModels ()
64
65
setContent {
65
66
Theme {
66
67
Surface {
@@ -71,17 +72,17 @@ class MainActivity : ComponentActivity() {
71
72
horizontalAlignment = Alignment .CenterHorizontally
72
73
) {
73
74
SearchBar (
74
- viewModel
75
- ) { viewModel.modelSearch (it) }
76
- val results by viewModel .results.collectAsState(emptyList())
77
- if (results.isNotEmpty()) Results (results, viewModel )
75
+ searchModel
76
+ ) { searchModel.search (it) }
77
+ val results by searchModel .results.collectAsState(emptyList())
78
+ if (results.isNotEmpty()) Results (results, searchModel )
78
79
}
79
80
}
80
81
}
81
82
}
82
83
}
83
84
84
- class Model : ViewModel () {
85
+ class SearchModel : ViewModel () {
85
86
private val _search = MutableStateFlow (" " )
86
87
val search: StateFlow <String > get() = _search
87
88
@@ -93,11 +94,14 @@ class MainActivity : ComponentActivity() {
93
94
94
95
private var job: Job ? = null
95
96
96
- fun modelSearch (query : String , page : Int = 1) {
97
+ fun search (query : String , page : Int = 1) {
97
98
job?.cancel()
98
- if (query.isEmpty()) return
99
99
_search .value = query
100
100
_indicatorPos .value = query.length
101
+ if (query.isEmpty()) {
102
+ _results .update { emptyList() }
103
+ return
104
+ }
101
105
job = viewModelScope.launch {
102
106
val thisQuery = _search .value
103
107
search(query, page, { word ->
@@ -107,32 +111,28 @@ class MainActivity : ComponentActivity() {
107
111
})
108
112
}
109
113
}
114
+
110
115
fun updateIndicator (pos : Int ) {
111
116
_indicatorPos .value = pos
112
117
}
113
118
}
114
119
115
120
@Composable
116
121
private fun SearchBar (
117
- viewModel : Model ,
122
+ searchModel : SearchModel ,
118
123
onValueChange : (String ) -> Unit
119
124
) {
120
- val search by viewModel .search.collectAsState(" " )
121
- val iPos by viewModel .indicatorPos.collectAsState(0 )
125
+ val search by searchModel .search.collectAsState(" " )
126
+ val iPos by searchModel .indicatorPos.collectAsState(0 )
122
127
TextField (
123
128
value = TextFieldValue (text = search, selection = TextRange (iPos)),
124
129
onValueChange = { newValue ->
125
130
onValueChange(newValue.text)
126
131
},
127
132
modifier = Modifier
128
133
.fillMaxWidth()
129
- .padding(top = 16 .dp, bottom = 8 .dp)
130
134
.border(3 .dp, Color (0xFF6E6E6E ), RoundedCornerShape (6 .dp)),
131
135
placeholder = { Text (" Search" ) },
132
- colors = TextFieldDefaults .colors(
133
- unfocusedIndicatorColor = Color .Transparent ,
134
- focusedIndicatorColor = Color .Transparent
135
- ),
136
136
leadingIcon = {
137
137
// @todo add keyword filter
138
138
},
@@ -163,13 +163,18 @@ class MainActivity : ComponentActivity() {
163
163
)
164
164
}
165
165
}
166
- }
166
+ },
167
+ singleLine = true ,
168
+ colors = TextFieldDefaults .colors(
169
+ unfocusedIndicatorColor = Color .Transparent ,
170
+ focusedIndicatorColor = Color .Transparent
171
+ )
167
172
)
168
173
}
169
174
170
175
@Composable
171
- fun Results (results : List <JishoData >, viewModel : Model ) {
172
- val search by viewModel .search.collectAsState()
176
+ fun Results (results : List <JishoData >, searchModel : SearchModel ) {
177
+ val search by searchModel .search.collectAsState()
173
178
LazyColumn (
174
179
modifier = Modifier
175
180
.fillMaxSize()
@@ -199,7 +204,7 @@ class MainActivity : ComponentActivity() {
199
204
text = annotatedString,
200
205
modifier = Modifier
201
206
.pointerInput(annotatedString) {
202
- tapGesture(viewModel , annotatedString, textLayoutResult)
207
+ tapGesture(searchModel , annotatedString, textLayoutResult)
203
208
}
204
209
.padding(bottom = 18 .dp),
205
210
onTextLayout = { textLayoutResult = it },
@@ -274,7 +279,7 @@ class MainActivity : ComponentActivity() {
274
279
text = annotatedString,
275
280
modifier = Modifier
276
281
.pointerInput(annotatedString) {
277
- tapGesture(viewModel , annotatedString, textLayoutResult)
282
+ tapGesture(searchModel , annotatedString, textLayoutResult)
278
283
}
279
284
.padding(10 .dp),
280
285
onTextLayout = { textLayoutResult = it },
@@ -309,7 +314,7 @@ class MainActivity : ComponentActivity() {
309
314
}
310
315
311
316
private suspend fun PointerInputScope.tapGesture (
312
- viewModel : Model ,
317
+ searchModel : SearchModel ,
313
318
annotatedString : AnnotatedString ,
314
319
textLayoutResult : TextLayoutResult ?
315
320
) {
@@ -321,8 +326,8 @@ class MainActivity : ComponentActivity() {
321
326
offsetPosition,
322
327
offsetPosition
323
328
).firstOrNull()?.let { annotation ->
324
- viewModel.modelSearch (annotation.item)
325
- viewModel .updateIndicator(annotation.item.length)
329
+ searchModel.search (annotation.item)
330
+ searchModel .updateIndicator(annotation.item.length)
326
331
}
327
332
}
328
333
}
0 commit comments