Graph horizontal scroll state is preserved when changing between tabs

This commit is contained in:
Abdelilah El Aissaoui 2022-06-23 10:35:47 +02:00
parent 02e37583f9
commit 2975059bd2
2 changed files with 16 additions and 17 deletions

View File

@ -106,7 +106,8 @@ fun Log(
if (logStatus is LogStatus.Loaded) { if (logStatus is LogStatus.Loaded) {
val hasUncommitedChanges = logStatus.hasUncommitedChanges val hasUncommitedChanges = logStatus.hasUncommitedChanges
val commitList = logStatus.plotCommitList 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 searchFilter = logViewModel.logSearchFilterResults.collectAsState()
val searchFilterValue = searchFilter.value val searchFilterValue = searchFilter.value
// With this method, whenever the scroll changes, the log is recomposed and the graph list is updated with // 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("") } } onShowSearch = { scope.launch { logViewModel.onSearchValueChanged("") } }
) )
val horizontalScrollState = rememberScrollState(0)
Box { Box {
GraphList( GraphList(
commitList = commitList, commitList = commitList,
selectedCommit = selectedCommit, selectedCommit = selectedCommit,
selectedItem = selectedItem, selectedItem = selectedItem,
stateHorizontal = horizontalScrollState, horizontalScrollState = horizontalScrollState,
graphWidth = graphWidth, graphWidth = graphWidth,
scrollState = verticalScrollState, verticalScrollState = verticalScrollState,
hasUncommitedChanges = hasUncommitedChanges, hasUncommitedChanges = hasUncommitedChanges,
commitsLimit = logStatus.commitsLimit, commitsLimit = logStatus.commitsLimit,
) )
@ -196,11 +196,14 @@ fun Log(
// Scrollbar used to scroll horizontally the graph nodes // Scrollbar used to scroll horizontally the graph nodes
// Added after every component to have the highest priority when clicking // Added after every component to have the highest priority when clicking
HorizontalScrollbar( 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( .padding(start = 4.dp, bottom = 4.dp), style = LocalScrollbarStyle.current.copy(
unhoverColor = MaterialTheme.colors.scrollbarNormal, unhoverColor = MaterialTheme.colors.scrollbarNormal,
hoverColor = MaterialTheme.colors.scrollbarHover, hoverColor = MaterialTheme.colors.scrollbarHover,
), adapter = rememberScrollbarAdapter(horizontalScrollState) ),
adapter = rememberScrollbarAdapter(horizontalScrollState)
) )
if (verticalScrollState.firstVisibleItemIndex > 0) { if (verticalScrollState.firstVisibleItemIndex > 0) {
@ -430,9 +433,9 @@ fun MessagesList(
@Composable @Composable
fun GraphList( fun GraphList(
commitList: GraphCommitList, commitList: GraphCommitList,
stateHorizontal: ScrollState = rememberScrollState(0), horizontalScrollState: ScrollState,
verticalScrollState: LazyListState,
graphWidth: Dp, graphWidth: Dp,
scrollState: LazyListState,
hasUncommitedChanges: Boolean, hasUncommitedChanges: Boolean,
selectedCommit: RevCommit?, selectedCommit: RevCommit?,
selectedItem: SelectedItem, selectedItem: SelectedItem,
@ -450,7 +453,6 @@ fun GraphList(
graphRealWidth = graphWidth graphRealWidth = graphWidth
} }
Box( Box(
Modifier Modifier
.width(graphWidth) .width(graphWidth)
@ -460,11 +462,11 @@ fun GraphList(
Box( Box(
modifier = Modifier modifier = Modifier
.fillMaxSize() .fillMaxSize()
.horizontalScroll(stateHorizontal) .horizontalScroll(horizontalScrollState)
.padding(bottom = 8.dp) .padding(bottom = 8.dp)
) { ) {
LazyColumn( LazyColumn(
state = scrollState, modifier = Modifier.width(graphRealWidth) state = verticalScrollState, modifier = Modifier.width(graphRealWidth)
) { ) {
if (hasUncommitedChanges) { if (hasUncommitedChanges) {
item { item {

View File

@ -1,5 +1,6 @@
package app.viewmodels package app.viewmodels
import androidx.compose.foundation.ScrollState
import androidx.compose.foundation.lazy.LazyListState import androidx.compose.foundation.lazy.LazyListState
import app.preferences.AppPreferences import app.preferences.AppPreferences
import app.extensions.delayedStateChange import app.extensions.delayedStateChange
@ -54,12 +55,8 @@ class LogViewModel @Inject constructor(
private val _logDialog = MutableStateFlow<LogDialog>(LogDialog.None) private val _logDialog = MutableStateFlow<LogDialog>(LogDialog.None)
val logDialog: StateFlow<LogDialog> = _logDialog val logDialog: StateFlow<LogDialog> = _logDialog
val lazyListState = MutableStateFlow( val verticalListState = MutableStateFlow(LazyListState(0, 0))
LazyListState( val horizontalListState = MutableStateFlow(ScrollState(0))
0,
0
)
)
private val scope = CoroutineScope(Dispatchers.IO) private val scope = CoroutineScope(Dispatchers.IO)