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

Fix element highlighting in amenity_coverage #5212

Merged
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 @@ -246,7 +246,7 @@ fun questTypeRegistry(
9 to AddCarWashType(),

10 to AddBenchBackrest(),
11 to AddAmenityCover(),
11 to AddAmenityCover(featureDictionaryFuture),

12 to AddBridgeStructure(),

Expand Down
Original file line number Diff line number Diff line change
@@ -1,40 +1,63 @@
package de.westnordost.streetcomplete.quests.amenity_cover

import de.westnordost.osmfeatures.FeatureDictionary
import de.westnordost.streetcomplete.R
import de.westnordost.streetcomplete.data.elementfilter.toElementFilterExpression
import de.westnordost.streetcomplete.data.osm.geometry.ElementGeometry
import de.westnordost.streetcomplete.data.osm.mapdata.Element
import de.westnordost.streetcomplete.data.osm.mapdata.MapDataWithGeometry
import de.westnordost.streetcomplete.data.osm.mapdata.filter
import de.westnordost.streetcomplete.data.osm.osmquests.OsmFilterQuestType
import de.westnordost.streetcomplete.data.osm.osmquests.OsmElementQuestType
import de.westnordost.streetcomplete.data.user.achievements.EditTypeAchievement.OUTDOORS
import de.westnordost.streetcomplete.osm.Tags
import de.westnordost.streetcomplete.quests.YesNoQuestForm
import de.westnordost.streetcomplete.util.ktx.toYesNo
import java.util.concurrent.FutureTask

class AddAmenityCover : OsmFilterQuestType<Boolean>() {
class AddAmenityCover (
private val featureDictionaryFuture: FutureTask<FeatureDictionary>
) : OsmElementQuestType<Boolean> {

override val elementFilter = """
private val nodesFilter by lazy { """
nodes with
(leisure = picnic_table
or amenity = bbq)
and access !~ private|no
and !covered
and (!seasonal or seasonal = no)
"""
""".toElementFilterExpression() }
override val changesetComment = "Specify whether various amenities are covered"
override val wikiLink = "Key:covered"
override val icon = R.drawable.ic_quest_picnic_table_cover
override val isDeleteElementEnabled = true
override val achievements = listOf(OUTDOORS)

override fun getTitle(tags: Map<String, String>) = R.string.quest_amenityCover_title
override fun getApplicableElements(mapData: MapDataWithGeometry): Iterable<Element> =
mapData.filter { isApplicableTo(it) }

override fun getHighlightedElements(element: Element, getMapData: () -> MapDataWithGeometry) =
getMapData().filter("nodes with leisure = picnic_table")
override fun isApplicableTo(element: Element) =
nodesFilter.matches(element) && hasAnyName(element.tags)

private fun hasAnyName(tags: Map<String, String>): Boolean =
featureDictionaryFuture.get().byTags(tags).isSuggestion(false).find().isNotEmpty()

override fun getHighlightedElements(element: Element, getMapData: () -> MapDataWithGeometry): Sequence<Element> {
/* put markers for objects that are exactly the same as for which this quest is asking for
e.g. it's a ticket validator? -> display other ticket validators. Etc. */
val feature = featureDictionaryFuture.get()
.byTags(element.tags)
.isSuggestion(false) // not brands
.find()
.firstOrNull() ?: return emptySequence()

return getMapData().filter { it.tags.containsAll(feature.tags) }.asSequence()
}

override fun createForm() = YesNoQuestForm()

override fun applyAnswerTo(answer: Boolean, tags: Tags, geometry: ElementGeometry, timestampEdited: Long) {
tags["covered"] = answer.toYesNo()
}
}

private fun <X, Y> Map<X, Y>.containsAll(other: Map<X, Y>) = other.all { this[it.key] == it.value }