Skip to content

Commit 2171bf6

Browse files
committed
search supports single line.
single line will now allow direct searches instead of having to manually filter out the whitespaces (new lines) - removed padding cause it no longer has a purpose - using viewModels for better lifecycle care.
1 parent b42a18f commit 2171bf6

File tree

2 files changed

+31
-26
lines changed

2 files changed

+31
-26
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 = 40
18-
versionName = "1.0-40"
17+
versionCode = 41
18+
versionName = "1.0-41"
1919

2020
}
2121

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

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package jisho
33
import android.os.Bundle
44
import androidx.activity.ComponentActivity
55
import androidx.activity.compose.setContent
6+
import androidx.activity.viewModels
67
import androidx.compose.foundation.background
78
import androidx.compose.foundation.border
89
import androidx.compose.foundation.gestures.detectTapGestures
@@ -60,7 +61,7 @@ class MainActivity : ComponentActivity() {
6061
override fun onCreate(savedInstanceState: Bundle?) {
6162
getActionBar()?.hide()
6263
super.onCreate(savedInstanceState)
63-
val viewModel = Model()
64+
val searchModel: SearchModel by viewModels()
6465
setContent {
6566
Theme {
6667
Surface {
@@ -71,17 +72,17 @@ class MainActivity : ComponentActivity() {
7172
horizontalAlignment = Alignment.CenterHorizontally
7273
) {
7374
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)
7879
}
7980
}
8081
}
8182
}
8283
}
8384

84-
class Model : ViewModel() {
85+
class SearchModel : ViewModel() {
8586
private val _search = MutableStateFlow("")
8687
val search: StateFlow<String> get() = _search
8788

@@ -93,11 +94,14 @@ class MainActivity : ComponentActivity() {
9394

9495
private var job: Job? = null
9596

96-
fun modelSearch(query: String, page: Int = 1) {
97+
fun search(query: String, page: Int = 1) {
9798
job?.cancel()
98-
if (query.isEmpty()) return
9999
_search.value = query
100100
_indicatorPos.value = query.length
101+
if (query.isEmpty()) {
102+
_results.update { emptyList() }
103+
return
104+
}
101105
job = viewModelScope.launch {
102106
val thisQuery = _search.value
103107
search(query, page, { word ->
@@ -107,32 +111,28 @@ class MainActivity : ComponentActivity() {
107111
})
108112
}
109113
}
114+
110115
fun updateIndicator(pos: Int) {
111116
_indicatorPos.value = pos
112117
}
113118
}
114119

115120
@Composable
116121
private fun SearchBar(
117-
viewModel: Model,
122+
searchModel: SearchModel,
118123
onValueChange: (String) -> Unit
119124
) {
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)
122127
TextField(
123128
value = TextFieldValue(text = search, selection = TextRange(iPos)),
124129
onValueChange = { newValue ->
125130
onValueChange(newValue.text)
126131
},
127132
modifier = Modifier
128133
.fillMaxWidth()
129-
.padding(top = 16.dp, bottom = 8.dp)
130134
.border(3.dp, Color(0xFF6E6E6E), RoundedCornerShape(6.dp)),
131135
placeholder = { Text("Search") },
132-
colors = TextFieldDefaults.colors(
133-
unfocusedIndicatorColor = Color.Transparent,
134-
focusedIndicatorColor = Color.Transparent
135-
),
136136
leadingIcon = {
137137
// @todo add keyword filter
138138
},
@@ -163,13 +163,18 @@ class MainActivity : ComponentActivity() {
163163
)
164164
}
165165
}
166-
}
166+
},
167+
singleLine = true,
168+
colors = TextFieldDefaults.colors(
169+
unfocusedIndicatorColor = Color.Transparent,
170+
focusedIndicatorColor = Color.Transparent
171+
)
167172
)
168173
}
169174

170175
@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()
173178
LazyColumn(
174179
modifier = Modifier
175180
.fillMaxSize()
@@ -199,7 +204,7 @@ class MainActivity : ComponentActivity() {
199204
text = annotatedString,
200205
modifier = Modifier
201206
.pointerInput(annotatedString) {
202-
tapGesture(viewModel, annotatedString, textLayoutResult)
207+
tapGesture(searchModel, annotatedString, textLayoutResult)
203208
}
204209
.padding(bottom = 18.dp),
205210
onTextLayout = { textLayoutResult = it },
@@ -274,7 +279,7 @@ class MainActivity : ComponentActivity() {
274279
text = annotatedString,
275280
modifier = Modifier
276281
.pointerInput(annotatedString) {
277-
tapGesture(viewModel, annotatedString, textLayoutResult)
282+
tapGesture(searchModel, annotatedString, textLayoutResult)
278283
}
279284
.padding(10.dp),
280285
onTextLayout = { textLayoutResult = it },
@@ -309,7 +314,7 @@ class MainActivity : ComponentActivity() {
309314
}
310315

311316
private suspend fun PointerInputScope.tapGesture(
312-
viewModel: Model,
317+
searchModel: SearchModel,
313318
annotatedString: AnnotatedString,
314319
textLayoutResult: TextLayoutResult?
315320
) {
@@ -321,8 +326,8 @@ class MainActivity : ComponentActivity() {
321326
offsetPosition,
322327
offsetPosition
323328
).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)
326331
}
327332
}
328333
}

0 commit comments

Comments
 (0)