|
|
@@ -19,26 +19,39 @@ class SearchViewModel : BaseViewModel() {
|
|
|
App.instance.networkService.getHttpService(SearchHttpService::class.java)
|
|
|
}
|
|
|
|
|
|
+ val searchResultSet = mutableSetOf<String>()
|
|
|
val searchResultLD = ExtMutableLiveData<List<SearchResultItemData>>()
|
|
|
val searchRltLD = ExtMutableLiveData<Rlt<Any>>()
|
|
|
private val searchResultList = mutableListOf<SearchResultItemData>()
|
|
|
private val pageHandler = PageHandler()
|
|
|
private var keyword: String? = null
|
|
|
-
|
|
|
- fun search(keyword: String) {
|
|
|
+ fun search(keyword: String?) {
|
|
|
pageHandler.reset()
|
|
|
this.keyword = keyword
|
|
|
+ searchResultSet.clear()
|
|
|
searchResultList.clear()
|
|
|
- searchMore()
|
|
|
+ searchMore(keyword)
|
|
|
+ }
|
|
|
+
|
|
|
+ fun reSearch() {
|
|
|
+ search(keyword)
|
|
|
+ }
|
|
|
+
|
|
|
+ fun hasMoreData(): Boolean {
|
|
|
+ return !pageHandler.isEnd
|
|
|
}
|
|
|
|
|
|
- fun searchMore() {
|
|
|
+ fun searchMore(keyword: String? = this@SearchViewModel.keyword) {
|
|
|
if (pageHandler.isEnd) {
|
|
|
searchRltLD.send(Rlt.Failed(NoMoreDataError()))
|
|
|
return
|
|
|
}
|
|
|
viewModelScope.launch {
|
|
|
- val keyword = this@SearchViewModel.keyword ?: return@launch
|
|
|
+ if (keyword.isNullOrEmpty()) {
|
|
|
+ searchRltLD.send(Rlt.Success(Any()))
|
|
|
+ searchResultLD.send(emptyList())
|
|
|
+ return@launch
|
|
|
+ }
|
|
|
val rlt = searchHttpService.search(
|
|
|
SearchReq(
|
|
|
keyword = keyword,
|
|
|
@@ -47,18 +60,31 @@ class SearchViewModel : BaseViewModel() {
|
|
|
)
|
|
|
when (rlt) {
|
|
|
is Rlt.Failed -> {
|
|
|
+ if (keyword != this@SearchViewModel.keyword) {
|
|
|
+ return@launch
|
|
|
+ }
|
|
|
searchRltLD.send(rlt)
|
|
|
}
|
|
|
|
|
|
is Rlt.Success -> {
|
|
|
+ if (keyword != this@SearchViewModel.keyword) {
|
|
|
+ return@launch
|
|
|
+ }
|
|
|
pageHandler.nextPage(rlt.data.data?.next)
|
|
|
- searchResultList.addAll(rlt.data.data?.list?.map {
|
|
|
+
|
|
|
+ val nextList = rlt.data.data?.list?.filter {
|
|
|
+ !searchResultSet.contains(it.uid)
|
|
|
+ } ?: emptyList()
|
|
|
+
|
|
|
+ val nextItemList = nextList.map {
|
|
|
it.online?.let { online ->
|
|
|
ProfileModule.updateUserOnline(it.uid, online)
|
|
|
}
|
|
|
|
|
|
SearchResultItemData(it)
|
|
|
- } ?: emptyList())
|
|
|
+ }
|
|
|
+ searchResultSet.addAll(nextList.map { it.uid })
|
|
|
+ searchResultList.addAll(nextItemList)
|
|
|
searchRltLD.send(rlt)
|
|
|
searchResultLD.send(searchResultList)
|
|
|
}
|