Last active
May 30, 2024 11:38
-
-
Save muhammetkdr/d7cfd04537b68393128cc8a189b29780 to your computer and use it in GitHub Desktop.
now the dialog works dynamically
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| data class DialogParams( | |
| @StyleRes val dialogTheme: Int, | |
| @StringRes val dialogTitle: Int, | |
| @StringRes val dialogMsg: Int, | |
| @StringRes val dialogPositiveButton:Int, | |
| @StringRes val dialogNegativeButton:Int, | |
| ) | |
| class PermissionManager( | |
| private val fragment: Fragment, | |
| private val permissions: Array<String>, | |
| private val onPermissionGranted: (() -> Unit), | |
| private val onPermissionDenied: (() -> Unit), | |
| private val dialogParams: DialogParams | |
| ) { | |
| private val requestPermissionLauncher: ActivityResultLauncher<Array<String>> = | |
| fragment.registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { permissions -> | |
| if (permissions.all { it.value }) { | |
| onPermissionGranted.invoke() | |
| } else { | |
| onPermissionDenied.invoke() | |
| } | |
| } | |
| fun requestPermissions() { | |
| if (permissions.all { fragment.requireContext().hasPermission(it) }) { | |
| onPermissionGranted.invoke() | |
| } else { | |
| if (shouldShowPermissionRationale()) { | |
| showRationaleDialog() | |
| } else { | |
| requestPermissionLauncher.launch(permissions) | |
| } | |
| } | |
| } | |
| private fun shouldShowPermissionRationale(): Boolean { | |
| return permissions.any { | |
| fragment.shouldShowRequestPermissionRationale(it) | |
| } | |
| } | |
| private fun showRationaleDialog() { | |
| if (shouldShowPermissionRationale()){ | |
| MaterialAlertDialogBuilder( | |
| fragment.requireContext(), | |
| dialogParams.dialogTheme | |
| ) | |
| .setTitle(fragment.requireContext().getString(dialogParams.dialogTitle)) | |
| .setMessage(fragment.requireContext().getString(dialogParams.dialogMsg)) | |
| .setCancelable(false) | |
| .setPositiveButton(fragment.requireContext().getString(dialogParams.dialogPositiveButton)) { dialog, _ -> | |
| requestPermissionLauncher.launch(permissions) | |
| dialog.dismiss() | |
| } | |
| .setNegativeButton(fragment.getString(dialogParams.dialogNegativeButton)) { dialog, _ -> | |
| onPermissionDenied.invoke() | |
| dialog.dismiss() | |
| } | |
| .show() | |
| } | |
| } | |
| private fun Context.hasPermission(permission: String): Boolean { | |
| return ContextCompat.checkSelfPermission( | |
| this, | |
| permission | |
| ) == android.content.pm.PackageManager.PERMISSION_GRANTED | |
| } | |
| } | |
| // In Fragment | |
| private val permissionsManager = PermissionManager( | |
| fragment = this, | |
| permissions = arrayOf( | |
| Manifest.permission.ACCESS_FINE_LOCATION, | |
| Manifest.permission.ACCESS_COARSE_LOCATION // add the type of permissions you want here | |
| ), | |
| onPermissionGranted = ::onPermissionGranted, | |
| onPermissionDenied = ::onPermissionDenied, | |
| dialogParams = DialogParams( | |
| dialogTheme = R.style.DialogTheme, | |
| dialogTitle = R.string.dialog_title, | |
| dialogMsg = R.string.dialog_message, | |
| dialogPositiveButton = R.string.dialog_approve, | |
| dialogNegativeButton = R.string.dialog_cancel | |
| ) | |
| override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | |
| super.onViewCreated(view, savedInstanceState) | |
| permissionsManager.requestPermissions() | |
| } | |
| private fun onPermissionGranted(){ | |
| getLocation() | |
| } | |
| private fun onPermissionDenied(){ | |
| getDefaultLocation() // or request for location permission again.. | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment