mirror of
https://github.com/bitwarden/android.git
synced 2025-02-17 04:19:54 +03:00
Use a Channel for generatorResultFlow implementation (#811)
This commit is contained in:
parent
f2a7998bb0
commit
49e3d555e3
2 changed files with 13 additions and 7 deletions
|
@ -9,7 +9,6 @@ import com.bitwarden.generators.UsernameGeneratorRequest
|
||||||
import com.x8bit.bitwarden.data.auth.datasource.disk.AuthDiskSource
|
import com.x8bit.bitwarden.data.auth.datasource.disk.AuthDiskSource
|
||||||
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.model.LocalDataState
|
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.platform.repository.util.observeWhenSubscribedAndLoggedIn
|
||||||
import com.x8bit.bitwarden.data.tools.generator.datasource.disk.GeneratorDiskSource
|
import com.x8bit.bitwarden.data.tools.generator.datasource.disk.GeneratorDiskSource
|
||||||
import com.x8bit.bitwarden.data.tools.generator.datasource.disk.PasswordHistoryDiskSource
|
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.tools.generator.repository.model.UsernameGenerationOptions
|
||||||
import com.x8bit.bitwarden.data.vault.datasource.sdk.VaultSdkSource
|
import com.x8bit.bitwarden.data.vault.datasource.sdk.VaultSdkSource
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
|
import kotlinx.coroutines.channels.Channel
|
||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.Flow
|
||||||
import kotlinx.coroutines.flow.MutableStateFlow
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
import kotlinx.coroutines.flow.StateFlow
|
import kotlinx.coroutines.flow.StateFlow
|
||||||
import kotlinx.coroutines.flow.asSharedFlow
|
|
||||||
import kotlinx.coroutines.flow.asStateFlow
|
import kotlinx.coroutines.flow.asStateFlow
|
||||||
import kotlinx.coroutines.flow.launchIn
|
import kotlinx.coroutines.flow.launchIn
|
||||||
import kotlinx.coroutines.flow.map
|
import kotlinx.coroutines.flow.map
|
||||||
import kotlinx.coroutines.flow.onEach
|
import kotlinx.coroutines.flow.onEach
|
||||||
import kotlinx.coroutines.flow.onStart
|
import kotlinx.coroutines.flow.onStart
|
||||||
|
import kotlinx.coroutines.flow.receiveAsFlow
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import java.time.Instant
|
import java.time.Instant
|
||||||
import javax.inject.Singleton
|
import javax.inject.Singleton
|
||||||
|
@ -58,13 +58,13 @@ class GeneratorRepositoryImpl(
|
||||||
private val mutablePasswordHistoryStateFlow =
|
private val mutablePasswordHistoryStateFlow =
|
||||||
MutableStateFlow<LocalDataState<List<PasswordHistoryView>>>(LocalDataState.Loading)
|
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>>>
|
override val passwordHistoryStateFlow: StateFlow<LocalDataState<List<PasswordHistoryView>>>
|
||||||
get() = mutablePasswordHistoryStateFlow.asStateFlow()
|
get() = mutablePasswordHistoryStateFlow.asStateFlow()
|
||||||
|
|
||||||
override val generatorResultFlow: Flow<GeneratorResult>
|
override val generatorResultFlow: Flow<GeneratorResult>
|
||||||
get() = mutableGeneratorResultFlow.asSharedFlow()
|
get() = generatorResultChannel.receiveAsFlow()
|
||||||
|
|
||||||
init {
|
init {
|
||||||
mutablePasswordHistoryStateFlow
|
mutablePasswordHistoryStateFlow
|
||||||
|
@ -100,7 +100,7 @@ class GeneratorRepositoryImpl(
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun emitGeneratorResult(generatorResult: GeneratorResult) {
|
override fun emitGeneratorResult(generatorResult: GeneratorResult) {
|
||||||
mutableGeneratorResultFlow.tryEmit(generatorResult)
|
generatorResultChannel.trySend(generatorResult)
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun generatePassword(
|
override suspend fun generatePassword(
|
||||||
|
|
|
@ -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.VaultIdentityTitle
|
||||||
import com.x8bit.bitwarden.ui.vault.model.VaultLinkedFieldType
|
import com.x8bit.bitwarden.ui.vault.model.VaultLinkedFieldType
|
||||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||||
|
import kotlinx.coroutines.flow.first
|
||||||
import kotlinx.coroutines.flow.launchIn
|
import kotlinx.coroutines.flow.launchIn
|
||||||
import kotlinx.coroutines.flow.map
|
import kotlinx.coroutines.flow.map
|
||||||
import kotlinx.coroutines.flow.onEach
|
import kotlinx.coroutines.flow.onEach
|
||||||
|
@ -109,8 +110,12 @@ class VaultAddEditViewModel @Inject constructor(
|
||||||
|
|
||||||
generatorRepository
|
generatorRepository
|
||||||
.generatorResultFlow
|
.generatorResultFlow
|
||||||
.map {
|
.map { result ->
|
||||||
VaultAddEditAction.Internal.GeneratorResultReceive(generatorResult = it)
|
// Wait until we have a Content screen to update
|
||||||
|
mutableStateFlow.first {
|
||||||
|
it.viewState is VaultAddEditState.ViewState.Content
|
||||||
|
}
|
||||||
|
VaultAddEditAction.Internal.GeneratorResultReceive(generatorResult = result)
|
||||||
}
|
}
|
||||||
.onEach(::sendAction)
|
.onEach(::sendAction)
|
||||||
.launchIn(viewModelScope)
|
.launchIn(viewModelScope)
|
||||||
|
@ -874,6 +879,7 @@ class VaultAddEditViewModel @Inject constructor(
|
||||||
is VaultAddEditAction.Internal.UpdateCipherResultReceive -> {
|
is VaultAddEditAction.Internal.UpdateCipherResultReceive -> {
|
||||||
handleUpdateCipherResultReceive(action)
|
handleUpdateCipherResultReceive(action)
|
||||||
}
|
}
|
||||||
|
|
||||||
is VaultAddEditAction.Internal.DeleteCipherReceive -> handleDeleteCipherReceive(action)
|
is VaultAddEditAction.Internal.DeleteCipherReceive -> handleDeleteCipherReceive(action)
|
||||||
is VaultAddEditAction.Internal.TotpCodeReceive -> handleVaultTotpCodeReceive(action)
|
is VaultAddEditAction.Internal.TotpCodeReceive -> handleVaultTotpCodeReceive(action)
|
||||||
is VaultAddEditAction.Internal.VaultDataReceive -> handleVaultDataReceive(action)
|
is VaultAddEditAction.Internal.VaultDataReceive -> handleVaultDataReceive(action)
|
||||||
|
|
Loading…
Add table
Reference in a new issue