Skip to content

Commit

Permalink
[Android][iOS] Confirmation of credential sent (#74)
Browse files Browse the repository at this point in the history
This adds a toast component that informs the user when the credential is successfully shared with the verifier and updates the component that handles the QR code to add a new verification method and change how it's listed.
  • Loading branch information
Juliano1612 authored Jan 8, 2025
1 parent acb35ae commit e0ba60a
Show file tree
Hide file tree
Showing 8 changed files with 439 additions and 274 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import com.spruceid.mobilesdkexample.navigation.Screen
import com.spruceid.mobilesdkexample.navigation.SetupNavGraph
import com.spruceid.mobilesdkexample.ui.theme.ColorBase1
import com.spruceid.mobilesdkexample.ui.theme.MobileSdkTheme
import com.spruceid.mobilesdkexample.utils.Toast
import com.spruceid.mobilesdkexample.viewmodels.CredentialPacksViewModel
import com.spruceid.mobilesdkexample.viewmodels.CredentialPacksViewModelFactory
import com.spruceid.mobilesdkexample.viewmodels.HelpersViewModel
Expand Down Expand Up @@ -118,6 +119,8 @@ class MainActivity : ComponentActivity() {
helpersViewModel = helpersViewModel
)
}
// Global Toast Host
Toast.ToastHost()
}
}
}
Expand Down
114 changes: 114 additions & 0 deletions example/src/main/java/com/spruceid/mobilesdkexample/utils/Toast.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package com.spruceid.mobilesdkexample.utils

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.spruceid.mobilesdkexample.R
import com.spruceid.mobilesdkexample.ui.theme.ColorEmerald200
import com.spruceid.mobilesdkexample.ui.theme.ColorEmerald50
import com.spruceid.mobilesdkexample.ui.theme.ColorEmerald900
import com.spruceid.mobilesdkexample.ui.theme.Inter
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

enum class ToastType {
SUCCESS
}

object Toast {
private val toastMessage = mutableStateOf<String?>(null)
private val toastType = mutableStateOf(ToastType.SUCCESS)

fun showSuccess(message: String) {
toastType.value = ToastType.SUCCESS
toastMessage.value = message
}

@Composable
fun ToastHost(
duration: Long = 3000L,
onDismiss: () -> Unit = { toastMessage.value = null }
) {
val scope = rememberCoroutineScope()
val currentMessage = rememberUpdatedState(toastMessage.value)

currentMessage.value?.let { message ->
LaunchedEffect(message) {
scope.launch {
delay(duration)
onDismiss()
}
}
when (toastType.value) {
ToastType.SUCCESS -> SuccessToast(message = message)
}
}
}
}

@Composable
fun SuccessToast(
message: String,
) {
Box(
modifier = Modifier
.wrapContentHeight()
.padding(all = 20.dp)
.padding(top = 20.dp)
.background(
color = ColorEmerald50,
shape = RoundedCornerShape(6.dp)
)
.border(
width = 1.dp,
color = ColorEmerald200,
shape = RoundedCornerShape(6.dp)
)
) {
Row(
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.Center,
modifier = Modifier
.fillMaxWidth()
.padding(vertical = 8.dp)
) {
Image(
painter = painterResource(id = R.drawable.success_toast_icon),
contentDescription = stringResource(id = R.string.success_toast_icon),
modifier = Modifier
.width(20.dp)
.height(20.dp)
)
Text(
text = message,
color = ColorEmerald900,
fontFamily = Inter,
fontWeight = FontWeight.Normal,
fontSize = 15.sp,
modifier = Modifier.padding(start = 10.dp)
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,12 @@ fun AddVerificationMethodView(
val jsonArray = JSONArray(content)
for (i in 0 until jsonArray.length()) {
val json = jsonArray.getJSONObject(i)
val credentialName = json.getString("credential_name")
verificationMethodsViewModel.saveVerificationMethod(
VerificationMethods(
type = json.getString("type"),
name = json.getString("name"),
description = json.getString("description"),
name = credentialName,
description = "Verifies $credentialName Credentials",
verifierName = json.getString("verifier_name"),
url = json.getString("url")
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ fun VerifierHomeBody(
}
items(verificationMethods.value) { verificationMethod ->
VerifierListItem(
title = verificationMethod.verifierName,
title = verificationMethod.name,
description = verificationMethod.description,
type = getBadgeType(verificationMethod.type),
modifier = Modifier.clickable {
Expand Down
Loading

0 comments on commit e0ba60a

Please sign in to comment.