Skip to content

Commit 4244373

Browse files
authored
Refactor focus change listener to improve readability and avoid crashes (commons-app#6080)
1 parent 75ca96a commit 4244373

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

app/src/main/java/fr/free/nrw/commons/nearby/PlaceAdapterDelegate.kt

+10-7
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,18 @@ fun placeAdapterDelegate(
4141
showOrHideAndScrollToIfLast()
4242
onItemClick?.invoke(item)
4343
}
44-
root.setOnFocusChangeListener { view1: View?, hasFocus: Boolean ->
44+
root.setOnFocusChangeListener { _: View?, hasFocus: Boolean ->
45+
val parentView = root.parent.parent.parent as? RelativeLayout
46+
val bottomSheetBehavior = parentView?.let { BottomSheetBehavior.from(it) }
47+
48+
// Hide button layout if focus is lost, otherwise show it if it's not already visible
4549
if (!hasFocus && nearbyButtonLayout.buttonLayout.isShown) {
4650
nearbyButtonLayout.buttonLayout.visibility = GONE
47-
} else if (hasFocus && !nearbyButtonLayout.buttonLayout.isShown &&
48-
BottomSheetBehavior.from(root.parent.parent.parent as RelativeLayout).state !=
49-
BottomSheetBehavior.STATE_HIDDEN
50-
) {
51-
showOrHideAndScrollToIfLast()
52-
onItemClick?.invoke(item)
51+
} else if (hasFocus && !nearbyButtonLayout.buttonLayout.isShown) {
52+
if (bottomSheetBehavior?.state != BottomSheetBehavior.STATE_HIDDEN) {
53+
showOrHideAndScrollToIfLast()
54+
onItemClick?.invoke(item)
55+
}
5356
}
5457
}
5558
nearbyButtonLayout.cameraButton.setOnClickListener { onCameraClicked(item, inAppCameraLocationPermissionLauncher, cameraPickLauncherForResult) }

0 commit comments

Comments
 (0)