Skip to content

Commit

Permalink
allow pre-filling new profile from existing profile
Browse files Browse the repository at this point in the history
  • Loading branch information
Helium314 committed Nov 22, 2021
1 parent 00c1bd0 commit 1195579
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<QuestPresetsSource.Listener>()
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<QuestPreset>()
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)
Expand All @@ -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)
Expand Down

0 comments on commit 1195579

Please sign in to comment.