mac
2024-01-02 d1ebb94e1a17b7c25d4fcf73d85345d92cc86b5c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package com.hdl.photovoltaic.ui.message;
 
 
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
 
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
 
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.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;
 
/**
 * 消息-发生中
 */
public class NascentMessageFragment extends CustomBaseFragment {
    private FragmentNascentMessageBinding viewBinding;
 
    private MessageAdapter messageNascentAdapter;
 
    private final List<MessageBean> mList = new ArrayList<>();
 
    // 添加上拉加载更多的逻辑
    private int visibleThreshold = 5; // 当前显示的最后一项与总共有多少条数据之间的差值
    private int currentPage = 1; // 当前页码
    private boolean isLoadingMore = false; // 标记正在加载更多数据
 
    LinearLayoutManager layoutManager;
 
    @Override
    public Object getContentView() {
        viewBinding = FragmentNascentMessageBinding.inflate(getLayoutInflater());
        return viewBinding.getRoot();
    }
 
    @Override
    public void onBindView(Bundle savedInstanceState) {
        //初始化数据
//        initData();
        //初始化界面
        initView();
        //初始化监听器
        initEvent();
    }
 
    private void initData() {
        mList.clear();
        mList.addAll(HdlMessageLogic.getInstance().getNascentMessageMemoryList());
    }
 
    private void initEvent() {
        //设置下拉箭头颜色
        viewBinding.messageNascentSrl.setColorSchemeResources(R.color.text_FF245EC3);
        viewBinding.messageNascentSrl.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                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);
                startActivity(intent);
 
            }
        });
        viewBinding.messageNascentRecyclerview.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
 
//                int totalItemCount = layoutManager.getItemCount();
//                int lastVisibleItemPosition = layoutManager.findLastVisibleItemPosition();
//
//                if (!isLoadingMore && totalItemCount <= (lastVisibleItemPosition + visibleThreshold)) {
//                    loadNextPage();
//                }
            }
        });
    }
 
    private void initView() {
        layoutManager = new LinearLayoutManager(_mActivity);
        viewBinding.messageNascentRecyclerview.setLayoutManager(layoutManager);
        messageNascentAdapter = new MessageAdapter(this.mList, _mActivity);
        viewBinding.messageNascentRecyclerview.setAdapter(messageNascentAdapter);
    }
 
    /**
     * 刷新列表数据
     */
    private void updateListData() {
        if (this.messageNascentAdapter != null) {
            this.messageNascentAdapter.setList(mList);
            this.messageNascentAdapter.notifyDataSetChanged();
        }
    }
 
    @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())) {
                initData();
                getNascentMessageList(true);
                // 取消粘性事件
                EventBus.getDefault().removeStickyEvent(eventBus);
            }
 
        } else if (eventBus.getTopic().equals(ConstantManage.message_function_push_post)) {
            if (eventBus.getType().equals(MessageStateType.untreated)) {
                //消息详情回调
                initData();
                updateListData();
            }
        }
    }
 
    /**
     * 获取发生中的消息列表
     *
     * @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 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;
//    }
}