Skip to content

Commit d4a9bac

Browse files
Fixes Issue commons-app#5832: Navigation Banner Appears in Media Details Screen (commons-app#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 <nicolas.raoul@gmail.com>
1 parent 0e73551 commit d4a9bac

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

app/src/main/java/fr/free/nrw/commons/media/MediaDetailFragment.kt

+26-9
Original file line numberDiff line numberDiff line change
@@ -467,18 +467,35 @@ class MediaDetailFragment : CommonsDaggerSupportFragment(), CategoryEditHelper.C
467467
}
468468
}
469469

470+
/**
471+
* Retrieves the ContributionsFragment that is potentially the parent, grandparent, etc
472+
* fragment of this fragment.
473+
*
474+
* @return The ContributionsFragment instance. If the ContributionsFragment instance could not
475+
* be found, null is returned.
476+
*/
477+
private fun getContributionsFragmentParent(): ContributionsFragment? {
478+
var fragment: Fragment? = this
479+
480+
while (fragment != null && fragment !is ContributionsFragment) {
481+
fragment = fragment.parentFragment
482+
}
483+
484+
if (fragment == null) {
485+
return null
486+
}
487+
488+
return fragment as ContributionsFragment
489+
}
490+
470491
override fun onResume() {
471492
super.onResume()
472-
if (parentFragment != null && requireParentFragment().parentFragment != null) {
473-
// Added a check because, not necessarily, the parent fragment
474-
// will have a parent fragment, say in the case when MediaDetailPagerFragment
475-
// is directly started by the CategoryImagesActivity
476-
if (parentFragment is ContributionsFragment) {
477-
(((parentFragment as ContributionsFragment)
478-
.parentFragment) as ContributionsFragment).binding.cardViewNearby.visibility =
479-
View.GONE
480-
}
493+
494+
val contributionsFragment: ContributionsFragment? = this.getContributionsFragmentParent()
495+
if (contributionsFragment?.binding != null) {
496+
contributionsFragment.binding.cardViewNearby.visibility = View.GONE
481497
}
498+
482499
// detail provider is null when fragment is shown in review activity
483500
media = if (detailProvider != null) {
484501
detailProvider!!.getMediaAtPosition(index)

0 commit comments

Comments
 (0)