From 868bcde79d3bf676ea88497dec26d5d880922af0 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Thu, 28 Mar 2024 16:20:08 +0100 Subject: [PATCH] feat: dismiss keyboard at back press on search page --- .../libretube/extensions/AnyChildFocused.kt | 15 +++++++++++++++ .../libretube/ui/activities/MainActivity.kt | 6 ++++++ 2 files changed, 21 insertions(+) create mode 100644 app/src/main/java/com/github/libretube/extensions/AnyChildFocused.kt diff --git a/app/src/main/java/com/github/libretube/extensions/AnyChildFocused.kt b/app/src/main/java/com/github/libretube/extensions/AnyChildFocused.kt new file mode 100644 index 0000000000..2985cde45c --- /dev/null +++ b/app/src/main/java/com/github/libretube/extensions/AnyChildFocused.kt @@ -0,0 +1,15 @@ +package com.github.libretube.extensions + +import android.view.View +import android.view.ViewGroup +import androidx.core.view.children + +fun View.anyChildFocused(): Boolean { + if (isFocused) return true + + if (this is ViewGroup) { + return children.any { it.anyChildFocused() } + } + + return false +} \ No newline at end of file diff --git a/app/src/main/java/com/github/libretube/ui/activities/MainActivity.kt b/app/src/main/java/com/github/libretube/ui/activities/MainActivity.kt index c48b18794e..787e7b4193 100644 --- a/app/src/main/java/com/github/libretube/ui/activities/MainActivity.kt +++ b/app/src/main/java/com/github/libretube/ui/activities/MainActivity.kt @@ -31,6 +31,7 @@ import com.github.libretube.compat.PictureInPictureCompat import com.github.libretube.constants.IntentData import com.github.libretube.constants.PreferenceKeys import com.github.libretube.databinding.ActivityMainBinding +import com.github.libretube.extensions.anyChildFocused import com.github.libretube.extensions.toID import com.github.libretube.helpers.NavBarHelper import com.github.libretube.helpers.NavigationHelper @@ -170,6 +171,11 @@ class MainActivity : BaseActivity() { onUserLeaveHint() } + R.id.searchFragment -> { + if (searchView.anyChildFocused()) searchView.clearFocus() + else navController.popBackStack() + } + R.id.searchResultFragment -> { navController.popBackStack(R.id.searchFragment, true) || navController.popBackStack()