Skip to content

Commit

Permalink
Fixed: Virtual keyboard resetting the search in the library.
Browse files Browse the repository at this point in the history
* The issue was with android 14 device, it is working fine in below devices.
* Improved the back press handling in CoreMainActivity using the latest onBackPressedDispatcher API, ensuring proper functionality on Android 14 and above.
* Enhanced the back press behavior in OnlineLibraryFragment while searching for ZIM files, so the SearchView does not close immediately when the user presses the back button to simply dismiss the keyboard.
  • Loading branch information
MohitMaliDeveloper committed Nov 28, 2024
1 parent 2cd2e15 commit 2912bc9
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.requestNotificat
import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.viewModel
import org.kiwix.kiwixmobile.core.extensions.closeKeyboard
import org.kiwix.kiwixmobile.core.extensions.coreMainActivity
import org.kiwix.kiwixmobile.core.extensions.isKeyboardVisible
import org.kiwix.kiwixmobile.core.extensions.setBottomMarginToFragmentContainerView
import org.kiwix.kiwixmobile.core.extensions.setUpSearchView
import org.kiwix.kiwixmobile.core.extensions.snack
Expand Down Expand Up @@ -327,8 +328,11 @@ class OnlineLibraryFragment : BaseFragment(), FragmentActivityExtensions {
}

override fun onBackPressed(activity: AppCompatActivity): FragmentActivityExtensions.Super {
getActivity()?.finish()
return FragmentActivityExtensions.Super.ShouldNotCall
if (isKeyboardVisible()) {
closeKeyboard()
return FragmentActivityExtensions.Super.ShouldNotCall

Check warning on line 333 in app/src/main/java/org/kiwix/kiwixmobile/nav/destination/library/OnlineLibraryFragment.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/java/org/kiwix/kiwixmobile/nav/destination/library/OnlineLibraryFragment.kt#L332-L333

Added lines #L332 - L333 were not covered by tests
}
return FragmentActivityExtensions.Super.ShouldCall

Check warning on line 335 in app/src/main/java/org/kiwix/kiwixmobile/nav/destination/library/OnlineLibraryFragment.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/java/org/kiwix/kiwixmobile/nav/destination/library/OnlineLibraryFragment.kt#L335

Added line #L335 was not covered by tests
}

