-
Notifications
You must be signed in to change notification settings - Fork 3.1k
[FEATURE REQUEST] Maintain scroll position in file list #4570
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
Feature: Maintain scroll position when user scrolls and navigates in file/folder list | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is also a limit of characters length for the title, so I would just write the way you named the PR: |
||
|
||
Saves scroll position when user scrolls and navigates in file/folder list | ||
|
||
https://github.com/owncloud/android/issues/4528 | ||
https://github.com/owncloud/android/pull/4570 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -798,10 +798,15 @@ class MainFileListFragment : Fragment(), | |
collectLatestLifecycleFlow(mainFileListViewModel.fileListUiState) { fileListUiState -> | ||
if (fileListUiState !is MainFileListViewModel.FileListUiState.Success) return@collectLatestLifecycleFlow | ||
|
||
saveScrollPosition() | ||
|
||
fileListAdapter.updateFileList( | ||
filesToAdd = fileListUiState.folderContent, | ||
fileListOption = fileListUiState.fileListOption, | ||
) | ||
|
||
restoreScrollPosition() | ||
|
||
Comment on lines
+801
to
+809
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmmm I don't know if I like this to be here... there are 2 main points that make me doubt here:
|
||
showOrHideEmptyView(fileListUiState) | ||
|
||
binding.spaceHeader.root.apply { | ||
|
@@ -994,6 +999,38 @@ class MainFileListFragment : Fragment(), | |
_binding = null | ||
} | ||
|
||
/** | ||
* Saves the current scroll position of the RecyclerView by capturing the indices of | ||
* the last visible items from the StaggeredGridLayoutManager. | ||
* | ||
* This position is stored in the ViewModel so it can be restored later, | ||
* typically after the adapter data is updated. This ensures continuity | ||
* in the user’s scroll experience when navigating between fragments or refreshing data. | ||
*/ | ||
private fun saveScrollPosition() { | ||
val layoutManager = binding.recyclerViewMainFileList.layoutManager as? StaggeredGridLayoutManager | ||
mainFileListViewModel.lastVisibleItemPositions = layoutManager?.findLastVisibleItemPositions(null) | ||
} | ||
|
||
/** | ||
* Restores the scroll position of the RecyclerView based on the previously | ||
* saved positions in the ViewModel. It scrolls to the maximum of the last | ||
* visible item positions captured earlier using [saveScrollPosition]. | ||
* | ||
* This method is posted to the queue to ensure it runs after the | ||
* RecyclerView has completed its layout pass and the adapter update has been applied. | ||
*/ | ||
private fun restoreScrollPosition() { | ||
binding.recyclerViewMainFileList.post { | ||
mainFileListViewModel.lastVisibleItemPositions?.let { positions -> | ||
val maxPosition = positions.maxOrNull() ?: 0 | ||
binding.recyclerViewMainFileList.scrollToPosition(maxPosition) | ||
mainFileListViewModel.lastVisibleItemPositions = null | ||
} | ||
} | ||
} | ||
|
||
|
||
fun updateFileListOption(newFileListOption: FileListOption, file: OCFile) { | ||
mainFileListViewModel.updateFileListOption(newFileListOption) | ||
binding.swipeRefreshMainFileList.isEnabled = newFileListOption != FileListOption.AV_OFFLINE | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -94,6 +94,7 @@ class MainFileListViewModel( | |
fileListOptionParam: FileListOption, | ||
) : ViewModel() { | ||
|
||
var lastVisibleItemPositions: IntArray? = null | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's usually less secure to expose variables out of the ViewModel as in this case... but let's see how things go after resolving the previous comments 😉 |
||
private val showHiddenFiles: Boolean = sharedPreferencesProvider.getBoolean(PREF_SHOW_HIDDEN_FILES, false) | ||
|
||
val currentFolderDisplayed: MutableStateFlow<OCFile> = MutableStateFlow(initialFolderToDisplay) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Feature
is not a valid prefix for Calens. Check the Calens README.md to see the valid prefixes, I'd say in this case the best one isEnhancement