Fixed scroll state behavior for commit changes scroll

- Scrolls position (both for files and message) was not being reset when changing between commits.

- Scrolls position was not preserved when swapping between tabs.
This commit is contained in:
Abdelilah El Aissaoui 2023-05-12 10:13:32 +02:00
parent 86316fca9f
commit c75d787945
No known key found for this signature in database
GPG Key ID: 7587FC860F594869
2 changed files with 33 additions and 9 deletions

View File

@ -2,6 +2,7 @@ package com.jetpackduba.gitnuro.ui
import androidx.compose.foundation.* import androidx.compose.foundation.*
import androidx.compose.foundation.layout.* import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyListState
import androidx.compose.foundation.lazy.items import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.selection.SelectionContainer import androidx.compose.foundation.text.selection.SelectionContainer
@ -50,7 +51,8 @@ fun CommitChanges(
val commitChangesStatus = commitChangesViewModel.commitChangesState.collectAsState().value val commitChangesStatus = commitChangesViewModel.commitChangesState.collectAsState().value
val showSearch by commitChangesViewModel.showSearch.collectAsState() val showSearch by commitChangesViewModel.showSearch.collectAsState()
val changesListScroll by commitChangesViewModel.changesLazyListState.collectAsState()
val textScroll by commitChangesViewModel.textScroll.collectAsState()
var searchFilter by remember(commitChangesViewModel, showSearch, commitChangesStatus) { var searchFilter by remember(commitChangesViewModel, showSearch, commitChangesStatus) {
mutableStateOf(commitChangesViewModel.searchFilter.value) mutableStateOf(commitChangesViewModel.searchFilter.value)
@ -66,11 +68,14 @@ fun CommitChanges(
diffSelected = diffSelected, diffSelected = diffSelected,
commit = commitChangesStatus.commit, commit = commitChangesStatus.commit,
changes = commitChangesStatus.changesFiltered, changes = commitChangesStatus.changesFiltered,
onDiffSelected = onDiffSelected,
onBlame = onBlame, onBlame = onBlame,
onHistory = onHistory, onHistory = onHistory,
showSearch = showSearch, showSearch = showSearch,
changesListScroll = changesListScroll,
textScroll = textScroll,
searchFilter = searchFilter, searchFilter = searchFilter,
onDiffSelected = onDiffSelected,
onSearchFilterToggled = { visible -> onSearchFilterToggled = { visible ->
commitChangesViewModel.onSearchFilterToggled(visible) commitChangesViewModel.onSearchFilterToggled(visible)
}, },
@ -87,12 +92,14 @@ fun CommitChanges(
fun CommitChangesView( fun CommitChangesView(
commit: RevCommit, commit: RevCommit,
changes: List<DiffEntry>, changes: List<DiffEntry>,
onDiffSelected: (DiffEntry) -> Unit,
diffSelected: DiffEntryType?, diffSelected: DiffEntryType?,
onBlame: (String) -> Unit, changesListScroll: LazyListState,
onHistory: (String) -> Unit, textScroll: ScrollState,
showSearch: Boolean, showSearch: Boolean,
searchFilter: TextFieldValue, searchFilter: TextFieldValue,
onBlame: (String) -> Unit,
onHistory: (String) -> Unit,
onDiffSelected: (DiffEntry) -> Unit,
onSearchFilterToggled: (Boolean) -> Unit, onSearchFilterToggled: (Boolean) -> Unit,
onSearchFilterChanged: (TextFieldValue) -> Unit, onSearchFilterChanged: (TextFieldValue) -> Unit,
) { ) {
@ -107,7 +114,6 @@ fun CommitChangesView(
.padding(end = 8.dp, bottom = 8.dp) .padding(end = 8.dp, bottom = 8.dp)
.fillMaxSize(), .fillMaxSize(),
) { ) {
val scroll = rememberScrollState(0)
val searchFocusRequester = remember { FocusRequester() } val searchFocusRequester = remember { FocusRequester() }
Column( Column(
@ -173,6 +179,7 @@ fun CommitChangesView(
CommitLogChanges( CommitLogChanges(
diffSelected = diffSelected, diffSelected = diffSelected,
changesListScroll = changesListScroll,
diffEntries = changes, diffEntries = changes,
onDiffSelected = onDiffSelected, onDiffSelected = onDiffSelected,
onBlame = onBlame, onBlame = onBlame,
@ -195,7 +202,7 @@ fun CommitChangesView(
.fillMaxWidth() .fillMaxWidth()
.height(120.dp) .height(120.dp)
.padding(8.dp) .padding(8.dp)
.verticalScroll(scroll), .verticalScroll(textScroll),
) )
} }
@ -293,14 +300,16 @@ fun Author(
@Composable @Composable
fun CommitLogChanges( fun CommitLogChanges(
diffEntries: List<DiffEntry>, diffEntries: List<DiffEntry>,
onDiffSelected: (DiffEntry) -> Unit,
diffSelected: DiffEntryType?, diffSelected: DiffEntryType?,
changesListScroll: LazyListState,
onBlame: (String) -> Unit, onBlame: (String) -> Unit,
onHistory: (String) -> Unit, onHistory: (String) -> Unit,
onDiffSelected: (DiffEntry) -> Unit,
) { ) {
ScrollableLazyColumn( ScrollableLazyColumn(
modifier = Modifier modifier = Modifier
.fillMaxSize() .fillMaxSize(),
state = changesListScroll,
) { ) {
items(items = diffEntries) { diffEntry -> items(items = diffEntries) { diffEntry ->
ContextMenu( ContextMenu(

View File

@ -1,5 +1,7 @@
package com.jetpackduba.gitnuro.viewmodels package com.jetpackduba.gitnuro.viewmodels
import androidx.compose.foundation.ScrollState
import androidx.compose.foundation.lazy.LazyListState
import androidx.compose.ui.text.input.TextFieldValue import androidx.compose.ui.text.input.TextFieldValue
import com.jetpackduba.gitnuro.extensions.delayedStateChange import com.jetpackduba.gitnuro.extensions.delayedStateChange
import com.jetpackduba.gitnuro.extensions.filePath import com.jetpackduba.gitnuro.extensions.filePath
@ -26,6 +28,14 @@ class CommitChangesViewModel @Inject constructor(
private val _searchFilter = MutableStateFlow(TextFieldValue("")) private val _searchFilter = MutableStateFlow(TextFieldValue(""))
val searchFilter: StateFlow<TextFieldValue> = _searchFilter val searchFilter: StateFlow<TextFieldValue> = _searchFilter
val changesLazyListState = MutableStateFlow(
LazyListState(firstVisibleItemIndex = 0, firstVisibleItemScrollOffset = 0)
)
val textScroll = MutableStateFlow(
ScrollState(0)
)
private val _commitChangesState = MutableStateFlow<CommitChangesState>(CommitChangesState.Loading) private val _commitChangesState = MutableStateFlow<CommitChangesState>(CommitChangesState.Loading)
val commitChangesState: StateFlow<CommitChangesState> = val commitChangesState: StateFlow<CommitChangesState> =
combine(_commitChangesState, _showSearch, _searchFilter) { state, showSearch, filter -> combine(_commitChangesState, _showSearch, _searchFilter) { state, showSearch, filter ->
@ -66,6 +76,11 @@ class CommitChangesViewModel @Inject constructor(
_showSearch.value = false _showSearch.value = false
_searchFilter.value = TextFieldValue("") _searchFilter.value = TextFieldValue("")
changesLazyListState.value = LazyListState(
0,
0
)
textScroll.value = ScrollState(0)
} }
} }