Skip to content

Commit a1af824

Browse files
committed
Use geojson source for paths
Signed-off-by: Kyle Corry <kylecorry31@gmail.com>
1 parent 1c2e430 commit a1af824

7 files changed

Lines changed: 49 additions & 76 deletions

File tree

app/src/main/java/com/kylecorry/trail_sense/tools/map/ui/MapToolLayerManager.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ import com.kylecorry.trail_sense.tools.beacons.map_layers.BeaconLayerManager
3939
import com.kylecorry.trail_sense.tools.navigation.infrastructure.Navigator
4040
import com.kylecorry.trail_sense.tools.navigation.map_layers.NavigationLayer
4141
import com.kylecorry.trail_sense.tools.navigation.map_layers.NavigationLayerManager
42-
import com.kylecorry.trail_sense.tools.paths.map_layers.LegacyPathLayer
4342
import com.kylecorry.trail_sense.tools.paths.map_layers.PathLayer
4443
import com.kylecorry.trail_sense.tools.paths.map_layers.PathLayerManager
4544
import com.kylecorry.trail_sense.tools.photo_maps.infrastructure.tiles.PhotoMapRegionLoader
@@ -167,7 +166,6 @@ class MapToolLayerManager {
167166
layerManager = MultiLayerManager(
168167
listOfNotNull(
169168
if (prefs.map.pathLayer.isEnabled.get()) PathLayerManager(
170-
context,
171169
pathLayer
172170
) else null,
173171
if (prefs.map.myLocationLayer.isEnabled.get()) MyLocationLayerManager(

app/src/main/java/com/kylecorry/trail_sense/tools/navigation/ui/layers/NavigationCompassLayerManager.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ class NavigationCompassLayerManager {
9797

9898
layerManager = MultiLayerManager(
9999
listOfNotNull(
100-
if (isPathLayerEnabled) PathLayerManager(context, pathLayer) else null,
100+
if (isPathLayerEnabled) PathLayerManager(pathLayer) else null,
101101
if (isMyLocationLayerEnabled) MyLocationLayerManager(
102102
myLocationLayer,
103103
Color.WHITE,
Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,49 @@
11
package com.kylecorry.trail_sense.tools.paths.map_layers
22

3+
import android.content.Context
4+
import com.kylecorry.andromeda.core.cache.AppServiceRegistry
35
import com.kylecorry.andromeda.geojson.GeoJsonFeature
46
import com.kylecorry.andromeda.geojson.GeoJsonFeatureCollection
57
import com.kylecorry.andromeda.geojson.GeoJsonObject
68
import com.kylecorry.sol.science.geology.CoordinateBounds
79
import com.kylecorry.trail_sense.shared.extensions.lineString
810
import com.kylecorry.trail_sense.shared.map_layers.ui.layers.geojson.sources.GeoJsonSource
9-
import com.kylecorry.trail_sense.tools.navigation.ui.IMappablePath
11+
import com.kylecorry.trail_sense.tools.paths.domain.Path
12+
import com.kylecorry.trail_sense.tools.paths.infrastructure.PathLoader
13+
import com.kylecorry.trail_sense.tools.paths.infrastructure.persistence.PathService
14+
import com.kylecorry.trail_sense.tools.paths.ui.asMappable
15+
import kotlinx.coroutines.flow.first
1016

1117
class PathGeoJsonSource : GeoJsonSource {
1218

13-
var paths: List<IMappablePath> = emptyList()
19+
private val context = AppServiceRegistry.get<Context>()
20+
private val pathService = AppServiceRegistry.get<PathService>()
21+
private val pathLoader = PathLoader(pathService)
22+
private var paths = emptyList<Path>()
23+
private var loaded = false
1424

1525
override suspend fun load(
1626
bounds: CoordinateBounds,
1727
metersPerPixel: Float
1828
): GeoJsonObject? {
19-
return GeoJsonFeatureCollection(paths.map {
29+
// If paths haven't been loaded yet, load them
30+
if (paths.isEmpty()) {
31+
paths = pathService.getPaths().first().filter { it.style.visible }
32+
}
33+
34+
pathLoader.update(paths, bounds, bounds, !loaded)
35+
loaded = true
36+
37+
val points = pathLoader.getPointsWithBacktrack(context)
38+
39+
val mappablePaths = points.mapNotNull {
40+
val path =
41+
paths.firstOrNull { p -> p.id == it.key } ?: return@mapNotNull null
42+
43+
it.value.asMappable(context, path)
44+
}
45+
46+
return GeoJsonFeatureCollection(mappablePaths.map {
2047
GeoJsonFeature.lineString(
2148
it.points.map { point -> point.coordinate },
2249
it.id,
@@ -27,4 +54,9 @@ class PathGeoJsonSource : GeoJsonSource {
2754
)
2855
})
2956
}
57+
58+
fun reload() {
59+
loaded = false
60+
paths = emptyList()
61+
}
3062
}

app/src/main/java/com/kylecorry/trail_sense/tools/paths/map_layers/PathLayer.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ class PathLayer : GeoJsonLayer<PathGeoJsonSource>(PathGeoJsonSource()) {
1414
renderer.configureLineStringRenderer(shouldRenderWithDrawLines = shouldRenderWithDrawLines)
1515
}
1616

17-
fun setPaths(paths: List<IMappablePath>) {
18-
source.paths = paths
17+
fun reload() {
18+
source.reload()
1919
invalidate()
2020
}
2121
}
Lines changed: 10 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,106 +1,50 @@
11
package com.kylecorry.trail_sense.tools.paths.map_layers
22

3-
import android.content.Context
4-
import com.kylecorry.andromeda.core.coroutines.onDefault
3+
import com.kylecorry.andromeda.core.cache.AppServiceRegistry
54
import com.kylecorry.luna.coroutines.CoroutineQueueRunner
6-
import com.kylecorry.sol.science.geology.CoordinateBounds
75
import com.kylecorry.sol.units.Coordinate
8-
import com.kylecorry.trail_sense.tools.paths.domain.Path
9-
import com.kylecorry.trail_sense.tools.paths.domain.PathPoint
10-
import com.kylecorry.trail_sense.tools.paths.domain.hiking.HikingService
11-
import com.kylecorry.trail_sense.tools.paths.infrastructure.PathLoader
12-
import com.kylecorry.trail_sense.tools.paths.infrastructure.persistence.PathService
13-
import com.kylecorry.trail_sense.tools.paths.ui.IPathLayer
14-
import com.kylecorry.trail_sense.tools.paths.ui.asMappable
156
import com.kylecorry.trail_sense.shared.map_layers.ui.layers.BaseLayerManager
7+
import com.kylecorry.trail_sense.tools.paths.infrastructure.persistence.PathService
168
import kotlinx.coroutines.CoroutineScope
179
import kotlinx.coroutines.Dispatchers
1810
import kotlinx.coroutines.cancel
1911
import kotlinx.coroutines.launch
2012

2113
class PathLayerManager(
22-
private val context: Context,
23-
private val layer: PathLayer,
24-
private val shouldCorrectElevations: Boolean = false
14+
private val layer: PathLayer
2515
) :
2616
BaseLayerManager() {
2717

28-
private val pathService = PathService.Companion.getInstance(context)
29-
private val hikingService = HikingService()
30-
private val pathLoader = PathLoader(pathService)
31-
private var paths = emptyList<Path>()
18+
private val pathService = AppServiceRegistry.get<PathService>()
3219
private val scope = CoroutineScope(Dispatchers.Default)
33-
private val loadRunner = CoroutineQueueRunner(2, scope)
3420
private val listenerRunner = CoroutineQueueRunner(scope = scope)
35-
private var loaded = false
3621
private var wasBacktrackOn = false
3722

23+
3824
override fun start() {
39-
loaded = false
4025
scope.launch {
4126
listenerRunner.skipIfRunning {
4227
pathService.getPaths().collect {
43-
paths = it.filter { path -> path.style.visible }
4428
wasBacktrackOn = pathService.getBacktrackPathId() != null
45-
loaded = false
46-
loadRunner.replace {
47-
loadPaths(true)
48-
}
29+
// Paths changed, so we need to reload the paths
30+
layer.reload()
4931
}
5032
}
5133
}
5234
}
5335

5436
override fun stop() {
5537
listenerRunner.cancel()
56-
loadRunner.cancel()
5738
scope.cancel()
5839
}
5940

60-
override fun onBoundsChanged(bounds: CoordinateBounds?) {
61-
super.onBoundsChanged(bounds)
62-
scope.launch {
63-
loadRunner.enqueue {
64-
loadPaths(false)
65-
}
66-
}
67-
}
6841

6942
override fun onLocationChanged(location: Coordinate, accuracy: Float?) {
7043
super.onLocationChanged(location, accuracy)
71-
if (!wasBacktrackOn){
44+
if (!wasBacktrackOn) {
7245
return
7346
}
74-
scope.launch {
75-
loadRunner.enqueue {
76-
loadPaths(false)
77-
}
78-
}
79-
}
80-
81-
private suspend fun loadPaths(reload: Boolean) = onDefault {
82-
bounds?.let {
83-
pathLoader.update(paths, it, it, reload || !loaded)
84-
loaded = true
85-
}
86-
87-
val points = pathLoader.getPointsWithBacktrack(context)
88-
onPathsChanged(paths, points)
89-
}
90-
91-
private fun onPathsChanged(paths: List<Path>, points: Map<Long, List<PathPoint>>) {
92-
val mappablePaths = points.mapNotNull {
93-
val path =
94-
paths.firstOrNull { p -> p.id == it.key } ?: return@mapNotNull null
95-
96-
val correctedPoints = if (shouldCorrectElevations) {
97-
hikingService.correctElevations(it.value.sortedBy { it.id }).reversed()
98-
} else {
99-
it.value
100-
}
101-
102-
correctedPoints.asMappable(context, path)
103-
}
104-
layer.setPaths(mappablePaths)
47+
// Location changed while backtrack is on, so we need to update the current location
48+
layer.invalidate()
10549
}
10650
}

app/src/main/java/com/kylecorry/trail_sense/tools/photo_maps/ui/PhotoMapCalibrationFragment.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ class PhotoMapCalibrationFragment : BoundFragment<FragmentPhotoMapCalibrationBin
8888
super.onResume()
8989
layerManager = MultiLayerManager(
9090
listOf(
91-
PathLayerManager(requireContext(), pathLayer),
91+
PathLayerManager(pathLayer),
9292
MyLocationLayerManager(
9393
myLocationLayer,
9494
Resources.getPrimaryMarkerColor(requireContext()),

app/src/main/java/com/kylecorry/trail_sense/tools/photo_maps/ui/PhotoMapToolLayerManager.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,6 @@ class PhotoMapToolLayerManager {
147147
layerManager = MultiLayerManager(
148148
listOfNotNull(
149149
if (prefs.photoMaps.pathLayer.isEnabled.get()) PathLayerManager(
150-
context,
151150
pathLayer
152151
) else null,
153152
if (prefs.photoMaps.myLocationLayer.isEnabled.get()) MyLocationLayerManager(

0 commit comments

Comments
 (0)