Skip to content

Commit

Permalink
Merge pull request #4117 from kiwix/Fixes#4112
Browse files Browse the repository at this point in the history
Fixed: Virtual keyboard reset the search (in library).
  • Loading branch information
kelson42 authored Nov 28, 2024
2 parents 2cd2e15 + 2912bc9 commit 116180e
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
}
return FragmentActivityExtensions.Super.ShouldCall
}

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())
}

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
}
if (activeFragments().filterIsInstance<FragmentActivityExtensions>().isEmpty()) {
isEnabled = false
return onBackPressedDispatcher.onBackPressed().also {
isEnabled = true
}
}
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()
} else {
isEnabled = false
onBackPressedDispatcher.onBackPressed()
isEnabled = true
}
}
}
}
}
}

override fun onCreateOptionsMenu(menu: Menu): Boolean {

Expand Down

0 comments on commit 116180e

Please sign in to comment.