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