Skip to content

Commit

Permalink
Debounce addition of picked files in the UI to avoid spamming the use…
Browse files Browse the repository at this point in the history
…r with animations
  • Loading branch information
LunarX committed Oct 18, 2024
1 parent 9776057 commit 3ccf72c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -48,7 +46,15 @@ class NewTransferViewModel @Inject constructor(
) : ViewModel() {

private val _files = MutableStateFlow<List<FileUi>>(emptyList())
val files: StateFlow<List<FileUi>> = _files
private val files: StateFlow<List<FileUi>> = _files
@OptIn(FlowPreview::class)
val filesDebounced = files
.debounce(50)
.stateIn(
scope = viewModelScope,
started = SharingStarted.Lazily,
initialValue = emptyList(),
)

private val _failedFiles = MutableSharedFlow<PickedFile>()
val failedFiles: SharedFlow<PickedFile> = _failedFiles
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ fun ImportFilesScreen(
newTransferViewModel: NewTransferViewModel = hiltViewModel<NewTransferViewModel>(),
closeActivity: () -> Unit,
) {
val files by newTransferViewModel.files.collectAsStateWithLifecycle()
val files by newTransferViewModel.filesDebounced.collectAsStateWithLifecycle()
val filesToImportCount by newTransferViewModel.filesToImportCount.collectAsStateWithLifecycle()
ImportFilesScreen(
files = { files },
Expand Down

0 comments on commit 3ccf72c

Please sign in to comment.