Skip to content

Commit

Permalink
Merge pull request #152 from MohamedRejeb/0.5.x
Browse files Browse the repository at this point in the history
Fix Permission.Notification crash in IOS
  • Loading branch information
MohamedRejeb authored Aug 20, 2024
2 parents 37605fe + 68bc77f commit f73de15
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 84 deletions.
1 change: 0 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,4 @@ plugins {
alias(libs.plugins.kotlinSerialization).apply(false)
alias(libs.plugins.composeCompiler).apply(false)
alias(libs.plugins.composeMultiplatform).apply(false)
alias(libs.plugins.conventionPlugin).apply(false)
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ private fun rememberMutablePermissionsState(
// Create list of MutablePermissionState for each permission
val context = LocalContext.current
val activity = context.findActivity()
val mutablePermissions: List<MutablePermissionState> = remember(permissions) {
val mutablePermissions = remember(permissions) {
return@remember permissions.map { permission ->
MutablePermissionState(
MutablePermissionStateImpl(
permission,
context,
activity,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ internal actual fun rememberMutablePermissionState(
): MutablePermissionState {
val context = LocalContext.current
val permissionState = remember(permission) {
MutablePermissionState(permission, context, context.findActivity(), onPermissionResult)
MutablePermissionStateImpl(permission, context, context.findActivity(), onPermissionResult)
}

// Refresh the permission status when the lifecycle is resumed
Expand Down Expand Up @@ -69,27 +69,18 @@ internal actual fun rememberMutablePermissionState(
*/
@ExperimentalPermissionsApi
@Stable
internal actual class MutablePermissionState(
actual override val permission: Permission,
private val context: Context?,
private val activity: Activity?,
internal class MutablePermissionStateImpl(
override val permission: Permission,
private val context: Context,
private val activity: Activity,
private val onPermissionResult: (Boolean) -> Unit,
) : PermissionState {

actual constructor(
permission: Permission,
) : this(
permission,
null,
null,
{}
)
) : MutablePermissionState {

private val androidPermission = permission.toAndroidPermission()

actual override var status: PermissionStatus by mutableStateOf(getPermissionStatus())
override var status: PermissionStatus by mutableStateOf(getPermissionStatus())

actual override fun launchPermissionRequest() {
override fun launchPermissionRequest() {
if (androidPermission.isEmpty()) return
else if (permission.isAlwaysGranted()) {
refreshPermissionStatus()
Expand All @@ -104,9 +95,7 @@ internal actual class MutablePermissionState(

internal var launcher: ActivityResultLauncher<String>? = null

actual override fun openAppSettings() {
if (context == null) return

override fun openAppSettings() {
val intent = when (permission) {
Permission.Notification -> if (supportsNotificationSettings()) {
createAppNotificationsIntent(context)
Expand All @@ -119,12 +108,12 @@ internal actual class MutablePermissionState(
context.startActivity(intent)
}

internal actual fun refreshPermissionStatus() {
override fun refreshPermissionStatus() {
status = getPermissionStatus()
}

private fun getPermissionStatus(): PermissionStatus {
if (context == null || activity == null || androidPermission.isEmpty()) {
if (androidPermission.isEmpty()) {
return PermissionStatus.Denied(false)
} else if (permission.isAlwaysGranted()) {
return PermissionStatus.Granted
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ package com.mohamedrejeb.calf.permissions

import androidx.compose.runtime.Composable
import androidx.compose.runtime.Stable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue

/**
* Creates a [MutablePermissionState] that is remembered across compositions.
Expand Down Expand Up @@ -34,17 +31,6 @@ internal expect fun rememberMutablePermissionState(
*/
@ExperimentalPermissionsApi
@Stable
internal expect class MutablePermissionState internal constructor(
permission: Permission,
) : PermissionState {

override val permission: Permission

override var status: PermissionStatus

override fun launchPermissionRequest()

override fun openAppSettings()

internal fun refreshPermissionStatus()
internal interface MutablePermissionState: PermissionState {
fun refreshPermissionStatus()
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ internal actual fun rememberMutablePermissionState(
onPermissionResult: (Boolean) -> Unit
): MutablePermissionState {
return remember(permission) {
MutablePermissionState(permission)
MutablePermissionStateImpl(permission)
}
}

Expand All @@ -37,17 +37,17 @@ internal actual fun rememberMutablePermissionState(
*/
@ExperimentalPermissionsApi
@Stable
internal actual class MutablePermissionState actual constructor(
actual override val permission: Permission,
) : PermissionState {
internal class MutablePermissionStateImpl(
override val permission: Permission,
) : MutablePermissionState {

actual override var status: PermissionStatus by mutableStateOf(getPermissionStatus())
override var status: PermissionStatus by mutableStateOf(getPermissionStatus())

actual override fun launchPermissionRequest() {}
override fun launchPermissionRequest() {}

actual override fun openAppSettings() {}
override fun openAppSettings() {}

internal actual fun refreshPermissionStatus() {}
override fun refreshPermissionStatus() {}

private fun getPermissionStatus(): PermissionStatus {
return PermissionStatus.Denied(false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import androidx.compose.runtime.Stable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import platform.Foundation.NSURL
import platform.UIKit.UIApplication
import platform.UIKit.UIApplicationOpenSettingsURLString
Expand All @@ -26,11 +30,14 @@ internal actual fun rememberMutablePermissionState(
permission: Permission,
onPermissionResult: (Boolean) -> Unit,
): MutablePermissionState {
val scope = rememberCoroutineScope()

val permissionState =
remember(permission) {
MutablePermissionState(
MutablePermissionStateImpl(
permission = permission,
onPermissionResult = onPermissionResult,
scope = scope,
)
}

Expand All @@ -46,23 +53,21 @@ internal actual fun rememberMutablePermissionState(
*/
@ExperimentalPermissionsApi
@Stable
internal actual class MutablePermissionState(
actual override val permission: Permission,
internal class MutablePermissionStateImpl(
override val permission: Permission,
private val onPermissionResult: (Boolean) -> Unit,
) : PermissionState {
actual constructor(
permission: Permission,
) : this(permission, {})
private val scope: CoroutineScope,
) : MutablePermissionState {

private val permissionDelegate = permission.getPermissionDelegate()

actual override var status: PermissionStatus by mutableStateOf(PermissionStatus.Denied(false))
override var status: PermissionStatus by mutableStateOf(PermissionStatus.Denied(false))

init {
refreshPermissionStatus()
}

actual override fun launchPermissionRequest() {
override fun launchPermissionRequest() {
permissionDelegate.launchPermissionRequest(
onPermissionResult = {
onPermissionResult(it)
Expand All @@ -71,14 +76,16 @@ internal actual class MutablePermissionState(
)
}

actual override fun openAppSettings() {
override fun openAppSettings() {
val settingsUrl = NSURL.URLWithString(UIApplicationOpenSettingsURLString) ?: return
UIApplication.sharedApplication.openURL(settingsUrl)
}

internal actual fun refreshPermissionStatus() {
override fun refreshPermissionStatus() {
permissionDelegate.getPermissionStatus { status ->
this.status = status
scope.launch(Dispatchers.Main) {
this@MutablePermissionStateImpl.status = status
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ internal actual fun rememberMutablePermissionState(
onPermissionResult: (Boolean) -> Unit,
): MutablePermissionState {
return remember(permission) {
MutablePermissionState(permission)
MutablePermissionStateImpl(permission)
}
}

Expand All @@ -37,16 +37,16 @@ internal actual fun rememberMutablePermissionState(
*/
@ExperimentalPermissionsApi
@Stable
internal actual class MutablePermissionState actual constructor(
actual override val permission: Permission,
) : PermissionState {
actual override var status: PermissionStatus by mutableStateOf(getPermissionStatus())
internal class MutablePermissionStateImpl(
override val permission: Permission,
) : MutablePermissionState {
override var status: PermissionStatus by mutableStateOf(getPermissionStatus())

actual override fun launchPermissionRequest() {}
override fun launchPermissionRequest() {}

actual override fun openAppSettings() {}
override fun openAppSettings() {}

internal actual fun refreshPermissionStatus() {}
override fun refreshPermissionStatus() {}

private fun getPermissionStatus(): PermissionStatus {
return PermissionStatus.Denied(false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ internal actual fun rememberMutablePermissionState(
onPermissionResult: (Boolean) -> Unit,
): MutablePermissionState {
return remember(permission) {
MutablePermissionState(permission)
MutablePermissionStateImpl(permission)
}
}

Expand All @@ -37,16 +37,16 @@ internal actual fun rememberMutablePermissionState(
*/
@ExperimentalPermissionsApi
@Stable
internal actual class MutablePermissionState actual constructor(
actual override val permission: Permission,
) : PermissionState {
actual override var status: PermissionStatus by mutableStateOf(getPermissionStatus())
internal class MutablePermissionStateImpl(
override val permission: Permission,
) : MutablePermissionState {
override var status: PermissionStatus by mutableStateOf(getPermissionStatus())

actual override fun launchPermissionRequest() {}
override fun launchPermissionRequest() {}

actual override fun openAppSettings() {}
override fun openAppSettings() {}

internal actual fun refreshPermissionStatus() {}
override fun refreshPermissionStatus() {}

private fun getPermissionStatus(): PermissionStatus {
return PermissionStatus.Denied(false)
Expand Down
7 changes: 0 additions & 7 deletions convention-plugins/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,3 @@ dependencies {
// hack to access version catalogue https://github.com/gradle/gradle/issues/15383
compileOnly(files(libs.javaClass.superclass.protectionDomain.codeSource.location))
}

gradlePlugin {
plugins.create(project.name) {
id = "com.mohamedrejeb.gradle.setup"
implementationClass = "com.mohamedrejeb.gradle.GradleSetupPlugin"
}
}

0 comments on commit f73de15

Please sign in to comment.