mac
2024-06-04 276156837ebca8b8b08a516e61fe105e670375fc
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
package com.hdl.photovoltaic.ui.message;
 
 
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
 
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.google.gson.reflect.TypeToken;
import com.hdl.linkpm.sdk.core.exception.HDLException;
import com.hdl.photovoltaic.R;
import com.hdl.photovoltaic.base.CustomBaseActivity;
import com.hdl.photovoltaic.databinding.ActivitySearchMessgeBinding;
import com.hdl.photovoltaic.listener.CloudCallBeak;
import com.hdl.photovoltaic.other.HdlCommonLogic;
import com.hdl.photovoltaic.other.HdlFileLogic;
import com.hdl.photovoltaic.other.HdlLogLogic;
import com.hdl.photovoltaic.other.HdlMessageLogic;
import com.hdl.photovoltaic.other.HdlThreadLogic;
import com.hdl.photovoltaic.ui.adapter.SearchHistoryAdapter;
import com.hdl.photovoltaic.ui.adapter.SearchMessageAdapter;
import com.hdl.photovoltaic.ui.bean.MessageBean;
import com.hdl.photovoltaic.utils.KeyboardStateObserverUtils;
 
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
 
/**
 * 消息搜索界面
 */
public class SearchMessageActivity extends CustomBaseActivity {
 
    private ActivitySearchMessgeBinding viewBinding;
 
    SearchMessageAdapter searchMessageAdapter;//设备适配器
    SearchHistoryAdapter searchHistoryAdapter;//历史记录适配器
 
    private final List<MessageBean> mList = new ArrayList<>();
    private int mCurrentPage = 0; // 当前页码
    private int mCurrentTotal = 0; // 总页码
    private boolean isLoadingMore = false; // 标记正在加载更多数据
 
    private String currSearchText;
 
    List<String> searchHistoryTitleList = new ArrayList<>();
 
    String mMessageStateType = "";
 
    @Override
    public Object getContentView() {
        viewBinding = ActivitySearchMessgeBinding.inflate(getLayoutInflater());
        return viewBinding.getRoot();
    }
 
    @Override
    public void onBindView(Bundle savedInstanceState) {
        setStatusBarTranslucent();
        mMessageStateType = getIntent().getStringExtra("MessageStateType");
        //历史记录文件夹创建
        HdlFileLogic.getInstance().createFileDir(HdlFileLogic.getInstance().getCurrentUserRootPath());
        //初始化数据
        initData();
        //初始化
        initView();
        //初始化界面监听器
        initEvent();
    }
 