private fun onRefreshStateChange(isRefreshing: Boolean?) {
Expand Down
1 change: 0 additions & 1 deletion core/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@
android:fullBackupContent="@xml/backup_rules"
android:dataExtractionRules = "@xml/data_extraction_rules"
android:hardwareAccelerated="true"
android:enableOnBackInvokedCallback="true"
android:largeHeap="true"
android:requestLegacyExternalStorage="true"
android:resizeableActivity="true"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import android.content.Context
import android.view.View
import android.view.inputmethod.InputMethodManager
import android.widget.Toast
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
Expand All @@ -36,6 +38,11 @@ fun Fragment.toast(stringId: Int, length: Int = Toast.LENGTH_LONG) {
requireActivity().toast(stringId, length)
}

fun Fragment.isKeyboardVisible(): Boolean {
val insets = ViewCompat.getRootWindowInsets(requireView()) ?: return false
return insets.isVisible(WindowInsetsCompat.Type.ime())

Check warning on line 43 in core/src/main/java/org/kiwix/kiwixmobile/core/extensions/FragmentExtensions.kt

View check run for this annotation

Codecov / codecov/patch

core/src/main/java/org/kiwix/kiwixmobile/core/extensions/FragmentExtensions.kt#L43

Added line #L43 was not covered by tests
}

fun Fragment.closeKeyboard() {
val inputMethodManager =
requireContext().getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import android.os.Process
import android.view.ActionMode
import android.view.Menu
import android.view.MenuItem
import androidx.activity.OnBackPressedCallback
import androidx.appcompat.app.ActionBarDrawerToggle
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
Expand All @@ -47,6 +48,7 @@ import org.kiwix.kiwixmobile.core.CoreApp
import org.kiwix.kiwixmobile.core.R
import org.kiwix.kiwixmobile.core.base.BaseActivity
import org.kiwix.kiwixmobile.core.base.FragmentActivityExtensions
import org.kiwix.kiwixmobile.core.base.FragmentActivityExtensions.Super.ShouldCall
import org.kiwix.kiwixmobile.core.data.remote.ObjectBoxToLibkiwixMigrator
import org.kiwix.kiwixmobile.core.data.remote.ObjectBoxToRoomMigrator
import org.kiwix.kiwixmobile.core.di.components.CoreActivityComponent
Expand Down Expand Up @@ -132,6 +134,7 @@ abstract class CoreMainActivity : BaseActivity(), WebViewProvider {
}
downloadManagerBroadcastReceiver.let(::registerReceiver)
createApplicationShortcuts()
handleBackPressed()
}

@Suppress("DEPRECATION")
Expand All @@ -149,6 +152,7 @@ abstract class CoreMainActivity : BaseActivity(), WebViewProvider {
}

override fun onDestroy() {
onBackPressedCallBack.remove()
downloadManagerBroadcastReceiver.let(::unregisterReceiver)
super.onDestroy()
}
Expand Down Expand Up @@ -277,28 +281,40 @@ abstract class CoreMainActivity : BaseActivity(), WebViewProvider {
externalLinkOpener.openExternalUrl(KIWIX_SUPPORT_URL.toUri().browserIntent())
}

override fun onBackPressed() {
if (navigationDrawerIsOpen()) {
closeNavigationDrawer()
return
}
if (activeFragments().filterIsInstance<FragmentActivityExtensions>().isEmpty()) {
return super.onBackPressedDispatcher.onBackPressed()
}
activeFragments().filterIsInstance<FragmentActivityExtensions>().forEach {
if (it.onBackPressed(this) == FragmentActivityExtensions.Super.ShouldCall) {
if (navController.currentDestination?.id?.equals(readerFragmentResId) == true &&
navController.previousBackStackEntry?.destination
?.id?.equals(searchFragmentResId) == false
) {
drawerToggle = null
finish()
} else {
super.onBackPressedDispatcher.onBackPressed()
private fun handleBackPressed() {
onBackPressedDispatcher.addCallback(this, onBackPressedCallBack)
}

private val onBackPressedCallBack =
object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
if (navigationDrawerIsOpen()) {
closeNavigationDrawer()
return

Check warning on line 293 in core/src/main/java/org/kiwix/kiwixmobile/core/main/CoreMainActivity.kt

View check run for this annotation

Codecov / codecov/patch

core/src/main/java/org/kiwix/kiwixmobile/core/main/CoreMainActivity.kt#L292-L293

Added lines #L292 - L293 were not covered by tests
}
if (activeFragments().filterIsInstance<FragmentActivityExtensions>().isEmpty()) {
isEnabled = false
return onBackPressedDispatcher.onBackPressed().also {
isEnabled = true
}

Check warning on line 299 in core/src/main/java/org/kiwix/kiwixmobile/core/main/CoreMainActivity.kt

View check run for this annotation

Codecov / codecov/patch

core/src/main/java/org/kiwix/kiwixmobile/core/main/CoreMainActivity.kt#L296-L299

Added lines #L296 - L299 were not covered by tests
}
activeFragments().filterIsInstance<FragmentActivityExtensions>().forEach {
if (it.onBackPressed(this@CoreMainActivity) == ShouldCall) {
if (navController.currentDestination?.id?.equals(readerFragmentResId) == true &&
navController.previousBackStackEntry?.destination
?.id?.equals(searchFragmentResId) == false
) {
drawerToggle = null
finish()

Check warning on line 308 in core/src/main/java/org/kiwix/kiwixmobile/core/main/CoreMainActivity.kt

View check run for this annotation

Codecov / codecov/patch

core/src/main/java/org/kiwix/kiwixmobile/core/main/CoreMainActivity.kt#L307-L308

Added lines #L307 - L308 were not covered by tests
} else {
isEnabled = false
onBackPressedDispatcher.onBackPressed()
isEnabled = true
}
}
}
}
}
}

override fun onCreateOptionsMenu(menu: Menu): Boolean {

Expand Down

0 comments on commit 2912bc9

Please sign in to comment.