Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

synchronize more on quest visibility updates #6111

Merged
merged 1 commit into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ class OsmQuestController internal constructor(
}

val obsoleteQuestKeys: List<OsmQuestKey>
val visibleQuests: Collection<OsmQuest>
synchronized(this) {
val previousQuests = db.getAllForElements(updated.map { it.key })
// quests that refer to elements that have been deleted shall be deleted
Expand All @@ -95,23 +96,26 @@ class OsmQuestController internal constructor(

obsoleteQuestKeys = getObsoleteQuestKeys(quests, previousQuests, deleteQuestKeys)
updateQuests(quests, obsoleteQuestKeys)
visibleQuests = quests.filterVisible()
}

onUpdated(added = quests, deletedKeys = obsoleteQuestKeys)
onUpdated(added = visibleQuests, deletedKeys = obsoleteQuestKeys)
}

/** Replace all quests of the given types in the given bounding box with the given quests.
* Called on download of a quest type for a bounding box. */
override fun onReplacedForBBox(bbox: BoundingBox, mapDataWithGeometry: MapDataWithGeometry) {
val quests = createQuestsForBBox(bbox, mapDataWithGeometry, allQuestTypes)
val obsoleteQuestKeys: List<OsmQuestKey>
val visibleQuests: Collection<OsmQuest>
synchronized(this) {
val previousQuests = db.getAllInBBox(bbox)
obsoleteQuestKeys = getObsoleteQuestKeys(quests, previousQuests, emptyList())
updateQuests(quests, obsoleteQuestKeys)
visibleQuests = quests.filterVisible()
}

onUpdated(added = quests, deletedKeys = obsoleteQuestKeys)
onUpdated(added = visibleQuests, deletedKeys = obsoleteQuestKeys)
}

override fun onCleared() {
Expand Down Expand Up @@ -378,17 +382,20 @@ class OsmQuestController internal constructor(
) {
if (added.isEmpty() && deletedKeys.isEmpty()) return

val visibleAdded = if (added.isNotEmpty()) {
listeners.forEach { it.onUpdated(added, deletedKeys) }
}

private fun Collection<OsmQuest>.filterVisible(): Collection<OsmQuest> =
if (isNotEmpty()) {
val hiddenIds = getHiddenQuests()
val bbox = added.map { it.position }.enclosingBoundingBox()
val bbox = map { it.position }.enclosingBoundingBox()
val hiddenPositions = getBlacklistedPositions(bbox)
added.filter { it.key !in hiddenIds && it.position.truncateTo6Decimals() !in hiddenPositions }
filter { it.key !in hiddenIds && it.position.truncateTo6Decimals() !in hiddenPositions }
} else {
added
this
}

listeners.forEach { it.onUpdated(visibleAdded, deletedKeys) }
}

private fun onInvalidated() {
listeners.forEach { it.onInvalidated() }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class VisibleQuestsSource(

private val osmQuestSourceListener = object : OsmQuestSource.Listener {
override fun onUpdated(addedQuests: Collection<OsmQuest>, deletedQuestKeys: Collection<OsmQuestKey>) {
updateVisibleQuests(addedQuests.filter(::isVisible), deletedQuestKeys)
updateVisibleQuests(addedQuests, deletedQuestKeys)
}
override fun onInvalidated() {
// apparently the visibility of many different quests have changed
Expand All @@ -55,7 +55,7 @@ class VisibleQuestsSource(

private val osmNoteQuestSourceListener = object : OsmNoteQuestSource.Listener {
override fun onUpdated(addedQuests: Collection<OsmNoteQuest>, deletedQuestIds: Collection<Long>) {
updateVisibleQuests(addedQuests.filter(::isVisible), deletedQuestIds.map { OsmNoteQuestKey(it) })
updateVisibleQuests(addedQuests, deletedQuestIds.map { OsmNoteQuestKey(it) })
}
override fun onInvalidated() {
// apparently the visibility of many different notes have changed
Expand Down Expand Up @@ -147,14 +147,21 @@ class VisibleQuestsSource(
fun trimCache() = cache.trim(SPATIAL_CACHE_TILES / 3)

private fun updateVisibleQuests(addedQuests: Collection<Quest>, deletedQuestKeys: Collection<QuestKey>) {
if (addedQuests.isEmpty() && deletedQuestKeys.isEmpty()) return
cache.update(addedQuests, deletedQuestKeys)
listeners.forEach { it.onUpdatedVisibleQuests(addedQuests, deletedQuestKeys) }
synchronized(this) {
val addedVisibleQuests = addedQuests.filter(::isVisible)
if (addedVisibleQuests.isEmpty() && deletedQuestKeys.isEmpty()) return

cache.update(addedVisibleQuests, deletedQuestKeys)

listeners.forEach { it.onUpdatedVisibleQuests(addedVisibleQuests, deletedQuestKeys) }
}
}

private fun invalidate() {
clearCache()
listeners.forEach { it.onVisibleQuestsInvalidated() }
synchronized(this) {
clearCache()
listeners.forEach { it.onVisibleQuestsInvalidated() }
}
}
}

Expand Down