From 2975059bd21a574d49a53545fb6e4e713f4d2e12 Mon Sep 17 00:00:00 2001 From: Abdelilah El Aissaoui Date: Thu, 23 Jun 2022 10:35:47 +0200 Subject: [PATCH] Graph horizontal scroll state is preserved when changing between tabs --- src/main/kotlin/app/ui/log/Log.kt | 24 ++++++++++--------- .../kotlin/app/viewmodels/LogViewModel.kt | 9 +++---- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/src/main/kotlin/app/ui/log/Log.kt b/src/main/kotlin/app/ui/log/Log.kt index 670fe65..62154c8 100644 --- a/src/main/kotlin/app/ui/log/Log.kt +++ b/src/main/kotlin/app/ui/log/Log.kt @@ -106,7 +106,8 @@ fun Log( if (logStatus is LogStatus.Loaded) { val hasUncommitedChanges = logStatus.hasUncommitedChanges val commitList = logStatus.plotCommitList - val verticalScrollState by logViewModel.lazyListState.collectAsState() + val verticalScrollState by logViewModel.verticalListState.collectAsState() + val horizontalScrollState by logViewModel.horizontalListState.collectAsState() val searchFilter = logViewModel.logSearchFilterResults.collectAsState() val searchFilterValue = searchFilter.value // With this method, whenever the scroll changes, the log is recomposed and the graph list is updated with @@ -152,15 +153,14 @@ fun Log( onShowSearch = { scope.launch { logViewModel.onSearchValueChanged("") } } ) - val horizontalScrollState = rememberScrollState(0) Box { GraphList( commitList = commitList, selectedCommit = selectedCommit, selectedItem = selectedItem, - stateHorizontal = horizontalScrollState, + horizontalScrollState = horizontalScrollState, graphWidth = graphWidth, - scrollState = verticalScrollState, + verticalScrollState = verticalScrollState, hasUncommitedChanges = hasUncommitedChanges, commitsLimit = logStatus.commitsLimit, ) @@ -196,11 +196,14 @@ fun Log( // Scrollbar used to scroll horizontally the graph nodes // Added after every component to have the highest priority when clicking HorizontalScrollbar( - modifier = Modifier.align(Alignment.BottomStart).width(graphWidth) + modifier = Modifier + .align(Alignment.BottomStart) + .width(graphWidth) .padding(start = 4.dp, bottom = 4.dp), style = LocalScrollbarStyle.current.copy( unhoverColor = MaterialTheme.colors.scrollbarNormal, hoverColor = MaterialTheme.colors.scrollbarHover, - ), adapter = rememberScrollbarAdapter(horizontalScrollState) + ), + adapter = rememberScrollbarAdapter(horizontalScrollState) ) if (verticalScrollState.firstVisibleItemIndex > 0) { @@ -430,9 +433,9 @@ fun MessagesList( @Composable fun GraphList( commitList: GraphCommitList, - stateHorizontal: ScrollState = rememberScrollState(0), + horizontalScrollState: ScrollState, + verticalScrollState: LazyListState, graphWidth: Dp, - scrollState: LazyListState, hasUncommitedChanges: Boolean, selectedCommit: RevCommit?, selectedItem: SelectedItem, @@ -450,7 +453,6 @@ fun GraphList( graphRealWidth = graphWidth } - Box( Modifier .width(graphWidth) @@ -460,11 +462,11 @@ fun GraphList( Box( modifier = Modifier .fillMaxSize() - .horizontalScroll(stateHorizontal) + .horizontalScroll(horizontalScrollState) .padding(bottom = 8.dp) ) { LazyColumn( - state = scrollState, modifier = Modifier.width(graphRealWidth) + state = verticalScrollState, modifier = Modifier.width(graphRealWidth) ) { if (hasUncommitedChanges) { item { diff --git a/src/main/kotlin/app/viewmodels/LogViewModel.kt b/src/main/kotlin/app/viewmodels/LogViewModel.kt index 50dc471..4ec977f 100644 --- a/src/main/kotlin/app/viewmodels/LogViewModel.kt +++ b/src/main/kotlin/app/viewmodels/LogViewModel.kt @@ -1,5 +1,6 @@ package app.viewmodels +import androidx.compose.foundation.ScrollState import androidx.compose.foundation.lazy.LazyListState import app.preferences.AppPreferences import app.extensions.delayedStateChange @@ -54,12 +55,8 @@ class LogViewModel @Inject constructor( private val _logDialog = MutableStateFlow(LogDialog.None) val logDialog: StateFlow = _logDialog - val lazyListState = MutableStateFlow( - LazyListState( - 0, - 0 - ) - ) + val verticalListState = MutableStateFlow(LazyListState(0, 0)) + val horizontalListState = MutableStateFlow(ScrollState(0)) private val scope = CoroutineScope(Dispatchers.IO)