Skip to content

Commit

Permalink
Merge branch 'newver'
Browse files Browse the repository at this point in the history
* Added possibility to replace only onGranted/onDenied for an entry
  • Loading branch information
lorenzofelletti committed Dec 23, 2022
2 parents 128aaa2 + 4169355 commit 20049d9
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package com.lorenzofelletti.permissions.dispatcher

import android.content.pm.PackageManager
import com.lorenzofelletti.permissions.PermissionManager
import com.lorenzofelletti.permissions.dispatcher.DispatcherEntry.Companion.doOnDenied
import com.lorenzofelletti.permissions.dispatcher.DispatcherEntry.Companion.doOnGranted
import com.lorenzofelletti.permissions.dispatcher.dsl.PermissionDispatcher
import com.lorenzofelletti.permissions.dispatcher.dsl.PermissionDispatcherDsl

Expand Down Expand Up @@ -110,14 +112,42 @@ class RequestResultsDispatcher(private val manager: PermissionManager) : Permiss
entries[entry.requestCode] = entry
}

/**
* Replaces an entry's onGranted action in the dispatcher if present, otherwise it does nothing.
*
* @param requestCode The request code associated to the entry
* @param onGranted The new onGranted action
*/
@PermissionDispatcherDsl
fun RequestResultsDispatcher.replaceEntryOnGranted(
requestCode: Int,
onGranted: () -> Unit
) {
entries[requestCode]?.doOnGranted(onGranted)
}

/**
* Replaces an entry's onDenied action in the dispatcher if present, otherwise it does nothing.
*
* @param requestCode The request code associated to the entry
* @param onDenied The new onDenied action
*/
@PermissionDispatcherDsl
fun RequestResultsDispatcher.replaceEntryOnDenied(requestCode: Int, onDenied: () -> Unit) {
entries[requestCode]?.doOnDenied(onDenied)
}

/**
* Replaces an entry in the dispatcher if present, otherwise adds it.
*
* @param requestCode The request code associated to the entry
* @param init A lambda that initializes the entry
*/
@PermissionDispatcherDsl
fun RequestResultsDispatcher.replaceEntry(requestCode: Int, init: DispatcherEntry.() -> Unit) {
fun RequestResultsDispatcher.replaceEntry(
requestCode: Int,
init: DispatcherEntry.() -> Unit
) {
entries[requestCode] = DispatcherEntry(manager, requestCode).apply(init)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ import android.app.Activity
import android.content.pm.PackageManager
import com.lorenzofelletti.permissions.dispatcher.DispatcherEntry
import com.lorenzofelletti.permissions.dispatcher.DispatcherEntry.Companion.checkPermissions
import com.lorenzofelletti.permissions.dispatcher.DispatcherEntry.Companion.doOnDenied
import com.lorenzofelletti.permissions.dispatcher.DispatcherEntry.Companion.doOnGranted
import com.lorenzofelletti.permissions.dispatcher.RequestResultsDispatcher
import com.lorenzofelletti.permissions.dispatcher.RequestResultsDispatcher.Companion.addEntry
import com.lorenzofelletti.permissions.dispatcher.RequestResultsDispatcher.Companion.removeEntry
import com.lorenzofelletti.permissions.dispatcher.RequestResultsDispatcher.Companion.replaceEntry
import com.lorenzofelletti.permissions.dispatcher.RequestResultsDispatcher.Companion.replaceEntryOnDenied
import com.lorenzofelletti.permissions.dispatcher.RequestResultsDispatcher.Companion.replaceEntryOnGranted
import com.lorenzofelletti.permissions.dispatcher.RequestResultsDispatcher.Companion.withRequestCode
import org.junit.Before
import org.junit.Test
Expand All @@ -19,16 +22,13 @@ import org.mockito.junit.MockitoJUnitRunner
@RunWith(MockitoJUnitRunner::class)
class RequestResultsDispatcherTest {
private lateinit var permissionManager: PermissionManager
private lateinit var dispatcher: RequestResultsDispatcher

@Before
fun setUp() {
val mockActivity = mock(Activity::class.java)
permissionManager = PermissionManager(mockActivity)

permissionManager.buildRequestResultsDispatcher {}

dispatcher = permissionManager.dispatcher
}

@Test
Expand All @@ -45,7 +45,7 @@ class RequestResultsDispatcherTest {
fun `test add entry declarative`() {
permissionManager.dispatcher.addEntry(0) {}

assert(dispatcher.entries.size == 1)
assert(permissionManager.dispatcher.entries.size == 1)
}

@Test
Expand Down Expand Up @@ -96,15 +96,108 @@ class RequestResultsDispatcherTest {

entry.doOnGranted { testVar = 1 }

dispatcher.addEntry(entry)
dispatcher.dispatchAction(
permissionManager.dispatcher.addEntry(entry)
permissionManager.dispatcher.dispatchAction(
0,
arrayOf(PackageManager.PERMISSION_GRANTED).toIntArray()
)?.invoke()

assert(testVar == 1)
}

@Test
fun `test dispatch on denied`() {
var testVar = 0
val entry = DispatcherEntry(permissionManager, 0)

entry.doOnDenied { testVar = 1 }

permissionManager.dispatcher.addEntry(entry)
permissionManager.dispatcher.dispatchAction(
0,
arrayOf(PackageManager.PERMISSION_DENIED).toIntArray()
)?.invoke()

assert(testVar == 1)
}

@Test
fun `test replace entry`() {
var testVar = 0

val permissionManager = permissionManagerWithEntryZero()

permissionManager.dispatcher.replaceEntry(0) {
doOnGranted { testVar = 2 }
}

permissionManager.dispatcher.dispatchAction(
0,
intArrayOf(PackageManager.PERMISSION_GRANTED)
)?.invoke()

assert(testVar == 2)
}

@Test
fun `test replace entry by passing new DispatcherEntry object`() {
val permissionManager = permissionManagerWithEntryZero()

var testVar = 0

// creates a new entry with the same request code
val newEntry = DispatcherEntry(permissionManager, 0)
newEntry.doOnGranted { testVar = 2 }

// replaces the old entry with the new one
permissionManager.dispatcher.replaceEntry(newEntry)

// dispatches the action
permissionManager.dispatcher.dispatchAction(
0,
intArrayOf(PackageManager.PERMISSION_GRANTED)
)?.invoke()

// checks if the new entry has been called
assert(testVar == 2)
}

@Test
fun `test replace entry's on granted`() {
val permissionManager = permissionManagerWithEntryZero()

var testVar = 0

permissionManager.dispatcher.replaceEntryOnGranted(0) {
testVar = 2
}

permissionManager.dispatcher.dispatchAction(
0,
intArrayOf(PackageManager.PERMISSION_GRANTED)
)?.invoke()

assert(testVar == 2)
}

@Test
fun `replace entry's on denied`() {
val permissionManager = permissionManagerWithEntryZero()

var testVar = 0

permissionManager.dispatcher.replaceEntryOnDenied(0) {
testVar = 2
}

permissionManager.dispatcher.dispatchAction(
0,
intArrayOf(PackageManager.PERMISSION_DENIED)
)?.invoke()

assert(testVar == 2)
}

private fun permissionManagerWithEntryZero(): PermissionManager {
val permissionManager = PermissionManager(mock(Activity::class.java))
permissionManager.buildRequestResultsDispatcher {
Expand Down

0 comments on commit 20049d9

Please sign in to comment.