diff --git a/app/src/main/java/com/shuwei/dish/match/net/NetViewModelV2.kt b/app/src/main/java/com/shuwei/dish/match/net/NetViewModelV2.kt index 07dcb20..7d4a541 100644 --- a/app/src/main/java/com/shuwei/dish/match/net/NetViewModelV2.kt +++ b/app/src/main/java/com/shuwei/dish/match/net/NetViewModelV2.kt @@ -114,7 +114,8 @@ class NetViewModelV2( ) { viewModelScope.launch { onLoading() - onResult(repository.getFoodOptions(keyword, pageNum, pageSize)) + val state = repository.getFoodOptions(keyword, pageNum, pageSize) + onResult(state) } } diff --git a/app/src/main/java/com/shuwei/dish/match/ui/MealListActivity.kt b/app/src/main/java/com/shuwei/dish/match/ui/MealListActivity.kt index ea0a2ed..4b4c58c 100644 --- a/app/src/main/java/com/shuwei/dish/match/ui/MealListActivity.kt +++ b/app/src/main/java/com/shuwei/dish/match/ui/MealListActivity.kt @@ -2,14 +2,17 @@ package com.shuwei.dish.match.ui import android.content.Intent import android.os.Bundle +import android.view.LayoutInflater import androidx.recyclerview.widget.LinearLayoutManager import com.shuwei.dish.match.adapter.MealListAdapter import com.shuwei.dish.match.base.BaseActivity import com.shuwei.dish.match.databinding.ActivityMealListBinding +import com.shuwei.dish.match.databinding.LayoutEmptyViewBinding import com.shuwei.dish.match.model.NutFoodOptionVO import com.shuwei.dish.match.net.NetViewModelV2 import com.shuwei.dish.match.net.UiState import com.shuwei.dish.match.utils.ext.gone +import com.shuwei.dish.match.utils.ext.startActivity import com.shuwei.dish.match.utils.ext.toast import com.shuwei.dish.match.utils.ext.visible @@ -20,13 +23,24 @@ class MealListActivity : BaseActivity() { companion object { const val MEAL_ITEM = "mealItem" + const val PAGE_SIZE = 30 } private val binding by lazy { ActivityMealListBinding.inflate(layoutInflater) } private val viewModelV2 by lazy { NetViewModelV2() } private val mealList = mutableListOf() - private val mealAdapter by lazy { MealListAdapter(mealList) } + private val mealAdapter by lazy { MealListAdapter(mealList).apply { + isStateViewEnable = true + setOnItemClickListener { _, _, position -> + val item = mealList.getOrNull(position) ?: return@setOnItemClickListener + startActivity{ + putExtra(PackActivity.MEAL_ITEM, item) + } + } + } } + private var emptyViewBinding: LayoutEmptyViewBinding? = null + private var currentPage = 1 override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -54,12 +68,15 @@ class MealListActivity : BaseActivity() { private fun initRecyclerView() { binding.rvMealList.layoutManager = LinearLayoutManager(this) binding.rvMealList.adapter = mealAdapter - mealAdapter.setOnItemClickListener { _, _, position -> - val item = mealList.getOrNull(position) ?: return@setOnItemClickListener - val intent = Intent(this, PackActivity::class.java).apply { - putExtra(PackActivity.MEAL_ITEM, item) - } - startActivity(intent) + binding.refreshLayout.setEnableRefresh(true) + binding.refreshLayout.setEnableLoadMore(true) + binding.refreshLayout.setOnRefreshListener { + currentPage = 1 + loadMealList() + } + binding.refreshLayout.setOnLoadMoreListener { + currentPage++ + loadMealList() } } @@ -67,32 +84,70 @@ class MealListActivity : BaseActivity() { * 加载餐品列表 */ private fun loadMealList() { + showLoading("加载中……") viewModelV2.getFoodOptionsWithCallback( keyword = null, - pageNum = 1, - pageSize = 100, - onLoading = { - showLoading("加载中……") - }, + pageNum = currentPage, + pageSize = PAGE_SIZE, + onLoading = {}, onResult = { state -> dismissLoading() when (state) { is UiState.Success -> { val records = state.data?.records - if (records.isNullOrEmpty()) { - toast("暂无数据") + if (currentPage == 1 && records.isNullOrEmpty()) { + binding.refreshLayout.finishRefresh() + loadEmptyView() return@getFoodOptionsWithCallback } - mealList.clear() - mealList.addAll(records) + if (currentPage == 1) { + mealList.clear() + } + mealList.addAll(records!!) mealAdapter.notifyDataSetChanged() + val isLoadMore = records.size >= PAGE_SIZE + binding.refreshLayout.setEnableLoadMore(isLoadMore) + if (isLoadMore) { + binding.refreshLayout.finishLoadMore() + } else { + binding.refreshLayout.finishLoadMoreWithNoMoreData() + } } is UiState.Error -> { toast(state.msg) + if (currentPage == 1) { + binding.refreshLayout.finishRefresh() + loadEmptyView() + } else { + currentPage-- + binding.refreshLayout.finishLoadMore() + } } else -> {} } } ) } + + /** + * 加载空视图 + */ + private fun loadEmptyView() { + mealList.clear() + mealAdapter.notifyDataSetChanged() + if (emptyViewBinding == null) { + emptyViewBinding = LayoutEmptyViewBinding.inflate( + LayoutInflater.from(this), binding.rvMealList, false + ) + } + emptyViewBinding?.run { + tvContent.text = "暂无数据" + tvSubContent.text = "暂无餐品数据" + mealAdapter.stateView = root + } + binding.refreshLayout.run { + setEnableRefresh(true) + setEnableLoadMore(false) + } + } } diff --git a/app/src/main/res/layout/activity_meal_list.xml b/app/src/main/res/layout/activity_meal_list.xml index dcc05e8..deacd7e 100644 --- a/app/src/main/res/layout/activity_meal_list.xml +++ b/app/src/main/res/layout/activity_meal_list.xml @@ -26,15 +26,30 @@ android:textColor="@color/black666" android:textSize="28sp" /> - + android:layout_height="match_parent"> + + + + + + + +