Skip to content

Commit

Permalink
Add internal support for path group export
Browse files Browse the repository at this point in the history
  • Loading branch information
kylecorry31 committed Sep 9, 2023
1 parent 25579d6 commit 57e50ae
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.kylecorry.trail_sense.navigation.paths.infrastructure.persistence
import android.content.Context
import androidx.lifecycle.LiveData
import androidx.lifecycle.map
import com.kylecorry.andromeda.core.coroutines.onIO
import com.kylecorry.trail_sense.navigation.paths.domain.PathPoint
import com.kylecorry.trail_sense.navigation.paths.domain.WaypointEntity
import com.kylecorry.trail_sense.shared.database.AppDatabase
Expand Down Expand Up @@ -71,12 +72,12 @@ class WaypointRepo private constructor(context: Context) : IWaypointRepo {
}
}

override suspend fun getAllInPaths(pathIds: List<Long>): List<PathPoint> {
override suspend fun getAllInPaths(pathIds: List<Long>): List<PathPoint> = onIO {
val points = mutableListOf<WaypointEntity>()
for (pathId in pathIds) {
points.addAll(waypointDao.getAllInPathSync(pathId))
}
return points.map { it.toPathPoint() }
points.map { it.toPathPoint() }
}

override fun getAllInPathLive(pathId: Long): LiveData<List<PathPoint>> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@ import android.content.Context
import androidx.lifecycle.LifecycleOwner
import com.kylecorry.andromeda.alerts.Alerts
import com.kylecorry.andromeda.core.coroutines.BackgroundMinimumState
import com.kylecorry.andromeda.core.coroutines.onIO
import com.kylecorry.andromeda.core.coroutines.onMain
import com.kylecorry.andromeda.fragments.inBackground
import com.kylecorry.andromeda.gpx.GPXData
import com.kylecorry.trail_sense.R
import com.kylecorry.trail_sense.navigation.paths.domain.FullPath
import com.kylecorry.trail_sense.navigation.paths.domain.IPath
import com.kylecorry.trail_sense.navigation.paths.domain.IPathService
import com.kylecorry.trail_sense.navigation.paths.domain.Path
import com.kylecorry.trail_sense.navigation.paths.domain.PathGPXConverter
import com.kylecorry.trail_sense.navigation.paths.domain.PathGroup
import com.kylecorry.trail_sense.navigation.paths.infrastructure.PathGroupLoader
import com.kylecorry.trail_sense.navigation.paths.infrastructure.persistence.PathService
import com.kylecorry.trail_sense.shared.io.IOService
import kotlinx.coroutines.Dispatchers
Expand All @@ -23,34 +28,50 @@ class ExportPathCommand(
private val lifecycleOwner: LifecycleOwner,
private val gpxService: IOService<GPXData>,
private val pathService: IPathService = PathService.getInstance(context)
) : IPathCommand {
) {

// TODO: Take in an IPath
override fun execute(path: Path) {
fun execute(path: IPath) {
lifecycleOwner.inBackground(BackgroundMinimumState.Created) {
// TODO: Load all children of the path (if any)
// TODO: Ask the user what paths to export (if the path is a Path, don't ask)
// TODO: Load all waypoints for the paths
// TODO: Assign parent groups and convert to FullPath
val waypoints = pathService.getWaypoints(path.id)
val parent = pathService.getGroup(path.parentId)
val full = FullPath(path, waypoints, parent)
val gpx = PathGPXConverter().toGPX(full)

val all = getPaths(path)
val paths = all.filterIsInstance<Path>()
val groups = all.filterIsInstance<PathGroup>().associateBy { it.id }

// TODO: Ask the user what paths to export, unless the path is a Path
val chosenIds = paths.map { it.id }

// Load the waypoints and associate them with the paths
val waypoints = pathService.getWaypoints(chosenIds)
val pathsToExport = paths
.filter { chosenIds.contains(it.id) }
.map {
val parent = it.parentId?.let { id -> groups[id] }
FullPath(it, waypoints[it.id] ?: emptyList(), parent)
}

// Export to a GPX file
val gpx = PathGPXConverter().toGPX(pathsToExport)
val exportFile = "trail-sense-${Instant.now().epochSecond}.gpx"
val success = gpxService.export(gpx, exportFile)
withContext(Dispatchers.Main) {

// Notify the user
onMain {
if (success) {
Alerts.toast(
context,
context.getString(R.string.path_exported)
)
Alerts.toast(context, context.getString(R.string.path_exported))
} else {
Alerts.toast(
context,
context.getString(R.string.export_path_error)
)
Alerts.toast(context, context.getString(R.string.export_path_error))
}
}
}
}

private suspend fun getPaths(path: IPath): List<IPath> = onIO {
if (path is Path) {
listOf(path)
} else {
listOf(path) + pathService.loader().getChildren(path.id)
}
}


}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.kylecorry.trail_sense.shared.grouping.persistence

import com.kylecorry.andromeda.core.coroutines.onIO
import com.kylecorry.trail_sense.shared.grouping.Groupable

class GroupLoader<T : Groupable>(
Expand All @@ -15,9 +16,9 @@ class GroupLoader<T : Groupable>(
return groupLoader.invoke(id)
}

private suspend fun loadChildren(id: Long?, maxDepth: Int?): List<T> {
private suspend fun loadChildren(id: Long?, maxDepth: Int?): List<T> = onIO {
if (maxDepth != null && maxDepth <= 0) {
return emptyList()
return@onIO emptyList()
}

val children = childLoader.invoke(id)
Expand All @@ -27,6 +28,6 @@ class GroupLoader<T : Groupable>(
maxDepth - 1
}
val subchildren = children.filter { it.isGroup }.flatMap { loadChildren(it.id, newDepth) }
return children + subchildren
children + subchildren
}
}

0 comments on commit 57e50ae

Please sign in to comment.