-
Notifications
You must be signed in to change notification settings - Fork 0
feat: Add 'PermissionManager' module #745
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e194904
feat: Add 'PermissionManager' module
NicolasBourdin88 f8e9e94
refactor: Apply suggestion from code review
NicolasBourdin88 0d12ef0
feat: Add 'WRITE_EXTERNAL_STORAGE' permission in 'PermissionManager'
NicolasBourdin88 9dccbb2
feat: Add permission key to recreate new instance on permission change
NicolasBourdin88 0aa1f2c
refactor: If permission already granted do nothing
NicolasBourdin88 3078686
refactor: Find better name
NicolasBourdin88 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| import org.jetbrains.kotlin.gradle.dsl.JvmTarget | ||
|
|
||
| plugins { | ||
| alias(core.plugins.android.library) | ||
| alias(core.plugins.kotlin.android) | ||
| alias(core.plugins.compose.compiler) | ||
| } | ||
|
|
||
| val coreCompileSdk: Int by rootProject.extra | ||
| val coreMinSdk: Int by rootProject.extra | ||
| val javaVersion: JavaVersion by rootProject.extra | ||
|
|
||
| android { | ||
| namespace = "com.infomaniak.core.permissionmanager" | ||
| compileSdk = coreCompileSdk | ||
|
|
||
| defaultConfig { | ||
| minSdk = coreMinSdk | ||
| } | ||
|
|
||
| compileOptions { | ||
| sourceCompatibility = javaVersion | ||
| targetCompatibility = javaVersion | ||
| } | ||
|
|
||
| buildFeatures { | ||
| compose = true | ||
| } | ||
|
|
||
| kotlin { | ||
| compilerOptions { | ||
| jvmTarget.set(JvmTarget.fromTarget(javaVersion.toString())) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| dependencies { | ||
| implementation(platform(core.compose.bom)) | ||
| implementation(core.compose.runtime) | ||
| implementation(core.compose.ui) | ||
| implementation(core.accompanist.permissions) | ||
| } |
45 changes: 45 additions & 0 deletions
45
...onManager/src/main/kotlin/com/infomaniak/core/permissionmanager/PermissionManagerState.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| /* | ||
| * Infomaniak Core - Android | ||
| * Copyright (C) 2026 Infomaniak Network SA | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
| package com.infomaniak.core.permissionmanager | ||
|
|
||
| import androidx.compose.runtime.Composable | ||
| import androidx.compose.runtime.Stable | ||
| import androidx.compose.runtime.remember | ||
| import com.google.accompanist.permissions.ExperimentalPermissionsApi | ||
| import com.google.accompanist.permissions.rememberPermissionState | ||
|
|
||
| @OptIn(ExperimentalPermissionsApi::class) | ||
| @Composable | ||
| fun rememberPermissionManagerState(permissionType: PermissionType): PermissionManagerState { | ||
| val permission = permissionType.permission | ||
|
|
||
| return if (permission == null) { | ||
| remember { UnsupportedApiPermissionManagerState } | ||
| } else { | ||
| val permissionState = rememberPermissionState(permission) | ||
| remember(permission) { SupportedApiPermissionManagerState(permissionState) } | ||
| } | ||
| } | ||
|
|
||
| @Stable | ||
| sealed interface PermissionManagerState { | ||
| fun launchPermissionRequestIfNeeded() | ||
|
|
||
| @Composable | ||
| fun dropIfDenied(action: () -> Unit): () -> Unit | ||
| } | ||
26 changes: 26 additions & 0 deletions
26
PermissionManager/src/main/kotlin/com/infomaniak/core/permissionmanager/PermissionType.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| /* | ||
| * Infomaniak Core - Android | ||
| * Copyright (C) 2026 Infomaniak Network SA | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
| package com.infomaniak.core.permissionmanager | ||
|
|
||
| import android.Manifest | ||
| import android.os.Build.VERSION.SDK_INT | ||
|
|
||
| enum class PermissionType(val permission: String?) { | ||
| Notification(permission = if (SDK_INT >= 33) Manifest.permission.POST_NOTIFICATIONS else null), | ||
| WriteExternalStorage(permission = if (SDK_INT >= 29) null else Manifest.permission.WRITE_EXTERNAL_STORAGE), | ||
| } | ||
|
NicolasBourdin88 marked this conversation as resolved.
|
||
62 changes: 62 additions & 0 deletions
62
...c/main/kotlin/com/infomaniak/core/permissionmanager/SupportedApiPermissionManagerState.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| /* | ||
| * Infomaniak Core - Android | ||
| * Copyright (C) 2026 Infomaniak Network SA | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
| package com.infomaniak.core.permissionmanager | ||
|
|
||
| import androidx.compose.runtime.Composable | ||
| import androidx.compose.runtime.LaunchedEffect | ||
| import androidx.compose.runtime.Stable | ||
| import androidx.compose.runtime.remember | ||
| import androidx.compose.runtime.snapshotFlow | ||
| import com.google.accompanist.permissions.ExperimentalPermissionsApi | ||
| import com.google.accompanist.permissions.PermissionState | ||
| import com.google.accompanist.permissions.isGranted | ||
| import kotlinx.coroutines.CompletableJob | ||
| import kotlinx.coroutines.Job | ||
| import kotlinx.coroutines.flow.first | ||
|
|
||
| @Stable | ||
| @OptIn(ExperimentalPermissionsApi::class) | ||
| internal class SupportedApiPermissionManagerState( | ||
| private val permissionState: PermissionState, | ||
| ) : PermissionManagerState { | ||
|
NicolasBourdin88 marked this conversation as resolved.
|
||
| override fun launchPermissionRequestIfNeeded() { | ||
| if (permissionState.status.isGranted) return | ||
| permissionState.launchPermissionRequest() | ||
| } | ||
|
|
||
| @Composable | ||
| override fun dropIfDenied(action: () -> Unit): () -> Unit = with(permissionState) { | ||
| val isGrantedFlow = remember(permission) { snapshotFlow { status.isGranted } } | ||
| val actionFired: CompletableJob = remember(permission) { Job() } | ||
|
|
||
| LaunchedEffect(permission) { | ||
| actionFired.join() | ||
| isGrantedFlow.first { it } | ||
| action() | ||
| } | ||
|
NicolasBourdin88 marked this conversation as resolved.
|
||
|
|
||
| return fun() { | ||
| if (status.isGranted) { | ||
| action() | ||
| } else { | ||
| launchPermissionRequest() | ||
| actionFired.complete() | ||
| } | ||
| } | ||
|
NicolasBourdin88 marked this conversation as resolved.
|
||
| } | ||
|
NicolasBourdin88 marked this conversation as resolved.
NicolasBourdin88 marked this conversation as resolved.
|
||
| } | ||
29 changes: 29 additions & 0 deletions
29
...main/kotlin/com/infomaniak/core/permissionmanager/UnsupportedApiPermissionManagerState.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| /* | ||
| * Infomaniak Core - Android | ||
| * Copyright (C) 2026 Infomaniak Network SA | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
| package com.infomaniak.core.permissionmanager | ||
|
|
||
| import androidx.compose.runtime.Composable | ||
| import androidx.compose.runtime.Stable | ||
|
|
||
| @Stable | ||
| internal object UnsupportedApiPermissionManagerState : PermissionManagerState { | ||
| override fun launchPermissionRequestIfNeeded() = Unit | ||
|
|
||
| @Composable | ||
| override fun dropIfDenied(action: () -> Unit): () -> Unit = action | ||
| } |
49 changes: 49 additions & 0 deletions
49
...kotlin/com/infomaniak/core/permissionmanager/rationale/RationalePermissionManagerState.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| /* | ||
| * Infomaniak Core - Android | ||
| * Copyright (C) 2026 Infomaniak Network SA | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
| package com.infomaniak.core.permissionmanager.rationale | ||
|
|
||
| import androidx.compose.runtime.Composable | ||
| import androidx.compose.runtime.Stable | ||
| import androidx.compose.runtime.mutableStateOf | ||
| import androidx.compose.runtime.remember | ||
| import androidx.compose.runtime.saveable.rememberSaveable | ||
| import com.google.accompanist.permissions.ExperimentalPermissionsApi | ||
| import com.google.accompanist.permissions.rememberPermissionState | ||
| import com.infomaniak.core.permissionmanager.PermissionType | ||
|
|
||
| @OptIn(ExperimentalPermissionsApi::class) | ||
| @Composable | ||
| fun rememberRationalePermissionManagerState(permissionType: PermissionType): RationalePermissionManagerState { | ||
| val permission = permissionType.permission | ||
|
|
||
| return if (permission == null) { | ||
| remember { UnsupportedApiRationalePermissionManagerState } | ||
| } else { | ||
| val permissionState = rememberPermissionState(permission) | ||
| val shouldShowRationale = rememberSaveable(permission) { mutableStateOf(false) } | ||
| remember(permission) { SupportedApiRationalePermissionManagerState(permissionState, shouldShowRationale) } | ||
| } | ||
| } | ||
|
|
||
| @Stable | ||
| sealed interface RationalePermissionManagerState { | ||
| val shouldShowRationale: Boolean | ||
|
|
||
| fun requestPermissionOrShowRationaleIfNeeded() | ||
| fun dismissAndAskPermission() | ||
| } |
50 changes: 50 additions & 0 deletions
50
...nfomaniak/core/permissionmanager/rationale/SupportedApiRationalePermissionManagerState.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| /* | ||
| * Infomaniak Core - Android | ||
| * Copyright (C) 2026 Infomaniak Network SA | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
| package com.infomaniak.core.permissionmanager.rationale | ||
|
|
||
| import androidx.compose.runtime.MutableState | ||
| import androidx.compose.runtime.Stable | ||
| import androidx.compose.runtime.getValue | ||
| import com.google.accompanist.permissions.ExperimentalPermissionsApi | ||
| import com.google.accompanist.permissions.PermissionState | ||
| import com.google.accompanist.permissions.isGranted | ||
| import com.google.accompanist.permissions.shouldShowRationale | ||
|
|
||
| @Stable | ||
| @OptIn(ExperimentalPermissionsApi::class) | ||
| internal class SupportedApiRationalePermissionManagerState( | ||
| private val permissionState: PermissionState, | ||
| private val _shouldShowRationale: MutableState<Boolean>, | ||
| ) : RationalePermissionManagerState { | ||
| override val shouldShowRationale: Boolean by _shouldShowRationale | ||
|
|
||
| override fun requestPermissionOrShowRationaleIfNeeded() { | ||
| if (permissionState.status.isGranted) return | ||
|
|
||
| if (permissionState.status.shouldShowRationale) { | ||
| _shouldShowRationale.value = true | ||
| } else { | ||
| permissionState.launchPermissionRequest() | ||
| } | ||
| } | ||
|
|
||
| override fun dismissAndAskPermission() { | ||
| _shouldShowRationale.value = false | ||
| permissionState.launchPermissionRequest() | ||
| } | ||
| } |
28 changes: 28 additions & 0 deletions
28
...omaniak/core/permissionmanager/rationale/UnsupportedApiRationalePermissionManagerState.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| /* | ||
| * Infomaniak Core - Android | ||
| * Copyright (C) 2026 Infomaniak Network SA | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
| package com.infomaniak.core.permissionmanager.rationale | ||
|
|
||
| import androidx.compose.runtime.Stable | ||
|
|
||
| @Stable | ||
| internal object UnsupportedApiRationalePermissionManagerState : RationalePermissionManagerState { | ||
| override val shouldShowRationale: Boolean get() = false | ||
|
|
||
| override fun requestPermissionOrShowRationaleIfNeeded() = Unit | ||
| override fun dismissAndAskPermission() = Unit | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.