Skip to content
This repository was archived by the owner on Aug 16, 2025. It is now read-only.

Commit b05249b

Browse files
committed
Refactor WaypointReader
1 parent 2204635 commit b05249b

File tree

4 files changed

+30
-33
lines changed

4 files changed

+30
-33
lines changed

src/main/kotlin/de/storchp/opentracks/osmplugin/dashboardapi/Waypoint.kt

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package de.storchp.opentracks.osmplugin.dashboardapi
22

33
import android.content.ContentResolver
4+
import android.content.Intent
45
import android.net.Uri
6+
import de.storchp.opentracks.osmplugin.map.MapData
57
import de.storchp.opentracks.osmplugin.map.MapUtils
68
import org.oscim.core.GeoPoint
79
import java.net.URLDecoder
@@ -14,7 +16,7 @@ data class Waypoint(
1416
val category: String? = null,
1517
val icon: String? = null,
1618
val trackId: Long = 0,
17-
val latLong: GeoPoint? = null,
19+
val latLong: GeoPoint,
1820
val photoUrl: String? = null,
1921
)
2022

@@ -47,11 +49,7 @@ object WaypointReader {
4749
val QUERY_POSITION_PATTERN: Pattern =
4850
Pattern.compile("q=([+-]?\\d+(?:\\.\\d+)?),\\s?([+-]?\\d+(?:\\.\\d+)?)")
4951

50-
fun fromGeoUri(uri: String?): Waypoint? {
51-
if (uri == null) {
52-
return null
53-
}
54-
52+
fun fromGeoUri(uri: String): Waypoint? {
5553
var schemeSpecific = uri.substring(uri.indexOf(":") + 1)
5654

5755
var name: String? = null
@@ -133,5 +131,16 @@ object WaypointReader {
133131
}
134132
}
135133
}
134+
135+
fun fromGeoIntent(intent: Intent, mapData: MapData) {
136+
require(intent.isGeoIntent())
137+
138+
fromGeoUri(intent.data.toString())
139+
?.let { waypoint ->
140+
mapData.addWaypointMarker(waypoint)
141+
mapData.updateMapPositionAndZoomLevel(waypoint.latLong, 15)
142+
}
143+
}
136144
}
137145

146+
fun Intent.isGeoIntent() = "geo" == scheme && data != null

src/main/kotlin/de/storchp/opentracks/osmplugin/map/MapActivity.kt

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import de.storchp.opentracks.osmplugin.dashboardapi.UpdateTrackStatistics
3232
import de.storchp.opentracks.osmplugin.dashboardapi.UpdateTrackpointsDebug
3333
import de.storchp.opentracks.osmplugin.dashboardapi.WaypointReader
3434
import de.storchp.opentracks.osmplugin.dashboardapi.isDashboardAction
35+
import de.storchp.opentracks.osmplugin.dashboardapi.isGeoIntent
3536
import de.storchp.opentracks.osmplugin.databinding.ActivityMapsBinding
3637
import de.storchp.opentracks.osmplugin.utils.PreferencesUtils
3738
import okhttp3.Cache
@@ -183,16 +184,8 @@ open class MapsActivity : BaseActivity(), OnItemGestureListener<MarkerInterface?
183184
showOnLockScreen(showOnLockScreen)
184185
showFullscreen(showFullscreen)
185186
}
186-
} else if ("geo" == intent.scheme && intent.data != null) {
187-
WaypointReader.fromGeoUri(intent.data.toString())
188-
?.let { waypoint ->
189-
val marker = MapUtils.createTappableMarker(this, waypoint)
190-
waypointsLayer!!.addItem(marker)
191-
val pos = map.getMapPosition()
192-
.setPosition(waypoint.latLong)
193-
.setZoomLevel(15)
194-
map.animator().animateTo(pos)
195-
}
187+
} else if (intent.isGeoIntent()) {
188+
WaypointReader.fromGeoIntent(intent, mapData!!)
196189
}
197190
}
198191

src/main/kotlin/de/storchp/opentracks/osmplugin/map/MapData.kt

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ data class MapData(
6868
}
6969

7070
fun addPauseMarker(geoPoint: GeoPoint) {
71-
val marker = MapUtils.createMarker(geoPoint, pauseMarkerSymbol)
71+
val marker = MapUtils.createMarker(latLong = geoPoint, markerSymbol = pauseMarkerSymbol)
7272
waypointsLayer.addItem(marker)
7373
}
7474

@@ -94,17 +94,25 @@ data class MapData(
9494
fun getBearing() = mapMode.getBearing(movementDirection)
9595

9696
fun updateMapPositionAndRotation(myPos: GeoPoint) {
97-
val newPos = map.getMapPosition().setPosition(myPos)
97+
val newPos = map.getMapPosition()
98+
.setPosition(myPos)
9899
.setBearing(getBearing())
99100
map.animator().animateTo(newPos)
100101
}
101102

103+
fun updateMapPositionAndZoomLevel(myPos: GeoPoint, zoom: Int) {
104+
val newPos = map.getMapPosition()
105+
.setPosition(myPos)
106+
.setZoomLevel(zoom)
107+
map.animator().animateTo(newPos)
108+
}
109+
102110
fun renderMap() {
103111
map.render()
104112
}
105113

106114
fun addWaypointMarker(waypoint: Waypoint) {
107-
val marker = MapUtils.createMarker(waypoint.id, waypoint.latLong!!, waypointMarkerSymbol)
115+
val marker = MapUtils.createMarker(waypoint.id, waypoint.latLong, waypointMarkerSymbol)
108116
waypointsLayer.addItem(marker)
109117
}
110118

src/main/kotlin/de/storchp/opentracks/osmplugin/map/MapUtils.kt

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import android.util.Log
99
import androidx.core.content.ContextCompat
1010
import de.storchp.opentracks.osmplugin.R
1111
import de.storchp.opentracks.osmplugin.dashboardapi.Trackpoint
12-
import de.storchp.opentracks.osmplugin.dashboardapi.Waypoint
1312
import de.storchp.opentracks.osmplugin.utils.UnitConversions
1413
import org.oscim.android.canvas.AndroidBitmap
1514
import org.oscim.core.GeoPoint
@@ -220,18 +219,8 @@ object MapUtils {
220219
billboard
221220
)
222221

223-
fun createPushpinMarker(context: Context, latLong: GeoPoint?, id: Long?) =
222+
fun createMarker(id: Long? = null, latLong: GeoPoint, markerSymbol: MarkerSymbol) =
224223
MarkerItem(id, latLong.toString(), "", latLong).apply {
225-
marker = createPushpinSymbol(context)
226-
}
227-
228-
fun createMarker(id: Long, latLong: GeoPoint, markerSymbol: MarkerSymbol) =
229-
MarkerItem(id, latLong.toString(), "", latLong).apply {
230-
marker = markerSymbol
231-
}
232-
233-
fun createMarker(latLong: GeoPoint, markerSymbol: MarkerSymbol) =
234-
MarkerItem(latLong.toString(), "", latLong).apply {
235224
marker = markerSymbol
236225
}
237226

@@ -256,6 +245,4 @@ object MapUtils {
256245
hotspot = HotspotPlace.BOTTOM_CENTER
257246
)
258247

259-
fun createTappableMarker(context: Context, waypoint: Waypoint) =
260-
createPushpinMarker(context, waypoint.latLong, waypoint.id)
261248
}

0 commit comments

Comments
 (0)