refactor: Update ListsViewModel to use OperationCounter (#990)

This commit is contained in:
Nik Clayton 2024-10-09 11:45:13 +02:00 committed by GitHub
parent 8587fafaa8
commit 21ff45ac34
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 22 additions and 22 deletions

View File

@ -24,13 +24,11 @@ import app.pachli.core.common.string.unicodeWrap
import app.pachli.core.data.repository.ListsError import app.pachli.core.data.repository.ListsError
import app.pachli.core.data.repository.ListsRepository import app.pachli.core.data.repository.ListsRepository
import app.pachli.core.network.model.UserListRepliesPolicy import app.pachli.core.network.model.UserListRepliesPolicy
import app.pachli.core.ui.OperationCounter
import com.github.michaelbull.result.onFailure 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
@ -57,40 +55,42 @@ 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) private val operationCounter = OperationCounter()
val operationCount = _operationCount.asStateFlow() val operationCount = operationCounter.count
val lists = listsRepository.lists val lists = listsRepository.lists
init { init {
listsRepository.refresh() viewModelScope.launch {
operationCounter { listsRepository.refresh() }
}
} }
fun refresh() = viewModelScope.launch { fun refresh() = viewModelScope.launch {
listsRepository.refresh() operationCounter { listsRepository.refresh() }
} }
fun createNewList(title: String, exclusive: Boolean, repliesPolicy: UserListRepliesPolicy) = viewModelScope.launch { fun createNewList(title: String, exclusive: Boolean, repliesPolicy: UserListRepliesPolicy) = viewModelScope.launch {
_operationCount.getAndUpdate { it + 1 } operationCounter {
listsRepository.createList(title, exclusive, repliesPolicy).onFailure { listsRepository.createList(title, exclusive, repliesPolicy).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, repliesPolicy: UserListRepliesPolicy) = viewModelScope.launch { fun updateList(listId: String, title: String, exclusive: Boolean, repliesPolicy: UserListRepliesPolicy) = viewModelScope.launch {
_operationCount.getAndUpdate { it + 1 } operationCounter {
listsRepository.editList(listId, title, exclusive, repliesPolicy).onFailure { listsRepository.editList(listId, title, exclusive, repliesPolicy).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 } operationCounter {
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 } } }
}
} }