Json laden
This commit is contained in:
@@ -2,6 +2,7 @@ plugins {
|
|||||||
alias(libs.plugins.android.application)
|
alias(libs.plugins.android.application)
|
||||||
alias(libs.plugins.kotlin.android)
|
alias(libs.plugins.kotlin.android)
|
||||||
alias(libs.plugins.kotlin.compose)
|
alias(libs.plugins.kotlin.compose)
|
||||||
|
alias(libs.plugins.kotlin.serialization)
|
||||||
}
|
}
|
||||||
|
|
||||||
android {
|
android {
|
||||||
@@ -68,7 +69,7 @@ dependencies {
|
|||||||
debugImplementation(libs.androidx.compose.ui.test.manifest)
|
debugImplementation(libs.androidx.compose.ui.test.manifest)
|
||||||
implementation("androidx.compose.material:material-icons-extended:1.7.8")
|
implementation("androidx.compose.material:material-icons-extended:1.7.8")
|
||||||
implementation("com.google.android.gms:play-services-location:21.3.0")
|
implementation("com.google.android.gms:play-services-location:21.3.0")
|
||||||
|
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.0")
|
||||||
// ArcGIS Maps for Kotlin - SDK dependency
|
// ArcGIS Maps for Kotlin - SDK dependency
|
||||||
implementation(libs.arcgis.maps.kotlin)
|
implementation(libs.arcgis.maps.kotlin)
|
||||||
// Toolkit dependencies
|
// Toolkit dependencies
|
||||||
|
|||||||
105
app/src/main/java/de/jadehs/strassenschadenpro2/ListViewModel.kt
Normal file
105
app/src/main/java/de/jadehs/strassenschadenpro2/ListViewModel.kt
Normal file
@@ -0,0 +1,105 @@
|
|||||||
|
package de.jadehs.strassenschadenpro2
|
||||||
|
|
||||||
|
import android.app.Application
|
||||||
|
import androidx.lifecycle.AndroidViewModel
|
||||||
|
import androidx.lifecycle.viewModelScope
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
|
import kotlinx.coroutines.flow.StateFlow
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
|
import kotlinx.coroutines.withContext
|
||||||
|
import kotlinx.serialization.Serializable
|
||||||
|
import kotlinx.serialization.json.Json
|
||||||
|
import java.net.HttpURLConnection
|
||||||
|
import java.net.URL
|
||||||
|
|
||||||
|
class ListViewModel(application: Application): AndroidViewModel(application) {
|
||||||
|
|
||||||
|
private val _features = MutableStateFlow<List<Feature>>(emptyList())
|
||||||
|
val features: StateFlow<List<Feature>> = _features
|
||||||
|
|
||||||
|
fun fetch() {
|
||||||
|
viewModelScope.launch {
|
||||||
|
try {
|
||||||
|
val data =
|
||||||
|
loadFeatures("https://services9.arcgis.com/UVxdrlZq3S3gqt7w/ArcGIS/rest/services/StrassenSchaeden/FeatureServer/0/query?where=1%3D1&objectIds=&geometry=&geometryType=esriGeometryEnvelope&inSR=&spatialRel=esriSpatialRelIntersects&resultType=none&distance=0.0&units=esriSRUnit_Meter&outDistance=&relationParam=&returnGeodetic=false&outFields=OBJECTID%2Ctyp%2Cbeschreibung&returnGeometry=true&featureEncoding=esriDefault&multipatchOption=xyFootprint&maxAllowableOffset=&geometryPrecision=&outSR=&defaultSR=&datumTransformation=&applyVCSProjection=false&returnIdsOnly=false&returnUniqueIdsOnly=false&returnCountOnly=false&returnExtentOnly=false&returnQueryGeometry=false&returnDistinctValues=false&cacheHint=false&collation=&orderByFields=&groupByFieldsForStatistics=&returnAggIds=false&outStatistics=&having=&resultOffset=&resultRecordCount=&returnZ=false&returnM=false&returnTrueCurves=false&returnExceededLimitFeatures=true&quantizationParameters=&sqlFormat=none&f=pgeojson&token=")
|
||||||
|
_features.value = data.features
|
||||||
|
} catch (e: Exception){
|
||||||
|
e.printStackTrace()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun loadFeatures(url: String): FeatureCollection {
|
||||||
|
val jsonParser = Json { ignoreUnknownKeys = true }
|
||||||
|
val jsonText = loadJsonFromUrl(url)
|
||||||
|
return jsonParser.decodeFromString(jsonText)
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun loadJsonFromUrl(urlString: String): String{
|
||||||
|
return withContext(Dispatchers.IO){
|
||||||
|
val url = URL(urlString)
|
||||||
|
val conn = url.openConnection() as HttpURLConnection
|
||||||
|
conn.requestMethod = "GET"
|
||||||
|
conn.connectTimeout = 5000
|
||||||
|
conn.readTimeout = 5000
|
||||||
|
|
||||||
|
try {
|
||||||
|
val stream = conn.inputStream
|
||||||
|
stream.bufferedReader().use { it.readText() }
|
||||||
|
} finally {
|
||||||
|
conn.disconnect()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Serializable
|
||||||
|
data class FeatureCollection(
|
||||||
|
val type: String? = null,
|
||||||
|
val features: List<Feature> = emptyList()
|
||||||
|
)
|
||||||
|
|
||||||
|
@Serializable
|
||||||
|
data class Feature(
|
||||||
|
val type: String? = null,
|
||||||
|
val id: Int? = null,
|
||||||
|
val geometry: Geometry? = null,
|
||||||
|
val properties: Properties? = null
|
||||||
|
)
|
||||||
|
|
||||||
|
@Serializable
|
||||||
|
data class Geometry(
|
||||||
|
val type: String? = null,
|
||||||
|
val coordinates: List<Double> = emptyList()
|
||||||
|
)
|
||||||
|
|
||||||
|
@Serializable
|
||||||
|
data class Properties(
|
||||||
|
val OBJECTID: Int? = null,
|
||||||
|
val Typ: String? = null,
|
||||||
|
val Beschreibung: String? = null
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -59,10 +59,11 @@ fun MainScreen(modifier: Modifier = Modifier, application: Application) {
|
|||||||
@Composable
|
@Composable
|
||||||
fun ContentScreen(modifier: Modifier = Modifier, selectedIndex: Int, application: Application) {
|
fun ContentScreen(modifier: Modifier = Modifier, selectedIndex: Int, application: Application) {
|
||||||
val mapViewModel = remember { MapViewModel(application) }
|
val mapViewModel = remember { MapViewModel(application) }
|
||||||
|
val listViewModel = remember { ListViewModel(application) }
|
||||||
when(selectedIndex) {
|
when(selectedIndex) {
|
||||||
0 -> MapPage(modifier = modifier, mapViewModel = mapViewModel)
|
0 -> MapPage(modifier = modifier, mapViewModel = mapViewModel)
|
||||||
1 -> CreatePage(modifier = modifier, mapViewModel = mapViewModel)
|
1 -> CreatePage(modifier = modifier, mapViewModel = mapViewModel)
|
||||||
2 -> ListPage()
|
2 -> ListPage(modifier = modifier, listViewModel = listViewModel)
|
||||||
3 -> SettingsPage()
|
3 -> SettingsPage()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -131,7 +131,11 @@ fun CreatePage(modifier: Modifier = Modifier, mapViewModel: MapViewModel) {
|
|||||||
location.latitude,
|
location.latitude,
|
||||||
SpatialReference.wgs84())
|
SpatialReference.wgs84())
|
||||||
val photoDataList = getPhotosFromAlbum(context,albumViewModel)
|
val photoDataList = getPhotosFromAlbum(context,albumViewModel)
|
||||||
mapViewModel.createFeatureWithAttributesAtPoint(point,beschreibung,typ,photoDataList)
|
mapViewModel.createFeatureWithAttributesAtPoint(
|
||||||
|
point,
|
||||||
|
beschreibung,
|
||||||
|
typ,
|
||||||
|
photoDataList)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}){
|
}){
|
||||||
|
|||||||
@@ -1,10 +1,20 @@
|
|||||||
package de.jadehs.strassenschadenpro2.pages
|
package de.jadehs.strassenschadenpro2.pages
|
||||||
|
|
||||||
|
import android.util.Log
|
||||||
import androidx.compose.material3.Text
|
import androidx.compose.material3.Text
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.runtime.LaunchedEffect
|
||||||
|
import androidx.compose.runtime.collectAsState
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
|
import de.jadehs.strassenschadenpro2.ListViewModel
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun ListPage(modifier: Modifier = Modifier) {
|
fun ListPage(modifier: Modifier = Modifier, listViewModel: ListViewModel) {
|
||||||
|
val features = listViewModel.features.collectAsState()
|
||||||
|
LaunchedEffect(Unit) {
|
||||||
|
listViewModel.fetch()
|
||||||
|
Log.d("test",""+features.value.size)
|
||||||
|
}
|
||||||
|
|
||||||
Text("Liste")
|
Text("Liste")
|
||||||
}
|
}
|
||||||
@@ -35,4 +35,5 @@ arcgis-maps-kotlin-toolkit-authentication = { group = "com.esri", name = "arcgis
|
|||||||
android-application = { id = "com.android.application", version.ref = "agp" }
|
android-application = { id = "com.android.application", version.ref = "agp" }
|
||||||
kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
|
kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
|
||||||
kotlin-compose = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" }
|
kotlin-compose = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" }
|
||||||
|
kotlin-serialization = { id="org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin"}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user