From 7da28343d329eb4defa8f0c33d5c838a6a88e91a Mon Sep 17 00:00:00 2001 From: QZHelen Date: Tue, 3 Dec 2024 06:28:06 +0000 Subject: [PATCH] save multiple documents --- .../createcred/CreateCredentialActivity.kt | 22 +++++++++---------- .../createcred/CreateCredentialViewModel.kt | 14 ++++++------ .../cmwallet/data/room/CredentialDao.kt | 3 +++ 3 files changed, 20 insertions(+), 19 deletions(-) diff --git a/app/src/main/java/com/credman/cmwallet/createcred/CreateCredentialActivity.kt b/app/src/main/java/com/credman/cmwallet/createcred/CreateCredentialActivity.kt index 2a61faf..7905e31 100644 --- a/app/src/main/java/com/credman/cmwallet/createcred/CreateCredentialActivity.kt +++ b/app/src/main/java/com/credman/cmwallet/createcred/CreateCredentialActivity.kt @@ -11,17 +11,14 @@ import androidx.activity.viewModels import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Row -import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.padding import androidx.compose.material3.Button import androidx.compose.material3.ExperimentalMaterial3Api import androidx.compose.material3.LinearProgressIndicator import androidx.compose.material3.ModalBottomSheet -import androidx.compose.material3.Scaffold import androidx.compose.material3.Text import androidx.compose.material3.TextButton -import androidx.compose.material3.TopAppBar import androidx.compose.material3.rememberModalBottomSheetState import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect @@ -79,8 +76,7 @@ class CreateCredentialActivity : ComponentActivity() { @Composable fun CreateCredentialScreen(viewModel: CreateCredentialViewModel) { val uiState = viewModel.uiState - val sheetState = rememberModalBottomSheetState() - var showBottomSheet by remember { mutableStateOf(true) } + val sheetState = rememberModalBottomSheetState(skipPartiallyExpanded = true) LaunchedEffect(uiState.state) { handleUiResult(uiState.state) } @@ -90,9 +86,9 @@ class CreateCredentialActivity : ComponentActivity() { onDismissRequest = {}, sheetState = sheetState ) { - val credential = uiState.credentialToSave + val credentials = uiState.credentialsToSave - if (credential == null) { + if (credentials == null) { LinearProgressIndicator( Modifier .fillMaxWidth() @@ -113,11 +109,13 @@ class CreateCredentialActivity : ComponentActivity() { fontSize = 20.sp ) } - Row( - modifier = Modifier - .padding(10.dp) - ) { - CredentialCard(credential, {}) + for (credential in credentials) { + Row( + modifier = Modifier + .padding(10.dp) + ) { + CredentialCard(credential, {}) + } } // when (val credentialDetails = credential.credential) { // is MdocCredential -> CredentialClaimList(credentialDetails) diff --git a/app/src/main/java/com/credman/cmwallet/createcred/CreateCredentialViewModel.kt b/app/src/main/java/com/credman/cmwallet/createcred/CreateCredentialViewModel.kt index c0c571b..665ad2d 100644 --- a/app/src/main/java/com/credman/cmwallet/createcred/CreateCredentialViewModel.kt +++ b/app/src/main/java/com/credman/cmwallet/createcred/CreateCredentialViewModel.kt @@ -18,7 +18,6 @@ import com.credman.cmwallet.CmWalletApplication.Companion.TAG import com.credman.cmwallet.data.model.Credential import com.credman.cmwallet.data.model.CredentialDisplayData import com.credman.cmwallet.data.model.CredentialItem -import com.credman.cmwallet.data.model.CredentialKey import com.credman.cmwallet.data.model.CredentialKeySoftware import com.credman.cmwallet.data.room.CredentialDatabaseItem import com.credman.cmwallet.loadECPrivateKey @@ -27,7 +26,6 @@ import com.credman.cmwallet.openid4vci.data.AuthorizationDetailResponseOpenIdCre import com.credman.cmwallet.openid4vci.data.CredentialRequest import com.credman.cmwallet.openid4vci.data.TokenRequest import com.credman.cmwallet.openid4vci.data.imageUriToImageB64 -import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.launch import org.json.JSONObject import java.security.KeyFactory @@ -42,7 +40,7 @@ sealed class Result { } data class CreateCredentialUiState( - val credentialToSave: CredentialItem? = null, + val credentialsToSave: List? = null, val state: Result? = null, ) @@ -112,6 +110,7 @@ class CreateCredentialViewModel : ViewModel() { tokenResponse.authorizationDetails?.forEach { authDetail -> when (authDetail) { is AuthorizationDetailResponseOpenIdCredential -> { + val newCredentials = mutableListOf() authDetail.credentialIdentifiers.forEach { credentialId -> val credentialResponse = openId4VCI.requestCredentialFromEndpoint( accessToken = tokenResponse.accessToken, @@ -141,8 +140,9 @@ class CreateCredentialViewModel : ViewModel() { ) } ) - uiState = uiState.copy(credentialToSave = newCredentialItem) + newCredentials.add(newCredentialItem) } + uiState = uiState.copy(credentialsToSave = newCredentials) } } } @@ -158,11 +158,11 @@ class CreateCredentialViewModel : ViewModel() { } fun onConfirm() { - val credentialToSave = uiState.credentialToSave - if (credentialToSave != null) { + val credentialsToSave = uiState.credentialsToSave + if (credentialsToSave != null) { viewModelScope.launch { CmWalletApplication.database.credentialDao().insertAll( - CredentialDatabaseItem(credentialToSave) + credentialsToSave.map { CredentialDatabaseItem(it) } ) } onResponse() diff --git a/app/src/main/java/com/credman/cmwallet/data/room/CredentialDao.kt b/app/src/main/java/com/credman/cmwallet/data/room/CredentialDao.kt index 811a606..8626543 100644 --- a/app/src/main/java/com/credman/cmwallet/data/room/CredentialDao.kt +++ b/app/src/main/java/com/credman/cmwallet/data/room/CredentialDao.kt @@ -13,6 +13,9 @@ interface CredentialDao { @Insert(onConflict = OnConflictStrategy.REPLACE) suspend fun insertAll(vararg creds: CredentialDatabaseItem) + @Insert(onConflict = OnConflictStrategy.REPLACE) + suspend fun insertAll(creds: List) + @Update suspend fun updateUsers(vararg creds: CredentialDatabaseItem)