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 showNotice = pages.all { it.items.isEmpty() }
binding?.apply {
emptyListTextview.isVisible = showNotice
if (showNotice) {
@ -408,7 +407,10 @@ class LibraryFragment : Fragment() {
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
// 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.setOnClickListener {
libraryViewModel.currentPage = position // updating selected library tab position
val currentItem =
binding?.viewpager?.currentItem ?: return@setOnClickListener
val distance = abs(position - currentItem)
hideViewpager(distance)
}
//Expand the appBar on tab focus
tab.view.setOnFocusChangeListener { view, b ->
tab.view.setOnFocusChangeListener { _, _ ->
binding?.searchBar?.setExpanded(true)
}
}.attach()
binding?.libraryTabLayout?.addOnTabSelectedListener(object: TabLayout.OnTabSelectedListener {
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,24 +486,29 @@ class LibraryFragment : Fragment() {
}
}
}
binding?.viewpager?.registerOnPageChangeCallback(object :
observe(libraryViewModel.currentPage) { position ->
val all = binding?.viewpager?.allViews?.toList()
?.filterIsInstance<AutofitRecyclerView>()
all?.forEach { view ->
view.isVisible = view.tag == position
view.isFocusable = view.tag == position
if (view.tag == position)
view.descendantFocusability = FOCUS_AFTER_DESCENDANTS
else
view.descendantFocusability = FOCUS_BLOCK_DESCENDANTS
}
}
/*binding?.viewpager?.registerOnPageChangeCallback(object :
ViewPager2.OnPageChangeCallback() {
override fun onPageSelected(position: Int) {
val all = binding?.viewpager?.allViews?.toList()
?.filterIsInstance<AutofitRecyclerView>()
all?.forEach { view ->
view.isVisible = view.tag == position
view.isFocusable = view.tag == position
if (view.tag == position)
view.descendantFocusability = FOCUS_AFTER_DESCENDANTS
else
view.descendantFocusability = FOCUS_BLOCK_DESCENDANTS
}
super.onPageSelected(position)
}
})
})*/
}
override fun onConfigurationChanged(newConfig: Configuration) {
(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"
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)
val pages: LiveData<Resource<List<SyncAPI.Page>>> = _pages