Skip to content

Commit ce9e10a

Browse files
committed
#76 (revert) Remove event getting methods
1 parent 91f7c6c commit ce9e10a

File tree

5 files changed

+0
-146
lines changed

5 files changed

+0
-146
lines changed

permission-flow/src/main/java/dev/shreyaspatil/permissionFlow/PermissionFlow.kt

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import dev.shreyaspatil.permissionFlow.PermissionFlow.Companion.init
2121
import dev.shreyaspatil.permissionFlow.impl.PermissionFlowImpl
2222
import kotlinx.coroutines.CoroutineDispatcher
2323
import kotlinx.coroutines.DelicateCoroutinesApi
24-
import kotlinx.coroutines.flow.Flow
2524
import kotlinx.coroutines.flow.StateFlow
2625
import kotlinx.coroutines.newFixedThreadPoolContext
2726

@@ -108,31 +107,6 @@ interface PermissionFlow {
108107
*/
109108
fun getPermissionState(permission: String): StateFlow<PermissionState>
110109

111-
/**
112-
* Returns [Flow] for a given [permission] events.
113-
*
114-
* Flow will emit [PermissionState] whenever the state of permission is changed after collecting
115-
* this flow. Initial state of permission won't be emitted.
116-
*
117-
* @param permission Unique permission identity (for e.g.
118-
* [android.Manifest.permission.READ_CONTACTS])
119-
*
120-
* Example:
121-
* ```
122-
* permissionFlow.getPermissionEvent(android.Manifest.permission.READ_CONTACTS)
123-
* .collect { state ->
124-
* if (state.isGranted) {
125-
* // Do something
126-
* } else {
127-
* if (state.isRationaleRequired) {
128-
* // Do something
129-
* }
130-
* }
131-
* }
132-
* ```
133-
*/
134-
fun getPermissionEvent(permission: String): Flow<PermissionState>
135-
136110
/**
137111
* Returns [StateFlow] of a combining state for [permissions]
138112
*

permission-flow/src/main/java/dev/shreyaspatil/permissionFlow/impl/PermissionFlowImpl.kt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import dev.shreyaspatil.permissionFlow.PermissionState
2424
import dev.shreyaspatil.permissionFlow.internal.ApplicationStateMonitor
2525
import dev.shreyaspatil.permissionFlow.watchmen.PermissionWatchmen
2626
import kotlinx.coroutines.CoroutineDispatcher
27-
import kotlinx.coroutines.flow.Flow
2827
import kotlinx.coroutines.flow.StateFlow
2928

3029
/** Default implementation of a [PermissionFlow] */
@@ -38,10 +37,6 @@ constructor(
3837
return watchmen.watchState(permission)
3938
}
4039

41-
override fun getPermissionEvent(permission: String): Flow<PermissionState> {
42-
return watchmen.watchStateEvents(permission)
43-
}
44-
4540
override fun getMultiplePermissionState(
4641
vararg permissions: String
4742
): StateFlow<MultiplePermissionState> {

permission-flow/src/main/java/dev/shreyaspatil/permissionFlow/watchmen/PermissionWatchmen.kt

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,13 @@ import dev.shreyaspatil.permissionFlow.utils.stateFlow.combineStates
2222
import kotlinx.coroutines.CoroutineDispatcher
2323
import kotlinx.coroutines.CoroutineName
2424
import kotlinx.coroutines.CoroutineScope
25-
import kotlinx.coroutines.FlowPreview
2625
import kotlinx.coroutines.Job
2726
import kotlinx.coroutines.SupervisorJob
2827
import kotlinx.coroutines.cancelChildren
29-
import kotlinx.coroutines.flow.Flow
3028
import kotlinx.coroutines.flow.MutableSharedFlow
3129
import kotlinx.coroutines.flow.MutableStateFlow
3230
import kotlinx.coroutines.flow.StateFlow
3331
import kotlinx.coroutines.flow.asStateFlow
34-
import kotlinx.coroutines.flow.filter
3532
import kotlinx.coroutines.flow.launchIn
3633
import kotlinx.coroutines.flow.onEach
3734
import kotlinx.coroutines.launch
@@ -72,12 +69,6 @@ internal class PermissionWatchmen(
7269
return combineStates(*permissionStates) { MultiplePermissionState(it.toList()) }
7370
}
7471

75-
fun watchStateEvents(permission: String): Flow<PermissionState> {
76-
// Add permission to state watchlist too
77-
watchState(permission)
78-
return getPermissionEvent(permission)
79-
}
80-
8172
fun notifyPermissionsChanged(permissions: Array<String>) {
8273
watchmenScope.launch {
8374
permissions.forEach { permission ->
@@ -111,9 +102,6 @@ internal class PermissionWatchmen(
111102
.state
112103
}
113104

114-
private fun getPermissionEvent(permission: String) =
115-
permissionEvents.filter { it.permission == permission }
116-
117105
/** Watches for the permission events and updates appropriate state holders of permission */
118106
private fun watchPermissionEvents() {
119107
if (watchEventsJob != null && watchEventsJob?.isActive == true) return

permission-flow/src/test/java/dev/shreyaspatil/permissionFlow/impl/PermissionFlowImplTest.kt

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import io.mockk.every
2222
import io.mockk.mockk
2323
import io.mockk.verify
2424
import kotlinx.coroutines.flow.MutableStateFlow
25-
import kotlinx.coroutines.flow.flow
2625
import org.junit.Assert.assertEquals
2726
import org.junit.Before
2827
import org.junit.Test
@@ -50,19 +49,6 @@ class PermissionFlowImplTest {
5049
assertEquals(expectedFlow, actualFlow)
5150
}
5251

53-
@Test
54-
fun testGetPermissionEvent() {
55-
// Given: Permission flow
56-
val expectedFlow = flow<PermissionState> {}
57-
every { watchmen.watchStateEvents("A") } returns expectedFlow
58-
59-
// When: Flow for any permission is retrieved
60-
val actualFlow = permissionFlow.getPermissionEvent("A")
61-
62-
// Then: Correct flow should be returned
63-
assertEquals(expectedFlow, actualFlow)
64-
}
65-
6652
@Test
6753
fun testGetMultiplePermissionState() {
6854
// Given: Permission flow

permission-flow/src/test/java/dev/shreyaspatil/permissionFlow/watchmen/PermissionWatchmenTest.kt

Lines changed: 0 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,8 @@ import io.mockk.every
2121
import io.mockk.mockk
2222
import io.mockk.verify
2323
import kotlinx.coroutines.ExperimentalCoroutinesApi
24-
import kotlinx.coroutines.async
2524
import kotlinx.coroutines.channels.BufferOverflow
2625
import kotlinx.coroutines.flow.MutableSharedFlow
27-
import kotlinx.coroutines.flow.first
2826
import kotlinx.coroutines.test.StandardTestDispatcher
2927
import kotlinx.coroutines.test.TestScope
3028
import kotlinx.coroutines.test.advanceUntilIdle
@@ -73,20 +71,6 @@ class PermissionWatchmenTest {
7371
assertEquals(1, foregroundEvents.subscriptionCount.value)
7472
}
7573

76-
@Test
77-
fun shouldWakeUpAndReturnFlow_whenWatchPermissionEventForTheFirstTime() {
78-
// Given: Permission
79-
val permission = "permission"
80-
81-
// When: Starts watching permission for the first time.
82-
val flow = watchmen.watchStateEvents(permission)
83-
84-
// Then: Should start watching activity foreground events.
85-
dispatcher.scheduler.runCurrent()
86-
verify(exactly = 1) { applicationStateMonitor.activityForegroundEvents }
87-
assertEquals(1, foregroundEvents.subscriptionCount.value)
88-
}
89-
9074
@Test
9175
fun shouldWakeUpAndReturnFlow_whenWatchMultiplePermissionForTheFirstTime() {
9276
// Given: Multiple Permission
@@ -152,44 +136,6 @@ class PermissionWatchmenTest {
152136
assertFalse(state.value.isGranted)
153137
}
154138

155-
@Test
156-
fun shouldNotEmitCurrentState_whenEventIsWatched() = runTest {
157-
// Given: Watching a permission flow as event and permission is not granted
158-
val permission = "permission"
159-
mockPermissions(permission to false)
160-
watchmen.notifyPermissionsChanged(permissions = arrayOf(permission))
161-
162-
// When: Watching state event
163-
val state = async { watchmen.watchStateEvents(permission).first() }
164-
advanceUntilIdle()
165-
166-
// Then: Event should not be emitted with current value
167-
assertTrue(state.isActive)
168-
169-
// When: Change in state is notified for the same permission
170-
watchmen.notifyPermissionsChanged(permissions = arrayOf(permission))
171-
172-
// Then: Current value of flow should be false i.e. Not granted
173-
assertEquals(permission, state.await().permission)
174-
assertFalse(state.await().isGranted)
175-
}
176-
177-
@Test
178-
fun shouldEmitStateEvent_whenPermissionChangesAreNotified() = runTest {
179-
// Given: Watching a permission flow as event
180-
val permission = "permission"
181-
val event = watchmen.watchStateEvents(permission)
182-
183-
// When: Change in state is notified for the same permission
184-
mockPermissions(permission to false)
185-
watchmen.notifyPermissionsChanged(permissions = arrayOf(permission))
186-
187-
// Then: Current value of flow should be false i.e. Not granted
188-
val state = event.first()
189-
assertEquals(permission, state.permission)
190-
assertFalse(state.isGranted)
191-
}
192-
193139
@Test
194140
fun shouldUpdateMultiplePermissionFlowState_whenPermissionChangesAreNotified() {
195141
// Given: Watching multiple permission state
@@ -225,24 +171,6 @@ class PermissionWatchmenTest {
225171
assertFalse(flow.value.isGranted)
226172
}
227173

228-
@Test
229-
fun shouldEmitPermissionFlowStateEvent_whenWatchmenWakesAfterSleeping() = runTest {
230-
// Given: Watching a permission
231-
val permission = "permission"
232-
mockPermissions(permission to true)
233-
val state = async { watchmen.watchStateEvents(permission).first() }
234-
advanceUntilIdle()
235-
236-
// When: Watchmen sleeps, permission state changes and watchmen wakes after that
237-
watchmen.sleep()
238-
mockPermissions(permission to false)
239-
watchmen.wakeUp()
240-
advanceUntilIdle()
241-
242-
// Then: Permission state should be get updated
243-
assertFalse(state.await().isGranted)
244-
}
245-
246174
@Test
247175
fun shouldUpdateMultiplePermissionFlowState_whenWatchmenWakesAfterSleeping() {
248176
// Given: Watching multiple permissions
@@ -278,23 +206,6 @@ class PermissionWatchmenTest {
278206
assertTrue(flow.value.isGranted)
279207
}
280208

281-
@Test
282-
fun shouldEmitStateEvent_whenPermissionChangesAreNotifiedEvenIfWatchmenIsSleeping() = runTest {
283-
// Given: Watching a permission flow events and watchmen is sleeping
284-
val permission = "permission"
285-
mockPermissions(permission to true)
286-
val state = async { watchmen.watchStateEvents(permission).first() }
287-
watchmen.sleep()
288-
289-
// When: Change in state is notified for the same permission
290-
mockPermissions(permission to false)
291-
watchmen.notifyPermissionsChanged(permissions = arrayOf(permission))
292-
advanceUntilIdle()
293-
294-
// Then: Current value of flow should be changed
295-
assertFalse(state.await().isGranted)
296-
}
297-
298209
@Test
299210
fun shouldNotUpdateMultipleFlowState_whenPermissionChangesAreNotifiedAndWatchmenIsSleeping() {
300211
// Given: Watching a permission flow and watchmen is sleeping

0 commit comments

Comments
 (0)