From c7d7ed6cb90e2ff9c133f2c4ba3dde68a2a2c2e6 Mon Sep 17 00:00:00 2001 From: Matt Gibson Date: Mon, 21 Oct 2024 14:50:52 -0700 Subject: [PATCH] initial scaffolding --- .../data/vault/datasource/disk/VaultDiskSource.kt | 6 ++++++ .../vault/datasource/disk/VaultDiskSourceImpl.kt | 7 +++++++ .../bitwarden/data/vault/manager/CipherManager.kt | 7 +++++++ .../data/vault/manager/CipherManagerImpl.kt | 14 ++++++++++++++ .../vault/feature/addedit/VaultAddEditViewModel.kt | 9 ++++++++- 5 files changed, 42 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/com/x8bit/bitwarden/data/vault/datasource/disk/VaultDiskSource.kt b/app/src/main/java/com/x8bit/bitwarden/data/vault/datasource/disk/VaultDiskSource.kt index 9c4329a65..924ff8c70 100644 --- a/app/src/main/java/com/x8bit/bitwarden/data/vault/datasource/disk/VaultDiskSource.kt +++ b/app/src/main/java/com/x8bit/bitwarden/data/vault/datasource/disk/VaultDiskSource.kt @@ -1,5 +1,6 @@ package com.x8bit.bitwarden.data.vault.datasource.disk +import com.bitwarden.vault.CipherView import com.x8bit.bitwarden.data.vault.datasource.network.model.SyncResponseJson import kotlinx.coroutines.flow.Flow @@ -9,6 +10,11 @@ import kotlinx.coroutines.flow.Flow @Suppress("TooManyFunctions") interface VaultDiskSource { + /** + * Saves a cipher to the offline data source for the given [userId]. + */ + suspend fun saveOfflineCipher(userId: String, cipher: CipherView) + /** * Saves a cipher to the data source for the given [userId]. */ diff --git a/app/src/main/java/com/x8bit/bitwarden/data/vault/datasource/disk/VaultDiskSourceImpl.kt b/app/src/main/java/com/x8bit/bitwarden/data/vault/datasource/disk/VaultDiskSourceImpl.kt index 60c42afbd..0360f9f3d 100644 --- a/app/src/main/java/com/x8bit/bitwarden/data/vault/datasource/disk/VaultDiskSourceImpl.kt +++ b/app/src/main/java/com/x8bit/bitwarden/data/vault/datasource/disk/VaultDiskSourceImpl.kt @@ -1,5 +1,6 @@ package com.x8bit.bitwarden.data.vault.datasource.disk +import com.bitwarden.vault.CipherView import com.x8bit.bitwarden.data.platform.manager.dispatcher.DispatcherManager import com.x8bit.bitwarden.data.platform.repository.util.bufferedMutableSharedFlow import com.x8bit.bitwarden.data.vault.datasource.disk.dao.CiphersDao @@ -30,6 +31,7 @@ import kotlinx.serialization.json.Json */ @Suppress("TooManyFunctions", "LongParameterList") class VaultDiskSourceImpl( + private val offlineCiphersDao: CiphersDao, private val ciphersDao: CiphersDao, private val collectionsDao: CollectionsDao, private val domainsDao: DomainsDao, @@ -45,6 +47,11 @@ class VaultDiskSourceImpl( private val forceFolderFlow = bufferedMutableSharedFlow>() private val forceSendFlow = bufferedMutableSharedFlow>() + override suspend fun saveOfflineCipher(userId: String, cipher: CipherView) { + TODO("Not yet implemented") + + } + override suspend fun saveCipher(userId: String, cipher: SyncResponseJson.Cipher) { ciphersDao.insertCiphers( ciphers = listOf( diff --git a/app/src/main/java/com/x8bit/bitwarden/data/vault/manager/CipherManager.kt b/app/src/main/java/com/x8bit/bitwarden/data/vault/manager/CipherManager.kt index c7b551202..320038c02 100644 --- a/app/src/main/java/com/x8bit/bitwarden/data/vault/manager/CipherManager.kt +++ b/app/src/main/java/com/x8bit/bitwarden/data/vault/manager/CipherManager.kt @@ -16,6 +16,13 @@ import com.x8bit.bitwarden.data.vault.repository.model.UpdateCipherResult */ @Suppress("TooManyFunctions") interface CipherManager { + /** + * Attempt to create a cipher in the offline repository + */ + suspend fun createOfflineCipher( + cipherView: CipherView + ): CreateCipherResult + /** * Attempt to create a cipher. */ diff --git a/app/src/main/java/com/x8bit/bitwarden/data/vault/manager/CipherManagerImpl.kt b/app/src/main/java/com/x8bit/bitwarden/data/vault/manager/CipherManagerImpl.kt index b3db64bcc..702667c93 100644 --- a/app/src/main/java/com/x8bit/bitwarden/data/vault/manager/CipherManagerImpl.kt +++ b/app/src/main/java/com/x8bit/bitwarden/data/vault/manager/CipherManagerImpl.kt @@ -46,6 +46,20 @@ class CipherManagerImpl( ) : CipherManager { private val activeUserId: String? get() = authDiskSource.userState?.activeUserId + override suspend fun createOfflineCipher(cipherView: CipherView): CreateCipherResult { + val userId = activeUserId ?: return CreateCipherResult.Error + + return vaultSdkSource.encryptCipher( + userId = userId, + cipherView = cipherView + ) + .flatMap { vaultDiskSource.saveOfflineCipher(userId = userId, cipher = it) } + .fold( + onFailure = { CreateCipherResult.Error }, + onSuccess = { CreateCipherResult.Success } + ) + } + override suspend fun createCipher(cipherView: CipherView): CreateCipherResult { val userId = activeUserId ?: return CreateCipherResult.Error return vaultSdkSource diff --git a/app/src/main/java/com/x8bit/bitwarden/ui/vault/feature/addedit/VaultAddEditViewModel.kt b/app/src/main/java/com/x8bit/bitwarden/ui/vault/feature/addedit/VaultAddEditViewModel.kt index 49d5f6020..74364d504 100644 --- a/app/src/main/java/com/x8bit/bitwarden/ui/vault/feature/addedit/VaultAddEditViewModel.kt +++ b/app/src/main/java/com/x8bit/bitwarden/ui/vault/feature/addedit/VaultAddEditViewModel.kt @@ -1858,7 +1858,7 @@ class VaultAddEditViewModel @Inject constructor( @Suppress("MaxLineLength") private suspend fun VaultAddEditState.ViewState.Content.createCipherForAddAndCloneItemStates(): CreateCipherResult { - return common.selectedOwner?.collections + val result = common.selectedOwner?.collections ?.filter { it.isSelected } ?.map { it.id } ?.let { @@ -1868,6 +1868,13 @@ class VaultAddEditViewModel @Inject constructor( ) } ?: vaultRepository.createCipher(cipherView = toCipherView()) + + if (result is CreateCipherResult.Error) { + // TODO: Ask for permission to store locally + vaultRepository.createOfflineCipher(cipherView = toCipherView()) + } + + return result; } private fun List.toUpdatedOwners(