From 6d51c59b18418e10165b69ced2155ebe0ec0a545 Mon Sep 17 00:00:00 2001 From: firelight <147925818+fire-light42@users.noreply.github.com> Date: Fri, 15 Dec 2023 15:24:08 +0000 Subject: [PATCH] fix visibility --- .../ui/library/LibraryFragment.kt | 50 +++++++++++-------- .../ui/library/LibraryViewModel.kt | 7 ++- 2 files changed, 34 insertions(+), 23 deletions(-) diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/library/LibraryFragment.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/library/LibraryFragment.kt index c3d7b8bd..c60c8ef0 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/ui/library/LibraryFragment.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/ui/library/LibraryFragment.kt @@ -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() + + 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() - 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() diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/library/LibraryViewModel.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/library/LibraryViewModel.kt index 7d56a223..c983ea2f 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/ui/library/LibraryViewModel.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/ui/library/LibraryViewModel.kt @@ -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 = MutableLiveData(0) + val currentPage: LiveData = _currentPage private val _pages: MutableLiveData>> = MutableLiveData(null) val pages: LiveData>> = _pages