mac
2024-03-15 a0e8f81afc4fe2a21a5c504d53e569ea50d463d6
app/src/main/java/com/hdl/photovoltaic/ui/message/NascentMessageFragment.java
@@ -1,11 +1,10 @@
package com.hdl.photovoltaic.ui.message;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
@@ -29,9 +28,7 @@
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.HouseIdBean;
import com.hdl.photovoltaic.ui.bean.MessageBean;
import com.hdl.photovoltaic.utils.GlideUtils;
import com.hdl.sdk.link.core.bean.eventbus.BaseEventBus;
import org.greenrobot.eventbus.EventBus;
@@ -50,10 +47,10 @@
    private MessageAdapter messageNascentAdapter;
    private final List<MessageBean> mList = new ArrayList<>();
    private int currentPage = 0; // 当前页码
    private int currentTotal = 0; // 总页码
    // 添加上拉加载更多的逻辑
    private int visibleThreshold = 5; // 当前显示的最后一项与总共有多少条数据之间的差值
    private int currentPage = 1; // 当前页码
    private boolean isLoadingMore = false; // 标记正在加载更多数据
    LinearLayoutManager layoutManager;
@@ -66,18 +63,14 @@
    @Override
    public void onBindView(Bundle savedInstanceState) {
        //初始化数据
//        initData();
        //初始化界面
        initView();
        //初始化监听器
        initEvent();
        //进来读取一次数据(为了更新currentPage,currentTotal这个值)
        loadNextPageNascentMessageList(1, true);
    }
    private void initData() {
        mList.clear();
        mList.addAll(HdlMessageLogic.getInstance().getNascentMessageMemoryList());
    }
    private void initEvent() {
        //设置下拉箭头颜色
@@ -85,9 +78,10 @@
        viewBinding.messageNascentSrl.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mList.clear();//表示强制重新请求消息列表
                updateListData();//清空之前缓存,因为遇到一个问题猛拉的时候报错;
                getNascentMessageList(false);
                viewBinding.messageNascentSrl.setRefreshing(false);
                mList.clear();//表示强制重新请求第一页消息列表
                HdlMessageLogic.getInstance().clearListMessage();//表示强制清空所有缓存信息
                loadNextPageNascentMessageList(1, true);
            }
        });
        messageNascentAdapter.setNoOnclickListener(new MessageAdapter.OnclickListener() {
@@ -107,24 +101,21 @@
        viewBinding.messageNascentRecyclerview.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
//                super.onScrolled(recyclerView, dx, dy);
                if (dy <= 0) {
                    //等于0是顶部
                    return;
                }
                LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
                assert layoutManager != null;
                // 只有垂直布局才会触发上拉事件
                if (!layoutManager.canScrollVertically()) {
                if (layoutManager == null) {
                    return;
                }
                int visibleItemCount = layoutManager.getChildCount();
                int totalItemCount = layoutManager.getItemCount();
                int lastVisibleItemPosition = layoutManager.findLastVisibleItemPosition();
                if (!isLoadingMore && totalItemCount <= (lastVisibleItemPosition + visibleThreshold)) {
//                    loadNextPage();
                int firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition();
                if (visibleItemCount > 0 && visibleItemCount + firstVisibleItemPosition == totalItemCount) {
                    if (!isLoadingMore) {
                        // 滑动到了底部,执行相应的操作
                        HdlLogLogic.print("--->滑动到了底部");
                        loadNextPageNascentMessageList(++currentPage, false);
                    }
                }
            }
        });
@@ -141,30 +132,63 @@
     * 刷新列表数据
     */
    private void updateListData() {
        if (this.messageNascentAdapter != null) {
            this.messageNascentAdapter.setList(mList);
            this.messageNascentAdapter.notifyDataSetChanged();
        }
        //刷新【发生中】信息数量总数
        this.refreshNascentCount();
        this.nullDataUpdateUi();
        HdlThreadLogic.runMainThread(new Runnable() {
            @Override
            public void run() {
                //更新本地缓存数据
                updatelocalityCacheData();
                //刷新列表
                if (messageNascentAdapter != null) {
                    messageNascentAdapter.setList(mList);
                    messageNascentAdapter.notifyDataSetChanged();
                }
                //显示未读信息总数量
                getUnreadCount();
                //显示没数据Ui样式
                nullDataUpdateUi();
            }
        });
    }
    /**
     * 刷新【发生中】信息数量总数
     */
    private void refreshNascentCount() {
    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 (mList.size() > 0) {
                        String s = getText(R.string.message_nascent) + "(" + mList.size() + ")";
                    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);
                        }
                    }
                }
            }