    private void initData() {
        try {
            String json = HdlFileLogic.getInstance().readFile(getHistoryFileNamePath());
            if (TextUtils.isEmpty(json)) {
                return;
            }
            searchHistoryTitleList = new Gson().fromJson(json, new TypeToken<List<String>>() {
            }.getType());
        } catch (Exception ignored) {
        }
    }
 
 
    private void initEvent() {
 
        //后退
        viewBinding.backRl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        viewBinding.messageSearchEt.addTextChangedListener(textWatcher);
        //搜索
        viewBinding.powerStationSearchTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //搜索关键字
                currSearchText = viewBinding.messageSearchEt.getText().toString().replace(" ", "");
                if (TextUtils.isEmpty(currSearchText)) {
                    HdlThreadLogic.toast(_mActivity, R.string.search_content_null);
                    return;
                }
                viewBinding.historyListParent.setVisibility(View.GONE);
                viewBinding.listParent.setVisibility(View.VISIBLE);
                addSearchTextToList();
                loadNextPageMessageList(true, 1, viewBinding.messageSearchEt.getText().toString(), true);
            }
        });
 
 
        //下拉箭头颜色
        viewBinding.listSrl.setColorSchemeResources(R.color.text_FF245EC3);
        //下拉读取
        viewBinding.listSrl.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                viewBinding.listSrl.setRefreshing(false);
                loadNextPageMessageList(true, 1, viewBinding.messageSearchEt.getText().toString(), true);
            }
        });
        //上拉读取
        viewBinding.listRcv.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int 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("--->滑动到了底部");
                        loadNextPageMessageList(false, ++mCurrentPage, viewBinding.messageSearchEt.getText().toString(), false);
                    }
                }
            }
        });
        //清除
        viewBinding.powerStationClearIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                currSearchText = "";
                viewBinding.messageSearchEt.setText("");
                viewBinding.historyListParent.setVisibility(View.VISIBLE);
                viewBinding.deviceNullDataIc.getRoot().setVisibility(View.GONE);
                viewBinding.listParent.setVisibility(View.GONE);
                searchHistoryAdapter.setList(searchHistoryTitleList);
                clearCacheData();
            }
        });
        //删除
        viewBinding.messageDelIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                searchHistoryTitleList = new ArrayList<>();
                searchHistoryAdapter.setList(searchHistoryTitleList);
                viewBinding.deviceNullDataIc.getRoot().setVisibility(View.GONE);
                HdlFileLogic.getInstance().deleteFile(getHistoryFileNamePath());
            }
        });
        //历史记录点击事件
        searchHistoryAdapter.setOnclickListener(new SearchHistoryAdapter.OnClickListener() {
            @Override
            public void onClick(int position, String title) {
                viewBinding.messageSearchEt.setText(title);
                //搜索关键字
                currSearchText = viewBinding.messageSearchEt.getText().toString().replace(" ", "");
                if (TextUtils.isEmpty(currSearchText)) {
                    HdlThreadLogic.toast(_mActivity, R.string.search_content_null);
                    return;
                }
                viewBinding.historyListParent.setVisibility(View.GONE);
                viewBinding.listParent.setVisibility(View.VISIBLE);
                addSearchTextToList();
                loadNextPageMessageList(true, 1, viewBinding.messageSearchEt.getText().toString(), true);
            }
        });
        //消息点击是事件
        searchMessageAdapter.setOnclickListener(new SearchMessageAdapter.OnClickListener() {
            @Override
            public void onClick(int position, MessageBean messageBean) {
                Intent intent = new Intent();
                intent.putExtra("messageBean", new Gson().toJson(messageBean));
                intent.setClass(_mActivity, MessageInfoActivity.class);
                startActivity(intent);
            }
        });
 
    }
 
    private void initView() {
        viewBinding.historyListParent.setVisibility(View.VISIBLE);
        viewBinding.listParent.setVisibility(View.GONE);
        viewBinding.powerStationClearIv.setVisibility(View.GONE);
        //初始化历史记录适配器
        searchHistoryAdapter = new SearchHistoryAdapter(_mActivity);
        viewBinding.historyListRcv.setLayoutManager(new LinearLayoutManager(_mActivity));
        viewBinding.historyListRcv.setAdapter(searchHistoryAdapter);
        searchHistoryAdapter.setList(searchHistoryTitleList);
        //初始化消息适配器
        searchMessageAdapter = new SearchMessageAdapter(_mActivity);
        viewBinding.listRcv.setLayoutManager(new LinearLayoutManager(_mActivity));
        viewBinding.listRcv.setAdapter(searchMessageAdapter);
        viewBinding.messageSearchEt.post(new Runnable() {
            @Override
            public void run() {
                viewBinding.messageSearchEt.requestFocus();
            }
        });
 
        //在界面中使用
        KeyboardStateObserverUtils.getKeyboardStateObserver(_mActivity).setKeyboardVisibilityListener(new KeyboardStateObserverUtils.OnKeyboardVisibilityListener() {
            @Override
            public void onKeyboardShow(int h) {
                //Toast.makeText(MainActivity.this,"键盘弹出",Toast.LENGTH_SHORT).show();
                viewBinding.messageSearchEt.requestFocus();
 
            }
 
            @Override
 
            public void onKeyboardHide(int h) {
                viewBinding.messageSearchEt.clearFocus();
            }
 
        });
    }
 
 
    /**
     * 输入电站名称进行过滤
     */
    private final TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
 
        }
 
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }
 
        @Override
        public void afterTextChanged(Editable s) {
            String et = viewBinding.messageSearchEt.getText().toString().replace(" ", "");
            if (TextUtils.isEmpty(et)) {
                viewBinding.powerStationClearIv.setVisibility(View.GONE);
            } else {
                viewBinding.powerStationClearIv.setVisibility(View.VISIBLE);
            }
            viewBinding.messageSearchEt.setSelection(viewBinding.messageSearchEt.length());
 
        }
    };
 
    /**
     * 刷新列表数据
     */
    private void updateListData() {
        HdlThreadLogic.runMainThread(new Runnable() {
            @Override
            public void run() {
                if (searchMessageAdapter != null) {
                    searchMessageAdapter.setList(mList);
                }
                nullDataUpdateUi();
            }
        });
    }
 
    /**
     * 物理按键返回事件
     */
    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }
 
    @Override
    protected void onDestroy() {
        viewBinding.messageSearchEt.removeTextChangedListener(textWatcher);
        if (searchHistoryTitleList.size() > 0) {
            HdlFileLogic.getInstance().deleteFile(getHistoryFileNamePath());
            HdlFileLogic.getInstance().appendFile(getHistoryFileNamePath(), new Gson().toJson(searchHistoryTitleList));
        }
        super.onDestroy();
    }
 
 
    /**
     * 加载一页数据
     *
     * @param isShowLoading 是否启动加载框 true启动
     * @param pageNo        页数
     * @param search_title  搜索文本
     * @param isClear       true表示清空缓存
     */
    private void loadNextPageMessageList(boolean isShowLoading, int pageNo, String search_title, boolean isClear) {
        if (isClear) {
            //表示从第一页可以读取,默认清空所有缓存数据;
            clearCacheData();
        }
        //第一页读取数据强制读
        if (pageNo > 1 && mCurrentPage > mCurrentTotal) {
            --mCurrentPage;
            //当前页不能大于总页数
            return;
        }
        isLoadingMore = true;//标记读取状态
        if (isShowLoading) {
            showLoading();
        }
        HdlMessageLogic.getInstance().getPageNoMessageList(
                "",
                pageNo,
                mMessageStateType,
                "",
                "",
                "",
                "",
                "",
                "",
                search_title,
                new CloudCallBeak<HdlMessageLogic.MessageListClass>() {
                    @Override
                    public void onSuccess(HdlMessageLogic.MessageListClass messageListClass) {
                        if (messageListClass != null) {
                            mCurrentTotal = (int) messageListClass.getTotalPage();
                            mCurrentPage = (int) messageListClass.getPageNo();
                            setMessageList(messageListClass.getList());
                            updateListData();
                        }
                        isLoadingMore = false;
                        if (isShowLoading) {
                            hideLoading();
                        }
                    }
 
                    @Override
                    public void onFailure(HDLException e) {
                        if (mCurrentPage > 1) {
                            --mCurrentPage;
                        }
                        isLoadingMore = false;
                        if (isShowLoading) {
                            hideLoading();
                        }
                    }
                });
 
 
    }
 
 
    private void clearCacheData() {
        if (mList != null && mList.size() > 0) {
            mList.clear();
        }
    }
 
    public void setMessageList(List<MessageBean> list) {
        if (list == null || list.size() == 0) {
            return;
        }
        if (this.mList.size() == 0) {
            this.mList.addAll(list);
            return;
        }
        for (int i = 0; i < list.size(); i++) {
            this.setSingleDevice(list.get(i));
        }
    }
 
    /**
     * 添加设备到列表里面
     *
     * @param messageBean -设备对象
     */
    public void setSingleDevice(MessageBean messageBean) {
        try {
            if (messageBean == null) {
                return;
            }
            boolean if_boolean = false;
            for (int i = 0; i < mList.size(); i++) {
                if (mList.get(i).getHomeId().equals(messageBean.getHomeId())) {
                    //存在替换
                    mList.remove(i);
                    mList.add(i, messageBean);
                    if_boolean = true;
                    break;
                }
            }
            if (!if_boolean) {
                //没有添加
                this.mList.add(messageBean);
            }
        } catch (Exception e) {
            String mes = e.getMessage();
            HdlLogLogic.print("--->" + mes);
        }
    }
 
    /**
     * 添加历史记录
     */
    private void addSearchTextToList() {
        int maxValue = 10;
        for (int i = 0; i < searchHistoryTitleList.size(); i++) {
            if (searchHistoryTitleList.get(i).equals(currSearchText)) {
                searchHistoryTitleList.remove(searchHistoryTitleList.get(i));
            }
        }
        searchHistoryTitleList.add(0, currSearchText);
        if (searchHistoryTitleList.size() > maxValue) {
            //历史记录不能超出10
            searchHistoryTitleList.remove(searchHistoryTitleList.get(maxValue));
        }
    }
 
    /**
     * 没有消息列表的样式
     */
    private void nullDataUpdateUi() {
        boolean is_data = mList != null && mList.size() > 0;
        HdlCommonLogic.getInstance().nullDataUpdateUi(_mActivity, viewBinding.deviceNullDataIc.getRoot(), viewBinding.deviceNullDataIc.nullDataGifAnimationIv, viewBinding.deviceNullDataIc.nullDataTv, getString(R.string.message_alarm_data_null), is_data);
    }
 
    /**
     * 获取【搜索历史记录文件】全路径
     */
    public String getHistoryFileNamePath() {
        return HdlFileLogic.getInstance().getCurrentUserRootPath() + "/message.txt";
    }
}