Skip to content

Commit f3a1bb3

Browse files
committed
update
1 parent 61f22d7 commit f3a1bb3

File tree

3 files changed

+64
-14
lines changed

3 files changed

+64
-14
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.wzq.jd.compose.app.page
2+
3+
/**
4+
* create by wzq on 2023/12/5
5+
*
6+
*/
7+
sealed class PageState {
8+
data object None : PageState()
9+
data object Loading : PageState()
10+
class Success(private val data: Any?) : PageState() {
11+
@Suppress("UNCHECKED_CAST")
12+
fun <T> get() = data as? T
13+
}
14+
class Failure(val exception: Throwable? = null, val tag: Any? = null) : PageState()
15+
}

app/src/main/java/com/wzq/jd/compose/app/page/search/SearchPage.kt

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import androidx.compose.foundation.lazy.LazyColumn
1212
import androidx.compose.foundation.lazy.items
1313
import androidx.compose.material.icons.Icons
1414
import androidx.compose.material.icons.filled.Check
15+
import androidx.compose.material3.CircularProgressIndicator
1516
import androidx.compose.material3.ElevatedFilterChip
1617
import androidx.compose.material3.ExperimentalMaterial3Api
1718
import androidx.compose.material3.Icon
@@ -21,10 +22,13 @@ import androidx.compose.material3.Text
2122
import androidx.compose.runtime.Composable
2223
import androidx.compose.runtime.mutableStateMapOf
2324
import androidx.compose.runtime.remember
25+
import androidx.compose.ui.Alignment
2426
import androidx.compose.ui.Modifier
2527
import androidx.compose.ui.unit.dp
2628
import androidx.navigation.NavController
29+
import com.wzq.jd.compose.app.data.model.ArticleItem
2730
import com.wzq.jd.compose.app.data.model.SearchHotWords
31+
import com.wzq.jd.compose.app.page.PageState
2832
import com.wzq.jd.compose.app.page.main.ArticleItemPage
2933

3034
/**
@@ -51,23 +55,40 @@ fun SearchPage(
5155
}
5256
},
5357
) { pv ->
54-
Box(modifier = Modifier.padding(pv)) {
55-
if (viewModel.result.isEmpty() || viewModel.keywords.value.isEmpty()) {
56-
DefaultPage(hotWords = viewModel.hotWords) {
57-
viewModel.searchResult(it)
58-
}
59-
} else {
60-
LazyColumn(content = {
61-
items(viewModel.result, key = { it.id }) {
62-
ArticleItemPage(itemData = it) {}
58+
Box(
59+
modifier = Modifier
60+
.padding(pv)
61+
.fillMaxSize()
62+
) {
63+
when (val state = viewModel.pageState.value) {
64+
PageState.None -> {
65+
DefaultPage(hotWords = viewModel.hotWords) {
66+
viewModel.searchResult(it)
6367
}
64-
})
68+
}
69+
70+
PageState.Loading -> {
71+
CircularProgressIndicator(Modifier.align(Alignment.Center))
72+
}
73+
74+
is PageState.Success -> {
75+
val list: List<ArticleItem> = state.get() ?: emptyList()
76+
LazyColumn(content = {
77+
items(list, key = { it.id }) {
78+
ArticleItemPage(itemData = it) {}
79+
}
80+
})
81+
}
82+
83+
else -> {}
6584
}
6685

6786
}
87+
6888
}
6989
}
7090

91+
7192
@OptIn(ExperimentalLayoutApi::class, ExperimentalMaterial3Api::class)
7293
@Composable
7394
private fun DefaultPage(hotWords: List<SearchHotWords>, onItemClick: (String) -> Unit) {

app/src/main/java/com/wzq/jd/compose/app/page/search/SearchViewModel.kt

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@ import androidx.lifecycle.viewModelScope
88
import com.wzq.jd.compose.app.data.NetworkUtil
99
import com.wzq.jd.compose.app.data.model.ArticleItem
1010
import com.wzq.jd.compose.app.data.model.SearchHotWords
11+
import com.wzq.jd.compose.app.page.PageState
12+
import kotlinx.coroutines.Dispatchers
13+
import kotlinx.coroutines.async
14+
import kotlinx.coroutines.delay
1115
import kotlinx.coroutines.launch
16+
import kotlinx.coroutines.withContext
1217

1318
/**
1419
* create by wzq on 2023/12/5
@@ -20,7 +25,8 @@ class SearchViewModel : ViewModel() {
2025
val keywords: State<String> = _keywords
2126

2227
val hotWords = mutableStateListOf<SearchHotWords>()
23-
val result = mutableStateListOf<ArticleItem>()
28+
29+
val pageState = mutableStateOf<PageState>(PageState.None)
2430

2531
init {
2632
getHotWords()
@@ -38,17 +44,25 @@ class SearchViewModel : ViewModel() {
3844
fun setKeyWords(key: String) {
3945
_keywords.value = key
4046
if (key.isEmpty()) {
41-
result.clear()
47+
pageState.value = PageState.None
4248
}
4349
}
4450

4551
fun searchResult(key: String) {
4652
setKeyWords(key)
53+
if (key.isEmpty()) {
54+
return
55+
}
56+
pageState.value = PageState.Loading
4757
viewModelScope.launch {
58+
withContext(Dispatchers.IO) {
59+
delay(1000) // TODO:
60+
}
4861
NetworkUtil.remoteRepo.getSearchResult(key)
4962
.onSuccess {
50-
result.clear()
51-
result.addAll(it.data.listData)
63+
pageState.value = PageState.Success(it.data.listData)
64+
}.onFailure {
65+
pageState.value = PageState.Failure(it)
5266
}
5367
}
5468
}

0 commit comments

Comments
 (0)