Skip to content

Commit

Permalink
fix: fixes incorrectly mapping from granted permissions to record typ…
Browse files Browse the repository at this point in the history
…es (#153)

* fix: fixes incorrect mapping between permission names to records

* style: reformatted file and remove unused import

* fix: fixes granted permissions loop order and adds a permission class

* chore: remove obsolete custom exception

* chore: update example

---------

Authored-by: Ugur Akin <[email protected]>
  • Loading branch information
matinzd authored Sep 10, 2024
1 parent 3477a77 commit c7851a7
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 17 deletions.
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

0 comments on commit c7851a7

Please sign in to comment.