1
+ @file:JvmName(" PermissionDispatcherDslContainer" )
2
+
1
3
package com.lorenzofelletti.permissions.dispatcher.dsl
2
4
3
5
import android.app.AlertDialog
6
+ import android.app.Dialog
4
7
import com.lorenzofelletti.permissions.dispatcher.DispatcherEntry
5
8
import com.lorenzofelletti.permissions.dispatcher.RequestResultsDispatcher
6
9
@@ -54,18 +57,6 @@ fun DispatcherEntry.rationale(onShowRationale: (List<String>, Int) -> Unit) {
54
57
this .onShowRationale = onShowRationale
55
58
}
56
59
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
-
69
60
/* *
70
61
* Shows a rationale dialog to the user.
71
62
* If the user clicks on the positive button, the permission request will be performed,
@@ -79,17 +70,48 @@ fun DispatcherEntry.showRationaleDialog(message: String) {
79
70
@PermissionDispatcherDsl
80
71
fun DispatcherEntry.showRationaleDialog (
81
72
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 ,
85
110
) {
86
111
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
+ }
93
115
}
94
116
}
95
117
@@ -149,8 +171,7 @@ fun RequestResultsDispatcher.addEntry(requestCode: Int, init: DispatcherEntry.()
149
171
*/
150
172
@PermissionDispatcherDsl
151
173
fun RequestResultsDispatcher.replaceEntryOnGranted (
152
- requestCode : Int ,
153
- onGranted : () -> Unit
174
+ requestCode : Int , onGranted : () -> Unit
154
175
) {
155
176
entries[requestCode]?.doOnGranted(onGranted)
156
177
}
@@ -174,8 +195,7 @@ fun RequestResultsDispatcher.replaceEntryOnDenied(requestCode: Int, onDenied: ()
174
195
*/
175
196
@PermissionDispatcherDsl
176
197
fun RequestResultsDispatcher.replaceEntry (
177
- requestCode : Int ,
178
- init : DispatcherEntry .() -> Unit
198
+ requestCode : Int , init : DispatcherEntry .() -> Unit
179
199
) {
180
200
entries[requestCode] = DispatcherEntry (manager, requestCode).apply (init )
181
201
}
0 commit comments