Skip to content

Commit

Permalink
Small adjustments to make demo app compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
Juliano1612 committed Oct 8, 2024
1 parent 34b0ca5 commit e0884d2
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 45 deletions.
38 changes: 29 additions & 9 deletions MobileSdk/src/main/java/com/spruceid/mobile/sdk/Credential.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.spruceid.mobile.sdk
import com.spruceid.mobile.sdk.rs.JsonVc
import com.spruceid.mobile.sdk.rs.JwtVc
import com.spruceid.mobile.sdk.rs.Mdoc
import org.json.JSONArray
import org.json.JSONObject

/**
Expand Down Expand Up @@ -32,14 +33,21 @@ private fun Mdoc.jsonEncodedDetailsInternal(elementIdentifiers: List<String>?):
}

if (jsonString != null) {
val json: JSONObject
val jsonObject: JSONObject
try {
json = JSONObject(jsonString)
} catch (e: Error) {
jsonObject = JSONObject(jsonString)
return@map Pair(id, jsonObject)
} catch (e: Exception) {
print("failed to decode '$id' as JSON: $e")
return@map null
}
return@map Pair(id, json)

try {
val jsonArray = JSONArray(jsonString)
return@map Pair(id, jsonArray)
} catch (e: Exception) {
print("failed to decode '$id' as JSON: $e")
return@map Pair(id, jsonString)
}
}

return@map null
Expand Down Expand Up @@ -67,7 +75,7 @@ fun JwtVc.credentialClaimsFiltered(claimNames: List<String>): JSONObject {
val new = JSONObject()
for (name in claimNames) {
if (old.has(name)) {
new.put(name, old.get(name))
new.put(name, keyPathFinder(old, name.split(".").toMutableList()))
}
}
return new
Expand All @@ -92,9 +100,21 @@ fun JsonVc.credentialClaimsFiltered(claimNames: List<String>): JSONObject {
val old = this.credentialClaims()
val new = JSONObject()
for (name in claimNames) {
if (old.has(name)) {
new.put(name, old.get(name))
}
new.put(name, keyPathFinder(old, name.split(".").toMutableList()))
}
return new
}

private fun keyPathFinder(json: Any, path: MutableList<String>): Any {
try {
val firstKey = path.first()
val element = (json as JSONObject)[firstKey]
path.removeAt(0)
if (path.isNotEmpty()) {
return keyPathFinder(element, path)
}
return element
} catch (e: Exception) {
return ""
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ fun GenericCredentialDetailsItem(credentialPack: CredentialPack) {
CardRenderingDetailsField(
// it's also possible just request the credentialSubject and cast it to JSONObject
keys = listOf(
"issuer.name",
"credentialSubject.image",
"credentialSubject.givenName",
"credentialSubject.familyName",
Expand All @@ -159,18 +160,14 @@ fun GenericCredentialDetailsItem(credentialPack: CredentialPack) {
"credentialSubject.driversLicense.family_name",
"credentialSubject.driversLicense.birth_date",
),
formatter = {values ->
val w3cvc = values.toList()
.first { (key, _) ->
val credential = credentialPack.getCredentialById(key)
(credential?.asJwtVc() != null || credential?.asJsonVc() != null)
}.second
formatter = { values ->
val w3cvc = values.toList().first().second

var portrait = ""
var firstName = ""
var lastName = ""
var birthDate = ""
if(w3cvc["credentialSubject.driversLicense"].toString().isNotEmpty()) {
if (w3cvc["credentialSubject.driversLicense"].toString().isNotEmpty()) {
portrait = w3cvc["credentialSubject.driversLicense.portrait"].toString()
firstName = w3cvc["credentialSubject.driversLicense.given_name"].toString()
lastName = w3cvc["credentialSubject.driversLicense.family_name"].toString()
Expand Down Expand Up @@ -204,7 +201,7 @@ fun GenericCredentialDetailsItem(credentialPack: CredentialPack) {
)
BitmapImage(
byteArray,
contentDescription = w3cvc["issuer.name"].toString(),
contentDescription = w3cvc["issuer.name"].toString(),
modifier = Modifier
.width(90.dp)
.padding(end = 12.dp)
Expand Down Expand Up @@ -255,11 +252,7 @@ fun GenericCredentialDetailsItem(credentialPack: CredentialPack) {
CardRenderingDetailsField(
keys = listOf("issuanceDate"),
formatter = { values ->
val w3cvc = values.toList()
.first { (key, _) ->
val credential = credentialPack.getCredentialById(key)
(credential?.asJwtVc() != null || credential?.asJsonVc() != null)
}.second
val w3cvc = values.toList().first().second

Row {
Column {
Expand Down Expand Up @@ -297,12 +290,8 @@ fun GenericCredentialDetailsItem(credentialPack: CredentialPack) {
fun GenericCredentialListItem(credentialPack: CredentialPack) {
val listRendering = CardRenderingListView(
titleKeys = listOf("name"),
titleFormatter = {values ->
val w3cvc = values.toList()
.first { (key, _) ->
val credential = credentialPack.getCredentialById(key)
(credential?.asJwtVc() != null || credential?.asJsonVc() != null)
}.second
titleFormatter = { values ->
val w3cvc = values.toList().first().second

Text(
text = w3cvc["name"].toString(),
Expand All @@ -314,12 +303,8 @@ fun GenericCredentialListItem(credentialPack: CredentialPack) {
)
},
descriptionKeys = listOf("description", "valid"),
descriptionFormatter = {values ->
val w3cvc = values.toList()
.first { (key, _) ->
val credential = credentialPack.getCredentialById(key)
(credential?.asJwtVc() != null || credential?.asJsonVc() != null)
}.second
descriptionFormatter = { values ->
val w3cvc = values.toList().first().second

Column {
Text(
Expand All @@ -330,7 +315,7 @@ fun GenericCredentialListItem(credentialPack: CredentialPack) {
color = TextBody
)
Spacer(modifier = Modifier.height(16.dp))
if(w3cvc["valid"].toString() == "true") {
if (w3cvc["valid"].toString() == "true") {
Row(verticalAlignment = Alignment.CenterVertically) {
Image(
painter = painterResource(id = R.drawable.valid),
Expand All @@ -350,11 +335,7 @@ fun GenericCredentialListItem(credentialPack: CredentialPack) {
},
leadingIconKeys = listOf("issuer.image", "issuer.name"),
leadingIconFormatter = { values ->
val w3cvc = values.toList()
.first { (key, _) ->
val credential = credentialPack.getCredentialById(key)
(credential?.asJwtVc() != null || credential?.asJsonVc() != null)
}.second
val w3cvc = values.toList().first().second
val byteArray = Base64.decode(
w3cvc["issuer.image"]
.toString()
Expand All @@ -370,7 +351,7 @@ fun GenericCredentialListItem(credentialPack: CredentialPack) {
) {
BitmapImage(
byteArray,
contentDescription = w3cvc["issuer.name"].toString(),
contentDescription = w3cvc["issuer.name"].toString(),
modifier = Modifier
.width(50.dp)
.padding(end = 12.dp)
Expand Down Expand Up @@ -439,7 +420,7 @@ fun GenericCredentialListItemQRCode() {
modifier = Modifier.fillMaxWidth(),
horizontalAlignment = Alignment.CenterHorizontally
) {
if(vc != null) {
if (vc != null) {
Image(
painter = rememberQrBitmapPainter(vc!!, size = 300.dp),
contentDescription = stringResource(id = R.string.vp_qr_code),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ fun ShareableCredentialListItemQRCode(credential: ParsedCredential) {
.fillMaxWidth()
.clickable {
showQRCode = !showQRCode
if(!showQRCode) {
if (!showQRCode) {
credentialViewModel.cancel()
}
}
Expand All @@ -242,7 +242,7 @@ fun ShareableCredentialListItemQRCode(credential: ParsedCredential) {
}

AnimatedVisibility(visible = showQRCode) {
if(showQRCode) {
if (showQRCode) {
Column(
modifier = Modifier.fillMaxWidth(),
horizontalAlignment = Alignment.CenterHorizontally
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ fun WalletHomeBody(rawCredentialsViewModel: IRawCredentialsViewModel) {
}
// item {
// vcs.map { vc ->
// GenericCredentialListItems(vc = vc)
// GenericCredentialListItems(vc_json = vc)
// }
// ShareableCredentialListItems(mdocBase64 = mdocBase64)
// }
Expand Down

0 comments on commit e0884d2

Please sign in to comment.