Skip to content
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

Refactor to support multi-package architecture #143

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 24 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,23 +50,30 @@ dependencies {

## List of supported permissions

The full list can be found in `dev.icerock.moko.permissions.Permission` enum.

* Camera: **Permission.CAMERA**
* Gallery: **Permission.GALLERY**
* Storage read: **Permission.STORAGE**
* Storage write: **Permission.WRITE_STORAGE**
* Fine location: **Permission.LOCATION**
* Coarse location: **Permission.COARSE_LOCATION**
* Background location: **Permission.BACKGROUND_LOCATION**
* Remote notifications: **Permission.REMOTE_NOTIFICATION**
* Audio recording: **Permission.RECORD_AUDIO**
* Bluetooth LE: **Permission.BLUETOOTH_LE**
* Bluetooth Scan: **Permission.BLUETOOTH_SCAN**
* Bluetooth Connect: **Permission.BLUETOOTH_CONNECT**
* Bluetooth Advertise: **Permission.BLUETOOTH_ADVERTISE**
* Contacts: **Permission.CONTACTS**
* Motion: **Permission.MOTION**
* `dev.icerock.moko:permissions-bluetooth`
* Bluetooth LE: **Permission.BLUETOOTH_LE**
* Bluetooth Scan: **Permission.BLUETOOTH_SCAN**
* Bluetooth Connect: **Permission.BLUETOOTH_CONNECT**
* Bluetooth Advertise: **Permission.BLUETOOTH_ADVERTISE**
* `dev.icerock.moko:permissions-camera`
* Camera: **Permission.CAMERA**
* `dev.icerock.moko:permissions-contacts`
* Contacts: **Permission.CONTACTS**
* `dev.icerock.moko:permissions-gallery`
* Gallery: **Permission.GALLERY**
* `dev.icerock.moko:permissions-location`
* Fine location: **Permission.LOCATION**
* Coarse location: **Permission.COARSE_LOCATION**
* Background location: **Permission.BACKGROUND_LOCATION**
* `dev.icerock.moko:permissions-microphone`
* Audio recording: **Permission.RECORD_AUDIO**
* `dev.icerock.moko:permissions-motion`
* Motion: **Permission.MOTION**
* `dev.icerock.moko:permissions-notifications`
* Remote notifications: **Permission.REMOTE_NOTIFICATION**
* `dev.icerock.moko:permissions-storage`
* Storage read: **Permission.STORAGE**
* Storage write: **Permission.WRITE_STORAGE**

## Usage

Expand Down
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[versions]
kotlinVersion = "1.9.10"
androidAppCompatVersion = "1.6.1"
androidxCoreVersion = "1.9.0" # TODO we have two versions (1.8 + 1.9) - unify them
composeMaterialVersion = "1.4.1"
composeActivityVersion = "1.7.0"
activityVersion = "1.7.0"
Expand All @@ -15,6 +16,7 @@ lifecycleRuntime = "2.6.1"
composeUiVersion = "1.0.1"

[libraries]
androidxCore = { module = "androidx.core:core", version.ref = "androidxCoreVersion" }
appCompat = { module = "androidx.appcompat:appcompat", version.ref = "androidAppCompatVersion" }
material = { module = "com.google.android.material:material", version.ref = "materialDesignVersion" }
composeMaterial = { module = "androidx.compose.material:material", version.ref = "composeMaterialVersion" }
Expand Down
19 changes: 19 additions & 0 deletions permissions-bluetooth/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright 2024 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license.
*/

plugins {
id("dev.icerock.moko.gradle.multiplatform.mobile")
id("dev.icerock.moko.gradle.publication")
id("dev.icerock.moko.gradle.stub.javadoc")
id("dev.icerock.moko.gradle.detekt")
}

android {
namespace = "dev.icerock.moko.permissions.bluetooth"
}

dependencies {
commonMainApi(projects.permissions)
commonMainImplementation(libs.coroutines)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package dev.icerock.moko.permissions.bluetooth

import android.Manifest
import android.content.Context
import android.os.Build
import dev.icerock.moko.permissions.PermissionDelegate

/**
* @see https://developer.android.com/guide/topics/connectivity/bluetooth/permissions
*/

actual val bluetoothLEDelegate = object : PermissionDelegate {
override fun getPermissionStateOverride(applicationContext: Context) = null

override fun getPlatformPermission() =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
listOf(
Manifest.permission.BLUETOOTH_CONNECT,
Manifest.permission.BLUETOOTH_SCAN,
Manifest.permission.BLUETOOTH_ADVERTISE,
Manifest.permission.ACCESS_COARSE_LOCATION
)
} else {
listOf(
Manifest.permission.BLUETOOTH,
Manifest.permission.ACCESS_COARSE_LOCATION
)
}
}

actual val bluetoothScanDelegate = object : PermissionDelegate {
override fun getPermissionStateOverride(applicationContext: Context) = null

override fun getPlatformPermission() =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
listOf(Manifest.permission.BLUETOOTH_SCAN)
} else {
listOf(Manifest.permission.BLUETOOTH)
}
}

