change: Move CustomFragmentStateAdapter to core.activity (#452)

This commit is contained in:
Nik Clayton 2024-02-16 20:56:00 +01:00 committed by GitHub
parent 96c7799539
commit b3978c4af7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 22 additions and 7 deletions

View File

@ -20,9 +20,9 @@ import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import app.pachli.components.account.media.AccountMediaFragment
import app.pachli.components.timeline.TimelineFragment
import app.pachli.core.activity.CustomFragmentStateAdapter
import app.pachli.core.activity.RefreshableFragment
import app.pachli.core.network.model.TimelineKind
import app.pachli.util.CustomFragmentStateAdapter
class AccountPagerAdapter(
activity: FragmentActivity,

View File

@ -19,7 +19,7 @@ package app.pachli.pager
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import app.pachli.TabViewData
import app.pachli.util.CustomFragmentStateAdapter
import app.pachli.core.activity.CustomFragmentStateAdapter
class MainPagerAdapter(var tabs: List<TabViewData>, activity: FragmentActivity) : CustomFragmentStateAdapter(activity) {

View File

@ -1,4 +1,5 @@
/* Copyright 2019 Tusky Contributors
/*
* Copyright 2024 Pachli Association
*
* This file is a part of Pachli.
*
@ -14,16 +15,30 @@
* see <http://www.gnu.org/licenses>.
*/
package app.pachli.util
package app.pachli.core.activity
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.fragment.app.FragmentManager
import androidx.lifecycle.Lifecycle
import androidx.viewpager2.adapter.FragmentStateAdapter
/**
* A [FragmentStateAdapter] that provides [getFragment].
*
* Useful if the hosting activity or fragment needs to call methods on
* one or more of the child fragments (e.g., to trigger a refresh).
*/
abstract class CustomFragmentStateAdapter(
private val activity: FragmentActivity,
) : FragmentStateAdapter(activity) {
open val fragmentManager: FragmentManager,
open val lifecycle: Lifecycle,
) : FragmentStateAdapter(fragmentManager, lifecycle) {
constructor(activity: FragmentActivity) :
this(activity.supportFragmentManager, activity.lifecycle)
constructor(fragment: Fragment) :
this(fragment.childFragmentManager, fragment.lifecycle)
fun getFragment(position: Int): Fragment? =
activity.supportFragmentManager.findFragmentByTag("f" + getItemId(position))
fragmentManager.findFragmentByTag("f" + getItemId(position))
}