@@ -180,9 +204,6 @@
        //接收外部点击事件
        if (eventBus.getTopic().equals(ConstantManage.message_function_tab_switch)) {
            if (eventBus.getType().equals(MessageFunctionTabSwitch.nascent.toString())) {
                initData();
                getNascentMessageList(true);
                getUnreadCount();
                // 取消粘性事件
                EventBus.getDefault().removeStickyEvent(eventBus);
@@ -191,82 +212,69 @@
        } else if (eventBus.getTopic().equals(ConstantManage.message_function_push_post)) {
            if (eventBus.getType().equals(MessageStateType.untreated)) {
                //消息详情回调
                initData();
                updatelocalityCacheData();
                updateListData();
                getUnreadCount();
            }
        }
    }
    /**
     * 获取发生中的消息列表
     *
     * @param isShowLoading 是否启动加载框 true启动
     * 更新本地缓存数据
     */
    private void getNascentMessageList(boolean isShowLoading) {
        if (mList.size() == 0) {
            if (isShowLoading) {
                showLoading();
            }
            HdlMessageLogic.getInstance().getNascentMessageList(new CloudCallBeak<List<MessageBean>>() {
                @Override
                public void onSuccess(List<MessageBean> list) {
                    HdlThreadLogic.runMainThread(new Runnable() {
                        @Override
                        public void run() {
                            if (isShowLoading) {
                                hideLoading();
                            }
                            if (!isShowLoading) {
                                viewBinding.messageNascentSrl.setRefreshing(false);
                            }
                            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);
                    }
                }
            });
        }
    private void updatelocalityCacheData() {
        mList.clear();
        mList.addAll(HdlMessageLogic.getInstance().getNascentMessageMemoryList());
    }
//    private void loadNextPage() {
//        isLoadingMore = true;
//
//        // 模拟网络请求获取新的数据
//        Handler handler = new Handler();
//        handler.postDelayed(() -> {
//            List<MessageBean> newData = fetchNewDataFromServer(currentPage++);
//            messageNascentAdapter.setList(newData);
//            isLoadingMore = false;
//        }, 2000);
//    }
//
//    private List<MessageBean> fetchNewDataFromServer(int pageNumber) {
//        // TODO: 从服务器获取新的数据并返回
//        pageNumber++;
//        List<MessageBean> newData = HdlMessageLogic.getInstance().getAllMessageList();
//        return newData;
//    }
    /**
     * 加载一页数据
     *
     * @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>() {
        HdlMessageLogic.getInstance().getMessageUntreatedCount("",new CloudCallBeak<String>() {
            @Override
            public void onSuccess(String data) {
@@ -274,27 +282,15 @@
                    @Override
                    public void run() {
                        JSONObject jsonObject = JSON.parseObject(data);
//                        if (jsonObject.containsKey("untreatedCount")) {
//                            //未处理数量
//                            jsonObject.getString("untreatedCount");
//                        }
                        String unreadCount = "0";
                        if (jsonObject.containsKey("untreatedCount")) {
                            //未处理数量
                            String untreatedCount = jsonObject.getString("untreatedCount");
                            refreshNascentCount(untreatedCount);
                        }
                        if (jsonObject.containsKey("unreadCount")) {
                            //未读数量
                            unreadCount = jsonObject.getString("unreadCount");
                        }
                        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 {
                                if (unreadCount.length() > 2) {
                                    //大于3位数,默认99+
                                    unreadCount = "99+";
                                }
                                textView.setVisibility(View.VISIBLE);
                                textView.setText(unreadCount);
                            }
                            String unreadCount = jsonObject.getString("unreadCount");
                            refreshUnreadNascentCount(unreadCount);
                        }
                    }
                });
@@ -316,9 +312,6 @@
     * 没有电站列表的样式
     */
    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);
        HdlCommonLogic.getInstance().nullDataUpdateUi(_mActivity, viewBinding.nullDataIc.getRoot(), viewBinding.nullDataIc.nullDataGifAnimationIv, viewBinding.nullDataIc.nullDataTv, getString(R.string.message_alarm_data_null), mList.size() > 0);
    }
}