From a034e242fe844a1ca45d2a28710cb7face1f8fe6 Mon Sep 17 00:00:00 2001
From: mac <user@users-MacBook-Pro.local>
Date: 星期日, 28 四月 2024 19:07:56 +0800
Subject: [PATCH] 2024年04月28日19:07:49

---
 app/src/main/java/com/hdl/photovoltaic/ui/message/NascentMessageFragment.java |  281 +++++++++++++++++++++++++++++++++++++++++++++-----------
 1 files changed, 226 insertions(+), 55 deletions(-)

diff --git a/app/src/main/java/com/hdl/photovoltaic/ui/message/NascentMessageFragment.java b/app/src/main/java/com/hdl/photovoltaic/ui/message/NascentMessageFragment.java
index 82b33db..1417088 100644
--- a/app/src/main/java/com/hdl/photovoltaic/ui/message/NascentMessageFragment.java
+++ b/app/src/main/java/com/hdl/photovoltaic/ui/message/NascentMessageFragment.java
@@ -1,23 +1,39 @@
 package com.hdl.photovoltaic.ui.message;
 
 
+import android.annotation.SuppressLint;
+import android.content.Intent;
 import android.os.Bundle;
+import android.text.TextUtils;
+import android.view.View;
+import android.widget.TextView;
 
+import androidx.annotation.NonNull;
 import androidx.recyclerview.widget.LinearLayoutManager;
+import androidx.recyclerview.widget.RecyclerView;
 import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
 
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
 import com.hdl.linkpm.sdk.core.exception.HDLException;
 import com.hdl.photovoltaic.R;
 import com.hdl.photovoltaic.base.CustomBaseFragment;
 import com.hdl.photovoltaic.config.ConstantManage;
 import com.hdl.photovoltaic.databinding.FragmentNascentMessageBinding;
 import com.hdl.photovoltaic.enums.MessageFunctionTabSwitch;
+import com.hdl.photovoltaic.enums.MessageStateType;
 import com.hdl.photovoltaic.listener.CloudCallBeak;
 import com.hdl.photovoltaic.other.HdlCommonLogic;
+import com.hdl.photovoltaic.other.HdlLogLogic;
 import com.hdl.photovoltaic.other.HdlMessageLogic;
+import com.hdl.photovoltaic.other.HdlThreadLogic;
 import com.hdl.photovoltaic.ui.adapter.MessageAdapter;
 import com.hdl.photovoltaic.ui.bean.MessageBean;
 import com.hdl.sdk.link.core.bean.eventbus.BaseEventBus;
+
+import org.greenrobot.eventbus.EventBus;
+import org.greenrobot.eventbus.Subscribe;
+import org.greenrobot.eventbus.ThreadMode;
 
 import java.util.ArrayList;
 import java.util.List;
@@ -31,6 +47,13 @@
     private MessageAdapter messageNascentAdapter;
 
     private final List<MessageBean> mList = new ArrayList<>();
