From 256c6139ac530ccd4475518b3e01562650bc766b Mon Sep 17 00:00:00 2001 From: Nik Clayton Date: Thu, 14 Mar 2024 18:18:34 +0100 Subject: [PATCH] feat: Show network activity in ListsActivity (#533) Modify `ListsViewModel` to keep track of the number of active network operations and export as a flow. Collect this in `ListsActivity` and show a `LinearProgressIndicator` while it is non-zero. --- app/src/main/java/app/pachli/ListsActivity.kt | 6 ++++++ .../app/pachli/viewmodel/ListsViewModel.kt | 18 +++++++++++++++--- app/src/main/res/layout/activity_lists.xml | 9 +++++++++ 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/app/pachli/ListsActivity.kt b/app/src/main/java/app/pachli/ListsActivity.kt index 8810c2bca..d475cb50e 100644 --- a/app/src/main/java/app/pachli/ListsActivity.kt +++ b/app/src/main/java/app/pachli/ListsActivity.kt @@ -118,6 +118,12 @@ class ListsActivity : BaseActivity(), MenuProvider { } } } + + lifecycleScope.launch { + viewModel.operationCount.collectLatest { + if (it == 0) binding.progressIndicator.hide() else binding.progressIndicator.show() + } + } } override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) { diff --git a/app/src/main/java/app/pachli/viewmodel/ListsViewModel.kt b/app/src/main/java/app/pachli/viewmodel/ListsViewModel.kt index 20b7f8ffe..843f4a965 100644 --- a/app/src/main/java/app/pachli/viewmodel/ListsViewModel.kt +++ b/app/src/main/java/app/pachli/viewmodel/ListsViewModel.kt @@ -25,6 +25,9 @@ import com.github.michaelbull.result.onFailure import dagger.hilt.android.lifecycle.HiltViewModel import javax.inject.Inject import kotlinx.coroutines.channels.Channel +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.asStateFlow +import kotlinx.coroutines.flow.getAndUpdate import kotlinx.coroutines.flow.receiveAsFlow import kotlinx.coroutines.launch @@ -45,6 +48,9 @@ internal class ListsViewModel @Inject constructor( private val _errors = Channel() val errors = _errors.receiveAsFlow() + private val _operationCount = MutableStateFlow(0) + val operationCount = _operationCount.asStateFlow() + val lists = listsRepository.lists init { @@ -56,20 +62,26 @@ internal class ListsViewModel @Inject constructor( } fun createNewList(title: String, exclusive: Boolean) = viewModelScope.launch { + _operationCount.getAndUpdate { it + 1 } + listsRepository.createList(title, exclusive).onFailure { _errors.send(Error.Create(title, it)) } - } + }.invokeOnCompletion { _operationCount.getAndUpdate { it - 1 } } fun updateList(listId: String, title: String, exclusive: Boolean) = viewModelScope.launch { + _operationCount.getAndUpdate { it + 1 } + listsRepository.editList(listId, title, exclusive).onFailure { _errors.send(Error.Update(title, it)) } - } + }.invokeOnCompletion { _operationCount.getAndUpdate { it - 1 } } fun deleteList(listId: String, title: String) = viewModelScope.launch { + _operationCount.getAndUpdate { it + 1 } + listsRepository.deleteList(listId).onFailure { _errors.send(Error.Delete(title, it)) } - } + }.invokeOnCompletion { _operationCount.getAndUpdate { it - 1 } } } diff --git a/app/src/main/res/layout/activity_lists.xml b/app/src/main/res/layout/activity_lists.xml index 1332e1420..10f0f7731 100644 --- a/app/src/main/res/layout/activity_lists.xml +++ b/app/src/main/res/layout/activity_lists.xml @@ -13,6 +13,15 @@ android:id="@+id/includedToolbar" layout="@layout/toolbar_basic" /> + +