From d4a9bacd9198519eaf8f7c310bc516e9d4079794 Mon Sep 17 00:00:00 2001 From: Jason-Whitmore Date: Mon, 13 Jan 2025 15:17:14 -0800 Subject: [PATCH] Fixes Issue #5832: Navigation Banner Appears in Media Details Screen (#6121) * 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. * MediaDetailFragment.kt: replace code that is meant to hide nearby card Before this commit, code would attempt to find and hide the nearby card that would appear when the user was looking at media details. However, this code did not work. After this commit, the old code has been replaced with code that correctly hides the nearby card. Also, this new code uses a helper method call and is overall easier to read. --------- Co-authored-by: Nicolas Raoul --- .../nrw/commons/media/MediaDetailFragment.kt | 35 ++++++++++++++----- 1 file changed, 26 insertions(+), 9 deletions(-) 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 66d9d08ef..084af4da9 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,18 +467,35 @@ 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) { - // Added a check because, not necessarily, the parent fragment - // will have a parent fragment, say in the case when MediaDetailPagerFragment - // is directly started by the CategoryImagesActivity - if (parentFragment is ContributionsFragment) { - (((parentFragment as ContributionsFragment) - .parentFragment) as ContributionsFragment).binding.cardViewNearby.visibility = - View.GONE - } + + val contributionsFragment: ContributionsFragment? = this.getContributionsFragmentParent() + if (contributionsFragment?.binding != null) { + contributionsFragment.binding.cardViewNearby.visibility = View.GONE } + // detail provider is null when fragment is shown in review activity media = if (detailProvider != null) { detailProvider!!.getMediaAtPosition(index)