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" /> + +