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 #142

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
@@ -1,11 +1,13 @@
package dev.matinzd.healthconnect.permissions

import android.util.Log
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.UnsupportedPermissionType
import dev.matinzd.healthconnect.utils.reactRecordTypeToClassMap

class PermissionUtils {
Expand Down Expand Up @@ -34,24 +36,32 @@ class PermissionUtils {
grantedPermissions.forEach {
val map = WritableNativeMap()

val (accessType, recordType) = extractPermissionResult(it)

map.putString("recordType", snakeToCamel(recordType))
map.putString("accessType", accessType)
pushMap(map)
try {
val (accessType, recordType) = extractPermissionResult(it)
map.putString("recordType", recordType)
map.putString("accessType", accessType)
pushMap(map)
} catch (e: UnsupportedPermissionType) {
Log.d("Unsupported Permission", "Encountered an unsupported permission type: $it")
}
ugurakin1 marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

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 extractPermissionResult(permissionName: String): Pair<String, String> {
for ((recordType, recordClass) in reactRecordTypeToClassMap) {
val readPermissionForRecord = HealthPermission.getReadPermission(recordClass)
if (readPermissionForRecord == permissionName) {
return Pair("read", recordType)
}

val writePermissionForRecord = HealthPermission.getWritePermission(recordClass)
if (writePermissionForRecord == permissionName) {
return Pair("write", recordType)
}
}

private fun snakeToCamel(word: String): String {
val components = word.split("_")
return components.joinToString("") { it[0].uppercase() + it.substring(1) }
throw UnsupportedPermissionType()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class InvalidBloodPressure : Exception("Blood pressure is not valid")
class InvalidMass : Exception("Mass is not valid")
class InvalidLength : Exception("Length is not valid")
class AggregationNotSupported : Exception("Aggregation is not supported for this record")
class UnsupportedPermissionType : Exception()

fun Promise.rejectWithException(exception: Exception) {
val code = when (exception) {
Expand Down