增加一些非空判断

This commit is contained in:
ningwenqiang 2024-08-28 11:22:13 +08:00
parent ba47239e12
commit 114a6472fb
7 changed files with 174 additions and 86 deletions

View File

@ -15,6 +15,7 @@ import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat; import androidx.core.content.ContextCompat;
import com.blankj.utilcode.util.LogUtils; import com.blankj.utilcode.util.LogUtils;
import com.google.android.exoplayer2.C;
import com.yunbao.common.R; import com.yunbao.common.R;
import com.yunbao.common.glide.ImgLoader; import com.yunbao.common.glide.ImgLoader;
import com.yunbao.common.interfaces.OnRecyclerListRefreshListener; import com.yunbao.common.interfaces.OnRecyclerListRefreshListener;
@ -24,6 +25,7 @@ import com.yunbao.common.utils.AppManager;
import com.yunbao.common.utils.ChatMsgTypeUtils; import com.yunbao.common.utils.ChatMsgTypeUtils;
import com.yunbao.common.utils.DateFormatUtil; import com.yunbao.common.utils.DateFormatUtil;
import com.yunbao.common.utils.StringUtil; import com.yunbao.common.utils.StringUtil;
import com.yunbao.common.utils.UiConversationCodeUtil;
import com.yunbao.common.utils.ViewUtils; import com.yunbao.common.utils.ViewUtils;
import com.yunbao.common.utils.WordUtil; import com.yunbao.common.utils.WordUtil;
import com.yunbao.common.views.weight.ViewClicksAntiShake; import com.yunbao.common.views.weight.ViewClicksAntiShake;
@ -34,6 +36,7 @@ import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import io.rong.imkit.IMCenter; import io.rong.imkit.IMCenter;
import io.rong.imkit.RongIM; import io.rong.imkit.RongIM;
@ -68,7 +71,6 @@ public class MainConversationListAdapter extends ConversationListAdapter {
public static final int FILTER_UNREAD = 2; public static final int FILTER_UNREAD = 2;
public static final int FILTER_READ = 4; public static final int FILTER_READ = 4;
public static final int FILTER_ALL = 7; public static final int FILTER_ALL = 7;
private int mFilter = FILTER_ALL; private int mFilter = FILTER_ALL;
public int getFilter() { public int getFilter() {
@ -79,62 +81,9 @@ public class MainConversationListAdapter extends ConversationListAdapter {
if (this.mFilter == mFilter) if (this.mFilter == mFilter)
return; return;
this.mFilter = mFilter; this.mFilter = mFilter;
super.setDataCollection(FilterData(mFilter, allDataList)); super.setDataCollection(UiConversationCodeUtil.FilterData(mFilter, allDataList));
} }
private List<BaseUiConversation> FilterData(int filterType, List<BaseUiConversation> data){
List<BaseUiConversation> showData= new ArrayList<>();;
Log.i("MainConversationListAdapter", "FilterData: 源" + data.size());
switch (filterType) {
case FILTER_ALL:
showData = data;
break;
case FILTER_ONLINE :
for (BaseUiConversation conversation : data) {
if ("0".equals(conversation.mCore.getDraft())) {
showData.add(conversation);
}
}
break;
case FILTER_OFFLINE:
for (BaseUiConversation conversation : data) {
if (!"0".equals(conversation.mCore.getDraft())) {
showData.add(conversation);
}
}
break;
case FILTER_READ:
for (BaseUiConversation conversation : data) {
boolean isRead;
if (conversation.mCore.getMessage().getMessageDirection() == Message.MessageDirection.SEND)
{
isRead = (conversation.mCore.getSentStatus() == Message.SentStatus.READ);
}else {
isRead = conversation.mCore.getReceivedStatus().isRead();
}
if (isRead) {
showData.add(conversation);
}
}
break;
case FILTER_UNREAD:
for (BaseUiConversation conversation : data) {
boolean isRead;
if (conversation.mCore.getMessage().getMessageDirection() == Message.MessageDirection.SEND)
{
isRead = (conversation.mCore.getSentStatus() == Message.SentStatus.READ);
}else {
isRead = conversation.mCore.getReceivedStatus().isRead();
}
if (!isRead) {
showData.add(conversation);
}
}
break;
}
Log.i("MainConversationListAdapter", "FilterData: 结果" + showData.size());
return showData;
}
@ -144,7 +93,7 @@ public class MainConversationListAdapter extends ConversationListAdapter {
public void setSrcList(List<BaseUiConversation> srcList) { public void setSrcList(List<BaseUiConversation> srcList) {
this.srcList = srcList; this.srcList = srcList;
super.setDataCollection(FilterData(mFilter, allDataList)); super.setDataCollection(UiConversationCodeUtil.FilterData(mFilter, allDataList));
// notifyDataSetChanged(); // notifyDataSetChanged();
} }
@ -239,13 +188,17 @@ public class MainConversationListAdapter extends ConversationListAdapter {
holder.setVisible(R.id.rc_conversation_unread_count, false); holder.setVisible(R.id.rc_conversation_unread_count, false);
} }
boolean isRead; boolean isRead = false;
if (conversation.mCore.getMessage().getMessageDirection() == Message.MessageDirection.SEND) Message message = conversation.mCore.getMessage();
{ if (message != null) {
Message.MessageDirection direction = message.getMessageDirection();
if (direction != null && direction == Message.MessageDirection.SEND) {
isRead = (conversation.mCore.getSentStatus() == Message.SentStatus.READ); isRead = (conversation.mCore.getSentStatus() == Message.SentStatus.READ);
} else { } else {
isRead = conversation.mCore.getReceivedStatus().isRead(); isRead = conversation.mCore.getReceivedStatus().isRead();
} }
}
if (isRead) { if (isRead) {
holder.setTextColor(R.id.rc_conversation_unread_tv, ContextCompat.getColor(holder.itemView.getContext(), R.color.rc_secondary_color)); holder.setTextColor(R.id.rc_conversation_unread_tv, ContextCompat.getColor(holder.itemView.getContext(), R.color.rc_secondary_color));
holder.setText(R.id.rc_conversation_unread_tv, WordUtil.getNewString(R.string.message_chat_msg_read_tip)); holder.setText(R.id.rc_conversation_unread_tv, WordUtil.getNewString(R.string.message_chat_msg_read_tip));
@ -255,7 +208,6 @@ public class MainConversationListAdapter extends ConversationListAdapter {
} }
if (holder.getView(R.id.rc_conversation_live_status) == null || IMLoginManager.get(holder.getContext()).getAnchorB() != 1) { if (holder.getView(R.id.rc_conversation_live_status) == null || IMLoginManager.get(holder.getContext()).getAnchorB() != 1) {
return; return;
} }
@ -272,8 +224,6 @@ public class MainConversationListAdapter extends ConversationListAdapter {
} else { } else {
holder.getView(R.id.rc_conversation_live_status).setVisibility(View.GONE); holder.getView(R.id.rc_conversation_live_status).setVisibility(View.GONE);
} }
} }
@ -383,9 +333,14 @@ public class MainConversationListAdapter extends ConversationListAdapter {
tmp.add(item); tmp.add(item);
} }
allDataList = tmp; allDataList = tmp;
List<BaseUiConversation> showData= FilterData(mFilter,tmp); if(mFilter!=FILTER_OFFLINE){
List<BaseUiConversation> showData = UiConversationCodeUtil.FilterData(mFilter, tmp);
if(!UiConversationCodeUtil.checkIsSame(showData,mDataList)){
super.setDataCollection(showData); super.setDataCollection(showData);
} }
}
}
private List<String> tmpUids = new ArrayList<>(); private List<String> tmpUids = new ArrayList<>();

