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

fix: fixes incorrectly mapping from granted permissions to record types #153

Merged
merged 5 commits into from
Sep 10, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import androidx.health.connect.client.PermissionController
import androidx.health.connect.client.permission.HealthPermission
import com.facebook.react.bridge.ReadableArray
import com.facebook.react.bridge.WritableNativeArray
import com.facebook.react.bridge.WritableNativeMap
import dev.matinzd.healthconnect.utils.InvalidRecordType
import dev.matinzd.healthconnect.utils.reactRecordTypeToClassMap

Expand All @@ -31,27 +30,20 @@ class PermissionUtils {

fun mapPermissionResult(grantedPermissions: Set<String>): WritableNativeArray {
return WritableNativeArray().apply {
grantedPermissions.forEach {
val map = WritableNativeMap()
for ((recordType, recordClass) in reactRecordTypeToClassMap) {
val readPermissionForRecord = HealthPermission.getReadPermission(recordClass)
val writePermissionForRecord = HealthPermission.getWritePermission(recordClass)

val (accessType, recordType) = extractPermissionResult(it)
if (grantedPermissions.contains(readPermissionForRecord)) {
pushMap(ReactPermission(AccessType.READ, recordType).toReadableMap())
}

map.putString("recordType", snakeToCamel(recordType))
map.putString("accessType", accessType)
pushMap(map)
if (grantedPermissions.contains(writePermissionForRecord)) {
pushMap(ReactPermission(AccessType.WRITE, recordType).toReadableMap())
}
}
}
}

private fun extractPermissionResult(it: String): Pair<String, String> {
val accessType = it.substring(it.lastIndexOf(".") + 1, it.indexOf("_")).lowercase()
val recordType = it.substring(it.indexOf("_") + 1).lowercase()
return Pair(accessType, recordType)
}

private fun snakeToCamel(word: String): String {
val components = word.split("_")
return components.joinToString("") { it[0].uppercase() + it.substring(1) }
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package dev.matinzd.healthconnect.permissions

import com.facebook.react.bridge.ReadableMap
import com.facebook.react.bridge.WritableNativeMap

data class ReactPermission(val accessType: AccessType, val recordType: String) {
fun toReadableMap(): ReadableMap {
val map = WritableNativeMap()
map.putString(ACCESS_TYPE, accessType.reactName)
map.putString(RECORD_TYPE, recordType)
return map
}

companion object {
private const val ACCESS_TYPE = "accessType"
private const val RECORD_TYPE = "recordType"
}
}

enum class AccessType(val reactName: String) {
READ("read"),
WRITE("write")
}
2 changes: 2 additions & 0 deletions example/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.health.READ_STEPS" />
<uses-permission android:name="android.permission.health.WRITE_STEPS" />
<uses-permission android:name="android.permission.health.READ_EXERCISE"/>
<uses-permission android:name="android.permission.health.WRITE_EXERCISE"/>

<application
android:name=".MainApplication"
Expand Down
8 changes: 8 additions & 0 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,14 @@ export default function App() {
accessType: 'write',
recordType: 'Steps',
},
{
accessType: 'write',
recordType: 'ExerciseSession',
},
{
accessType: 'read',
recordType: 'ExerciseSession',
},
]).then((permissions) => {
console.log('Granted permissions on request ', { permissions });
});
Expand Down
Loading