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
|
import android.net.Uri
|
||||||
|
|
||||||
data class AdventureStory(
|
data class AdventureStory(
|
||||||
|
val index: Int,
|
||||||
val title: String,
|
val title: String,
|
||||||
val category: String?,
|
val category: String?,
|
||||||
val revision: Long,
|
val revision: Long,
|
||||||
|
|
|
||||||
|
|
@ -25,6 +25,7 @@ class AdventureStoryParser @Inject constructor(
|
||||||
|
|
||||||
if (title != null && revision != null) {
|
if (title != null && revision != null) {
|
||||||
val adventure = AdventureStory(
|
val adventure = AdventureStory(
|
||||||
|
index = index,
|
||||||
title = title,
|
title = title,
|
||||||
category = row.parse(CATEGORY),
|
category = row.parse(CATEGORY),
|
||||||
revision = revision,
|
revision = revision,
|
||||||
|
|
|
||||||
|
|
@ -57,14 +57,13 @@ class AdventureDboFactory @Inject constructor() {
|
||||||
|
|
||||||
fun convertToDbo(
|
fun convertToDbo(
|
||||||
story: AdventureStory,
|
story: AdventureStory,
|
||||||
index: Int,
|
|
||||||
documentId: String,
|
documentId: String,
|
||||||
) = AdventureStoryDbo(
|
) = AdventureStoryDbo(
|
||||||
title = story.title,
|
title = story.title,
|
||||||
category = story.category,
|
category = story.category,
|
||||||
background = story.background.toString(),
|
background = story.background.toString(),
|
||||||
revision = story.revision,
|
revision = story.revision,
|
||||||
index = index,
|
index = story.index,
|
||||||
documentId = documentId,
|
documentId = documentId,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -145,6 +145,15 @@ class AdventureRepository @Inject constructor(
|
||||||
private suspend fun DatabaseUpdateScope.fetchAndCompareStories(
|
private suspend fun DatabaseUpdateScope.fetchAndCompareStories(
|
||||||
documentId: String,
|
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(
|
val cache = database.fetchAdventureStories(
|
||||||
documentId = documentId,
|
documentId = documentId,
|
||||||
).associateBy {
|
).associateBy {
|
||||||
|
|
@ -160,7 +169,7 @@ class AdventureRepository @Inject constructor(
|
||||||
|
|
||||||
update.forEach { item ->
|
update.forEach { item ->
|
||||||
when (toRemove.remove(item.id)) {
|
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)
|
else -> toInsert.add(item)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -248,10 +257,9 @@ class AdventureRepository @Inject constructor(
|
||||||
return googleRepository.fetch { sheet ->
|
return googleRepository.fetch { sheet ->
|
||||||
val request = sheet.get(documentId, Adventures.ADVENTURES)
|
val request = sheet.get(documentId, Adventures.ADVENTURES)
|
||||||
adventureStoryParser.parse(sheet = request.execute())
|
adventureStoryParser.parse(sheet = request.execute())
|
||||||
}.mapIndexed { index, story ->
|
}.map { story ->
|
||||||
adventureDboFactory.convertToDbo(
|
adventureDboFactory.convertToDbo(
|
||||||
story = story,
|
story = story,
|
||||||
index = index,
|
|
||||||
documentId = documentId,
|
documentId = documentId,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue