From d0fa60431971abd43bfa7d0d35a32cd7e4ceec5b Mon Sep 17 00:00:00 2001 From: Jason Whitmore Date: Fri, 10 Jan 2025 18:42:26 -0800 Subject: [PATCH] MediaDetailFragment.kt: add helper method to retrieve ContributionFragment instance Before this commit, there was no easy way to check for and retrieve the ContributionFragment instance that was either the parent or grandparent (parent's parent) fragment. A complicated if check was required to retrieve it. After this commit, there is a simple helper method which will retrieve the ContributionFragment instance. Existing code can now be replaced by calling this method. --- .../nrw/commons/media/MediaDetailFragment.kt | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/app/src/main/java/fr/free/nrw/commons/media/MediaDetailFragment.kt b/app/src/main/java/fr/free/nrw/commons/media/MediaDetailFragment.kt index 299f9b3be..3b2c0652e 100644 --- a/app/src/main/java/fr/free/nrw/commons/media/MediaDetailFragment.kt +++ b/app/src/main/java/fr/free/nrw/commons/media/MediaDetailFragment.kt @@ -467,6 +467,27 @@ class MediaDetailFragment : CommonsDaggerSupportFragment(), CategoryEditHelper.C } } + /** + * Retrieves the ContributionsFragment that is potentially the parent, grandparent, etc + * fragment of this fragment. + * + * @return The ContributionsFragment instance. If the ContributionsFragment instance could not + * be found, null is returned. + */ + private fun getContributionsFragmentParent(): ContributionsFragment? { + var fragment: Fragment? = this + + while (fragment != null && fragment !is ContributionsFragment) { + fragment = fragment.parentFragment + } + + if (fragment == null) { + return null + } + + return fragment as ContributionsFragment + } + override fun onResume() { super.onResume() if (parentFragment != null && requireParentFragment().parentFragment != null) {