ソースを参照

feat: 游客模式列表快速加载

DoggyZhang 3 ヶ月 前
コミット
6c3e9beda2

+ 4 - 3
module/playmate/src/main/java/com/adealink/weparty/playmate/list/GuestPlaymateListFragment.kt

@@ -30,6 +30,7 @@ import com.adealink.weparty.playmate.data.PlaymateListStarSort
 import com.adealink.weparty.playmate.databinding.FragmentPlaymateListBinding
 import com.adealink.weparty.playmate.list.adapter.PlaymateListItemViewBinder
 import com.adealink.weparty.playmate.list.comp.PlaymateListFilterComp
+import com.adealink.weparty.playmate.list.viewmodel.GuestPlaymateListViewModel
 import com.adealink.weparty.playmate.list.viewmodel.PlaymateListViewModel
 import com.adealink.weparty.playmate.viewmodel.PlaymateViewModelFactory
 import com.scwang.smart.refresh.layout.api.RefreshLayout
@@ -47,7 +48,7 @@ class GuestPlaymateListFragment : BaseFragment(R.layout.fragment_playmate_list),
 
     private val binding by viewBinding(FragmentPlaymateListBinding::bind)
     private val listAdapter by fastLazy { MultiTypeListAdapter(BaseListDiffUtil()) }