actual val bluetoothAdvertiseDelegate = object : PermissionDelegate {
override fun getPermissionStateOverride(applicationContext: Context) = null

override fun getPlatformPermission() =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
listOf(Manifest.permission.BLUETOOTH_ADVERTISE)
} else {
listOf(Manifest.permission.BLUETOOTH)
}
}

actual val bluetoothConnectDelegate = object : PermissionDelegate {
override fun getPermissionStateOverride(applicationContext: Context) = null

override fun getPlatformPermission() =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
listOf(Manifest.permission.BLUETOOTH_CONNECT)
} else {
listOf(Manifest.permission.BLUETOOTH)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package dev.icerock.moko.permissions.bluetooth

import dev.icerock.moko.permissions.Permission
import dev.icerock.moko.permissions.PermissionDelegate

internal expect val bluetoothLEDelegate: PermissionDelegate
internal expect val bluetoothScanDelegate: PermissionDelegate
internal expect val bluetoothAdvertiseDelegate: PermissionDelegate
internal expect val bluetoothConnectDelegate: PermissionDelegate

object BluetoothLEPermission : Permission {
override val delegate get() = bluetoothLEDelegate
}
object BluetoothScanPermission : Permission {
override val delegate get() = bluetoothScanDelegate
}
object BluetoothAdvertisePermission : Permission {
override val delegate get() = bluetoothAdvertiseDelegate
}
object BluetoothConnectPermission : Permission {
override val delegate get() = bluetoothConnectDelegate
}

val Permission.Companion.BLUETOOTH_LE get() = BluetoothLEPermission
val Permission.Companion.BLUETOOTH_SCAN get() = BluetoothScanPermission
val Permission.Companion.BLUETOOTH_ADVERTISE get() = BluetoothAdvertisePermission
val Permission.Companion.BLUETOOTH_CONNECT get() = BluetoothConnectPermission
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
* Copyright 2022 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license.
*/

package dev.icerock.moko.permissions.ios
package dev.icerock.moko.permissions.bluetooth

import dev.icerock.moko.permissions.DeniedAlwaysException
import dev.icerock.moko.permissions.DeniedException
import dev.icerock.moko.permissions.Permission
import dev.icerock.moko.permissions.PermissionState
import dev.icerock.moko.permissions.PermissionDelegate
import kotlinx.cinterop.ExperimentalForeignApi
import platform.CoreBluetooth.CBCentralManager
import platform.CoreBluetooth.CBCentralManagerDelegateProtocol
Expand Down Expand Up @@ -97,3 +98,8 @@ internal class BluetoothPermissionDelegate(
}
}
}

actual val bluetoothLEDelegate: PermissionDelegate = BluetoothPermissionDelegate(BluetoothLEPermission)
actual val bluetoothScanDelegate: PermissionDelegate = BluetoothPermissionDelegate(BluetoothScanPermission)
actual val bluetoothAdvertiseDelegate: PermissionDelegate = BluetoothPermissionDelegate(BluetoothAdvertisePermission)
actual val bluetoothConnectDelegate: PermissionDelegate = BluetoothPermissionDelegate(BluetoothConnectPermission)
19 changes: 19 additions & 0 deletions permissions-camera/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright 2024 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license.
*/

plugins {
id("dev.icerock.moko.gradle.multiplatform.mobile")
id("dev.icerock.moko.gradle.publication")
id("dev.icerock.moko.gradle.stub.javadoc")
id("dev.icerock.moko.gradle.detekt")
}

android {
namespace = "dev.icerock.moko.permissions.camera"
}

