fetch all labels at initialization

This commit is contained in:
Satindar Dhillon
2023-03-01 12:04:38 -08:00
parent c7860a8d74
commit 7f3c484ed7
4 changed files with 49 additions and 2 deletions

View File

@ -0,0 +1,12 @@
query GetLabels {
labels {
... on LabelsSuccess {
labels {
...LabelFields
}
}
... on LabelsError {
errorCodes
}
}
}

View File

@ -0,0 +1,8 @@
package app.omnivore.omnivore.dataService
import app.omnivore.omnivore.networking.savedItemLabels
suspend fun DataService.syncLabels() {
val fetchedLabels = networker.savedItemLabels()
db.savedItemLabelDao().insertAll(fetchedLabels)
}

View File

@ -0,0 +1,24 @@
package app.omnivore.omnivore.networking
import app.omnivore.omnivore.graphql.generated.GetLabelsQuery
import app.omnivore.omnivore.persistence.entities.SavedItemLabel
suspend fun Networker.savedItemLabels(): List<SavedItemLabel> {
try {
val result = authenticatedApolloClient().query(GetLabelsQuery()).execute()
val labels = result.data?.labels?.onLabelsSuccess?.labels ?: listOf()
return labels.map {
SavedItemLabel(
savedItemLabelId = it.labelFields.id,
name = it.labelFields.name,
color = it.labelFields.color,
createdAt = it.labelFields.createdAt as String?,
labelDescription = it.labelFields.description
)
}
} catch (e: java.lang.Exception) {
return listOf()
}
}

View File

@ -51,8 +51,11 @@ class LibraryViewModel @Inject constructor(
if (hasLoadedInitialFilters) { return }
hasLoadedInitialFilters = false
// TODO: Fetch all labels
viewModelScope.launch {
withContext(Dispatchers.IO) {
dataService.syncLabels()
}
}
runBlocking {
datastoreRepo.getString(DatastoreKeys.lastUsedSavedItemFilter)?.let { str ->