Skip to content

Commit 28deee6

Browse files
hotfix: fixed rationale dialog
1 parent 0cf88ab commit 28deee6

File tree

2 files changed

+51
-31
lines changed

2 files changed

+51
-31
lines changed

app/src/main/java/com/lorenzofelletti/permissionsexample/MainActivity.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ import com.lorenzofelletti.permissions.PermissionManager
88
import com.lorenzofelletti.permissions.dispatcher.dsl.*
99

1010
class MainActivity : AppCompatActivity() {
11-
private lateinit var permissionsUtilities: PermissionManager
11+
private lateinit var permissionManager: PermissionManager
1212

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

17-
permissionsUtilities = PermissionManager(this)
17+
permissionManager = PermissionManager(this)
1818

19-
permissionsUtilities buildRequestResultsDispatcher {
19+
permissionManager buildRequestResultsDispatcher {
2020
withRequestCode(POSITION_REQUEST_CODE) {
2121
checkPermissions(POSITION_REQUIRED_PERMISSIONS)
2222
showRationaleDialog(message = "Location permission is required to use this feature")
@@ -29,11 +29,11 @@ class MainActivity : AppCompatActivity() {
2929
}
3030
}
3131

32-
permissionsUtilities checkRequestAndDispatch POSITION_REQUEST_CODE
32+
permissionManager checkRequestAndDispatch POSITION_REQUEST_CODE
3333

3434
val button: Button = findViewById(R.id.button)
3535
button.setOnClickListener {
36-
permissionsUtilities.checkRequestAndDispatch(
36+
permissionManager.checkRequestAndDispatch(
3737
POSITION_REQUEST_CODE
3838
)
3939
}
@@ -43,7 +43,7 @@ class MainActivity : AppCompatActivity() {
4343
requestCode: Int, permissions: Array<out String>, grantResults: IntArray
4444
) {
4545
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
46-
permissionsUtilities.dispatchOnRequestPermissionsResult(requestCode, grantResults)
46+
permissionManager.dispatchOnRequestPermissionsResult(requestCode, grantResults)
4747
}
4848

4949
companion object {

permissions/src/main/java/com/lorenzofelletti/permissions/dispatcher/dsl/PermissionDispatcherDsl.kt

Lines changed: 45 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
@file:JvmName("PermissionDispatcherDslContainer")
2+
13
package com.lorenzofelletti.permissions.dispatcher.dsl
24

35
import android.app.AlertDialog
6+
import android.app.Dialog
47
import com.lorenzofelletti.permissions.dispatcher.DispatcherEntry
58
import com.lorenzofelletti.permissions.dispatcher.RequestResultsDispatcher
69

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

57-
/**
58-
* Shows a rationale dialog to the user.
59-
* If the user clicks on the positive button, the permission request will be performed, else
60-
* not.
61-
*
62-
* @param message the message to be shown
63-
*/
64-
@PermissionDispatcherDsl
65-
fun DispatcherEntry.showRationaleDialog(message: String) {
66-
showRationaleDialog(message, null, null, null)
67-
}
68-
6960
/**
7061
* Shows a rationale dialog to the user.
7162
* If the user clicks on the positive button, the permission request will be performed,
@@ -79,17 +70,48 @@ fun DispatcherEntry.showRationaleDialog(message: String) {
7970
@PermissionDispatcherDsl
8071
fun DispatcherEntry.showRationaleDialog(
8172
message: String,
82-
positiveButtonText: String? = "OK",
83-
negativeButtonText: String? = "Cancel",
84-
onNegativeButtonPressed: (() -> Unit)?
73+
positiveButtonText: String = "OK",
74+
negativeButtonText: String = "Cancel",
75+
onNegativeButtonPressed: (() -> Unit) = {}
76+
) {
77+
rationale { _, _ ->
78+
manager.activity.runOnUiThread {
79+
AlertDialog.Builder(manager.activity).setMessage(message)
80+
.setPositiveButton(positiveButtonText) { _, _ ->
81+
manager.checkRequestAndDispatch(requestCode, comingFromRationale = true)
82+
}.setNegativeButton(negativeButtonText) { _, _ ->
83+
onNegativeButtonPressed.invoke()
84+
}.show()
85+
}
86+
}
87+
}
88+
89+
/**
90+
* Sets the [AlertDialog] to be shown when the rationale is needed.
91+
*
92+
* By using this method, you can customize the dialog as you want, but do not call [Dialog.show] on
93+
* it, as it will be called automatically, just call [Dialog.create] instead.
94+
* Moreover, it is your responsibility to call the permission request when the positive button is
95+
* pressed.
96+
*
97+
* Example (Kotlin):
98+
* ```
99+
* val dialog = AlertDialog.Builder(this).setMessage("Message").
100+
* setPositiveButton("Proceed") {
101+
* manager.checkRequestAndDispatch(requestCode, comingFromRationale = true)
102+
* }.create()
103+
* ```
104+
*
105+
* @param dialog the dialog to be shown
106+
*/
107+
@PermissionDispatcherDsl
108+
fun DispatcherEntry.showRationaleDialog(
109+
dialog: AlertDialog,
85110
) {
86111
rationale { _, _ ->
87-
AlertDialog.Builder(manager.activity).setMessage(message)
88-
.setPositiveButton(positiveButtonText) { _, _ ->
89-
manager.checkRequestAndDispatch(requestCode, comingFromRationale = true)
90-
}.setNegativeButton(negativeButtonText) { _, _ ->
91-
onNegativeButtonPressed?.invoke()
92-
}.show()
112+
manager.activity.runOnUiThread {
113+
dialog.show()
114+
}
93115
}
94116
}
95117

@@ -149,8 +171,7 @@ fun RequestResultsDispatcher.addEntry(requestCode: Int, init: DispatcherEntry.()
149171
*/
150172
@PermissionDispatcherDsl
151173
fun RequestResultsDispatcher.replaceEntryOnGranted(
152-
requestCode: Int,
153-
onGranted: () -> Unit
174+
requestCode: Int, onGranted: () -> Unit
154175
) {
155176
entries[requestCode]?.doOnGranted(onGranted)
156177
}
@@ -174,8 +195,7 @@ fun RequestResultsDispatcher.replaceEntryOnDenied(requestCode: Int, onDenied: ()
174195
*/
175196
@PermissionDispatcherDsl
176197
fun RequestResultsDispatcher.replaceEntry(
177-
requestCode: Int,
178-
init: DispatcherEntry.() -> Unit
198+
requestCode: Int, init: DispatcherEntry.() -> Unit
179199
) {
180200
entries[requestCode] = DispatcherEntry(manager, requestCode).apply(init)
181201
}

0 commit comments

Comments
 (0)