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.
This commit is contained in:
Jason Whitmore 2025-01-10 18:42:26 -08:00
parent 038ae9acd4
commit d0fa604319

View file

@ -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) {