Skip to content

Commit

Permalink
save multiple documents
Browse files Browse the repository at this point in the history
  • Loading branch information
QZHelen committed Dec 3, 2024
1 parent fd52eb1 commit 7da2834
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand All @@ -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()
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -42,7 +40,7 @@ sealed class Result {
}

data class CreateCredentialUiState(
val credentialToSave: CredentialItem? = null,
val credentialsToSave: List<CredentialItem>? = null,
val state: Result? = null,
)

Expand Down Expand Up @@ -112,6 +110,7 @@ class CreateCredentialViewModel : ViewModel() {
tokenResponse.authorizationDetails?.forEach { authDetail ->
when (authDetail) {
is AuthorizationDetailResponseOpenIdCredential -> {
val newCredentials = mutableListOf<CredentialItem>()
authDetail.credentialIdentifiers.forEach { credentialId ->
val credentialResponse = openId4VCI.requestCredentialFromEndpoint(
accessToken = tokenResponse.accessToken,
Expand Down Expand Up @@ -141,8 +140,9 @@ class CreateCredentialViewModel : ViewModel() {
)
}
)
uiState = uiState.copy(credentialToSave = newCredentialItem)
newCredentials.add(newCredentialItem)
}
uiState = uiState.copy(credentialsToSave = newCredentials)
}
}
}
Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<CredentialDatabaseItem>)

@Update
suspend fun updateUsers(vararg creds: CredentialDatabaseItem)

Expand Down

0 comments on commit 7da2834

Please sign in to comment.