+    private int currentPage = 0; // 褰撳墠椤电爜
+    private int currentTotal = 0; // 鎬婚〉鐮�
+
+
+    private boolean isLoadingMore = false; // 鏍囪姝e湪鍔犺浇鏇村鏁版嵁
+
+    LinearLayoutManager layoutManager;
 
     @Override
     public Object getContentView() {
@@ -40,25 +63,14 @@
 
     @Override
     public void onBindView(Bundle savedInstanceState) {
-        //鍒濆鍖栨暟鎹�
-        initData();
         //鍒濆鍖栫晫闈�
         initView();
         //鍒濆鍖栫洃鍚櫒
         initEvent();
+        //杩涙潵璇诲彇涓�娆℃暟鎹�(涓轰簡鏇存柊currentPage锛宑urrentTotal杩欎釜鍊�)
+        loadNextPageNascentMessageList(1, true);
     }
 
-    private void initData() {
-//        mList = new ArrayList<>();
-//        for (int i = 0; i < 3; i++) {
-//            MessageBean messageBean = new MessageBean();
-//            messageBean.setTitle("鏉ヤ簡涓�鏉″憡璀︿俊鎭�" + i);
-//            messageBean.setHomeName(i + "");
-//            messageBean.setCreateTime((long) 1234567777);
-//            messageBean.setRead(false);
-//            mList.add(messageBean);
-//        }
-    }
 
     private void initEvent() {
         //璁剧疆涓嬫媺绠ご棰滆壊
@@ -66,20 +78,52 @@
         viewBinding.messageNascentSrl.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
             @Override
             public void onRefresh() {
-                mList.clear();//琛ㄧず寮哄埗閲嶆柊璇锋眰娑堟伅鍒楄〃
-                getNascentMessageList(false);
+                viewBinding.messageNascentSrl.setRefreshing(false);
+                mList.clear();//琛ㄧず寮哄埗閲嶆柊璇锋眰绗竴椤垫秷鎭垪琛�
+                HdlMessageLogic.getInstance().clearListMessage();//琛ㄧず寮哄埗娓呯┖鎵�鏈夌紦瀛樹俊鎭�
+                loadNextPageNascentMessageList(1, true);
             }
         });
         messageNascentAdapter.setNoOnclickListener(new MessageAdapter.OnclickListener() {
             @Override
             public void onClick(int position, MessageBean item) {
+                if (position < 0 || position > mList.size()) {
+                    return;
+                }
+                Intent intent = new Intent();
+                intent.putExtra("msgId", item.getMsgId().toString());
+                intent.setClass(_mActivity, MessageInfoActivity.class);
+                startActivity(intent);
 
+            }
+        });
+
+        viewBinding.messageNascentRecyclerview.addOnScrollListener(new RecyclerView.OnScrollListener() {
+            @Override
+            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
+//                super.onScrolled(recyclerView, dx, dy);
+
+                LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
+                if (layoutManager == null) {
+                    return;
+                }
+                int visibleItemCount = layoutManager.getChildCount();
+                int totalItemCount = layoutManager.getItemCount();
+                int firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition();
+                if (visibleItemCount > 0 && visibleItemCount + firstVisibleItemPosition == totalItemCount) {
+                    if (!isLoadingMore) {
+                        // 婊戝姩鍒颁簡搴曢儴锛屾墽琛岀浉搴旂殑鎿嶄綔
+                        HdlLogLogic.print("--->婊戝姩鍒颁簡搴曢儴");
+                        loadNextPageNascentMessageList(++currentPage, false);
+                    }
+                }
             }
         });
     }
 
     private void initView() {
-        viewBinding.messageNascentRecyclerview.setLayoutManager(new LinearLayoutManager(_mActivity));
+        layoutManager = new LinearLayoutManager(_mActivity);
+        viewBinding.messageNascentRecyclerview.setLayoutManager(layoutManager);
         messageNascentAdapter = new MessageAdapter(this.mList, _mActivity);
         viewBinding.messageNascentRecyclerview.setAdapter(messageNascentAdapter);
     }
@@ -88,59 +132,186 @@
      * 鍒锋柊鍒楄〃鏁版嵁
      */
     private void updateListData() {
-        if (this.messageNascentAdapter != null) {
-            this.messageNascentAdapter.setList(mList);
-            this.messageNascentAdapter.notifyDataSetChanged();
-        }
+        HdlThreadLogic.runMainThread(new Runnable() {
+            @Override
+            public void run() {
+                //鏇存柊鏈湴缂撳瓨鏁版嵁
+                updatelocalityCacheData();
+                //鍒锋柊鍒楄〃
+                if (messageNascentAdapter != null) {
+                    messageNascentAdapter.setList(mList);
+                    messageNascentAdapter.notifyDataSetChanged();
+                }
+                //鏄剧ず鏈淇℃伅鎬绘暟閲�
+                getUnreadCount();
+                //鏄剧ず娌℃暟鎹甎i鏍峰紡
+                nullDataUpdateUi();
+
+            }
+        });
     }
 
