initial scaffolding

This commit is contained in:
Matt Gibson 2024-10-21 14:50:52 -07:00
parent 36a718753d
commit c7d7ed6cb9
No known key found for this signature in database
GPG key ID: 7CBCA182C13B0912
5 changed files with 42 additions and 1 deletions

View file

@ -1,5 +1,6 @@
package com.x8bit.bitwarden.data.vault.datasource.disk package com.x8bit.bitwarden.data.vault.datasource.disk
import com.bitwarden.vault.CipherView
import com.x8bit.bitwarden.data.vault.datasource.network.model.SyncResponseJson import com.x8bit.bitwarden.data.vault.datasource.network.model.SyncResponseJson
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
@ -9,6 +10,11 @@ import kotlinx.coroutines.flow.Flow
@Suppress("TooManyFunctions") @Suppress("TooManyFunctions")
interface VaultDiskSource { 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]. * Saves a cipher to the data source for the given [userId].
*/ */

View file

@ -1,5 +1,6 @@
package com.x8bit.bitwarden.data.vault.datasource.disk 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.manager.dispatcher.DispatcherManager
import com.x8bit.bitwarden.data.platform.repository.util.bufferedMutableSharedFlow import com.x8bit.bitwarden.data.platform.repository.util.bufferedMutableSharedFlow
import com.x8bit.bitwarden.data.vault.datasource.disk.dao.CiphersDao import com.x8bit.bitwarden.data.vault.datasource.disk.dao.CiphersDao
@ -30,6 +31,7 @@ import kotlinx.serialization.json.Json
*/ */
@Suppress("TooManyFunctions", "LongParameterList") @Suppress("TooManyFunctions", "LongParameterList")
class VaultDiskSourceImpl( class VaultDiskSourceImpl(
private val offlineCiphersDao: CiphersDao,
private val ciphersDao: CiphersDao, private val ciphersDao: CiphersDao,
private val collectionsDao: CollectionsDao, private val collectionsDao: CollectionsDao,
private val domainsDao: DomainsDao, private val domainsDao: DomainsDao,
@ -45,6 +47,11 @@ class VaultDiskSourceImpl(
private val forceFolderFlow = bufferedMutableSharedFlow<List<SyncResponseJson.Folder>>() private val forceFolderFlow = bufferedMutableSharedFlow<List<SyncResponseJson.Folder>>()
private val forceSendFlow = bufferedMutableSharedFlow<List<SyncResponseJson.Send>>() private val forceSendFlow = bufferedMutableSharedFlow<List<SyncResponseJson.Send>>()
override suspend fun saveOfflineCipher(userId: String, cipher: CipherView) {
TODO("Not yet implemented")
}
override suspend fun saveCipher(userId: String, cipher: SyncResponseJson.Cipher) { override suspend fun saveCipher(userId: String, cipher: SyncResponseJson.Cipher) {
ciphersDao.insertCiphers( ciphersDao.insertCiphers(
ciphers = listOf( ciphers = listOf(

View file

@ -16,6 +16,13 @@ import com.x8bit.bitwarden.data.vault.repository.model.UpdateCipherResult
*/ */
@Suppress("TooManyFunctions") @Suppress("TooManyFunctions")
interface CipherManager { interface CipherManager {
/**
* Attempt to create a cipher in the offline repository
*/
suspend fun createOfflineCipher(
cipherView: CipherView
): CreateCipherResult
/** /**
* Attempt to create a cipher. * Attempt to create a cipher.
*/ */

View file

@ -46,6 +46,20 @@ class CipherManagerImpl(
) : CipherManager { ) : CipherManager {
private val activeUserId: String? get() = authDiskSource.userState?.activeUserId 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 { override suspend fun createCipher(cipherView: CipherView): CreateCipherResult {
val userId = activeUserId ?: return CreateCipherResult.Error val userId = activeUserId ?: return CreateCipherResult.Error
return vaultSdkSource return vaultSdkSource

View file

@ -1858,7 +1858,7 @@ class VaultAddEditViewModel @Inject constructor(
@Suppress("MaxLineLength") @Suppress("MaxLineLength")
private suspend fun VaultAddEditState.ViewState.Content.createCipherForAddAndCloneItemStates(): CreateCipherResult { private suspend fun VaultAddEditState.ViewState.Content.createCipherForAddAndCloneItemStates(): CreateCipherResult {
return common.selectedOwner?.collections val result = common.selectedOwner?.collections
?.filter { it.isSelected } ?.filter { it.isSelected }
?.map { it.id } ?.map { it.id }
?.let { ?.let {
@ -1868,6 +1868,13 @@ class VaultAddEditViewModel @Inject constructor(
) )
} }
?: vaultRepository.createCipher(cipherView = toCipherView()) ?: 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<VaultAddEditState.Owner>.toUpdatedOwners( private fun List<VaultAddEditState.Owner>.toUpdatedOwners(