Add permission modules

This commit is contained in:
Benoit Marty 2023-03-21 18:48:38 +01:00 committed by Benoit Marty
parent 23e11836b4
commit d8b37d6ead
23 changed files with 811 additions and 15 deletions

View file

@ -0,0 +1,139 @@
/*
* Copyright (c) 2023 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.element.android.libraries.permissions.impl
import android.annotation.SuppressLint
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.saveable.rememberSaveable
import com.google.accompanist.permissions.ExperimentalPermissionsApi
import com.google.accompanist.permissions.PermissionState
import com.google.accompanist.permissions.isGranted
import com.google.accompanist.permissions.rememberPermissionState
import com.google.accompanist.permissions.shouldShowRationale
import com.squareup.anvil.annotations.ContributesBinding
import io.element.android.libraries.di.AppScope
import io.element.android.libraries.permissions.api.PermissionsEvents
import io.element.android.libraries.permissions.api.PermissionsPresenter
import io.element.android.libraries.permissions.api.PermissionsState
import kotlinx.coroutines.launch
import timber.log.Timber
import javax.inject.Inject
@ContributesBinding(AppScope::class)
class DefaultPermissionsPresenter @Inject constructor(
private val permissionsStore: PermissionsStore,
) : PermissionsPresenter {
private lateinit var permission: String
// TODO Move to the constructor.
override fun setParameter(permission: String) {
this.permission = permission
}
@OptIn(ExperimentalPermissionsApi::class)
@SuppressLint("InlinedApi")
@Composable
override fun present(): PermissionsState {
val localCoroutineScope = rememberCoroutineScope()
// To reset the store: resetStore()
val isAlreadyDenied: Boolean by permissionsStore
.isPermissionDenied(permission)
.collectAsState(initial = false)
val isAlreadyAsked: Boolean by permissionsStore
.isPermissionAsked(permission)
.collectAsState(initial = false)
var permissionState: PermissionState? = null
fun onPermissionResult(result: Boolean) {
Timber.tag("PERMISSION").w("onPermissionResult: $result")
localCoroutineScope.launch {
permissionsStore.setPermissionAsked(permission, true)
}
if (!result) {
// Should show rational true -> denied.
if (permissionState?.status?.shouldShowRationale == true) {
Timber.tag("PERMISSION").w("onPermissionResult: reset the store")
localCoroutineScope.launch {
permissionsStore.setPermissionDenied(permission, true)
}
}
}
}
permissionState = rememberPermissionState(
permission = permission,
onPermissionResult = ::onPermissionResult
)
LaunchedEffect(this) {
if (permissionState.status.isGranted) {
// User may have granted permission from the settings, to reset the store regarding this permission
permissionsStore.resetPermission(permission)
}
}
val showDialog = rememberSaveable { mutableStateOf(true) }
fun handleEvents(event: PermissionsEvents) {
Timber.tag("PERMISSION").w("New event: $event")
when (event) {
PermissionsEvents.CloseDialog -> {
showDialog.value = false
}
PermissionsEvents.OpenSystemDialog -> {
permissionState.launchPermissionRequest()
showDialog.value = false
}
PermissionsEvents.OpenSystemSettings -> {
showDialog.value = false
}
}
}
return PermissionsState(
permission = permissionState.permission,
permissionGranted = permissionState.status.isGranted,
shouldShowRationale = permissionState.status.shouldShowRationale,
showDialog = showDialog.value,
permissionAlreadyAsked = isAlreadyAsked,
permissionAlreadyDenied = isAlreadyDenied,
eventSink = ::handleEvents
).also {
Timber.tag("PERMISSION").w("New state: $it")
}
}
@Composable
private fun resetStore() {
LaunchedEffect(this@DefaultPermissionsPresenter) {
launch {
permissionsStore.resetStore()
}
}
}
}

View file

@ -0,0 +1,76 @@
/*
* Copyright (c) 2023 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.element.android.libraries.permissions.impl
import android.content.Context
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.booleanPreferencesKey
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.preferencesDataStore
import com.squareup.anvil.annotations.ContributesBinding
import io.element.android.libraries.core.bool.orFalse
import io.element.android.libraries.di.AppScope
import io.element.android.libraries.di.ApplicationContext
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import javax.inject.Inject
private val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "permissions_store")
@ContributesBinding(AppScope::class)
class DefaultPermissionsStore @Inject constructor(
@ApplicationContext context: Context,
) : PermissionsStore {
private val store = context.dataStore
override suspend fun setPermissionDenied(permission: String, value: Boolean) {
store.edit { prefs ->
prefs[getDeniedPreferenceKey(permission)] = value
}
}
override fun isPermissionDenied(permission: String): Flow<Boolean> {
return store.data.map {
it[getDeniedPreferenceKey(permission)].orFalse()
}
}
override suspend fun setPermissionAsked(permission: String, value: Boolean) {
store.edit { prefs ->
prefs[getAskedPreferenceKey(permission)] = value
}
}
override fun isPermissionAsked(permission: String): Flow<Boolean> {
return store.data.map {
it[getAskedPreferenceKey(permission)].orFalse()
}
}
override suspend fun resetPermission(permission: String) {
setPermissionAsked(permission, false)
setPermissionDenied(permission, false)
}
override suspend fun resetStore() {
store.edit { it.clear() }
}
private fun getDeniedPreferenceKey(permission: String) = booleanPreferencesKey("${permission}_denied")
private fun getAskedPreferenceKey(permission: String) = booleanPreferencesKey("${permission}_asked")
}

View file

@ -0,0 +1,32 @@
/*
* Copyright (c) 2023 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.element.android.libraries.permissions.impl
import kotlinx.coroutines.flow.Flow
interface PermissionsStore {
suspend fun setPermissionDenied(permission: String, value: Boolean)
fun isPermissionDenied(permission: String): Flow<Boolean>
suspend fun setPermissionAsked(permission: String, value: Boolean)
fun isPermissionAsked(permission: String): Flow<Boolean>
suspend fun resetPermission(permission: String)
// To debug
suspend fun resetStore()
}

View file

@ -0,0 +1,45 @@
/*
* Copyright (c) 2023 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
@file:OptIn(ExperimentalCoroutinesApi::class)
package io.element.android.libraries.permissions.impl
import app.cash.molecule.RecompositionClock
import app.cash.molecule.moleculeFlow
import app.cash.turbine.test
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runTest
import org.junit.Test
const val A_PERMISSION = "A_PERMISSION"
class DefaultPermissionsPresenterTest {
@Test
fun `present - initial state`() = runTest {
val presenter = DefaultPermissionsPresenter(
InMemoryPermissionsStore()
)
moleculeFlow(RecompositionClock.Immediate) {
presenter.setParameter(A_PERMISSION)
presenter.present()
}.test {
val initialState = awaitItem()
assertThat(initialState.showDialog).isTrue()
}
}
}

View file

@ -0,0 +1,48 @@
/*
* Copyright (c) 2023 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.element.android.libraries.permissions.impl
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
class InMemoryPermissionsStore(
permissionDenied: Boolean = false,
permissionAsked: Boolean = false,
) : PermissionsStore {
private val permissionDeniedFlow = MutableStateFlow(permissionDenied)
private val permissionAskedFlow = MutableStateFlow(permissionAsked)
override suspend fun setPermissionDenied(permission: String, value: Boolean) {
permissionDeniedFlow.value = value
}
override fun isPermissionDenied(permission: String): Flow<Boolean> = permissionDeniedFlow
override suspend fun setPermissionAsked(permission: String, value: Boolean) {
permissionAskedFlow.value
}
override fun isPermissionAsked(permission: String): Flow<Boolean> = permissionAskedFlow
override suspend fun resetPermission(permission: String) {
setPermissionAsked(permission, false)
setPermissionDenied(permission, false)
}
override suspend fun resetStore() {
}
}