From b3978c4af7b0b3cffcc4445b9b73623e08b1088c Mon Sep 17 00:00:00 2001 From: Nik Clayton Date: Fri, 16 Feb 2024 20:56:00 +0100 Subject: [PATCH] change: Move CustomFragmentStateAdapter to core.activity (#452) --- .../components/account/AccountPagerAdapter.kt | 2 +- .../java/app/pachli/pager/MainPagerAdapter.kt | 2 +- .../activity}/CustomFragmentStateAdapter.kt | 25 +++++++++++++++---- 3 files changed, 22 insertions(+), 7 deletions(-) rename {app/src/main/java/app/pachli/util => core/activity/src/main/kotlin/app/pachli/core/activity}/CustomFragmentStateAdapter.kt (53%) diff --git a/app/src/main/java/app/pachli/components/account/AccountPagerAdapter.kt b/app/src/main/java/app/pachli/components/account/AccountPagerAdapter.kt index 00fb34083..570722891 100644 --- a/app/src/main/java/app/pachli/components/account/AccountPagerAdapter.kt +++ b/app/src/main/java/app/pachli/components/account/AccountPagerAdapter.kt @@ -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, diff --git a/app/src/main/java/app/pachli/pager/MainPagerAdapter.kt b/app/src/main/java/app/pachli/pager/MainPagerAdapter.kt index b6809de1d..3e06fcd0f 100644 --- a/app/src/main/java/app/pachli/pager/MainPagerAdapter.kt +++ b/app/src/main/java/app/pachli/pager/MainPagerAdapter.kt @@ -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, activity: FragmentActivity) : CustomFragmentStateAdapter(activity) { diff --git a/app/src/main/java/app/pachli/util/CustomFragmentStateAdapter.kt b/core/activity/src/main/kotlin/app/pachli/core/activity/CustomFragmentStateAdapter.kt similarity index 53% rename from app/src/main/java/app/pachli/util/CustomFragmentStateAdapter.kt rename to core/activity/src/main/kotlin/app/pachli/core/activity/CustomFragmentStateAdapter.kt index a276ee8a0..126081394 100644 --- a/app/src/main/java/app/pachli/util/CustomFragmentStateAdapter.kt +++ b/core/activity/src/main/kotlin/app/pachli/core/activity/CustomFragmentStateAdapter.kt @@ -1,4 +1,5 @@ -/* Copyright 2019 Tusky Contributors +/* + * Copyright 2024 Pachli Association * * This file is a part of Pachli. * @@ -14,16 +15,30 @@ * see . */ -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)) }