From 3ccf72cb399e3a55128d6de1913be9b40c1e9fe9 Mon Sep 17 00:00:00 2001 From: Gibran Chevalley Date: Wed, 16 Oct 2024 13:43:20 +0200 Subject: [PATCH] Debounce addition of picked files in the UI to avoid spamming the user with animations --- .../screen/newtransfer/NewTransferViewModel.kt | 16 +++++++++++----- .../newtransfer/importfiles/ImportFilesScreen.kt | 2 +- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/NewTransferViewModel.kt b/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/NewTransferViewModel.kt index a28e39f92..c56d4994f 100644 --- a/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/NewTransferViewModel.kt +++ b/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/NewTransferViewModel.kt @@ -29,11 +29,9 @@ import com.infomaniak.swisstransfer.ui.screen.newtransfer.TransferFilesManager.P import dagger.hilt.android.lifecycle.HiltViewModel import dagger.hilt.android.qualifiers.ApplicationContext import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.FlowPreview import kotlinx.coroutines.channels.Channel -import kotlinx.coroutines.flow.MutableSharedFlow -import kotlinx.coroutines.flow.MutableStateFlow -import kotlinx.coroutines.flow.SharedFlow -import kotlinx.coroutines.flow.StateFlow +import kotlinx.coroutines.flow.* import kotlinx.coroutines.launch import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.withLock @@ -48,7 +46,15 @@ class NewTransferViewModel @Inject constructor( ) : ViewModel() { private val _files = MutableStateFlow>(emptyList()) - val files: StateFlow> = _files + private val files: StateFlow> = _files + @OptIn(FlowPreview::class) + val filesDebounced = files + .debounce(50) + .stateIn( + scope = viewModelScope, + started = SharingStarted.Lazily, + initialValue = emptyList(), + ) private val _failedFiles = MutableSharedFlow() val failedFiles: SharedFlow = _failedFiles diff --git a/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/importfiles/ImportFilesScreen.kt b/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/importfiles/ImportFilesScreen.kt index d69d4d4dc..680cf9e03 100644 --- a/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/importfiles/ImportFilesScreen.kt +++ b/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/importfiles/ImportFilesScreen.kt @@ -45,7 +45,7 @@ fun ImportFilesScreen( newTransferViewModel: NewTransferViewModel = hiltViewModel(), closeActivity: () -> Unit, ) { - val files by newTransferViewModel.files.collectAsStateWithLifecycle() + val files by newTransferViewModel.filesDebounced.collectAsStateWithLifecycle() val filesToImportCount by newTransferViewModel.filesToImportCount.collectAsStateWithLifecycle() ImportFilesScreen( files = { files },