Commit 7798e571 authored by Christophe Henry's avatar Christophe Henry

Fix ViewPager not updated after navigating back

parent 9a4f1344
......@@ -4,11 +4,10 @@ import android.os.Bundle
import android.view.*
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentPagerAdapter
import androidx.viewpager.widget.ViewPager.OnPageChangeListener
import androidx.viewpager.widget.ViewPager.SimpleOnPageChangeListener
import com.google.android.material.bottomnavigation.BottomNavigationView
import fr.chenry.android.freshrss.FreshRSSApplication
import fr.chenry.android.freshrss.R
import fr.chenry.android.freshrss.components.subscriptions.SubscriptionSection.*
import fr.chenry.android.freshrss.store.Store
import fr.chenry.android.freshrss.utils.whenNotNull
import kotlinx.android.synthetic.main.fragment_main_subscription.*
......@@ -37,7 +36,9 @@ class MainSubscriptionFragment: Fragment(), BottomNavigationView.OnNavigationIte
subcription_fragment_container.currentItem = it.ordinal
}
subcription_fragment_container.addOnPageChangeListener(adapter)
subcription_fragment_container.addOnPageChangeListener(object: SimpleOnPageChangeListener() {
override fun onPageSelected(position: Int) = this@MainSubscriptionFragment.onPageSelected(position)
})
subcription_bottom_navigation.setOnNavigationItemSelectedListener(this)
}
......@@ -49,37 +50,20 @@ class MainSubscriptionFragment: Fragment(), BottomNavigationView.OnNavigationIte
return true
}
inner class MainSubscriptionPagerAdapter:
FragmentPagerAdapter(activity!!.supportFragmentManager),
OnPageChangeListener
{
private val allFragment by lazy(instantiateFragment(ALL))
private val unreadFragment by lazy(instantiateFragment(UNREAD))
private val favoritesFragment by lazy(instantiateFragment(FAVORITES))
override fun getItem(position: Int) = when(SubscriptionSection.byPosition(position)) {
FAVORITES -> favoritesFragment
ALL -> allFragment
UNREAD -> unreadFragment
}
fun onPageSelected(position: Int) = SubscriptionSection.byPosition(position).let {
Store.subscriptionsSection.value = it
subcription_bottom_navigation.selectedItemId = it.navigationButtonId
}
inner class MainSubscriptionPagerAdapter: FragmentPagerAdapter(childFragmentManager) {
override fun getItem(position: Int) = instantiateFragment(SubscriptionSection.byPosition(position))
override fun getCount() = SubscriptionSection.values().size
override fun onPageScrollStateChanged(state: Int) {}
override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {}
override fun onPageSelected(position: Int) = SubscriptionSection.byPosition(position).let {
Store.subscriptionsSection.value = it
subcription_bottom_navigation.selectedItemId = it.navigationButtonId
}
private fun instantiateFragment(subscriptionSection: SubscriptionSection) = {
private fun instantiateFragment(subscriptionSection: SubscriptionSection) =
Fragment.instantiate(
activity!!,
SubscriptionsFragment::class.qualifiedName!!,
Bundle().apply {putParcelable(SubscriptionsFragment.argumentKey, subscriptionSection)}
)
}
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment