Coverage Summary for Class: PermissionFlowImpl (dev.shreyaspatil.permissionFlow.impl)
Class |
Method, %
|
Branch, %
|
Line, %
|
Instruction, %
|
PermissionFlowImpl |
100%
(6/6)
|
|
100%
(7/7)
|
100%
(33/33)
|
PermissionFlowImpl$Companion |
100%
(2/2)
|
100%
(2/2)
|
100%
(6/6)
|
100%
(20/20)
|
Total |
100%
(8/8)
|
100%
(2/2)
|
100%
(13/13)
|
100%
(53/53)
|
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.impl
17
18 import android.app.Application
19 import android.content.Context
20 import androidx.annotation.VisibleForTesting
21 import dev.shreyaspatil.permissionFlow.MultiplePermissionState
22 import dev.shreyaspatil.permissionFlow.PermissionFlow
23 import dev.shreyaspatil.permissionFlow.PermissionState
24 import dev.shreyaspatil.permissionFlow.watchmen.PermissionWatchmen
25 import kotlinx.coroutines.CoroutineDispatcher
26 import kotlinx.coroutines.flow.StateFlow
27
28 /**
29 * Default implementation of a [PermissionFlow]
30 */
31 internal class PermissionFlowImpl @VisibleForTesting constructor(
32 private val watchmen: PermissionWatchmen,
33 ) : PermissionFlow {
34
35 override fun getPermissionState(permission: String): StateFlow<PermissionState> {
36 return watchmen.watch(permission)
37 }
38
39 override fun getMultiplePermissionState(vararg permissions: String): StateFlow<MultiplePermissionState> {
40 return watchmen.watchMultiple(permissions.toList().toTypedArray())
41 }
42
43 override fun notifyPermissionsChanged(vararg permissions: String) {
44 watchmen.notifyPermissionsChanged(permissions.toList().toTypedArray())
45 }
46
47 override fun startListening() {
48 watchmen.wakeUp()
49 }
50
51 override fun stopListening() {
52 watchmen.sleep()
53 }
54
55 internal companion object {
56 @Volatile
57 var instance: PermissionFlowImpl? = null
58 private set
59
60 @Synchronized
61 fun init(context: Context, dispatcher: CoroutineDispatcher) {
62 if (instance == null) {
63 val watchmen = PermissionWatchmen(
64 application = context.applicationContext as Application,
65 dispatcher = dispatcher,
66 )
67 instance = PermissionFlowImpl(watchmen)
68 }
69 }
70 }
71 }