View File

@ -14,6 +14,7 @@ import com.yunbao.common.R;
import com.yunbao.common.adapter.MainConversationListAdapter; import com.yunbao.common.adapter.MainConversationListAdapter;
import com.yunbao.common.utils.StringUtil; import com.yunbao.common.utils.StringUtil;
import com.yunbao.common.utils.ToastUtil; import com.yunbao.common.utils.ToastUtil;
import com.yunbao.common.utils.UiConversationCodeUtil;
import com.yunbao.common.utils.WordUtil; import com.yunbao.common.utils.WordUtil;
import java.util.ArrayList; import java.util.ArrayList;
@ -46,10 +47,13 @@ public class MainMessageChatFragment extends ConversationListFragment {
mConversationListViewModel.getConversationListLiveData().observe(this.getViewLifecycleOwner(), new Observer<List<BaseUiConversation>>() { mConversationListViewModel.getConversationListLiveData().observe(this.getViewLifecycleOwner(), new Observer<List<BaseUiConversation>>() {
@Override @Override
public void onChanged(List<BaseUiConversation> uiConversations) { public void onChanged(List<BaseUiConversation> uiConversations) {
if (listHashCode != uiConversations.hashCode()) { int hashCode = UiConversationCodeUtil.getListHasCode(uiConversations);
listHashCode = uiConversations.hashCode(); if (listHashCode != hashCode) {
Log.i("nwq", "刷新数据");
listHashCode = hashCode;
((MainConversationListAdapter) mAdapter).onFinish(); ((MainConversationListAdapter) mAdapter).onFinish();
} }
} }
}); });
mConversationListViewModel.getRefreshEventLiveData().observe(this.getViewLifecycleOwner(), new Observer<Event.RefreshEvent>() { mConversationListViewModel.getRefreshEventLiveData().observe(this.getViewLifecycleOwner(), new Observer<Event.RefreshEvent>() {
@ -58,7 +62,6 @@ public class MainMessageChatFragment extends ConversationListFragment {
listHashCode = 0; listHashCode = 0;
} }
}); });
} }
public void setEmptyView(int viewLayoutMsgId) { public void setEmptyView(int viewLayoutMsgId) {

View File

@ -0,0 +1,130 @@
package com.yunbao.common.utils;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;
import io.rong.imkit.conversationlist.model.BaseUiConversation;
import io.rong.imlib.model.Conversation;
import io.rong.imlib.model.Message;
public class UiConversationCodeUtil {
public static final int FILTER_OFFLINE = 0;
public static final int FILTER_ONLINE = 1;
public static final int FILTER_UNREAD = 2;
public static final int FILTER_READ = 4;
public static final int FILTER_ALL = 7;
public static boolean checkIsSame(List<BaseUiConversation> list1, List<BaseUiConversation> list2) {
if (list1.size() != list2.size()) return false;
int hashCode1 = getListHasCode(list1);
int hashCode2 = getListHasCode(list2);
return hashCode1 == hashCode2;
}
public static boolean checkIsSame(int hashCode1, List<BaseUiConversation> list2) {
int hashCode2 = getListHasCode(list2);
return hashCode1 == hashCode2;
}
public static int getListHasCode(List<BaseUiConversation> list) {
int hashCodeValue = 1;
for (BaseUiConversation conversation : list) {
if (conversation != null) {
hashCodeValue = 31 * hashCodeValue + getBaseUiConversationHashCode(conversation);
}
}
return hashCodeValue;
}
//根据BaseUiConversation对象的属性计算hashCode
private static int getBaseUiConversationHashCode(BaseUiConversation item) {
final int prime = 31;
int result = 1;
Conversation mCore = item.mCore;
result = prime * result + ((mCore.getPortraitUrl() == null) ? 0 : mCore.getPortraitUrl().hashCode());
result = prime * result + ((mCore.getConversationTitle() == null) ? 0 : mCore.getConversationTitle().hashCode());
result = prime * result + ((item.mConversationContent == null) ? 0 : item.mConversationContent.hashCode());
result = prime * result + (int) (mCore.getSentTime() ^ (mCore.getSentTime() >>> 32));
result = prime * result + ((mCore.getDraft() == null) ? 0 : mCore.getDraft().hashCode());
result = prime * result + mCore.getUnreadMessageCount();
Message message = mCore.getMessage();
if (message != null) {
Message.MessageDirection direction = message.getMessageDirection();
if (direction != null) {
result = prime * result + direction.hashCode();
}
}
result = prime * result + ((mCore.getSentStatus() == null) ? 0 : mCore.getSentStatus().hashCode());
result = prime * result + ((mCore.getReceivedStatus() == null) ? 0 : mCore.getReceivedStatus().hashCode());
result = prime * result + ((mCore.getTargetId() == null) ? 0 : mCore.getTargetId().hashCode());
return result;
}
public static List<BaseUiConversation> FilterData(int filterType, List<BaseUiConversation> data) {
List<BaseUiConversation> showData = new ArrayList<>();
;
Log.i("MainConversationListAdapter", "FilterData: 源" + data.size());
switch (filterType) {
case FILTER_ALL:
showData = data;
break;
case FILTER_ONLINE:
for (BaseUiConversation conversation : data) {
if ("0".equals(conversation.mCore.getDraft())) {
showData.add(conversation);
}
}
break;
case FILTER_OFFLINE:
for (BaseUiConversation conversation : data) {
if (!"0".equals(conversation.mCore.getDraft())) {
showData.add(conversation);
}
}
break;
case FILTER_READ:
for (BaseUiConversation conversation : data) {
boolean isRead;
if (conversation.mCore.getMessage() != null && conversation.mCore.getMessage().getMessageDirection() != null) {
if (conversation.mCore.getMessage().getMessageDirection() == Message.MessageDirection.SEND) {
isRead = (conversation.mCore.getSentStatus() == Message.SentStatus.READ);
} else {
isRead = conversation.mCore.getReceivedStatus().isRead();
}
if (isRead) {
showData.add(conversation);
}
}
}
break;
case FILTER_UNREAD:
for (BaseUiConversation conversation : data) {
boolean isRead;
if (conversation.mCore.getMessage() != null && conversation.mCore.getMessage().getMessageDirection() != null) {
if (conversation.mCore.getMessage().getMessageDirection() == Message.MessageDirection.SEND) {
isRead = (conversation.mCore.getSentStatus() == Message.SentStatus.READ);
} else {
isRead = conversation.mCore.getReceivedStatus().isRead();
}
if (!isRead) {
showData.add(conversation);
}
}
}
break;
}
return showData;
}
}

View File

@ -16,7 +16,7 @@
android:layout_weight="1" android:layout_weight="1"
android:text="@string/all_chats" android:text="@string/all_chats"
android:textColor="#777777" android:textColor="#777777"
android:textSize="14sp" android:textSize="15sp"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" app:layout_constraintTop_toTopOf="parent"
tools:textColor="@color/colorMainTab" /> tools:textColor="@color/colorMainTab" />
@ -44,7 +44,7 @@
android:layout_weight="1" android:layout_weight="1"
android:text="@string/online_only" android:text="@string/online_only"
android:textColor="#777777" android:textColor="#777777"
android:textSize="14sp" android:textSize="15sp"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/chatTypeTv1" /> app:layout_constraintTop_toBottomOf="@+id/chatTypeTv1" />
@ -69,7 +69,7 @@
android:layout_weight="1" android:layout_weight="1"
android:text="@string/offline_only" android:text="@string/offline_only"
android:textColor="#777777" android:textColor="#777777"
android:textSize="14sp" android:textSize="15sp"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/chatTypeTv2" /> app:layout_constraintTop_toBottomOf="@+id/chatTypeTv2" />
@ -94,7 +94,7 @@
android:layout_weight="1" android:layout_weight="1"
android:text="@string/read_only" android:text="@string/read_only"
android:textColor="#777777" android:textColor="#777777"
android:textSize="14sp" android:textSize="15sp"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/chatTypeTv3" /> app:layout_constraintTop_toBottomOf="@+id/chatTypeTv3" />
@ -120,7 +120,7 @@
android:layout_weight="1" android:layout_weight="1"
android:text="@string/unread_only" android:text="@string/unread_only"
android:textColor="#777777" android:textColor="#777777"
android:textSize="14sp" android:textSize="15sp"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/chatTypeTv4" /> app:layout_constraintTop_toBottomOf="@+id/chatTypeTv4" />

View File

@ -24,6 +24,6 @@ android.nonTransitiveRClass=false
systemProp.http.proxyHost=127.0.0.1 systemProp.http.proxyHost=127.0.0.1
systemProp.https.proxyHost=127.0.0.1 systemProp.https.proxyHost=127.0.0.1
systemProp.https.proxyPort=7890 systemProp.https.proxyPort=9098
systemProp.http.proxyPort=7890 systemProp.http.proxyPort=9098
#android.enableR8.fullMode=true #android.enableR8.fullMode=true

View File

@ -250,7 +250,7 @@ public class MainMessageViewHolder extends AbsMainViewHolder {
} }
MainMessageChatListFragment chatFragment = (MainMessageChatListFragment) fragment; MainMessageChatListFragment chatFragment = (MainMessageChatListFragment) fragment;
ImageView expendImg = (ImageView) view; ImageView expendImg = (ImageView) view;
expendImg.setImageResource(R.mipmap.ic_expend_down); expendImg.setImageResource(R.mipmap.ic_expend_up);
XPopup.Builder builder = new XPopup.Builder(mContext).atView(relativeLayouts.get(position)); XPopup.Builder builder = new XPopup.Builder(mContext).atView(relativeLayouts.get(position));
builder.offsetY(16); builder.offsetY(16);
builder.dismissOnTouchOutside(true); builder.dismissOnTouchOutside(true);
@ -271,7 +271,7 @@ public class MainMessageViewHolder extends AbsMainViewHolder {
@Override @Override
public void onDismiss(BasePopupView popupView) { public void onDismiss(BasePopupView popupView) {
expendImg.setImageResource(R.mipmap.ic_expend_up); expendImg.setImageResource(R.mipmap.ic_expend_down);
} }
@Override @Override

View File

@ -383,7 +383,7 @@
android:padding="5dp" android:padding="5dp"
android:visibility="gone" android:visibility="gone"
tools:visibility="visible" tools:visibility="visible"
android:src="@mipmap/ic_expend_up" android:src="@mipmap/ic_expend_down"
app:layout_constraintTop_toTopOf="parent" app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toEndOf="@+id/tab_top1" app:layout_constraintStart_toEndOf="@+id/tab_top1"
app:layout_constraintBottom_toBottomOf="parent"/> app:layout_constraintBottom_toBottomOf="parent"/>
@ -448,7 +448,7 @@
android:padding="5dp" android:padding="5dp"
android:visibility="gone" android:visibility="gone"
tools:visibility="visible" tools:visibility="visible"
android:src="@mipmap/ic_expend_up" android:src="@mipmap/ic_expend_down"
app:layout_constraintTop_toTopOf="parent" app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toEndOf="@+id/tab_top2" app:layout_constraintStart_toEndOf="@+id/tab_top2"
app:layout_constraintBottom_toBottomOf="parent"/> app:layout_constraintBottom_toBottomOf="parent"/>
@ -514,7 +514,7 @@
android:visibility="gone" android:visibility="gone"
tools:visibility="visible" tools:visibility="visible"
android:layout_centerHorizontal="true" android:layout_centerHorizontal="true"
android:src="@mipmap/ic_expend_up" android:src="@mipmap/ic_expend_down"
app:layout_constraintTop_toTopOf="parent" app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toEndOf="@+id/tab_top3" app:layout_constraintStart_toEndOf="@+id/tab_top3"
app:layout_constraintBottom_toBottomOf="parent"/> app:layout_constraintBottom_toBottomOf="parent"/>