Skip to content

Commit

Permalink
[Fix] Fix grid column count when persistent drawer is open.
Browse files Browse the repository at this point in the history
Fixes: #999
  • Loading branch information
zhanghai committed Aug 28, 2023
1 parent 72f500c commit ec3710e
Showing 1 changed file with 26 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -285,11 +285,6 @@ class FileListFragment : Fragment(), BreadcrumbLayout.Listener, FileListAdapter.
}
}
val viewLifecycleOwner = viewLifecycleOwner
if (binding.persistentDrawerLayout != null) {
Settings.FILE_LIST_PERSISTENT_DRAWER_OPEN.observe(viewLifecycleOwner) {
onPersistentDrawerOpenChanged(it)
}
}
viewModel.currentPathLiveData.observe(viewLifecycleOwner) { onCurrentPathChanged(it) }
viewModel.searchViewExpandedLiveData.observe(viewLifecycleOwner) {
onSearchViewExpandedChanged(it)
Expand All @@ -298,6 +293,14 @@ class FileListFragment : Fragment(), BreadcrumbLayout.Listener, FileListAdapter.
binding.breadcrumbLayout.setData(it)
}
viewModel.viewTypeLiveData.observe(viewLifecycleOwner) { onViewTypeChanged(it) }
// Live data only calls observeForever() on its sources when it is active, so we have to
// make view type live data active first (so that it can load its initial value) before we
// register another observer that needs to get the view type.
if (binding.persistentDrawerLayout != null) {
Settings.FILE_LIST_PERSISTENT_DRAWER_OPEN.observe(viewLifecycleOwner) {
onPersistentDrawerOpenChanged(it)
}
}
viewModel.sortOptionsLiveData.observe(viewLifecycleOwner) { onSortOptionsChanged(it) }
viewModel.viewSortPathSpecificLiveData.observe(viewLifecycleOwner) {
onViewSortPathSpecificChanged(it)
Expand Down Expand Up @@ -513,6 +516,7 @@ class FileListFragment : Fragment(), BreadcrumbLayout.Listener, FileListAdapter.
it.closeDrawer(GravityCompat.START)
}
}
updateSpanCount()
}

private fun onCurrentPathChanged(path: Path) {
Expand Down Expand Up @@ -586,14 +590,27 @@ class FileListFragment : Fragment(), BreadcrumbLayout.Listener, FileListAdapter.
}

private fun onViewTypeChanged(viewType: FileViewType) {
layoutManager.spanCount = when (viewType) {
FileViewType.LIST -> 1
FileViewType.GRID -> (resources.configuration.screenWidthDp / 180).coerceAtLeast(2)
}
updateSpanCount()
adapter.viewType = viewType
updateViewSortMenuItems()
}

private fun updateSpanCount() {
layoutManager.spanCount = when (viewModel.viewType) {
FileViewType.LIST -> 1
FileViewType.GRID -> {
var width = resources.configuration.screenWidthDp
val persistentDrawerLayout = binding.persistentDrawerLayout
if (persistentDrawerLayout != null &&
persistentDrawerLayout.isDrawerOpen(GravityCompat.START)) {
// R.dimen.navigation_max_width
width -= 320
}
(width / 180).coerceAtLeast(2)
}
}
}

private fun onSortOptionsChanged(sortOptions: FileSortOptions) {
adapter.sortOptions = sortOptions
updateViewSortMenuItems()
Expand Down

0 comments on commit ec3710e

Please sign in to comment.