-    @Override
+    /**
+     * 鍒锋柊銆愬彂鐢熶腑銆戜俊鎭暟閲忔�绘暟
+     */
+    private void refreshNascentCount(String totalCount) {
+        HdlThreadLogic.runMainThread(new Runnable() {
+            @Override
+            public void run() {
+                TextView textView = _mActivity.findViewById(R.id.message_tab_nascent_title_tv);
+                if (textView != null) {
+                    textView.setText(getText(R.string.message_nascent));
+                    if (!TextUtils.isEmpty(totalCount) && !totalCount.equals("0")) {
+                        String s = getText(R.string.message_nascent) + "(" + totalCount + ")";
+                        textView.setText(s);
+                    }
+                }
+            }
+        });
+    }
+
+    /**
+     * 鍒锋柊瑙掓爣銆愭湭璇汇�戜俊鎭暟閲忔�绘暟
+     */
+    private void refreshUnreadNascentCount(String unreadCount) {
+        HdlThreadLogic.runMainThread(new Runnable() {
+            @SuppressLint("SetTextI18n")
+            @Override
+            public void run() {
+                TextView textView = _mActivity.findViewById(R.id.my_message_bottom_il2).findViewById(R.id.red_count_tv);
+                if (textView != null) {
+                    if (TextUtils.isEmpty(unreadCount) || unreadCount.equals("0")) {
+                        textView.setVisibility(View.GONE);
+                    } else {
+                        textView.setVisibility(View.VISIBLE);
+                        if (unreadCount.length() > 2) {
+                            textView.setText("99+");
+                        } else {
+                            textView.setText(unreadCount);
+                        }
+                    }
+                }
+            }
+        });
+    }
+
+    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
     public void onEventMessage(BaseEventBus eventBus) {
         super.onEventMessage(eventBus);
+        if (eventBus == null) {
+            return;
+        }
         //鎺ユ敹澶栭儴鐐瑰嚮浜嬩欢
         if (eventBus.getTopic().equals(ConstantManage.message_function_tab_switch)) {
             if (eventBus.getType().equals(MessageFunctionTabSwitch.nascent.toString())) {
-                getNascentMessageList(true);
+                // 鍙栨秷绮樻�т簨浠�
+                EventBus.getDefault().removeStickyEvent(eventBus);
+
+            }
+
+        } else if (eventBus.getTopic().equals(ConstantManage.message_function_push_post)) {
+            if (eventBus.getType().equals(MessageStateType.untreated)) {
+                //娑堟伅璇︽儏鍥炶皟
+                updatelocalityCacheData();
+                updateListData();
             }
         }
     }
 
     /**
-     * 鑾峰彇鍙戠敓涓殑娑堟伅鍒楄〃
-     *
-     * @param isShowLoading 鏄惁鍚姩 true鍚姩
+     * 鏇存柊鏈湴缂撳瓨鏁版嵁
      */
