From 632b76081a313ba01d30e2de64295b9ba1aff113 Mon Sep 17 00:00:00 2001 From: Alex Hart Date: Thu, 8 May 2025 12:25:07 -0300 Subject: [PATCH] Fix tab scroll-to-top on click. --- .../ConversationListFragment.java | 1 + .../securesms/main/MainNavigationViewModel.kt | 34 +++++++++---------- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversationlist/ConversationListFragment.java b/app/src/main/java/org/thoughtcrime/securesms/conversationlist/ConversationListFragment.java index 422260ce4d..e8b546ed05 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversationlist/ConversationListFragment.java +++ b/app/src/main/java/org/thoughtcrime/securesms/conversationlist/ConversationListFragment.java @@ -407,6 +407,7 @@ public class ConversationListFragment extends MainFragment implements ActionMode lifecycleDisposable.bindTo(getViewLifecycleOwner()); lifecycleDisposable.add(mainNavigationViewModel.getTabClickEvents().filter(tab -> tab == MainNavigationListLocation.CHATS) .subscribe(unused -> { + Log.d(TAG, "Scroll to top please"); LinearLayoutManager layoutManager = (LinearLayoutManager) list.getLayoutManager(); int firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition(); if (firstVisibleItemPosition <= LIST_SMOOTH_SCROLL_TO_TOP_THRESHOLD) { diff --git a/app/src/main/java/org/thoughtcrime/securesms/main/MainNavigationViewModel.kt b/app/src/main/java/org/thoughtcrime/securesms/main/MainNavigationViewModel.kt index ec52f7c4b8..6a967363e3 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/main/MainNavigationViewModel.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/main/MainNavigationViewModel.kt @@ -19,7 +19,6 @@ import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.collectLatest -import kotlinx.coroutines.flow.filter import kotlinx.coroutines.flow.update import kotlinx.coroutines.launch import kotlinx.coroutines.reactive.asFlow @@ -66,7 +65,7 @@ class MainNavigationViewModel( * This is Rx because these are still accessed from Java. */ private val internalTabClickEvents: MutableSharedFlow = MutableSharedFlow() - val tabClickEvents: Observable = internalTabClickEvents.filter { Stories.isFeatureEnabled() }.asObservable() + val tabClickEvents: Observable = internalTabClickEvents.asObservable() init { performStoreUpdate(MainNavigationRepository.getNumberOfUnreadMessages()) { unreadChats, state -> @@ -177,30 +176,31 @@ class MainNavigationViewModel( } fun onChatsSelected() { - internalTabClickEvents.tryEmit(MainNavigationListLocation.CHATS) - internalMainNavigationState.update { - it.copy(selectedDestination = MainNavigationListLocation.CHATS) - } + onTabSelected(MainNavigationListLocation.CHATS) } fun onArchiveSelected() { - internalTabClickEvents.tryEmit(MainNavigationListLocation.ARCHIVE) - internalMainNavigationState.update { - it.copy(selectedDestination = MainNavigationListLocation.ARCHIVE) - } + onTabSelected(MainNavigationListLocation.ARCHIVE) } fun onCallsSelected() { - internalTabClickEvents.tryEmit(MainNavigationListLocation.CALLS) - internalMainNavigationState.update { - it.copy(selectedDestination = MainNavigationListLocation.CALLS) - } + onTabSelected(MainNavigationListLocation.CALLS) } fun onStoriesSelected() { - internalTabClickEvents.tryEmit(MainNavigationListLocation.STORIES) - internalMainNavigationState.update { - it.copy(selectedDestination = MainNavigationListLocation.STORIES) + onTabSelected(MainNavigationListLocation.STORIES) + } + + private fun onTabSelected(destination: MainNavigationListLocation) { + viewModelScope.launch { + val currentTab = internalMainNavigationState.value.selectedDestination + if (currentTab == destination) { + internalTabClickEvents.emit(destination) + } else { + internalMainNavigationState.update { + it.copy(selectedDestination = destination) + } + } } }