Преглед изворни кода

1.修改获取推送消息列表分页加载

王鹏鹏 пре 2 година
родитељ
комит
db429f5f22

+ 41 - 10
push/src/main/java/com/yingyang/push/activity/MessgeListActivity.kt

@@ -38,6 +38,7 @@ class MessgeListActivity : BaseMVVMActivity<ActivityMessgeListBinding, PushViewM
     /**
      * 消息列表
      */
+    private var page = 1
     private var pushList = mutableListOf<PushMessageBean>()
     private val pushListAdapter by lazy { PushListAdapter() }
 
@@ -49,13 +50,15 @@ class MessgeListActivity : BaseMVVMActivity<ActivityMessgeListBinding, PushViewM
             )
         )
         binding {
+            refreshPush.setEnableLoadMore(true)
+            refreshPush.setEnableRefresh(true)
             //消息类型
             rvPushType.adapter = pushTypeAdapter
             pushTypeAdapter.setData(pushTypeList)
             pushTypeAdapter.onPushClickListener = { bean ->
                 msgType = bean.dictValue
                 pushListAdapter.showEmptyView = false
-                getMeaageList()
+                getMeaageList(isRefresh = true, showLoading = true)
             }
             //消息列表
             rvPush.adapter = pushListAdapter
@@ -107,12 +110,20 @@ class MessgeListActivity : BaseMVVMActivity<ActivityMessgeListBinding, PushViewM
 
     @SuppressLint("ClickableViewAccessibility")
     override fun initListener() {
-        binding.layoutHead.setOnTouchListener(this@MessgeListActivity)
+        binding {
+            layoutHead.setOnTouchListener(this@MessgeListActivity)
+            refreshPush.setOnRefreshListener {
+                getMeaageList(isRefresh = true, showLoading = false)
+            }
+            refreshPush.setOnLoadMoreListener {
+                getMeaageList(isRefresh = false, showLoading = false)
+            }
+        }
     }
 
     override fun initData() {
         getPushList()
-        getMeaageList()
+        getMeaageList(isRefresh = true, showLoading = true)
     }
 
     /**
@@ -138,16 +149,26 @@ class MessgeListActivity : BaseMVVMActivity<ActivityMessgeListBinding, PushViewM
     /**
      * 获取推送消息列表
      */
-    private fun getMeaageList() {
-        viewModel.getPushList(msgType, fail = {
-            it.toast()
-        }, success = {
+    private fun getMeaageList(isRefresh: Boolean, showLoading: Boolean) {
+        if (isRefresh) {
+            page = 1
             pushList.clear()
             pushListAdapter.clearData()
-            if (it.isNullOrEmpty().not()) {
-                pushList.addAll(it!!)
+            binding.refreshPush.resetNoMoreData()
+        } else {
+            page++
+        }
+        viewModel.getPushList(msgType, page, showLoading, fail = {
+            endRefresh()
+            it.toast()
+        }, success = {
+            endRefresh()
+            if (it != null && it.records.isNullOrEmpty().not()) {
+                pushList.addAll(it.records)
+            } else {
+                binding.refreshPush.finishLoadMoreWithNoMoreData()
             }
-            pushListAdapter.showEmptyView = it.isNullOrEmpty()
+            pushListAdapter.showEmptyView = pushList.isEmpty()
             pushListAdapter.setData(pushList)
         })
     }
@@ -211,4 +232,14 @@ class MessgeListActivity : BaseMVVMActivity<ActivityMessgeListBinding, PushViewM
         }
     }
 
+    /**
+     * 结束刷新
+     */
+    private fun endRefresh() {
+        binding {
+            refreshPush.finishRefresh()
+            refreshPush.finishLoadMore()
+        }
+    }
+
 }

+ 20 - 10
push/src/main/java/com/yingyang/push/activity/PushViewModel.kt

@@ -1,9 +1,13 @@
 package com.yingyang.push.activity
 
-import com.yingyang.push.entity.PushMessageBean
+import android.util.Log
+import com.yingyang.push.entity.GetPushListBean
 import com.yingyang.push.entity.PushMessageTypeBean
+import com.yingyang.push.entity.PushMessagesBean
 import com.yingyang.push.net.PUSH_API
 import com.yingyangfly.baselib.mvvm.BaseViewModel
+import com.yingyangfly.baselib.net.XUtils
+import com.yingyangfly.baselib.utils.GsonUtil
 
 /**
  * @author 王鹏鹏
@@ -20,8 +24,7 @@ class PushViewModel : BaseViewModel() {
     ) = launchFlow(false) {
         PUSH_API.getSelectByDictType("warn_type")
     }.runUI(
-        success,
-        fail
+        success, fail
     )
 
     /**
@@ -29,13 +32,21 @@ class PushViewModel : BaseViewModel() {
      */
     fun getPushList(
         type: String,
+        index: Int,
+        showLoading: Boolean,
         fail: ((msg: String) -> Unit)? = null,
-        success: ((success: List<PushMessageBean>?) -> Unit)? = null,
-    ) = launchFlow(true) {
-        PUSH_API.getPushList(type)
+        success: ((success: PushMessagesBean?) -> Unit)? = null,
+    ) = launchFlow(showLoading) {
+        val getPushListBean = GetPushListBean().apply {
+            msgType = type
+            page = index.toString()
+            limit = "10"
+        }
+        Log.e("wpp", GsonUtil.GsonString(getPushListBean))
+        val body = XUtils.createJson(GsonUtil.GsonString(getPushListBean))
+        PUSH_API.getPushList(body)
     }.runUI(
-        success,
-        fail
+        success, fail
     )
 
     /**
@@ -48,7 +59,6 @@ class PushViewModel : BaseViewModel() {
     ) = launchFlow(false) {
         PUSH_API.updateReadMsg(msgId)
     }.runUI(
-        success,
-        fail
+        success, fail
     )
 }

+ 1 - 3
push/src/main/java/com/yingyang/push/adapter/PushListAdapter.kt

@@ -19,9 +19,7 @@ class PushListAdapter(override val layoutId: Int = R.layout.item_push_list) :
 
     @SuppressLint("NotifyDataSetChanged")
     override fun onBindViewHolder(
-        binding: ItemPushListBinding,
-        item: PushMessageBean,
-        position: Int
+        binding: ItemPushListBinding, item: PushMessageBean, position: Int
     ) {
         binding.data = item
         binding.tvDate.text = item.createTime

+ 10 - 0
push/src/main/java/com/yingyang/push/entity/GetPushListBean.kt

@@ -0,0 +1,10 @@
+package com.yingyang.push.entity
+
+/**
+ * 获取推送消息列表
+ */
+class GetPushListBean {
+    var msgType: String? = null
+    var page: String? = null
+    var limit: String? = null
+}

+ 9 - 0
push/src/main/java/com/yingyang/push/entity/PushMessagesBean.kt

@@ -0,0 +1,9 @@
+package com.yingyang.push.entity
+
+data class PushMessagesBean(
+    val current: Int,
+    val pages: Int,
+    val records: List<PushMessageBean>,
+    val size: Int,
+    val total: Int
+)

+ 4 - 2
push/src/main/java/com/yingyang/push/net/PushApiService.kt

@@ -1,8 +1,10 @@
 package com.yingyang.push.net
 
-import com.yingyang.push.entity.PushMessageBean
 import com.yingyang.push.entity.PushMessageTypeBean
+import com.yingyang.push.entity.PushMessagesBean
 import com.yingyangfly.baselib.net.BaseResp
+import okhttp3.RequestBody
+import retrofit2.http.Body
 import retrofit2.http.POST
 import retrofit2.http.Query
 
@@ -18,7 +20,7 @@ interface PushApiService {
      * 获取推送消息列表
      */
     @POST("app/warn/list")
-    suspend fun getPushList(@Query("msgType") msgType: String): BaseResp<List<PushMessageBean>>
+    suspend fun getPushList(@Body requestBody: RequestBody): BaseResp<PushMessagesBean>
 
     /**
      * 更改消息状态

+ 14 - 7
push/src/main/res/layout/activity_messge_list.xml

@@ -71,18 +71,25 @@
                     app:layout_constraintStart_toStartOf="parent"
                     app:layout_constraintTop_toTopOf="parent" />
 
-                <androidx.recyclerview.widget.RecyclerView
-                    android:id="@+id/rvPush"
+                <com.scwang.smartrefresh.layout.SmartRefreshLayout
+                    android:id="@+id/refreshPush"
                     android:layout_width="match_parent"
                     android:layout_height="0dp"
-                    android:layout_marginStart="@dimen/divider_36px"
-                    android:layout_marginTop="@dimen/divider_12px"
-                    android:layout_marginEnd="@dimen/divider_36px"
-                    app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
                     app:layout_constraintBottom_toBottomOf="parent"
                     app:layout_constraintEnd_toEndOf="parent"
                     app:layout_constraintStart_toStartOf="parent"
-                    app:layout_constraintTop_toBottomOf="@+id/tvTitle" />
+                    app:layout_constraintTop_toBottomOf="@+id/tvTitle">
+
+                    <androidx.recyclerview.widget.RecyclerView
+                        android:id="@+id/rvPush"
+                        android:layout_width="match_parent"
+                        android:layout_height="match_parent"
+                        android:layout_marginStart="@dimen/divider_36px"
+                        android:layout_marginTop="@dimen/divider_12px"
+                        android:layout_marginEnd="@dimen/divider_36px"
+                        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
+
+                </com.scwang.smartrefresh.layout.SmartRefreshLayout>
             </androidx.constraintlayout.widget.ConstraintLayout>
         </LinearLayout>
     </androidx.constraintlayout.widget.ConstraintLayout>