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.
This commit is contained in:
parent
1deaa2c1ff
commit
256c6139ac
|
@ -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) {
|
override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) {
|
||||||
|
|
|
@ -25,6 +25,9 @@ import com.github.michaelbull.result.onFailure
|
||||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
import kotlinx.coroutines.channels.Channel
|
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.flow.receiveAsFlow
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
|
||||||
|
@ -45,6 +48,9 @@ internal class ListsViewModel @Inject constructor(
|
||||||
private val _errors = Channel<Error>()
|
private val _errors = Channel<Error>()
|
||||||
val errors = _errors.receiveAsFlow()
|
val errors = _errors.receiveAsFlow()
|
||||||
|
|
||||||
|
private val _operationCount = MutableStateFlow(0)
|
||||||
|
val operationCount = _operationCount.asStateFlow()
|
||||||
|
|
||||||
val lists = listsRepository.lists
|
val lists = listsRepository.lists
|
||||||
|
|
||||||
init {
|
init {
|
||||||
|
@ -56,20 +62,26 @@ internal class ListsViewModel @Inject constructor(
|
||||||
}
|
}
|
||||||
|
|
||||||
fun createNewList(title: String, exclusive: Boolean) = viewModelScope.launch {
|
fun createNewList(title: String, exclusive: Boolean) = viewModelScope.launch {
|
||||||
|
_operationCount.getAndUpdate { it + 1 }
|
||||||
|
|
||||||
listsRepository.createList(title, exclusive).onFailure {
|
listsRepository.createList(title, exclusive).onFailure {
|
||||||
_errors.send(Error.Create(title, it))
|
_errors.send(Error.Create(title, it))
|
||||||
}
|
}
|
||||||
}
|
}.invokeOnCompletion { _operationCount.getAndUpdate { it - 1 } }
|
||||||
|
|
||||||
fun updateList(listId: String, title: String, exclusive: Boolean) = viewModelScope.launch {
|
fun updateList(listId: String, title: String, exclusive: Boolean) = viewModelScope.launch {
|
||||||
|
_operationCount.getAndUpdate { it + 1 }
|
||||||
|
|
||||||
listsRepository.editList(listId, title, exclusive).onFailure {
|
listsRepository.editList(listId, title, exclusive).onFailure {
|
||||||
_errors.send(Error.Update(title, it))
|
_errors.send(Error.Update(title, it))
|
||||||
}
|
}
|
||||||
}
|
}.invokeOnCompletion { _operationCount.getAndUpdate { it - 1 } }
|
||||||
|
|
||||||
fun deleteList(listId: String, title: String) = viewModelScope.launch {
|
fun deleteList(listId: String, title: String) = viewModelScope.launch {
|
||||||
|
_operationCount.getAndUpdate { it + 1 }
|
||||||
|
|
||||||
listsRepository.deleteList(listId).onFailure {
|
listsRepository.deleteList(listId).onFailure {
|
||||||
_errors.send(Error.Delete(title, it))
|
_errors.send(Error.Delete(title, it))
|
||||||
}
|
}
|
||||||
}
|
}.invokeOnCompletion { _operationCount.getAndUpdate { it - 1 } }
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,6 +13,15 @@
|
||||||
android:id="@+id/includedToolbar"
|
android:id="@+id/includedToolbar"
|
||||||
layout="@layout/toolbar_basic" />
|
layout="@layout/toolbar_basic" />
|
||||||
|
|
||||||
|
<com.google.android.material.progressindicator.LinearProgressIndicator
|
||||||
|
android:id="@+id/progressIndicator"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
app:layout_constraintTop_toBottomOf="@id/includedToolbar"
|
||||||
|
android:visibility="gone"
|
||||||
|
android:indeterminate="true"
|
||||||
|
android:contentDescription="" />
|
||||||
|
|
||||||
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
|
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
|
||||||
android:id="@+id/swipeRefreshLayout"
|
android:id="@+id/swipeRefreshLayout"
|
||||||
android:layout_width="0dp"
|
android:layout_width="0dp"
|
||||||
|
|
Loading…
Reference in New Issue