From e9bc0def61836e1a079429da9497aac1ffa1ea3c Mon Sep 17 00:00:00 2001 From: Hamza Ahmed Khan <70560433+hamza-vd@users.noreply.github.com> Date: Mon, 7 Oct 2024 18:51:06 +0500 Subject: [PATCH] Add Exit Dialog (#3487) * Add exit dialog * Fix a typo * Show Dialog only when Map or Register is visible * PR feedback changes * Fix failing check * Remove incorrect test * Refactor dialog condition logic --------- Co-authored-by: Benjamin Mwalimu --- .../fhircore/quest/ui/main/AppMainActivity.kt | 26 +++++++++++++++++++ android/quest/src/main/res/values/strings.xml | 2 ++ .../quest/ui/main/AppMainActivityTest.kt | 1 - 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/android/quest/src/main/java/org/smartregister/fhircore/quest/ui/main/AppMainActivity.kt b/android/quest/src/main/java/org/smartregister/fhircore/quest/ui/main/AppMainActivity.kt index 81c1f8371b..c1c47cdbfd 100644 --- a/android/quest/src/main/java/org/smartregister/fhircore/quest/ui/main/AppMainActivity.kt +++ b/android/quest/src/main/java/org/smartregister/fhircore/quest/ui/main/AppMainActivity.kt @@ -24,6 +24,7 @@ import android.os.Bundle import android.provider.Settings import android.view.View import android.widget.Toast +import androidx.activity.OnBackPressedCallback import androidx.activity.result.ActivityResult import androidx.activity.result.ActivityResultLauncher import androidx.activity.result.contract.ActivityResultContracts @@ -51,6 +52,8 @@ import org.smartregister.fhircore.engine.domain.model.LauncherType import org.smartregister.fhircore.engine.rulesengine.services.LocationCoordinate import org.smartregister.fhircore.engine.sync.OnSyncListener import org.smartregister.fhircore.engine.sync.SyncListenerManager +import org.smartregister.fhircore.engine.ui.base.AlertDialogue +import org.smartregister.fhircore.engine.ui.base.AlertIntent import org.smartregister.fhircore.engine.ui.base.BaseMultiLanguageActivity import org.smartregister.fhircore.engine.util.DispatcherProvider import org.smartregister.fhircore.engine.util.extension.parcelable @@ -152,6 +155,7 @@ open class AppMainActivity : BaseMultiLanguageActivity(), QuestionnaireHandler, } setupLocationServices() + overrideOnBackPressListener() findViewById(R.id.mainScreenProgressBar).apply { visibility = View.GONE } findViewById(R.id.mainScreenProgressBarText).apply { visibility = View.GONE } @@ -300,4 +304,26 @@ open class AppMainActivity : BaseMultiLanguageActivity(), QuestionnaireHandler, } } } + + private fun overrideOnBackPressListener() { + onBackPressedDispatcher.addCallback( + object : OnBackPressedCallback(true) { + override fun handleOnBackPressed() { + val navHostFragment = + (supportFragmentManager.findFragmentById(R.id.nav_host) as NavHostFragment) + if (navHostFragment.childFragmentManager.backStackEntryCount == 0) { + AlertDialogue.showAlert( + this@AppMainActivity, + alertIntent = AlertIntent.CONFIRM, + title = getString(R.string.exit_app), + message = getString(R.string.exit_app_message), + cancellable = false, + confirmButtonListener = { finish() }, + neutralButtonListener = { dialog -> dialog.dismiss() }, + ) + } else navHostFragment.navController.navigateUp() + } + }, + ) + } } diff --git a/android/quest/src/main/res/values/strings.xml b/android/quest/src/main/res/values/strings.xml index adb04213d9..77345f7495 100644 --- a/android/quest/src/main/res/values/strings.xml +++ b/android/quest/src/main/res/values/strings.xml @@ -133,4 +133,6 @@ Place your camera over the entire QR Code to start scanning Failed to get GPS location \u0020\u002a + Exit App + Are you sure you want to exit from the app? diff --git a/android/quest/src/test/java/org/smartregister/fhircore/quest/ui/main/AppMainActivityTest.kt b/android/quest/src/test/java/org/smartregister/fhircore/quest/ui/main/AppMainActivityTest.kt index da6fbea110..cde4637068 100644 --- a/android/quest/src/test/java/org/smartregister/fhircore/quest/ui/main/AppMainActivityTest.kt +++ b/android/quest/src/test/java/org/smartregister/fhircore/quest/ui/main/AppMainActivityTest.kt @@ -37,7 +37,6 @@ import io.mockk.slot import io.mockk.spyk import java.io.Serializable import java.time.OffsetDateTime -import kotlin.test.assertNotNull import kotlinx.coroutines.test.runTest import org.hl7.fhir.r4.model.QuestionnaireResponse import org.junit.Assert