package com.hdl.photovoltaic.ui.message; import android.annotation.SuppressLint; import android.content.Intent; import android.os.AsyncTask; import android.os.Build; import android.os.Bundle; import android.text.Editable; import android.text.TextUtils; import android.text.TextWatcher; import android.view.View; import android.widget.TextView; import androidx.recyclerview.widget.LinearLayoutManager; import com.google.gson.Gson; 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.FragementMessageHouseBinding; import com.hdl.photovoltaic.enums.HomepageTitleTabSwitch; 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.adapter.MessageHouseAdapter; import com.hdl.photovoltaic.ui.bean.MessageBean; import com.hdl.photovoltaic.ui.bean.UnCountBean; import com.hdl.photovoltaic.utils.KeyboardStateObserverUtils; import com.hdl.photovoltaic.widget.refreshlayout.BGARefreshLayout; import com.hdl.photovoltaic.widget.refreshlayout.HDLRefreshViewHolder; 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 MessageHouseFragment extends CustomBaseFragment { private FragementMessageHouseBinding viewBinding; private MessageHouseAdapter mMessageHouseAdapter; private boolean isFirstLoading = true; private final List mList = new ArrayList<>(); @Override public Object getContentView() { viewBinding = FragementMessageHouseBinding.inflate(getLayoutInflater()); return viewBinding.getRoot(); } @Override public void onBindView(Bundle savedInstanceState) { //初始化界面 initView(); //初始化监听器 initEvent(); //初始化数据 initData(true); } /** * 获取账号下全部电站消息 * * @param homeName 电站名称(搜索用的),如果是空字符串默认读取全部 * @param isShowLoading 是否需要加载窗(true=需要,false=不需要) */ private void getMessageHouseList(String homeName, boolean isShowLoading) { if (isShowLoading) { showLoading(); } HdlMessageLogic.getInstance().getMessageHouseList(homeName, new CloudCallBeak>() { @Override public void onSuccess(List list) { isFirstLoading = false; if (isShowLoading) { hideLoading(); } mList.clear(); if (list != null && !list.isEmpty()) { mList.addAll(list); } updateRecyclerViewList(); } @Override public void onFailure(HDLException e) { if (isShowLoading) { hideLoading(); } } }); } /** * 获取账号下全部电站消息 * * @param homeId 消息id(不传已读所有电站的,传值已读指定电站的) * @param isShowLoading 是否需要加载窗(true=需要,false=不需要) */ private void messageAllRead(String homeId, boolean isShowLoading) { if (isShowLoading) { showLoading(); } HdlMessageLogic.getInstance().messageAllRead(homeId, new CloudCallBeak() { @Override public void onSuccess(Boolean obj) { if (isShowLoading) { hideLoading(); } if (!mList.isEmpty()) { for (int i = 0; i < mList.size(); i++) { MessageBean messageBean = mList.get(i); messageBean.setUnreadCount(0L); } updateRecyclerViewList(); } } @Override public void onFailure(HDLException e) { if (isShowLoading) { hideLoading(); } } }); } /** * 读取消息【未读数量,未处理总数量,已处理总数量】 */ private void getUnreadCount() { HdlMessageLogic.getInstance().getMessageUntreatedCount("", new CloudCallBeak() { @Override public void onSuccess(UnCountBean unCountBean) { HdlThreadLogic.runMainThread(new Runnable() { @Override public void run() { if (unCountBean == null) { return; } refreshUnreadCount(unCountBean.getUnreadCount()); } }); } @Override public void onFailure(HDLException e) { HdlThreadLogic.runMainThread(new Runnable() { @Override public void run() { 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 initData(boolean isShowLoading) { this.getMessageHouseList("", isShowLoading); this.getUnreadCount(); } private void initEvent() { //搜索 viewBinding.powerStationSearchEt.addTextChangedListener(powerStationSearchWatcher); //清除搜索文案 viewBinding.powerStationClearIv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { viewBinding.powerStationSearchEt.setText(""); viewBinding.powerStationClearIv.setVisibility(View.GONE); getMessageHouseList("", false); } }); //全部已读 viewBinding.powerStationReadIv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { messageAllRead("", true); refreshUnreadCount(0); } }); if (mMessageHouseAdapter != null) { mMessageHouseAdapter.setOnclickListener(new MessageHouseAdapter.OnClickListener() { @Override public void onClick(int position, MessageBean messageBean) { Intent intent = new Intent(); intent.putExtra("messageBean", new Gson().toJson(messageBean)); intent.setClass(_mActivity, MessageActivity.class); startActivity(intent); } }); } } /** * 重新更新列表数据 */ private void updateRecyclerViewList() { if (mMessageHouseAdapter == null) { return; } mMessageHouseAdapter.setList(mList); nullDataUpdateUi(); } /** * 刷新【消息模块】下角标数值 * * @param unreadCount 未读信息数量总数 */ private void refreshUnreadCount(int unreadCount) { HdlThreadLogic.runMainThread(new Runnable() { @SuppressLint("SetTextI18n") @Override public void run() { TextView textView = _mActivity.findViewById(R.id.my_message_bottom_il2).findViewById(R.id.red_count_tv); if (textView != null) { if (unreadCount == 0) { textView.setVisibility(View.GONE); } else { textView.setVisibility(View.VISIBLE); if (unreadCount > 99) { textView.setText("99+"); } else { textView.setText(unreadCount + ""); } } } } }); } /** * 没有消息列表的样式 */ private void nullDataUpdateUi() { HdlCommonLogic.getInstance().nullDataUpdateUi(_mActivity, viewBinding.deviceNullDataIc.getRoot(), viewBinding.deviceNullDataIc.nullDataGifAnimationIv, viewBinding.deviceNullDataIc.nullDataTv, getString(R.string.message_alarm_data_null), mList.size() > 0); } private void initView() { mMessageHouseAdapter = new MessageHouseAdapter(_mActivity); viewBinding.houseListRcv.setLayoutManager(new LinearLayoutManager(_mActivity)); viewBinding.houseListRcv.setAdapter(mMessageHouseAdapter); this.pullToRefresh(); //在界面中使用 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.powerStationSearchEt.clearFocus(); } }); } /** * 下拉刷新的逻辑 */ protected void pullToRefresh() { viewBinding.houseListRbl.setDelegate(new BGARefreshLayout.BGARefreshLayoutDelegate() { @SuppressLint("StaticFieldLeak") @Override public void onBGARefreshLayoutBeginRefreshing(BGARefreshLayout refreshLayout) { new AsyncTask() { @Override protected Void doInBackground(Void... params) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(Void aVoid) { // 执行任务(如更新UI、跳转等) viewBinding.houseListRbl.endRefreshing(); initData(false); } }.execute(); } @SuppressLint("StaticFieldLeak") @Override public boolean onBGARefreshLayoutBeginLoadingMore(BGARefreshLayout refreshLayout) { new AsyncTask() { @Override protected Void doInBackground(Void... params) { try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(Void aVoid) { viewBinding.houseListRbl.endLoadingMore(); //加载最新数据完成 } }.execute(); return true; } }); HDLRefreshViewHolder hdlRefreshViewHolder = new HDLRefreshViewHolder(_mActivity, true); hdlRefreshViewHolder.setPullDownImageResource(R.mipmap.loading_44); hdlRefreshViewHolder.setChangeToReleaseRefreshAnimResId(R.drawable.loading_refresh); hdlRefreshViewHolder.setRefreshingAnimResId(R.drawable.loading_refresh_end); viewBinding.houseListRbl.setRefreshViewHolder(hdlRefreshViewHolder); } @Subscribe(threadMode = ThreadMode.MAIN, sticky = true) public void onEventMessage(BaseEventBus eventBus) { super.onEventMessage(eventBus); if (eventBus == null) { return; } //接收外部点击事件 if (eventBus.getTopic().equals(ConstantManage.homepage_title_tab_switch)) { if (eventBus.getType().equals(HomepageTitleTabSwitch.message.toString())) { // 取消粘性事件 EventBus.getDefault().removeStickyEvent(eventBus); HdlLogLogic.print("正在点击【消息】"); //第二次进来不显示加载框 if (!isFirstLoading) { initData(false); } } } else if (eventBus.getTopic().equals(ConstantManage.refresh_message_house)) { // 取消粘性事件 EventBus.getDefault().removeStickyEvent(eventBus); //第二次进来不显示加载框 if (!isFirstLoading) { initData(false); } } } /** * */ private final TextWatcher powerStationSearchWatcher = 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) { //搜索关键字 String currSearchText = s.toString().replace(" ", ""); if (TextUtils.isEmpty(currSearchText)) { viewBinding.powerStationClearIv.setVisibility(View.GONE); // HdlThreadLogic.toast(_mActivity, R.string.search_content_null); return; } viewBinding.powerStationClearIv.setVisibility(View.VISIBLE); viewBinding.powerStationSearchEt.clearFocus(); getMessageHouseList(currSearchText, false); } @Override public void afterTextChanged(Editable s) { } }; @Override public void onDestroy() { super.onDestroy(); viewBinding.powerStationSearchEt.removeTextChangedListener(powerStationSearchWatcher); } }