dependencies {
commonMainApi(projects.permissions)
commonMainImplementation(libs.coroutines)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package dev.icerock.moko.permissions.camera

import android.Manifest
import android.content.Context
import dev.icerock.moko.permissions.PermissionDelegate

actual val cameraDelegate = object : PermissionDelegate {
override fun getPermissionStateOverride(applicationContext: Context) = null
override fun getPlatformPermission() = listOf(Manifest.permission.CAMERA)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package dev.icerock.moko.permissions.camera

import dev.icerock.moko.permissions.Permission
import dev.icerock.moko.permissions.PermissionDelegate

internal expect val cameraDelegate: PermissionDelegate

object CameraPermission : Permission {
override val delegate get() = cameraDelegate
}

val Permission.Companion.CAMERA get() = CameraPermission
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package dev.icerock.moko.permissions.camera

import dev.icerock.moko.permissions.Permission
import dev.icerock.moko.permissions.PermissionDelegate
import dev.icerock.moko.permissions.ios.AVCaptureDelegate
import platform.AVFoundation.AVMediaTypeVideo

actual val cameraDelegate: PermissionDelegate = AVCaptureDelegate(
AVMediaTypeVideo,
Permission.CAMERA
)
19 changes: 19 additions & 0 deletions permissions-contacts/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright 2024 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license.
*/

plugins {
id("dev.icerock.moko.gradle.multiplatform.mobile")
id("dev.icerock.moko.gradle.publication")
id("dev.icerock.moko.gradle.stub.javadoc")
id("dev.icerock.moko.gradle.detekt")
}

android {
namespace = "dev.icerock.moko.permissions.contacts"
}

dependencies {
commonMainApi(projects.permissions)
commonMainImplementation(libs.coroutines)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package dev.icerock.moko.permissions.contacts

import android.Manifest
import android.content.Context
import dev.icerock.moko.permissions.PermissionDelegate

actual val contactsDelegate = object : PermissionDelegate {
override fun getPermissionStateOverride(applicationContext: Context) = null

override fun getPlatformPermission() =
listOf(
Manifest.permission.READ_CONTACTS,
Manifest.permission.WRITE_CONTACTS
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package dev.icerock.moko.permissions.contacts

import dev.icerock.moko.permissions.Permission
import dev.icerock.moko.permissions.PermissionDelegate

internal expect val contactsDelegate: PermissionDelegate

object ContactPermission : Permission {
override val delegate get() = contactsDelegate
}

val Permission.Companion.CONTACTS get() = ContactPermission
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,27 @@
* Copyright 2022 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license.
*/

package dev.icerock.moko.permissions.ios
package dev.icerock.moko.permissions.contacts

import dev.icerock.moko.permissions.DeniedAlwaysException
import dev.icerock.moko.permissions.LocationManagerDelegate
import dev.icerock.moko.permissions.Permission
import dev.icerock.moko.permissions.PermissionState
import dev.icerock.moko.permissions.PermissionDelegate
import platform.Contacts.CNAuthorizationStatus
import platform.Contacts.CNAuthorizationStatusAuthorized
import platform.Contacts.CNAuthorizationStatusDenied
import platform.Contacts.CNAuthorizationStatusNotDetermined
import platform.Contacts.CNAuthorizationStatusRestricted
import platform.Contacts.CNContactStore
import platform.Contacts.CNEntityType
import platform.Contacts.CNLabelContactRelationManager
import platform.CoreLocation.CLAuthorizationStatus
import platform.CoreLocation.CLLocationManager
import platform.CoreLocation.kCLAuthorizationStatusAuthorizedAlways
import platform.CoreLocation.kCLAuthorizationStatusAuthorizedWhenInUse
import platform.CoreLocation.kCLAuthorizationStatusDenied
import platform.CoreLocation.kCLAuthorizationStatusNotDetermined
import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine

internal class ContactsPermissionDelegate(
private class ContactsPermissionDelegate(
private val permission: Permission,
private val contactStore: CNContactStore
) : PermissionDelegate {
private val contactStore = CNContactStore()

override suspend fun providePermission() {
return provideLocationPermission(
CNContactStore.authorizationStatusForEntityType(
Expand Down Expand Up @@ -75,3 +69,5 @@ internal class ContactsPermissionDelegate(
}
}
}

actual val contactsDelegate: PermissionDelegate = ContactsPermissionDelegate(ContactPermission)
19 changes: 19 additions & 0 deletions permissions-gallery/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright 2024 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license.
*/

plugins {
id("dev.icerock.moko.gradle.multiplatform.mobile")
id("dev.icerock.moko.gradle.publication")
id("dev.icerock.moko.gradle.stub.javadoc")
id("dev.icerock.moko.gradle.detekt")
}

android {
namespace = "dev.icerock.moko.permissions.gallery"
}

dependencies {
commonMainApi(projects.permissions)
commonMainImplementation(libs.coroutines)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package dev.icerock.moko.permissions.gallery

import android.Manifest
import android.content.Context
import android.os.Build
import dev.icerock.moko.permissions.PermissionDelegate

actual val galleryDelegate = object : PermissionDelegate {
override fun getPermissionStateOverride(applicationContext: Context) = null

override fun getPlatformPermission() =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
listOf(
Manifest.permission.READ_MEDIA_IMAGES,
Manifest.permission.READ_MEDIA_VIDEO
)
} else {
listOf(Manifest.permission.READ_EXTERNAL_STORAGE)
}
}
Loading