mac
2024-01-30 f2d9afb007d65cc6c5124442f1bc35fab129d1aa
app/src/main/java/com/hdl/photovoltaic/ui/message/NascentMessageFragment.java
@@ -3,25 +3,44 @@
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.MotionEvent;
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.google.gson.Gson;
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.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;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
@@ -33,6 +52,12 @@
    private MessageAdapter messageNascentAdapter;
    private final List<MessageBean> mList = new ArrayList<>();
    private int currentPage = 0; // 当前页码
    private int total = 0; // 最后页码
    private boolean isLoadingMore = false; // 标记正在加载更多数据
    LinearLayoutManager layoutManager;
    @Override
    public Object getContentView() {
@@ -43,7 +68,7 @@
    @Override
    public void onBindView(Bundle savedInstanceState) {
        //初始化数据
        initData();
//        initData();
        //初始化界面
        initView();
        //初始化监听器
@@ -51,7 +76,8 @@
    }
    private void initData() {
        getNascentMessageList(true);
        mList.clear();
        mList.addAll(HdlMessageLogic.getInstance().getNascentMessageMemoryList());
    }
    private void initEvent() {
@@ -60,13 +86,18 @@
        viewBinding.messageNascentSrl.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mList.clear();//表示强制重新请求消息列表
                getNascentMessageList(false);
                viewBinding.messageNascentSrl.setRefreshing(false);
//                mList.clear();//表示强制重新请求消息列表
//                updateListData();//清空之前缓存,因为遇到一个问题猛拉的时候报错;
//                getNascentMessageList(false);
            }
        });
        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);
@@ -74,10 +105,33 @@
            }
        });
        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);
    }
@@ -86,59 +140,189 @@
     * 刷新列表数据
     */
    private void updateListData() {
        if (this.messageNascentAdapter != null) {
            this.messageNascentAdapter.setList(mList);
            this.messageNascentAdapter.notifyDataSetChanged();
        }
        HdlThreadLogic.runMainThread(new Runnable() {
            @Override
            public void run() {
                //更新缓存数据
                initData();
                //刷新列表
                if (messageNascentAdapter != null) {
                    messageNascentAdapter.setList(mList);
                    messageNascentAdapter.notifyDataSetChanged();
                }
                //显示发生中消息总数量
                refreshNascentCount();
                //显示没数据Ui样式
                nullDataUpdateUi();
                //显示未读信息总数量
                getUnreadCount();
            }
        });
    }
    @Override
    /**
     * 刷新【发生中】信息数量总数
     */
    private void refreshNascentCount() {
        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() + ")";
                        textView.setText(s);
                    }
                }
            }
        });
    }
    @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);
                initData();
                loadNextPageNascentMessageList(1, true);
//                getUnreadCount();
                // 取消粘性事件
                EventBus.getDefault().removeStickyEvent(eventBus);
            }
        } else if (eventBus.getTopic().equals(ConstantManage.message_function_push_post)) {
            if (eventBus.getType().equals(MessageStateType.untreated)) {
                //消息详情回调
                initData();
                updateListData();
//                getUnreadCount();
            }
        }
    }
    /**
     * 获取发生中的消息列表
     * 加载一页数据
     *
     * @param isShowLoading 是否启动 true启动
     * @param pageNo        页数
     * @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);
                    }
                    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 loadNextPageNascentMessageList(int pageNo, boolean isShowLoading) {
        if (pageNo == 1 && mList.size() > 0) {
            //没有缓存数据,才可以读取第一页
            return;
        }
        if (currentPage > total) {
            //当前页不能大于总页数
            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) {
                    total = (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() {
        String untreatedMemoryCount = HdlMessageLogic.getInstance().getMessageUntreatedMemoryCount();
        TextView textView = _mActivity.findViewById(R.id.my_message_bottom_il2).findViewById(R.id.red_count_tv);
        if (textView != null) {
            if (TextUtils.isEmpty(untreatedMemoryCount) || untreatedMemoryCount.equals("0")) {
                textView.setVisibility(View.GONE);
            } else {
                if (untreatedMemoryCount.length() > 2) {
                    //大于3位数,默认99+
                    untreatedMemoryCount = "99+";
                }
                textView.setVisibility(View.VISIBLE);
                textView.setText(untreatedMemoryCount);
            }
        }
//
//        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")) {
////                            //未处理数量
////                            jsonObject.getString("untreatedCount");
////                        }
//                        String unreadCount = "0";
//                        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);
//                            }
//
//
//                        }
//                    }
//                });
//
//            }
//
//            @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);
    }
}