Coverage Summary for Class: RequestPermissionsContract (dev.shreyaspatil.permissionFlow.contract)

Class Class, % Method, % Branch, % Line, % Instruction, %
RequestPermissionsContract 100% (1/1) 100% (3/3) 100% (8/8) 100% (50/50)


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 package dev.shreyaspatil.permissionFlow.contract 17  18 import android.content.Context 19 import android.content.Intent 20 import androidx.activity.ComponentActivity 21 import androidx.activity.result.ActivityResultLauncher 22 import androidx.activity.result.contract.ActivityResultContract 23 import androidx.activity.result.contract.ActivityResultContracts 24 import androidx.activity.result.contract.ActivityResultContracts.RequestMultiplePermissions 25 import dev.shreyaspatil.permissionFlow.PermissionFlow 26 import dev.shreyaspatil.permissionFlow.utils.registerForPermissionFlowRequestsResult 27  28 /** 29  * An [ActivityResultContract] which delegates request and response to 30  * [ActivityResultContracts.RequestMultiplePermissions] and silently notifies [PermissionFlow] 31  * regarding state change of a permissions which are requested through [ActivityResultLauncher]. 32  * 33  * Refer to [ComponentActivity.registerForPermissionFlowRequestsResult] for actual usage. 34  */ 35  36 class RequestPermissionsContract( 37  private val contract: RequestMultiplePermissions = RequestMultiplePermissions(), 38  private val permissionFlow: PermissionFlow = PermissionFlow.getInstance(), 39 ) : ActivityResultContract<Array<String>, Map<String, Boolean>>() { 40  41  override fun createIntent(context: Context, input: Array<String>): Intent { 42  return contract.createIntent(context, input ?: emptyArray()) 43  } 44  45  override fun parseResult(resultCode: Int, intent: Intent?): Map<String, Boolean> { 46  return contract.parseResult(resultCode, intent).also { 47  val permissions = it.keys.filterNotNull().toTypedArray() 48  permissionFlow.notifyPermissionsChanged(*permissions) 49  } 50  } 51 }