-
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: android 14 permission support (#60)
* feat: android 14 permission support BREAKING CHANGE: You will need to make some changes to your `MainActivity.kt` file * fix: force unwrap * chore: upgrade example to 0.73.3 * chore: update docs * chore: update docs
- Loading branch information
Showing
12 changed files
with
442 additions
and
497 deletions.
There are no files selected for viewing
This file contains 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 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
35 changes: 35 additions & 0 deletions
35
...id/src/main/java/dev/matinzd/healthconnect/permissions/HealthConnectPermissionDelegate.kt
This file contains 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,35 @@ | ||
package dev.matinzd.healthconnect.permissions | ||
|
||
import androidx.activity.result.ActivityResultLauncher | ||
import androidx.health.connect.client.PermissionController | ||
import com.facebook.react.ReactActivity | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.cancel | ||
import kotlinx.coroutines.channels.Channel | ||
import kotlinx.coroutines.launch | ||
|
||
object HealthConnectPermissionDelegate { | ||
private lateinit var requestPermission: ActivityResultLauncher<Set<String>> | ||
private val channel = Channel<Set<String>>() | ||
private val coroutineScope = CoroutineScope(Dispatchers.IO) | ||
|
||
fun setPermissionDelegate( | ||
activity: ReactActivity, | ||
providerPackageName: String = "com.google.android.apps.healthdata" | ||
) { | ||
val contract = PermissionController.createRequestPermissionResultContract(providerPackageName) | ||
|
||
requestPermission = activity.registerForActivityResult(contract) { | ||
coroutineScope.launch { | ||
channel.send(it) | ||
coroutineContext.cancel() | ||
} | ||
} | ||
} | ||
|
||
suspend fun launch(permissions: Set<String>): Set<String> { | ||
requestPermission.launch(permissions) | ||
return channel.receive() | ||
} | ||
} |
This file contains 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 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 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
13 changes: 10 additions & 3 deletions
13
example/android/app/src/main/java/com/healthconnectexample/MainActivity.kt
This file contains 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 |
---|---|---|
@@ -1,22 +1,29 @@ | ||
package com.healthconnectexample | ||
|
||
import android.os.Bundle | ||
import com.facebook.react.ReactActivity | ||
import com.facebook.react.ReactActivityDelegate | ||
import com.facebook.react.defaults.DefaultNewArchitectureEntryPoint.fabricEnabled | ||
import com.facebook.react.defaults.DefaultReactActivityDelegate | ||
import dev.matinzd.healthconnect.permissions.HealthConnectPermissionDelegate | ||
|
||
class MainActivity : ReactActivity() { | ||
|
||
/** | ||
* Returns the name of the main component registered from JavaScript. This is used to schedule | ||
* rendering of the component. | ||
*/ | ||
override fun getMainComponentName(): String = "HealthConnectExample" | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
// In order to handle contract results, we need to set the permission delegate. | ||
HealthConnectPermissionDelegate.setPermissionDelegate(this) | ||
} | ||
|
||
/** | ||
* Returns the instance of the [ReactActivityDelegate]. We use [DefaultReactActivityDelegate] | ||
* which allows you to enable New Architecture with a single boolean flags [fabricEnabled] | ||
*/ | ||
override fun createReactActivityDelegate(): ReactActivityDelegate = | ||
DefaultReactActivityDelegate(this, mainComponentName, fabricEnabled) | ||
} | ||
DefaultReactActivityDelegate(this, mainComponentName, fabricEnabled) | ||
} |
This file contains 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.