Skip to content

Commit

Permalink
hotfix: fixed rationale dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzofelletti committed Dec 24, 2022
1 parent 0cf88ab commit 28deee6
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import com.lorenzofelletti.permissions.PermissionManager
import com.lorenzofelletti.permissions.dispatcher.dsl.*

class MainActivity : AppCompatActivity() {
private lateinit var permissionsUtilities: PermissionManager
private lateinit var permissionManager: PermissionManager

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

permissionsUtilities = PermissionManager(this)
permissionManager = PermissionManager(this)

permissionsUtilities buildRequestResultsDispatcher {
permissionManager buildRequestResultsDispatcher {
withRequestCode(POSITION_REQUEST_CODE) {
checkPermissions(POSITION_REQUIRED_PERMISSIONS)
showRationaleDialog(message = "Location permission is required to use this feature")
Expand All @@ -29,11 +29,11 @@ class MainActivity : AppCompatActivity() {
}
}

permissionsUtilities checkRequestAndDispatch POSITION_REQUEST_CODE
permissionManager checkRequestAndDispatch POSITION_REQUEST_CODE

val button: Button = findViewById(R.id.button)
button.setOnClickListener {
permissionsUtilities.checkRequestAndDispatch(
permissionManager.checkRequestAndDispatch(
POSITION_REQUEST_CODE
)
}
Expand All @@ -43,7 +43,7 @@ class MainActivity : AppCompatActivity() {
requestCode: Int, permissions: Array<out String>, grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
permissionsUtilities.dispatchOnRequestPermissionsResult(requestCode, grantResults)
permissionManager.dispatchOnRequestPermissionsResult(requestCode, grantResults)
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
@file:JvmName("PermissionDispatcherDslContainer")

package com.lorenzofelletti.permissions.dispatcher.dsl

import android.app.AlertDialog
import android.app.Dialog
import com.lorenzofelletti.permissions.dispatcher.DispatcherEntry
import com.lorenzofelletti.permissions.dispatcher.RequestResultsDispatcher

Expand Down Expand Up @@ -54,18 +57,6 @@ fun DispatcherEntry.rationale(onShowRationale: (List<String>, Int) -> Unit) {
this.onShowRationale = onShowRationale
}

/**
* Shows a rationale dialog to the user.
* If the user clicks on the positive button, the permission request will be performed, else
* not.
*
* @param message the message to be shown
*/
@PermissionDispatcherDsl
fun DispatcherEntry.showRationaleDialog(message: String) {
showRationaleDialog(message, null, null, null)
}

/**
* Shows a rationale dialog to the user.
* If the user clicks on the positive button, the permission request will be performed,
Expand All @@ -79,17 +70,48 @@ fun DispatcherEntry.showRationaleDialog(message: String) {
@PermissionDispatcherDsl
fun DispatcherEntry.showRationaleDialog(
message: String,
positiveButtonText: String? = "OK",
negativeButtonText: String? = "Cancel",
onNegativeButtonPressed: (() -> Unit)?
positiveButtonText: String = "OK",
negativeButtonText: String = "Cancel",
onNegativeButtonPressed: (() -> Unit) = {}
) {
rationale { _, _ ->
manager.activity.runOnUiThread {
AlertDialog.Builder(manager.activity).setMessage(message)
.setPositiveButton(positiveButtonText) { _, _ ->
manager.checkRequestAndDispatch(requestCode, comingFromRationale = true)
}.setNegativeButton(negativeButtonText) { _, _ ->
onNegativeButtonPressed.invoke()
}.show()
}
}
}

/**
* Sets the [AlertDialog] to be shown when the rationale is needed.
*
* By using this method, you can customize the dialog as you want, but do not call [Dialog.show] on
* it, as it will be called automatically, just call [Dialog.create] instead.
* Moreover, it is your responsibility to call the permission request when the positive button is
* pressed.
*
* Example (Kotlin):
* ```
* val dialog = AlertDialog.Builder(this).setMessage("Message").
* setPositiveButton("Proceed") {
* manager.checkRequestAndDispatch(requestCode, comingFromRationale = true)
* }.create()
* ```
*
* @param dialog the dialog to be shown
*/
@PermissionDispatcherDsl
fun DispatcherEntry.showRationaleDialog(
dialog: AlertDialog,
) {
rationale { _, _ ->
AlertDialog.Builder(manager.activity).setMessage(message)
.setPositiveButton(positiveButtonText) { _, _ ->
manager.checkRequestAndDispatch(requestCode, comingFromRationale = true)
}.setNegativeButton(negativeButtonText) { _, _ ->
onNegativeButtonPressed?.invoke()
}.show()
manager.activity.runOnUiThread {
dialog.show()
}
}
}

Expand Down Expand Up @@ -149,8 +171,7 @@ fun RequestResultsDispatcher.addEntry(requestCode: Int, init: DispatcherEntry.()
*/
@PermissionDispatcherDsl
fun RequestResultsDispatcher.replaceEntryOnGranted(
requestCode: Int,
onGranted: () -> Unit
requestCode: Int, onGranted: () -> Unit
) {
entries[requestCode]?.doOnGranted(onGranted)
}
Expand All @@ -174,8 +195,7 @@ fun RequestResultsDispatcher.replaceEntryOnDenied(requestCode: Int, onDenied: ()
*/
@PermissionDispatcherDsl
fun RequestResultsDispatcher.replaceEntry(
requestCode: Int,
init: DispatcherEntry.() -> Unit
requestCode: Int, init: DispatcherEntry.() -> Unit
) {
entries[requestCode] = DispatcherEntry(manager, requestCode).apply(init)
}

0 comments on commit 28deee6

Please sign in to comment.