Skip to content

Commit

Permalink
会话列表页面,支持双击底部 item 滑动到下一个未读会话 fix #IBCGUU
Browse files Browse the repository at this point in the history
  • Loading branch information
imndx committed Dec 20, 2024
1 parent d571c48 commit ada5a96
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
16 changes: 14 additions & 2 deletions chat/src/main/java/cn/wildfire/chat/app/main/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,11 @@ public class MainActivity extends WfcBaseActivity {
private boolean isInitialized = false;

private ContactListFragment contactListFragment;
private ConversationListFragment conversationListFragment;

private ContactViewModel contactViewModel;
private ConversationListViewModel conversationListViewModel;
private long lastSelectConversatonListItemTimestamp = 0;

protected void bindViews() {
super.bindViews();
Expand Down Expand Up @@ -331,7 +333,7 @@ private void initView() {
//设置ViewPager的最大缓存页面
contentViewPager.setOffscreenPageLimit(4);

ConversationListFragment conversationListFragment = new ConversationListFragment();
conversationListFragment = new ConversationListFragment();
contactListFragment = new ContactListFragment();
DiscoveryFragment discoveryFragment = new DiscoveryFragment();
MeFragment meFragment = new MeFragment();
Expand All @@ -347,7 +349,17 @@ private void initView() {
contentViewPager.registerOnPageChangeCallback(this.onPageChangeCallback);

bottomNavigationView.setOnItemReselectedListener(item -> {
// do nothing
switch (item.getItemId()) {
case R.id.conversation_list:
long now = System.currentTimeMillis();
if(now - lastSelectConversatonListItemTimestamp < 200){
conversationListFragment.scrollToNextUnreadConversation();
}
lastSelectConversatonListItemTimestamp = now;
break;
default:
break;
}
});
bottomNavigationView.setOnItemSelectedListener(item -> {
switch (item.getItemId()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@ public void setConversationInfos(List<ConversationInfo> conversationInfos) {
submit(this.statusNotifications, conversationInfos);
}

public int getNextUnreadConversationPosition(int position) {
for (int i = position + 1; i < getItemCount(); i++) {
ConversationInfo conversationInfo = conversationInfos.get(i - headerCount());
if (!conversationInfo.isSilent && (conversationInfo.unreadCount.unread + conversationInfo.unreadCount.unreadMention + conversationInfo.unreadCount.unreadMentionAll) > 0) {
return i;
}
}
return -1;
}

public void setOnClickConversationItemListener(OnClickConversationItemListener onClickConversationItemListener) {
this.onClickConversationItemListener = onClickConversationItemListener;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import androidx.lifecycle.ViewModelProvider;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.LinearSmoothScroller;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.SimpleItemAnimator;

Expand Down Expand Up @@ -49,6 +50,7 @@ public class ConversationListFragment extends ProgressFragment {
private StatusNotificationViewModel statusNotificationViewModel;
private LinearLayoutManager layoutManager;
private OnClickConversationItemListener onClickConversationItemListener;
private RecyclerView.SmoothScroller smoothScroller;

@Override
protected int contentLayout() {
Expand Down Expand Up @@ -90,6 +92,13 @@ private void init() {
recyclerView.setAdapter(adapter);
((SimpleItemAnimator) recyclerView.getItemAnimator()).setSupportsChangeAnimations(false);

smoothScroller = new LinearSmoothScroller(getContext()) {
@Override
protected int getVerticalSnapPreference() {
return LinearSmoothScroller.SNAP_TO_START;
}
};

UserViewModel userViewModel = ViewModelProviders.of(this).get(UserViewModel.class);
userViewModel.userInfoLiveData().observe(this, new Observer<List<UserInfo>>() {
@Override
Expand Down Expand Up @@ -164,6 +173,20 @@ public void onChanged(Object o) {
reloadConversations();
}

public void scrollToNextUnreadConversation() {
int start = layoutManager.findFirstVisibleItemPosition();
int end = layoutManager.findLastVisibleItemPosition();
int nextUnreadConversationPosition = adapter.getNextUnreadConversationPosition(start);
if (nextUnreadConversationPosition > 0) {
if (nextUnreadConversationPosition >= start && nextUnreadConversationPosition <= end) {
smoothScroller.setTargetPosition(nextUnreadConversationPosition);
layoutManager.startSmoothScroll(smoothScroller);
} else {
recyclerView.smoothScrollToPosition(nextUnreadConversationPosition);
}
}
}

private void loadAndShowPCOnlineNotification() {
List<PCOnlineInfo> pcOnlineInfos = ChatManager.Instance().getPCOnlineInfos();
statusNotificationViewModel.clearStatusNotificationByType(PCOnlineStatusNotification.class);
Expand Down

0 comments on commit ada5a96

Please sign in to comment.