Fix tab scroll-to-top on click.

This commit is contained in:
Alex Hart
2025-05-08 12:25:07 -03:00
committed by Michelle Tang
parent a474666ea7
commit 632b76081a
2 changed files with 18 additions and 17 deletions

View File

@@ -407,6 +407,7 @@ public class ConversationListFragment extends MainFragment implements ActionMode
lifecycleDisposable.bindTo(getViewLifecycleOwner()); lifecycleDisposable.bindTo(getViewLifecycleOwner());
lifecycleDisposable.add(mainNavigationViewModel.getTabClickEvents().filter(tab -> tab == MainNavigationListLocation.CHATS) lifecycleDisposable.add(mainNavigationViewModel.getTabClickEvents().filter(tab -> tab == MainNavigationListLocation.CHATS)
.subscribe(unused -> { .subscribe(unused -> {
Log.d(TAG, "Scroll to top please");
LinearLayoutManager layoutManager = (LinearLayoutManager) list.getLayoutManager(); LinearLayoutManager layoutManager = (LinearLayoutManager) list.getLayoutManager();
int firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition(); int firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition();
if (firstVisibleItemPosition <= LIST_SMOOTH_SCROLL_TO_TOP_THRESHOLD) { if (firstVisibleItemPosition <= LIST_SMOOTH_SCROLL_TO_TOP_THRESHOLD) {

View File

@@ -19,7 +19,6 @@ import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.collectLatest import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.update import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.reactive.asFlow import kotlinx.coroutines.reactive.asFlow
@@ -66,7 +65,7 @@ class MainNavigationViewModel(
* This is Rx because these are still accessed from Java. * This is Rx because these are still accessed from Java.
*/ */
private val internalTabClickEvents: MutableSharedFlow<MainNavigationListLocation> = MutableSharedFlow() private val internalTabClickEvents: MutableSharedFlow<MainNavigationListLocation> = MutableSharedFlow()
val tabClickEvents: Observable<MainNavigationListLocation> = internalTabClickEvents.filter { Stories.isFeatureEnabled() }.asObservable() val tabClickEvents: Observable<MainNavigationListLocation> = internalTabClickEvents.asObservable()
init { init {
performStoreUpdate(MainNavigationRepository.getNumberOfUnreadMessages()) { unreadChats, state -> performStoreUpdate(MainNavigationRepository.getNumberOfUnreadMessages()) { unreadChats, state ->
@@ -177,30 +176,31 @@ class MainNavigationViewModel(
} }
fun onChatsSelected() { fun onChatsSelected() {
internalTabClickEvents.tryEmit(MainNavigationListLocation.CHATS) onTabSelected(MainNavigationListLocation.CHATS)
internalMainNavigationState.update {
it.copy(selectedDestination = MainNavigationListLocation.CHATS)
}
} }
fun onArchiveSelected() { fun onArchiveSelected() {
internalTabClickEvents.tryEmit(MainNavigationListLocation.ARCHIVE) onTabSelected(MainNavigationListLocation.ARCHIVE)
internalMainNavigationState.update {
it.copy(selectedDestination = MainNavigationListLocation.ARCHIVE)
}
} }
fun onCallsSelected() { fun onCallsSelected() {
internalTabClickEvents.tryEmit(MainNavigationListLocation.CALLS) onTabSelected(MainNavigationListLocation.CALLS)
internalMainNavigationState.update {
it.copy(selectedDestination = MainNavigationListLocation.CALLS)
}
} }
fun onStoriesSelected() { fun onStoriesSelected() {
internalTabClickEvents.tryEmit(MainNavigationListLocation.STORIES) onTabSelected(MainNavigationListLocation.STORIES)
internalMainNavigationState.update { }
it.copy(selectedDestination = 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)
}
}
} }
} }