Use a Channel for generatorResultFlow implementation (#811)

This commit is contained in:
Brian Yencho 2024-01-27 13:48:53 -06:00 committed by Álison Fernandes
parent f2a7998bb0
commit 49e3d555e3
2 changed files with 13 additions and 7 deletions

View file

@ -9,7 +9,6 @@ import com.bitwarden.generators.UsernameGeneratorRequest
import com.x8bit.bitwarden.data.auth.datasource.disk.AuthDiskSource
import com.x8bit.bitwarden.data.platform.manager.dispatcher.DispatcherManager
import com.x8bit.bitwarden.data.platform.repository.model.LocalDataState
import com.x8bit.bitwarden.data.platform.repository.util.bufferedMutableSharedFlow
import com.x8bit.bitwarden.data.platform.repository.util.observeWhenSubscribedAndLoggedIn
import com.x8bit.bitwarden.data.tools.generator.datasource.disk.GeneratorDiskSource
import com.x8bit.bitwarden.data.tools.generator.datasource.disk.PasswordHistoryDiskSource
@ -27,15 +26,16 @@ import com.x8bit.bitwarden.data.tools.generator.repository.model.PasscodeGenerat
import com.x8bit.bitwarden.data.tools.generator.repository.model.UsernameGenerationOptions
import com.x8bit.bitwarden.data.vault.datasource.sdk.VaultSdkSource
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.onStart
import kotlinx.coroutines.flow.receiveAsFlow
import kotlinx.coroutines.launch
import java.time.Instant
import javax.inject.Singleton
@ -58,13 +58,13 @@ class GeneratorRepositoryImpl(
private val mutablePasswordHistoryStateFlow =
MutableStateFlow<LocalDataState<List<PasswordHistoryView>>>(LocalDataState.Loading)
private val mutableGeneratorResultFlow = bufferedMutableSharedFlow<GeneratorResult>()
private val generatorResultChannel = Channel<GeneratorResult>(capacity = Int.MAX_VALUE)
override val passwordHistoryStateFlow: StateFlow<LocalDataState<List<PasswordHistoryView>>>
get() = mutablePasswordHistoryStateFlow.asStateFlow()
override val generatorResultFlow: Flow<GeneratorResult>
get() = mutableGeneratorResultFlow.asSharedFlow()
get() = generatorResultChannel.receiveAsFlow()
init {
mutablePasswordHistoryStateFlow
@ -100,7 +100,7 @@ class GeneratorRepositoryImpl(
}
override fun emitGeneratorResult(generatorResult: GeneratorResult) {
mutableGeneratorResultFlow.tryEmit(generatorResult)
generatorResultChannel.trySend(generatorResult)
}
override suspend fun generatePassword(

View file

@ -34,6 +34,7 @@ import com.x8bit.bitwarden.ui.vault.model.VaultCardExpirationMonth
import com.x8bit.bitwarden.ui.vault.model.VaultIdentityTitle
import com.x8bit.bitwarden.ui.vault.model.VaultLinkedFieldType
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onEach
@ -109,8 +110,12 @@ class VaultAddEditViewModel @Inject constructor(
generatorRepository
.generatorResultFlow
.map {
VaultAddEditAction.Internal.GeneratorResultReceive(generatorResult = it)
.map { result ->
// Wait until we have a Content screen to update
mutableStateFlow.first {
it.viewState is VaultAddEditState.ViewState.Content
}
VaultAddEditAction.Internal.GeneratorResultReceive(generatorResult = result)
}
.onEach(::sendAction)
.launchIn(viewModelScope)
@ -874,6 +879,7 @@ class VaultAddEditViewModel @Inject constructor(
is VaultAddEditAction.Internal.UpdateCipherResultReceive -> {
handleUpdateCipherResultReceive(action)
}
is VaultAddEditAction.Internal.DeleteCipherReceive -> handleDeleteCipherReceive(action)
is VaultAddEditAction.Internal.TotpCodeReceive -> handleVaultTotpCodeReceive(action)
is VaultAddEditAction.Internal.VaultDataReceive -> handleVaultDataReceive(action)