Skip to content

Commit

Permalink
Display credential info
Browse files Browse the repository at this point in the history
  • Loading branch information
Juliano1612 committed Sep 25, 2024
1 parent 507286d commit ab7823b
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,7 @@ interface RawCredentialsDao {

@Query("SELECT * FROM raw_credentials")
fun getAllRawCredentials(): List<RawCredentials>

@Query("DELETE FROM raw_credentials")
fun deleteAllRawCredentials(): Int
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,9 @@ class RawCredentialsRepository(private val rawCredentialsDao: RawCredentialsDao)
suspend fun getRawCredentials(): List<RawCredentials> {
return rawCredentialsDao.getAllRawCredentials()
}

@WorkerThread
suspend fun deleteAllRawCredentials(): Int {
return rawCredentialsDao.deleteAllRawCredentials()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ fun SetupNavGraph(
composable(
route = Screen.WalletSettingsHomeScreen.route,
) {
WalletSettingsHomeView(navController)
WalletSettingsHomeView(navController, rawCredentialsViewModel)
}
composable(
route = Screen.AddToWalletScreen.route,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ val mockAchievementCredential = """
"https://www.w3.org/ns/credentials/v2",
"https://purl.imsglobal.org/spec/ob/v3p0/context-3.0.3.json"
],
"awardedDate": "2024-09-23T18:12:12+0000",
"achievement": {
"name": "Team Membership",
"type": "Achievement"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import kotlinx.coroutines.launch
abstract class IRawCredentialsViewModel : ViewModel(){
abstract val rawCredentials: StateFlow<List<RawCredentials>>
abstract suspend fun saveRawCredential(rawCredential: RawCredentials)
abstract suspend fun deleteAllRawCredentials()
abstract fun generateRawCredentialsCSV(): String
}

Expand All @@ -31,6 +32,11 @@ class RawCredentialsViewModel(private val rawCredentialsRepository: RawCredentia
_rawCredentials.value = rawCredentialsRepository.getRawCredentials()
}

override suspend fun deleteAllRawCredentials() {
rawCredentialsRepository.deleteAllRawCredentials()
_rawCredentials.value = rawCredentialsRepository.getRawCredentials()
}

override fun generateRawCredentialsCSV(): String {
val heading = "ID, Raw Credential\n"
return heading +
Expand All @@ -50,6 +56,8 @@ class RawCredentialsViewModelPreview(override val rawCredentials: StateFlow<List
)) : IRawCredentialsViewModel() {
override suspend fun saveRawCredential(credential: RawCredentials) {}

override suspend fun deleteAllRawCredentials() {}

override fun generateRawCredentialsCSV(): String {
return ""
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.spruceid.mobilesdkexample.wallet

import android.util.Log
import androidx.compose.foundation.Image
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
Expand Down Expand Up @@ -33,6 +34,7 @@ import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.spruceid.mobile.sdk.W3CVC
import com.spruceid.mobilesdkexample.R
import com.spruceid.mobilesdkexample.ui.theme.Bg
import com.spruceid.mobilesdkexample.ui.theme.CredentialBorder
Expand All @@ -41,7 +43,14 @@ import com.spruceid.mobilesdkexample.ui.theme.Inter
import com.spruceid.mobilesdkexample.ui.theme.TextBody
import com.spruceid.mobilesdkexample.ui.theme.TextHeader
import com.spruceid.mobilesdkexample.utils.mockAchievementCredential
import org.json.JSONArray
import org.json.JSONObject
import java.text.SimpleDateFormat
import java.time.LocalDateTime
import java.time.OffsetDateTime
import java.time.format.DateTimeFormatter
import java.util.Date
import java.util.Locale

class AchievementCredentialItem {
private var credential: JSONObject
Expand All @@ -57,14 +66,17 @@ class AchievementCredentialItem {

@Composable
fun listComponent() {
val achievementName = keyPathFinder(credential, mutableListOf("achievement", "name")).toString()
val issuerName = keyPathFinder(credential, mutableListOf("issuer", "name")).toString()

Row(
Modifier.height(intrinsicSize = IntrinsicSize.Max)
) {
// Leading icon
Column {
// Title
Text(
text = "[Team Membership]",
text = achievementName,
fontFamily = Inter,
fontWeight = FontWeight.Medium,
fontSize = 22.sp,
Expand All @@ -75,7 +87,7 @@ class AchievementCredentialItem {
// Description
Column {
Text(
text = "[Development Council]",
text = issuerName,
fontFamily = Inter,
fontWeight = FontWeight.Normal,
fontSize = 14.sp,
Expand Down Expand Up @@ -105,21 +117,36 @@ class AchievementCredentialItem {

@Composable
fun detailsComponent() {
val awardedDate = keyPathFinder(credential, mutableListOf("awardedDate")).toString()
val ISO8601DateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss[.SSS]Z")
val parsedDate = OffsetDateTime.parse(awardedDate, ISO8601DateFormat)
val dateTimeFormatter = DateTimeFormatter.ofPattern("MMM dd, yyyy 'at' h:mm a")

val identity = keyPathFinder(credential, mutableListOf("credentialSubject", "identity")) as JSONArray
val details = MutableList(identity.length()) { i ->
val obj = identity.get(i) as JSONObject
Pair(obj["identityType"].toString(), obj["identityHash"].toString())
}

details.add(0, Pair("awardedDate", parsedDate.format(dateTimeFormatter)))

Row(
Modifier.padding(horizontal = 12.dp)
) {
Column {
listOf(1, 2, 3, 4).map {
details.map { detail ->
Text(
text = "[Field]",
text = splitCamelCase(detail.first),
fontFamily = Inter,
fontWeight = FontWeight.Normal,
fontSize = 14.sp,
color = TextBody,
modifier = Modifier.padding(top = 10.dp)
)
Text(
text = "[Value]"
text = detail.second,
fontFamily = Inter,
fontSize = 14.sp
)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.spruceid.mobilesdkexample.wallet

import android.util.Log
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
Expand Down Expand Up @@ -42,8 +40,6 @@ fun AddToWalletView(
val credential = AchievementCredentialItem(rawCredential)
val scope = rememberCoroutineScope()

Log.d("AAA", rawCredential)

Column(
Modifier
.padding(all = 20.dp)
Expand Down Expand Up @@ -100,7 +96,7 @@ fun AddToWalletView(

Button(
onClick = {
// navController.popBackStack()
navController.popBackStack()
},
shape = RoundedCornerShape(5.dp),
colors = ButtonDefaults.buttonColors(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,9 @@ fun WalletHomeBody(rawCredentialsViewModel: IRawCredentialsViewModel) {
.padding(top = 20.dp)
) {
items(rawCredentials) { rawCredential ->
Text(
text = "${rawCredential.id}",
fontFamily = Inter,
fontWeight = FontWeight.SemiBold,
fontSize = 17.sp,
// color = PrimaryDark,
modifier = Modifier.padding(bottom = 4.dp),
)
AchievementCredentialItem(rawCredential.rawCredential).component()
}
item {
AchievementCredentialItem(rawCredential = "{}").component()
vcs.map { vc ->
GenericCredentialListItems(vc = vc)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.asImageBitmap
import androidx.core.content.ContextCompat
import org.json.JSONObject

@Composable
fun BitmapImage(
Expand Down Expand Up @@ -48,4 +49,29 @@ fun checkAndRequestBluetoothPermissions(
// Request permissions
launcher.launch(permissions)
}
}

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 ""
}
}

fun splitCamelCase(s: String): String {
return s.replace(
String.format(
"%s|%s|%s",
"(?<=[A-Z])(?=[A-Z][a-z])",
"(?<=[^A-Z])(?=[A-Z])",
"(?<=[A-Za-z])(?=[^A-Za-z])"
).toRegex(), " "
).replaceFirstChar(Char::titlecase)
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.rotate
Expand All @@ -27,10 +28,13 @@ import com.spruceid.mobilesdkexample.R
import com.spruceid.mobilesdkexample.ui.theme.Inter
import com.spruceid.mobilesdkexample.ui.theme.TextHeader
import com.spruceid.mobilesdkexample.ui.theme.VerifiedRedInvalid
import com.spruceid.mobilesdkexample.viewmodels.IRawCredentialsViewModel
import kotlinx.coroutines.launch

@Composable
fun WalletSettingsHomeView(
navController: NavController
navController: NavController,
rawCredentialsViewModel: IRawCredentialsViewModel
) {
Column(
Modifier
Expand All @@ -42,7 +46,7 @@ fun WalletSettingsHomeView(
navController.popBackStack()
}
)
WalletSettingsHomeBody()
WalletSettingsHomeBody(rawCredentialsViewModel)
}
}

Expand Down Expand Up @@ -77,15 +81,19 @@ fun WalletSettingsHomeHeader(
}

@Composable
fun WalletSettingsHomeBody() {
fun WalletSettingsHomeBody(rawCredentialsViewModel: IRawCredentialsViewModel) {
val scope = rememberCoroutineScope()

Column(
Modifier
.padding(horizontal = 20.dp)
.padding(top = 10.dp),
) {
Button(
onClick = {
// TODO
scope.launch {
rawCredentialsViewModel.deleteAllRawCredentials()
}
},
shape = RoundedCornerShape(5.dp),
colors = ButtonDefaults.buttonColors(
Expand Down

0 comments on commit ab7823b

Please sign in to comment.