Coverage Summary for Class: PermissionResultLauncher (dev.shreyaspatil.permissionFlow.utils)

Class Class, % Method, % Branch, % Line, % Instruction, %
PermissionResultLauncher 100% (1/1) 100% (4/4) 68.4% (13/19) 45.2% (28/62)


1 /** 2  * Copyright 2022 Shreyas Patil 3  * 4  * Licensed under the Apache License, Version 2.0 (the "License"); 5  * you may not use this file except in compliance with the License. 6  * You may obtain a copy of the License at 7  * 8  * http://www.apache.org/licenses/LICENSE-2.0 9  * 10  * Unless required by applicable law or agreed to in writing, software 11  * distributed under the License is distributed on an "AS IS" BASIS, 12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13  * See the License for the specific language governing permissions and 14  * limitations under the License. 15  */ 16 @file:JvmName("PermissionResultLauncher") 17  18 package dev.shreyaspatil.permissionFlow.utils 19  20 import androidx.activity.ComponentActivity 21 import androidx.activity.result.ActivityResultCallback 22 import androidx.activity.result.ActivityResultLauncher 23 import androidx.activity.result.ActivityResultRegistry 24 import androidx.fragment.app.Fragment 25 import dev.shreyaspatil.permissionFlow.PermissionFlow 26 import dev.shreyaspatil.permissionFlow.contract.RequestPermissionsContract 27  28 /** 29  * Returns a [ActivityResultLauncher] for this Activity which internally notifies [PermissionFlow] 30  * about the state change whenever permission state is changed with this launcher. 31  * 32  * Usage: 33  * ``` 34  * class MyActivity: AppCompatActivity() { 35  * private val permissionLauncher = registerForPermissionFlowRequestsResult() 36  * 37  * fun askContactPermission() { 38  * permissionLauncher.launch(android.Manifest.permission.READ_CONTACTS) 39  * } 40  * } 41  * ``` 42  * 43  * @param requestPermissionsContract A contract specifying permission request and result. 44  * @param activityResultRegistry Activity result registry. By default it uses Activity's Result 45  * registry. 46  * @param callback Callback of a permission state change. 47  */ 48 @JvmOverloads 49 fun ComponentActivity.registerForPermissionFlowRequestsResult( 50  requestPermissionsContract: RequestPermissionsContract = RequestPermissionsContract(), 51  activityResultRegistry: ActivityResultRegistry = getActivityResultRegistry(), 52  callback: ActivityResultCallback<Map<String, Boolean>> = emptyCallback(), 53 ): ActivityResultLauncher<Array<String>> = 54  registerForActivityResult( 55  requestPermissionsContract, 56  activityResultRegistry, 57  callback, 58  ) 59  60 /** 61  * Returns a [ActivityResultLauncher] for this Fragment which internally notifies [PermissionFlow] 62  * about the state change whenever permission state is changed with this launcher. 63  * 64  * Usage: 65  * ``` 66  * class MyFragment: Fragment() { 67  * private val permissionLauncher = registerForPermissionFlowRequestsResult() 68  * 69  * fun askContactPermission() { 70  * permissionLauncher.launch(android.Manifest.permission.READ_CONTACTS) 71  * } 72  * } 73  * ``` 74  * 75  * @param requestPermissionsContract A contract specifying permission request and result. registry. 76  * @param callback Callback of a permission state change. 77  */ 78 @JvmOverloads 79 fun Fragment.registerForPermissionFlowRequestsResult( 80  requestPermissionsContract: RequestPermissionsContract = RequestPermissionsContract(), 81  callback: ActivityResultCallback<Map<String, Boolean>> = emptyCallback(), 82 ): ActivityResultLauncher<Array<String>> = 83  registerForActivityResult( 84  requestPermissionsContract, 85  callback, 86  ) 87  88 /** 89  * Returns a [ActivityResultLauncher] for this Fragment which internally notifies [PermissionFlow] 90  * about the state change whenever permission state is changed with this launcher. 91  * 92  * Usage: 93  * ``` 94  * class MyFragment: Fragment() { 95  * private val permissionLauncher = registerForPermissionFlowRequestsResult() 96  * 97  * fun askContactPermission() { 98  * permissionLauncher.launch(android.Manifest.permission.READ_CONTACTS) 99  * } 100  * } 101  * ``` 102  * 103  * @param requestPermissionsContract A contract specifying permission request and result. 104  * @param activityResultRegistry Activity result registry. By default it uses Activity's Result 105  * registry. 106  * @param callback Callback of a permission state change. 107  */ 108 @JvmOverloads 109 fun Fragment.registerForPermissionFlowRequestsResult( 110  requestPermissionsContract: RequestPermissionsContract = RequestPermissionsContract(), 111  activityResultRegistry: ActivityResultRegistry, 112  callback: ActivityResultCallback<Map<String, Boolean>> = emptyCallback(), 113 ): ActivityResultLauncher<Array<String>> = 114  registerForActivityResult( 115  requestPermissionsContract, 116  activityResultRegistry, 117  callback, 118  ) 119  120 private fun <T> emptyCallback() = ActivityResultCallback<T> {}