From 1195579ab09d47f319dc883810c47a4240493d14 Mon Sep 17 00:00:00 2001 From: Helium Date: Mon, 22 Nov 2021 21:47:20 +0100 Subject: [PATCH] allow pre-filling new profile from existing profile --- README.md | 1 + .../visiblequests/QuestPresetsController.kt | 14 ++++++++- .../questselection/QuestPresetsFragment.kt | 29 +++++++++++++++++-- 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 83df06165f..9c98c4e300 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,7 @@ New quests may require knowledge about the changed/added OSM tags. * allow creating text notes in a gpx file * notes.gpx file must be copied manually from Android/data/de.westnordost.streetcomplete.h3/files * button for normal OSM notes is hidden if keyboard is shown. sorry. +* allow pre-filling new profile from existing profile * delete cache: choose between deleting tile cache (background) or OSM data (quests) * option to show quest geometries even without selecting a quest pin (ways are shown at intermediate zoom, areas at high zoom) * option to zoom in/out using volume buttons diff --git a/app/src/main/java/de/westnordost/streetcomplete/data/visiblequests/QuestPresetsController.kt b/app/src/main/java/de/westnordost/streetcomplete/data/visiblequests/QuestPresetsController.kt index b858516cd5..53374da50b 100644 --- a/app/src/main/java/de/westnordost/streetcomplete/data/visiblequests/QuestPresetsController.kt +++ b/app/src/main/java/de/westnordost/streetcomplete/data/visiblequests/QuestPresetsController.kt @@ -6,7 +6,9 @@ import javax.inject.Singleton @Singleton class QuestPresetsController @Inject constructor( private val questPresetsDao: QuestPresetsDao, - private val selectedQuestPresetStore: SelectedQuestPresetStore + private val selectedQuestPresetStore: SelectedQuestPresetStore, + private val questTypeOrderDao: QuestTypeOrderDao, + private val visibleQuestTypeDao: VisibleQuestTypeDao ): QuestPresetsSource { private val listeners = CopyOnWriteArrayList() @@ -27,6 +29,16 @@ import javax.inject.Singleton return presetId } + fun add(presetName: String, copyFromId: Long): Long { + val presetId = questPresetsDao.add(presetName) + onAddedQuestPreset(presetId, presetName) + val order = questTypeOrderDao.getAll(copyFromId) + order.forEach { questTypeOrderDao.put(presetId, it) } + val visibilities = visibleQuestTypeDao.getAll(copyFromId) + visibilities.forEach { visibleQuestTypeDao.put(presetId, it.key, it.value) } + return presetId + } + fun delete(presetId: Long) { if (presetId == selectedId) { selectedId = 0 diff --git a/app/src/main/java/de/westnordost/streetcomplete/settings/questselection/QuestPresetsFragment.kt b/app/src/main/java/de/westnordost/streetcomplete/settings/questselection/QuestPresetsFragment.kt index b44d2082d7..a4a9d30af4 100644 --- a/app/src/main/java/de/westnordost/streetcomplete/settings/questselection/QuestPresetsFragment.kt +++ b/app/src/main/java/de/westnordost/streetcomplete/settings/questselection/QuestPresetsFragment.kt @@ -7,6 +7,7 @@ import androidx.fragment.app.Fragment import de.westnordost.streetcomplete.HasTitle import de.westnordost.streetcomplete.Injector import de.westnordost.streetcomplete.R +import de.westnordost.streetcomplete.data.visiblequests.QuestPreset import de.westnordost.streetcomplete.data.visiblequests.QuestPresetsController import de.westnordost.streetcomplete.databinding.DialogInputTextBinding import de.westnordost.streetcomplete.databinding.FragmentQuestPresetsBinding @@ -34,10 +35,31 @@ class QuestPresetsFragment : Fragment(R.layout.fragment_quest_presets), HasTitle override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) binding.questPresetsList.adapter = questPresetsAdapter - binding.addPresetButton.setOnClickListener { onClickAddPreset() } + binding.addPresetButton.setOnClickListener { showProfileSelector() } } - private fun onClickAddPreset() { + private fun showProfileSelector() { + val c = context ?: return + val presets = mutableListOf() + presets.add(QuestPreset(0, c.getString(R.string.quest_presets_default_name))) + presets.addAll(questPresetsController.getAll()) + var dialog: AlertDialog? = null + val array = presets.map { it.name }.toTypedArray() + val builder = AlertDialog.Builder(c) + .setTitle("Copy from another profile?") + .setSingleChoiceItems(array, -1) { _, i -> + dialog?.dismiss() + onClickAddPreset(presets[i].id) + } + .setNegativeButton("empty profile") { _, _ -> + dialog?.dismiss() + onClickAddPreset(null) + } + dialog = builder.create() + dialog.show() + } + + private fun onClickAddPreset(copyFrom: Long?) { val ctx = context ?: return val dialogBinding = DialogInputTextBinding.inflate(layoutInflater) @@ -49,7 +71,8 @@ class QuestPresetsFragment : Fragment(R.layout.fragment_quest_presets), HasTitle .setPositiveButton(android.R.string.ok) { _,_ -> val name = dialogBinding.editText.text.toString().trim() viewLifecycleScope.launch(Dispatchers.IO) { - questPresetsController.add(name) + if (copyFrom == null) questPresetsController.add(name) + else questPresetsController.add(name, copyFrom) } } .setNegativeButton(android.R.string.cancel, null)