Fix adventure story indexing.
This commit is contained in:
parent
1a6523f3c0
commit
ba049043c3
4 changed files with 14 additions and 5 deletions
|
|
@ -3,6 +3,7 @@ package com.pixelized.rplexicon.data.model.adventure
|
|||
import android.net.Uri
|
||||
|
||||
data class AdventureStory(
|
||||
val index: Int,
|
||||
val title: String,
|
||||
val category: String?,
|
||||
val revision: Long,
|
||||
|
|
|
|||
|
|
@ -25,6 +25,7 @@ class AdventureStoryParser @Inject constructor(
|
|||
|
||||
if (title != null && revision != null) {
|
||||
val adventure = AdventureStory(
|
||||
index = index,
|
||||
title = title,
|
||||
category = row.parse(CATEGORY),
|
||||
revision = revision,
|
||||
|
|
|
|||
|
|
@ -57,14 +57,13 @@ class AdventureDboFactory @Inject constructor() {
|
|||
|
||||
fun convertToDbo(
|
||||
story: AdventureStory,
|
||||
index: Int,
|
||||
documentId: String,
|
||||
) = AdventureStoryDbo(
|
||||
title = story.title,
|
||||
category = story.category,
|
||||
background = story.background.toString(),
|
||||
revision = story.revision,
|
||||
index = index,
|
||||
index = story.index,
|
||||
documentId = documentId,
|
||||
)
|
||||
|
||||
|
|
|
|||
|
|
@ -145,6 +145,15 @@ class AdventureRepository @Inject constructor(
|
|||
private suspend fun DatabaseUpdateScope.fetchAndCompareStories(
|
||||
documentId: String,
|
||||
) {
|
||||
fun shouldUpdate(
|
||||
cache: AdventureStoryDbo?,
|
||||
item: AdventureStoryDbo
|
||||
): Boolean {
|
||||
return cache?.let {
|
||||
it.revision < item.revision || cache.index != item.index
|
||||
} ?: false
|
||||
}
|
||||
|
||||
val cache = database.fetchAdventureStories(
|
||||
documentId = documentId,
|
||||
).associateBy {
|
||||
|
|
@ -160,7 +169,7 @@ class AdventureRepository @Inject constructor(
|
|||
|
||||
update.forEach { item ->
|
||||
when (toRemove.remove(item.id)) {
|
||||
true -> if ((cache[item.id]?.revision ?: 0) < item.revision) toUpdate.add(item)
|
||||
true -> if (shouldUpdate(cache = cache[item.id], item = item)) toUpdate.add(item)
|
||||
else -> toInsert.add(item)
|
||||
}
|
||||
}
|
||||
|
|
@ -248,10 +257,9 @@ class AdventureRepository @Inject constructor(
|
|||
return googleRepository.fetch { sheet ->
|
||||
val request = sheet.get(documentId, Adventures.ADVENTURES)
|
||||
adventureStoryParser.parse(sheet = request.execute())
|
||||
}.mapIndexed { index, story ->
|
||||
}.map { story ->
|
||||
adventureDboFactory.convertToDbo(
|
||||
story = story,
|
||||
index = index,
|
||||
documentId = documentId,
|
||||
)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue