Category management - Quest.

This commit is contained in:
Thomas Andres Gomez 2023-11-15 12:16:14 +01:00
parent e0593c9cbf
commit 020af02c29
3 changed files with 47 additions and 11 deletions

View file

@ -14,6 +14,8 @@ import com.pixelized.rplexicon.utilitary.exceptions.IncompatibleSheetStructure
import com.pixelized.rplexicon.utilitary.extentions.context
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.combine
@ -49,7 +51,8 @@ class QuestListViewModel @Inject constructor(
.groupBy(
keySelector = {
QuestCategoryUio(
title = it.group ?: context.getString(R.string.default_category_other),
title = it.group
?: context.getString(R.string.default_category_other),
)
},
valueTransform = { item ->
@ -71,15 +74,36 @@ class QuestListViewModel @Inject constructor(
}
}
launch(Dispatchers.IO) {
update(force = false)
withContext(Dispatchers.Main) {
_isLoading.value = true
}
awaitAll(
async { updateQuests(force = false) },
async { updateCategoryOrder(force = false) },
)
withContext(Dispatchers.Main) {
_isLoading.value = false
}
}
}
}
suspend fun update(force: Boolean) {
withContext(context = Dispatchers.Main) {
_isLoading.value = true
fun update(force: Boolean) {
viewModelScope.launch(Dispatchers.IO) {
withContext(Dispatchers.Main) {
_isLoading.value = true
}
awaitAll(
async { updateQuests(force = force) },
async { updateCategoryOrder(force = force) },
)
withContext(Dispatchers.Main) {
_isLoading.value = false
}
}
}
private suspend fun updateQuests(force: Boolean) {
try {
if (force || repository.lastSuccessFullUpdate.shouldUpdate()) {
repository.fetchQuests()
@ -95,12 +119,24 @@ class QuestListViewModel @Inject constructor(
Log.e(TAG, exception.message, exception)
_error.emit(FetchErrorUio.Default)
}
// clean the loading state
finally {
withContext(context = Dispatchers.Main) {
_isLoading.value = false
}
private suspend fun updateCategoryOrder(force: Boolean) {
try {
if (force || order.lastSuccessFullUpdate.shouldUpdate()) {
order.fetchCategoryOrder()
}
}
// the data sheet structure is not as expected
catch (exception: IncompatibleSheetStructure) {
Log.e(TAG, exception.message, exception)
_error.emit(FetchErrorUio.Structure(type = FetchErrorUio.Structure.Type.CATEGORY_ORDER))
}
// default exception
catch (exception: Exception) {
Log.e(TAG, exception.message, exception)
_error.emit(FetchErrorUio.Default)
}
}
companion object {

View file

@ -2,7 +2,7 @@
<string name="app_name">Rp-Compagnon</string>
<string name="action_close">Fermer</string>
<string name="default_category_other">Autre</string>
<string name="default_category_other">Autres</string>
<string name="error_generic">Une erreur s\'est produite.</string>
<string name="error_structure_unknowed">La structure des données semble avoir changé et n\'est plus compatible avec cette application.</string>

View file

@ -2,7 +2,7 @@
<string name="app_name">Rp-Companion</string>
<string name="action_close">Close</string>
<string name="default_category_other">Other</string>
<string name="default_category_other">Others</string>
<string name="error_generic">An error occurred.</string>
<string name="error_structure_unknowed">The file structure appears to have changed and is no longer compatible with this application</string>