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

Class Class, % Method, % Branch, % Line, % Instruction, %
PermissionResultLauncher 100% (1/1) 100% (3/3) 76.5% (13/17) 66.7% (62/93)


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  * ``` 35  * class MyActivity: AppCompatActivity() { 36  * private val permissionLauncher = registerForPermissionFlowRequestsResult() 37  * 38  * fun askContactPermission() { 39  * permissionLauncher.launch(android.Manifest.permission.READ_CONTACTS) 40  * } 41  * } 42  * ``` 43  * 44  * @param requestPermissionsContract A contract specifying permission request and result. 45  * @param activityResultRegistry Activity result registry. By default it uses Activity's Result 46  * registry. 47  * @param callback Callback of a permission state change. 48  */ 49 @JvmOverloads 50 fun ComponentActivity.registerForPermissionFlowRequestsResult( 51  requestPermissionsContract: RequestPermissionsContract = RequestPermissionsContract(), 52  activityResultRegistry: ActivityResultRegistry = getActivityResultRegistry(), 53  callback: ActivityResultCallback<Map<String, Boolean>> = emptyCallback() 54 ): ActivityResultLauncher<Array<String>> = 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  * ``` 67  * class MyFragment: Fragment() { 68  * private val permissionLauncher = registerForPermissionFlowRequestsResult() 69  * 70  * fun askContactPermission() { 71  * permissionLauncher.launch(android.Manifest.permission.READ_CONTACTS) 72  * } 73  * } 74  * ``` 75  * 76  * @param requestPermissionsContract A contract specifying permission request and result. 77  * @param activityResultRegistry Activity result registry. By default it uses Activity's Result 78  * registry. 79  * @param callback Callback of a permission state change. 80  */ 81 @JvmOverloads 82 fun Fragment.registerForPermissionFlowRequestsResult( 83  requestPermissionsContract: RequestPermissionsContract = RequestPermissionsContract(), 84  activityResultRegistry: ActivityResultRegistry = requireActivity().activityResultRegistry, 85  callback: ActivityResultCallback<Map<String, Boolean>> = emptyCallback() 86 ): ActivityResultLauncher<Array<String>> = registerForActivityResult( 87  requestPermissionsContract, 88  activityResultRegistry, 89  callback 90 ) 91  92 private fun <T> emptyCallback() = ActivityResultCallback<T> {}