Alert Dialogue Box appears on long press to check/uncheck "Don't ask again".

This commit is contained in:
Amitesh-exp 2025-08-26 21:28:22 +05:30
parent a825321abc
commit 57e0f16cef

View file

@ -114,40 +114,62 @@ class MoreFragment : Fragment() {
findNavController().navigate(R.id.observeSourcesFragment) findNavController().navigate(R.id.observeSourcesFragment)
} }
terminateApp.setOnClickListener { fun showTerminateConfirmationDialog() {
if (mainSharedPreferences.getBoolean("ask_terminate_app", true)){ val shouldAskToTerminate = mainSharedPreferences.getBoolean("ask_terminate_app", true)
var doNotShowAgain = false val isDoNotAskAgainChecked = !shouldAskToTerminate
var doNotShowAgainFinalState = isDoNotAskAgainChecked
val terminateDialog = MaterialAlertDialogBuilder(requireContext()) val terminateDialog = MaterialAlertDialogBuilder(requireContext())
terminateDialog.setTitle(getString(R.string.confirm_delete_history)) terminateDialog.setTitle(getString(R.string.confirm_terminate))
val dialogView = layoutInflater.inflate(R.layout.dialog_terminate_app, null) val dialogView = layoutInflater.inflate(R.layout.dialog_terminate_app, null)
val checkbox = dialogView.findViewById<CheckBox>(R.id.doNotShowAgain) val checkbox = dialogView.findViewById<CheckBox>(R.id.doNotShowAgain)
terminateDialog.setView(dialogView) terminateDialog.setView(dialogView)
checkbox.setOnCheckedChangeListener { compoundButton, b -> checkbox.isChecked = doNotShowAgainFinalState
doNotShowAgain = compoundButton.isChecked checkbox.setOnCheckedChangeListener { _, isChecked ->
doNotShowAgainFinalState = isChecked
} }
terminateDialog.setNegativeButton(getString(R.string.cancel)) { dialogInterface: DialogInterface, _: Int -> dialogInterface.cancel() } terminateDialog.setNegativeButton(getString(R.string.cancel)) { dialogInterface, _ ->
terminateDialog.setPositiveButton(getString(R.string.ok)) { diag: DialogInterface?, _: Int -> dialogInterface.cancel()
}
terminateDialog.setPositiveButton(getString(R.string.ok)) { _, _ ->
lifecycleScope.launch { lifecycleScope.launch {
downloadViewModel.pauseAllDownloads() downloadViewModel.pauseAllDownloads()
if (doNotShowAgain){ mainSharedPreferencesEditor.putBoolean("ask_terminate_app", !doNotShowAgainFinalState).commit()
mainSharedPreferencesEditor.putBoolean("ask_terminate_app", false).apply()
}
mainSharedPreferencesEditor.commit()
mainActivity.finishAndRemoveTask() mainActivity.finishAndRemoveTask()
mainActivity.finishAffinity() mainActivity.finishAffinity()
exitProcess(0) exitProcess(0)
} }
} }
terminateDialog.show() terminateDialog.show()
}
// This handles the long press
terminateApp.setOnLongClickListener {
// A long press will ALWAYS show the dialog, ignoring the saved preference.
showTerminateConfirmationDialog()
// Return true to indicate that the long click event is consumed
// and the regular OnClickListener should NOT be triggered.
true
}
// This handles the regular (short) press
terminateApp.setOnClickListener {
// Check the saved preference, just like before.
if (mainSharedPreferences.getBoolean("ask_terminate_app", true)) {
// If the user wants to be asked, show the dialog.
showTerminateConfirmationDialog()
} else { } else {
// If they checked "Don't ask again", terminate immediately.
mainActivity.finishAndRemoveTask() mainActivity.finishAndRemoveTask()
mainActivity.finishAffinity() mainActivity.finishAffinity()
exitProcess(0) exitProcess(0)
} }
} }
settings.setOnClickListener { settings.setOnClickListener {