Skip to content

Commit

Permalink
add gpx option for note creation
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed Mar 19, 2021
1 parent 93cad3c commit 0d36542
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.cancel
import kotlinx.coroutines.launch
import java.io.File
import java.util.*
import javax.inject.Inject
import kotlin.math.PI
Expand Down Expand Up @@ -412,8 +413,59 @@ class MainFragment : Fragment(R.layout.fragment_main),
}
}

override fun onCreatedNoteInstead(questId: Long, group: QuestGroup, questTitle: String, note: String) {
closeQuestDetailsFor(questId, group)
// the quest is deleted from DB on creating a note, so need to fetch quest before
val quest = questController.get(questId, group)
// how to get that ElementKey, so i can make a link?
// ...in CreateNotesUploader
if (quest != null)
onCreatedGpxNote("$questTitle: $note", quest.center)
}

/* ------------------------------- CreateNoteFragment.Listener ------------------------------ */
private fun onCreatedGpxNote(note: String, position: LatLon) {
val path = context?.getExternalFilesDir(null) ?: return
path.mkdirs()
val fileName = "notes.gpx"
val notefile = File(path,fileName)
val createdNoteFile = notefile.createNewFile()
if (createdNoteFile) // if this was a new file
notefile.writeText("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<gpx \n" +
" xmlns=\"http://www.topografix.com/GPX/1/1\" \n" +
" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \n" +
" xsi:schemaLocation=\"http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd\">\n" +
"</gpx>", Charsets.UTF_8)
// now delete the last 6 characters
val notetext = notefile.readText(Charsets.UTF_8).dropLast(6)
// then add gpx text
notefile.writeText(notetext +" <wpt lon=\"" + position.longitude + "\" lat=\"" + position.latitude + "\">\n" +
" <name>" + note.replace("&","&amp;")
.replace("<","&lt;")
.replace(">","&gt;")
.replace("\"","&quot;")
.replace("'","&apos;") + "</name>\n" +
" </wpt>\n" +
"</gpx>", Charsets.UTF_8)
}

override fun onCreatedNote(note: String, screenPosition: Point) {
closeBottomSheet()

val mapFragment = mapFragment ?: return
val mapView = mapFragment.view ?: return

val mapPosition = mapView.getLocationInWindow().toPointF()
val notePosition = PointF(screenPosition)
notePosition.offset(-mapPosition.x, -mapPosition.y)
val position = mapFragment.getPositionAt(notePosition) ?: throw NullPointerException()

onCreatedGpxNote(note,position)

listener?.onCreatedNote(screenPosition)
showMarkerSolvedAnimation(R.drawable.ic_quest_create_note, PointF(screenPosition))
}
override fun onCreatedNote(note: String, imagePaths: List<String>?, screenPosition: Point) {
closeBottomSheet()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import de.westnordost.streetcomplete.R
import de.westnordost.streetcomplete.quests.note_discussion.AttachPhotoFragment
import de.westnordost.streetcomplete.util.TextChangedWatcher
import kotlinx.android.synthetic.main.form_leave_note.*
import kotlinx.android.synthetic.main.quest_buttonpanel_done_cancel.*
import kotlinx.android.synthetic.main.quest_buttonpanel_gpx_cancel_done.*

/** Abstract base class for a bottom sheet that lets the user create a note */
abstract class AbstractCreateNoteFragment : AbstractBottomSheetFragment() {
Expand All @@ -40,7 +40,8 @@ abstract class AbstractCreateNoteFragment : AbstractBottomSheetFragment() {

val buttonPanel = view.findViewById<ViewGroup>(R.id.buttonPanel)
buttonPanel.removeAllViews()
inflater.inflate(R.layout.quest_buttonpanel_done_cancel, buttonPanel)
// inflater.inflate(R.layout.quest_buttonpanel_done_cancel, buttonPanel)
inflater.inflate(R.layout.quest_buttonpanel_gpx_cancel_done, buttonPanel)

return view
}
Expand All @@ -56,6 +57,7 @@ abstract class AbstractCreateNoteFragment : AbstractBottomSheetFragment() {

cancelButton.setOnClickListener { activity?.onBackPressed() }
doneButton.setOnClickListener { onClickOk() }
gpxButton.setOnClickListener { onComposedNote(noteText) }

updateDoneButtonEnablement()
}
Expand All @@ -73,7 +75,10 @@ abstract class AbstractCreateNoteFragment : AbstractBottomSheetFragment() {

private fun updateDoneButtonEnablement() {
doneButton.isEnabled = !noteText.isEmpty()
gpxButton.isEnabled = !noteText.isEmpty()
}

protected abstract fun onComposedNote(text: String, imagePaths: List<String>?)

protected abstract fun onComposedNote(text: String)
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class CreateNoteFragment : AbstractCreateNoteFragment() {
interface Listener {
/** Called when the user wants to leave a note which is not related to a quest */
fun onCreatedNote(note: String, imagePaths: List<String>?, screenPosition: Point)
fun onCreatedNote(note: String, screenPosition: Point)
}
private val listener: Listener? get() = parentFragment as? Listener
?: activity as? Listener
Expand Down Expand Up @@ -85,6 +86,17 @@ class CreateNoteFragment : AbstractCreateNoteFragment() {
listener?.onCreatedNote(text, imagePaths, screenPos)
}

override fun onComposedNote(text: String) {
if (closeKeyboard()) return

val screenPos = createNoteMarker.getLocationInWindow()
screenPos.offset(createNoteMarker.width / 2, createNoteMarker.height / 2)

markerLayoutContainer?.visibility = View.INVISIBLE

listener?.onCreatedNote(text, screenPos)
}

private fun closeKeyboard(): Boolean {
val imm = context?.getSystemService<InputMethodManager>()
return imm?.hideSoftInputFromWindow(noteInput.windowToken, 0) ?: false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class LeaveNoteInsteadFragment : AbstractCreateNoteFragment(), IsShowingQuestDet

interface Listener {
fun onCreatedNoteInstead(questId: Long, group: QuestGroup, questTitle: String, note: String, imagePaths: List<String>?)
fun onCreatedNoteInstead(questId: Long, group: QuestGroup, questTitle: String, note: String)
}
private val listener: Listener? get() = parentFragment as? Listener ?: activity as? Listener

Expand Down Expand Up @@ -42,6 +43,10 @@ class LeaveNoteInsteadFragment : AbstractCreateNoteFragment(), IsShowingQuestDet
listener?.onCreatedNoteInstead(questId, questGroup, questTitle, text, imagePaths)
}

override fun onComposedNote(text: String) {
listener?.onCreatedNoteInstead(questId, questGroup, questTitle, text)
}

companion object {
private const val ARG_QUEST_TITLE = "questTitle"
private const val ARG_QUEST_ID = "questId"
Expand Down
31 changes: 31 additions & 0 deletions app/src/main/res/layout/quest_buttonpanel_gpx_cancel_done.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:parentTag="com.google.android.flexbox.FlexboxLayout"
tools:flexWrap="wrap"
tools:justifyContent="space_between">

<Button
android:id="@+id/doneButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/BottomSheetButtonBarItem"
android:text="OSM note" />

<Button
android:id="@+id/cancelButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/BottomSheetButtonBarItem"
android:text="@string/confirmation_discard_negative"
/>

<Button
android:id="@+id/gpxButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/BottomSheetButtonBarItem"
android:text="GPX"
/>

</merge>

0 comments on commit 0d36542

Please sign in to comment.