|
1 | 1 | package org.scottishtecharmy.soundscape.screens.markers_routes.screens.routedetailsscreen |
2 | 2 |
|
| 3 | +import android.content.Context |
| 4 | +import android.net.Uri |
| 5 | +import androidx.core.content.FileProvider.getUriForFile |
3 | 6 | import androidx.lifecycle.ViewModel |
4 | 7 | import androidx.lifecycle.viewModelScope |
5 | 8 | import dagger.hilt.android.lifecycle.HiltViewModel |
6 | 9 | import kotlinx.coroutines.flow.MutableStateFlow |
7 | 10 | import kotlinx.coroutines.flow.StateFlow |
8 | 11 | import kotlinx.coroutines.flow.asStateFlow |
9 | 12 | import kotlinx.coroutines.launch |
| 13 | +import org.scottishtecharmy.soundscape.MainActivity |
10 | 14 | import org.scottishtecharmy.soundscape.SoundscapeServiceConnection |
11 | 15 | import org.scottishtecharmy.soundscape.database.local.dao.RouteDao |
| 16 | +import org.scottishtecharmy.soundscape.database.local.model.RouteWithMarkers |
| 17 | +import java.io.File |
| 18 | +import java.io.FileOutputStream |
12 | 19 | import javax.inject.Inject |
13 | 20 |
|
14 | 21 | @HiltViewModel |
@@ -45,6 +52,106 @@ class RouteDetailsViewModel @Inject constructor( |
45 | 52 | soundscapeServiceConnection.routeStop() |
46 | 53 | } |
47 | 54 |
|
| 55 | +// |
| 56 | +// Example JSON for shared route: |
| 57 | +// |
| 58 | +// { |
| 59 | +// "name": "Test Route", |
| 60 | +// "id": "F41302B9-A91A-4D8F-8958-6C052E334076", |
| 61 | +// "routeDescription": "", |
| 62 | +// "waypoints": [ |
| 63 | +// { |
| 64 | +// "marker": { |
| 65 | +// "nickname": "Home", |
| 66 | +// "location": { |
| 67 | +// "name": "Home", |
| 68 | +// "coordinate": { |
| 69 | +// "latitude": 55.9553596, |
| 70 | +// "longitude": -3.2031666 |
| 71 | +// } |
| 72 | +// }, |
| 73 | +// "estimatedAddress": "28A Heriot Row, Edinburgh, Scotland, EH3 6EN", |
| 74 | +// "id": "489A82CE-EF43-46F8-85FB-8D79F88763F6", |
| 75 | +// "lastUpdatedDate": 759964150.733105 |
| 76 | +// }, |
| 77 | +// "index": 0, |
| 78 | +// "markerId": "489A82CE-EF43-46F8-85FB-8D79F88763F6" |
| 79 | +// }, |
| 80 | +// ] |
| 81 | +// } |
| 82 | +// |
| 83 | + private fun generateRouteJson(route: RouteWithMarkers?, outputFile: File) { |
| 84 | + |
| 85 | + val outputStream = FileOutputStream(outputFile, false) |
| 86 | + outputStream.write( |
| 87 | + ( |
| 88 | + "{\n" + |
| 89 | + "\t\"name\": \"${route?.route?.name ?: ""}\",\n" + |
| 90 | + "\t\"id\": \"${route?.route?.routeId ?: 0L}\",\n" + |
| 91 | + "\t\"routeDescription\": \"${route?.route?.description ?: ""}\",\n" + |
| 92 | + "\t\"waypoints\": [\n" |
| 93 | + ).toByteArray() |
| 94 | + ) |
| 95 | + |
| 96 | + for ((index, marker) in (route?.markers ?: emptyList()).withIndex()) { |
| 97 | + if (index > 0) { |
| 98 | + outputStream.write(",\n".toByteArray()) |
| 99 | + } |
| 100 | + outputStream.write( |
| 101 | + ( |
| 102 | + "\t\t{\n" + |
| 103 | + "\t\t\t\"marker\": {\n" + |
| 104 | + "\t\t\t\t\"nickname\": \"${marker.name}\",\n" + |
| 105 | + "\t\t\t\t\"location\": {\n" + |
| 106 | + "\t\t\t\t\t\"name\": \"${marker.name}\",\n" + |
| 107 | + "\t\t\t\t\t\"coordinate\": {\n" + |
| 108 | + "\t\t\t\t\t\t\"latitude\": ${marker.latitude},\n" + |
| 109 | + "\t\t\t\t\t\t\"longitude\": ${marker.longitude}\n" + |
| 110 | + "\t\t\t\t\t}\n" + |
| 111 | + "\t\t\t\t},\n" + |
| 112 | + "\t\t\t\t\"estimatedAddress\": \"${marker.fullAddress}\",\n" + |
| 113 | + "\t\t\t\t\"id\": \"${marker.markerId}\"\n" + |
| 114 | + "\t\t\t},\n" + |
| 115 | + "\t\t\t\"index\": $index,\n" + |
| 116 | + "\t\t\t\"markerId\": \"${marker.markerId}\"\n" + |
| 117 | + "\t\t}" |
| 118 | + ).toByteArray() |
| 119 | + ) |
| 120 | + } |
| 121 | + outputStream.write( |
| 122 | + ( |
| 123 | + "\n\t]\n" + |
| 124 | + "}\n" |
| 125 | + ).toByteArray() |
| 126 | + ) |
| 127 | + } |
| 128 | + |
| 129 | + private fun writeRouteAndReturnUri(context: Context, route: RouteWithMarkers?) : Uri? { |
| 130 | + |
| 131 | + // Write the route to a file and share it |
| 132 | + val path = "${context.filesDir}/route/" |
| 133 | + val routeStorageDir = File(path) |
| 134 | + if (!routeStorageDir.exists()) { |
| 135 | + routeStorageDir.mkdirs() |
| 136 | + } |
| 137 | + val outputFile = File(routeStorageDir, "route.json") |
| 138 | + generateRouteJson(route, outputFile) |
| 139 | + |
| 140 | + return getUriForFile(context, "org.scottishtecharmy.fileprovider", outputFile) |
| 141 | + } |
| 142 | + |
| 143 | + |
| 144 | + fun shareRoute(context: Context, routeId: Long) { |
| 145 | + // Get the route from the database |
| 146 | + val route = routeDao.getRouteWithMarkers(routeId) |
| 147 | + if(route != null) { |
| 148 | + // Write the route to a file, and then call the MainActivity shareRoute function with a |
| 149 | + // shared URI to the file. |
| 150 | + val shareUri = writeRouteAndReturnUri(context, route) |
| 151 | + (context as MainActivity).shareRoute(shareUri) |
| 152 | + } |
| 153 | + } |
| 154 | + |
48 | 155 | fun clearErrorMessage() { |
49 | 156 | _uiState.value = _uiState.value.copy(errorMessage = null) |
50 | 157 | } |
|
0 commit comments