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%
(8/8)
|
100%
(25/25)
|
Total |
100%
(8/8)
|
100%
(2/2)
|
100%
(15/15)
|
100%
(58/58)
|
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.internal.ApplicationStateMonitor
25 import dev.shreyaspatil.permissionFlow.watchmen.PermissionWatchmen
26 import kotlinx.coroutines.CoroutineDispatcher
27 import kotlinx.coroutines.flow.StateFlow
28
29 /** Default implementation of a [PermissionFlow] */
30 internal class PermissionFlowImpl
31 @VisibleForTesting
32 constructor(
33 private val watchmen: PermissionWatchmen,
34 ) : PermissionFlow {
35
36 override fun getPermissionState(permission: String): StateFlow<PermissionState> {
37 return watchmen.watchState(permission)
38 }
39
40 override fun getMultiplePermissionState(
41 vararg permissions: String
42 ): StateFlow<MultiplePermissionState> {
43 return watchmen.watchMultipleState(permissions.toList().toTypedArray())
44 }
45
46 override fun notifyPermissionsChanged(vararg permissions: String) {
47 watchmen.notifyPermissionsChanged(permissions.toList().toTypedArray())
48 }
49
50 override fun startListening() {
51 watchmen.wakeUp()
52 }
53
54 override fun stopListening() {
55 watchmen.sleep()
56 }
57
58 internal companion object {
59 @Volatile
60 var instance: PermissionFlowImpl? = null
61 private set
62
63 @Synchronized
64 fun init(context: Context, dispatcher: CoroutineDispatcher) {
65 if (instance == null) {
66 val monitor = ApplicationStateMonitor(context.applicationContext as Application)
67 val watchmen =
68 PermissionWatchmen(
69 appStateMonitor = monitor,
70 dispatcher = dispatcher,
71 )
72 instance = PermissionFlowImpl(watchmen)
73 }
74 }
75 }
76 }