-    private val playmateListViewModel by viewModels<PlaymateListViewModel> { PlaymateViewModelFactory() }
+    private val playmateListViewModel by viewModels<GuestPlaymateListViewModel> { PlaymateViewModelFactory() }
 
     override fun onCreate(savedInstanceState: Bundle?) {
         super.onCreate(savedInstanceState)
@@ -111,11 +112,11 @@ class GuestPlaymateListFragment : BaseFragment(R.layout.fragment_playmate_list),
 
     override fun loadData() {
         super.loadData()
-        binding.refreshLayout.autoRefresh()
+        playmateListViewModel.refreshList(true)
     }
 
     private fun refreshPlaymateList() {
-        playmateListViewModel.refreshList()
+        playmateListViewModel.refreshList(false)
     }
 
     private fun loadMorePlaymateList() {

+ 133 - 0
module/playmate/src/main/java/com/adealink/weparty/playmate/list/viewmodel/GuestPlaymateListViewModel.kt

@@ -0,0 +1,133 @@
+package com.adealink.weparty.playmate.list.viewmodel
+
+import androidx.lifecycle.MutableLiveData
+import com.adealink.frame.base.Rlt
+import com.adealink.frame.mvvm.livedata.ExtMutableLiveData
+import com.adealink.frame.mvvm.viewmodel.BaseViewModel
+import com.adealink.frame.network.data.PageReq
+import com.adealink.weparty.App
+import com.adealink.weparty.network.data.NoMoreDataError
+import com.adealink.weparty.playmate.data.PlaymateListData
+import com.adealink.weparty.playmate.data.PlaymateListPriceSort
+import com.adealink.weparty.playmate.data.PlaymateListReq
+import com.adealink.weparty.playmate.data.PlaymateListStarSort
+import com.adealink.weparty.playmate.datasource.remote.PlaymateHttpService
+import com.adealink.weparty.util.PageHandler
+import kotlinx.coroutines.launch
+
+
+class GuestPlaymateListViewModel : BaseViewModel() {
+
+    companion object {
+        /**
+         * 利用缓存数据,提升路畅
+         */
+        private val playmateList = mutableListOf<PlaymateListData>()
+        private val pageHandler = PageHandler(pageSize = 20)
+    }
+
+    private val playmateHttpService by lazy {
+        App.instance.networkService.getHttpService(PlaymateHttpService::class.java)
+    }
+
+    val listLD = MutableLiveData<List<PlaymateListData>>()
+    val listRltLD = ExtMutableLiveData<Rlt<Any>>()
+
+    private var categoryCode: String? = null
+    val starRankLD = ExtMutableLiveData<PlaymateListStarSort>()
+
+    val priceRankLD = ExtMutableLiveData<PlaymateListPriceSort>()
+
+    fun setCategoryCode(code: String?) {
+        categoryCode = code
+        pageHandler.reset()
+        playmateList.clear()
+    }
+
+    fun changeStarSort() {
+        val current = starRankLD.value ?: PlaymateListStarSort.DEFAULT
+        val next = when (current) {
+            PlaymateListStarSort.ASC -> {
+                PlaymateListStarSort.DESC
+            }
+
+            PlaymateListStarSort.DESC -> {
+                PlaymateListStarSort.DEFAULT
+            }
+
+            PlaymateListStarSort.DEFAULT -> {
+                PlaymateListStarSort.ASC
+            }
+        }
+        this.starRankLD.send(next)
+    }
+
+    fun changePriceSort() {
+        val current = priceRankLD.value ?: PlaymateListPriceSort.DEFAULT
+        val next = when (current) {
+            PlaymateListPriceSort.ASC -> {
+                PlaymateListPriceSort.DESC
+            }
+
+            PlaymateListPriceSort.DESC -> {
+                PlaymateListPriceSort.DEFAULT
+            }
+
+            PlaymateListPriceSort.DEFAULT -> {
+                PlaymateListPriceSort.ASC
+            }
+        }
+        this.priceRankLD.send(next)
+    }
+
+    fun refreshList(cache: Boolean) {
+        viewModelScope.launch {
+            if(cache){
+                if(playmateList.isNotEmpty()){
+                    listRltLD.send(Rlt.Success(Unit))
+                    listLD.send(playmateList)
+                    return@launch
+                }
+            }
+            pageHandler.reset()
+            playmateList.clear()
+            pullList()
+        }
+    }
+
+    fun loadMoreList() {
+        viewModelScope.launch {
+            pullList()
+        }
+    }
+
+    private suspend fun pullList() {
+        if (pageHandler.isEnd) {
+            listRltLD.send(Rlt.Failed(NoMoreDataError()))
+            return
+        }
+        val rlt = playmateHttpService.getPlaymateList(
+            PlaymateListReq(
+                code = categoryCode,
+
+                sortByStar = starRankLD.value?.sort ?: PlaymateListStarSort.DEFAULT.sort,
+                sortByPrice = priceRankLD.value?.sort ?: PlaymateListPriceSort.DEFAULT.sort,
+
+                page = PageReq(size = pageHandler.pageSize, next = pageHandler.currentPage)
+            )
+        )
+        when (rlt) {
+            is Rlt.Failed -> {
+                listRltLD.send(rlt)
+            }
+
+            is Rlt.Success -> {
+                pageHandler.nextPage(rlt.data.data?.next)
+                playmateList.addAll(rlt.data.data?.list ?: emptyList())
+                listRltLD.send(rlt)
+                listLD.send(playmateList)
+            }
+        }
+    }
+
+}

+ 4 - 0
module/playmate/src/main/java/com/adealink/weparty/playmate/viewmodel/PlaymateViewModelFactory.kt

@@ -3,6 +3,7 @@ package com.adealink.weparty.playmate.viewmodel
 import androidx.lifecycle.ViewModel
 import androidx.lifecycle.ViewModelProvider
 import com.adealink.weparty.playmate.detail.viewmodel.PlaymateDetailViewModel
+import com.adealink.weparty.playmate.list.viewmodel.GuestPlaymateListViewModel
 import com.adealink.weparty.playmate.list.viewmodel.PlaymateListViewModel
 
 @Suppress("UNCHECKED_CAST")
@@ -20,6 +21,9 @@ class PlaymateViewModelFactory : ViewModelProvider.NewInstanceFactory() {
                 isAssignableFrom(PlaymateDetailViewModel::class.java) ->
                     PlaymateDetailViewModel()
 
+                isAssignableFrom(GuestPlaymateListViewModel::class.java) ->
+                    GuestPlaymateListViewModel()
+
                 else ->
                     throw IllegalArgumentException("Unknown ViewModel class: ${modelClass.name}")
             } as T