fix visibility

This commit is contained in:
firelight 2023-12-15 15:24:08 +00:00 committed by GitHub
parent f98ce0558d
commit 6d51c59b18
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 23 deletions

View file

@ -385,7 +385,6 @@ class LibraryFragment : Fragment() {
val pages = resource.value val pages = resource.value
val showNotice = pages.all { it.items.isEmpty() } val showNotice = pages.all { it.items.isEmpty() }
binding?.apply { binding?.apply {
emptyListTextview.isVisible = showNotice emptyListTextview.isVisible = showNotice
if (showNotice) { if (showNotice) {
@ -408,7 +407,10 @@ class LibraryFragment : Fragment() {
0, 0,
viewpager.adapter?.itemCount ?: 0 viewpager.adapter?.itemCount ?: 0
) )
binding?.viewpager?.setCurrentItem(libraryViewModel.currentPage, false)
libraryViewModel.currentPage.value?.let { page ->
binding?.viewpager?.setCurrentItem(page, false)
}
// Only stop loading after 300ms to hide the fade effect the viewpager produces when updating // Only stop loading after 300ms to hide the fade effect the viewpager produces when updating
// Without this there would be a flashing effect: // Without this there would be a flashing effect:
@ -449,27 +451,26 @@ class LibraryFragment : Fragment() {
tab.view.nextFocusDownId = R.id.search_result_root tab.view.nextFocusDownId = R.id.search_result_root
tab.view.setOnClickListener { tab.view.setOnClickListener {
libraryViewModel.currentPage = position // updating selected library tab position
val currentItem = val currentItem =
binding?.viewpager?.currentItem ?: return@setOnClickListener binding?.viewpager?.currentItem ?: return@setOnClickListener
val distance = abs(position - currentItem) val distance = abs(position - currentItem)
hideViewpager(distance) hideViewpager(distance)
} }
//Expand the appBar on tab focus //Expand the appBar on tab focus
tab.view.setOnFocusChangeListener { view, b -> tab.view.setOnFocusChangeListener { _, _ ->
binding?.searchBar?.setExpanded(true) binding?.searchBar?.setExpanded(true)
} }
}.attach() }.attach()
binding?.libraryTabLayout?.addOnTabSelectedListener(object: TabLayout.OnTabSelectedListener { binding?.libraryTabLayout?.addOnTabSelectedListener(object: TabLayout.OnTabSelectedListener {
override fun onTabSelected(tab: TabLayout.Tab?) { override fun onTabSelected(tab: TabLayout.Tab?) {
libraryViewModel.currentPage = binding?.libraryTabLayout?.selectedTabPosition ?:0 binding?.libraryTabLayout?.selectedTabPosition?.let { page ->
libraryViewModel.switchPage(page)
} }
override fun onTabUnselected(tab: TabLayout.Tab?) {} }
override fun onTabReselected(tab: TabLayout.Tab?) {} override fun onTabUnselected(tab: TabLayout.Tab?) = Unit
override fun onTabReselected(tab: TabLayout.Tab?) = Unit
}) })
} }
} }
@ -485,9 +486,8 @@ class LibraryFragment : Fragment() {
} }
} }
} }
binding?.viewpager?.registerOnPageChangeCallback(object :
ViewPager2.OnPageChangeCallback() { observe(libraryViewModel.currentPage) { position ->
override fun onPageSelected(position: Int) {
val all = binding?.viewpager?.allViews?.toList() val all = binding?.viewpager?.allViews?.toList()
?.filterIsInstance<AutofitRecyclerView>() ?.filterIsInstance<AutofitRecyclerView>()
@ -500,9 +500,15 @@ class LibraryFragment : Fragment() {
else else
view.descendantFocusability = FOCUS_BLOCK_DESCENDANTS view.descendantFocusability = FOCUS_BLOCK_DESCENDANTS
} }
}
/*binding?.viewpager?.registerOnPageChangeCallback(object :
ViewPager2.OnPageChangeCallback() {
override fun onPageSelected(position: Int) {
super.onPageSelected(position) super.onPageSelected(position)
} }
}) })*/
} }
override fun onConfigurationChanged(newConfig: Configuration) { override fun onConfigurationChanged(newConfig: Configuration) {
(binding?.viewpager?.adapter as? ViewpagerAdapter)?.rebind() (binding?.viewpager?.adapter as? ViewpagerAdapter)?.rebind()

View file

@ -28,7 +28,12 @@ enum class ListSorting(@StringRes val stringRes: Int) {
const val LAST_SYNC_API_KEY = "last_sync_api" const val LAST_SYNC_API_KEY = "last_sync_api"
class LibraryViewModel : ViewModel() { class LibraryViewModel : ViewModel() {
var currentPage: Int = 0 fun switchPage(page : Int) {
_currentPage.postValue(page)
}
private val _currentPage: MutableLiveData<Int> = MutableLiveData(0)
val currentPage: LiveData<Int> = _currentPage
private val _pages: MutableLiveData<Resource<List<SyncAPI.Page>>> = MutableLiveData(null) private val _pages: MutableLiveData<Resource<List<SyncAPI.Page>>> = MutableLiveData(null)
val pages: LiveData<Resource<List<SyncAPI.Page>>> = _pages val pages: LiveData<Resource<List<SyncAPI.Page>>> = _pages