From a77dcb0250488ca6567490ed9fb5ce969bf41db7 Mon Sep 17 00:00:00 2001
From: mac <user@users-MacBook-Pro.local>
Date: 星期三, 29 五月 2024 14:55:03 +0800
Subject: [PATCH] Merge branch 'wjc_new' into feature/hxb

---
 app/src/main/java/com/hdl/photovoltaic/ui/message/SearchMessageActivity.java |  413 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 413 insertions(+), 0 deletions(-)

diff --git a/app/src/main/java/com/hdl/photovoltaic/ui/message/SearchMessageActivity.java b/app/src/main/java/com/hdl/photovoltaic/ui/message/SearchMessageActivity.java
new file mode 100644
index 0000000..067e6f3
--- /dev/null
+++ b/app/src/main/java/com/hdl/photovoltaic/ui/message/SearchMessageActivity.java
@@ -0,0 +1,413 @@
+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.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 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; // 鏍囪姝e湪鍔犺浇鏇村鏁版嵁
+
+    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.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);
+                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);
+    }
+
+
+    /**
+     * 杈撳叆鐢电珯鍚嶇О杩涜杩囨护
+     */
+    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);
+                }
+            }
+        });
+    }
+
+    /**
+     * 鐗╃悊鎸夐敭杩斿洖浜嬩欢
+     */
+    @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(currSearchText);
+        // 鍊掑簭鎺掑垪鏁版嵁(鎼滅储鏈�鍚庢帓鍦ㄥ墠闈�)
+        Collections.reverse(searchHistoryTitleList);
+        if (searchHistoryTitleList.size() > maxValue) {
+            //鍘嗗彶璁板綍涓嶈兘瓒呭嚭10
+            searchHistoryTitleList.remove(searchHistoryTitleList.get(maxValue));
+        }
+
+    }
+
+    /**
+     * 鑾峰彇銆愭悳绱㈠巻鍙茶褰曟枃浠躲�戝叏璺緞
+     */
+    public String getHistoryFileNamePath() {
+        return HdlFileLogic.getInstance().getCurrentUserRootPath() + "/message.txt";
+    }
+}
\ No newline at end of file

--
Gitblit v1.8.0