-    private void getNascentMessageList(boolean isShowLoading) {
-        if (mList.size() == 0) {
-            if (isShowLoading) {
-                showLoading();
-            }
-            HdlMessageLogic.getInstance().getMessageList(HdlMessageLogic.MessageStatus.untreated, new CloudCallBeak<List<MessageBean>>() {
-                @Override
-                public void onSuccess(List<MessageBean> list) {
-                    if (isShowLoading) {
-                        hideLoading();
-                    }
-                    if (!isShowLoading) {
-                        viewBinding.messageNascentSrl.setRefreshing(false);
-                    }
+    private void updatelocalityCacheData() {
+        mList.clear();
+        mList.addAll(HdlMessageLogic.getInstance().getNascentMessageMemoryList());
+    }
 
-                    if (list != null && list.size() != 0) {
-                        mList.addAll(list);
-                        updateListData();
-                    }
-                }
-
-                @Override
-                public void onFailure(HDLException e) {
-                    if (isShowLoading) {
-                        hideLoading();
-                    }
-                    if (!isShowLoading) {
-                        viewBinding.messageNascentSrl.setRefreshing(false);
-                    }
-                }
-            });
+    /**
+     * 鍔犺浇涓�椤垫暟鎹�
+     *
+     * @param pageNo        椤垫暟
+     * @param isShowLoading 鏄惁鍚姩鍔犺浇妗� true鍚姩
+     */
+    private void loadNextPageNascentMessageList(int pageNo, boolean isShowLoading) {
+        //绗竴椤佃鍙栨暟鎹己鍒惰
+        if (pageNo > 1 && currentPage > currentTotal) {
+            --currentPage;
+            //褰撳墠椤典笉鑳藉ぇ浜庢�婚〉鏁�
+            return;
         }
+        isLoadingMore = true;//鏍囪璇诲彇鐘舵��
+        if (isShowLoading) {
+            showLoading();
+        }
+        HdlMessageLogic.getInstance().getPageNoMessageList(pageNo, MessageStateType.untreated, new CloudCallBeak<HdlMessageLogic.MessageListClass>() {
+            @Override
+            public void onSuccess(HdlMessageLogic.MessageListClass messageListClass) {
+                if (messageListClass != null) {
+                    currentTotal = (int) messageListClass.getTotalPage();
+                    currentPage = (int) messageListClass.getPageNo();
+                    updateListData();
+                }
+                isLoadingMore = false;
+                if (isShowLoading) {
+                    hideLoading();
+                }
+            }
+
+            @Override
+            public void onFailure(HDLException e) {
+                if (currentPage > 1) {
+                    --currentPage;
+                }
+                isLoadingMore = false;
+                if (isShowLoading) {
+                    hideLoading();
+                }
+            }
+        });
+    }
+
+    /**
+     * 璇诲彇娑堟伅銆愭湭璇绘暟閲忋��
+     */
+    private void getUnreadCount() {
+        HdlMessageLogic.getInstance().getMessageUntreatedCount("",new CloudCallBeak<String>() {
+            @Override
+            public void onSuccess(String data) {
+
+                HdlThreadLogic.runMainThread(new Runnable() {
+                    @Override
+                    public void run() {
+                        JSONObject jsonObject = JSON.parseObject(data);
+                        if (jsonObject.containsKey("untreatedCount")) {
+                            //鏈鐞嗘暟閲�
+                            String untreatedCount = jsonObject.getString("untreatedCount");
+                            refreshNascentCount(untreatedCount);
+                        }
+                        if (jsonObject.containsKey("unreadCount")) {
+                            //鏈鏁伴噺
+                            String unreadCount = jsonObject.getString("unreadCount");
+                            refreshUnreadNascentCount(unreadCount);
+                        }
+                    }
+                });
+
+            }
+
+            @Override
+            public void onFailure(HDLException e) {
+                TextView textView = _mActivity.findViewById(R.id.my_message_bottom_il2).findViewById(R.id.red_count_tv);
+                if (textView != null) {
+                    textView.setVisibility(View.GONE);
+                }
+            }
+        });
+    }
+
+
+    /**
+     * 娌℃湁鐢电珯鍒楄〃鐨勬牱寮�
+     */
+    private void nullDataUpdateUi() {
+        HdlCommonLogic.getInstance().nullDataUpdateUi(_mActivity, viewBinding.nullDataIc.getRoot(), viewBinding.nullDataIc.nullDataGifAnimationIv, viewBinding.nullDataIc.nullDataTv, getString(R.string.message_alarm_data_null), mList.size() > 0);
     }
 }

--
Gitblit v1.8.0