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 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.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);
|
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);
|
}
|
}
|
});
|
}
|
|
/**
|
* 物理按键返回事件
|
*/
|
@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));
|
}
|
}
|
|
/**
|
* 获取【搜索历史记录文件】全路径
|
*/
|
public String getHistoryFileNamePath() {
|
return HdlFileLogic.getInstance().getCurrentUserRootPath() + "/message.txt";
|
}
|
}
|