Compare commits
3 Commits
37d8bb064b
...
f5ac96807c
| Author | SHA1 | Date | |
|---|---|---|---|
| f5ac96807c | |||
| 91b885f67c | |||
| 6b5ea3593b |
BIN
.gitignore
vendored
BIN
.gitignore
vendored
Binary file not shown.
4
.idea/deploymentTargetSelector.xml
generated
4
.idea/deploymentTargetSelector.xml
generated
@@ -4,10 +4,10 @@
|
||||
<selectionStates>
|
||||
<SelectionState runConfigName="app">
|
||||
<option name="selectionMode" value="DROPDOWN" />
|
||||
<DropdownSelection timestamp="2025-12-19T16:43:39.005516700Z">
|
||||
<DropdownSelection timestamp="2025-12-21T13:16:32.335138100Z">
|
||||
<Target type="DEFAULT_BOOT">
|
||||
<handle>
|
||||
<DeviceId pluginId="PhysicalDevice" identifier="serial=N0AA003656K80600629" />
|
||||
<DeviceId pluginId="LocalEmulator" identifier="path=C:\Users\sklau\.android\avd\Medium_Phone.avd" />
|
||||
</handle>
|
||||
</Target>
|
||||
</DropdownSelection>
|
||||
|
||||
@@ -58,6 +58,7 @@ dependencies {
|
||||
implementation(libs.androidx.compose.ui.graphics)
|
||||
implementation(libs.androidx.compose.ui.tooling.preview)
|
||||
implementation(libs.androidx.compose.material3)
|
||||
implementation(libs.androidx.material3)
|
||||
testImplementation(libs.junit)
|
||||
androidTestImplementation(libs.androidx.junit)
|
||||
androidTestImplementation(libs.androidx.espresso.core)
|
||||
@@ -74,4 +75,8 @@ dependencies {
|
||||
implementation(platform(libs.arcgis.maps.kotlin.toolkit.bom))
|
||||
implementation(libs.arcgis.maps.kotlin.toolkit.geoview.compose)
|
||||
implementation(libs.arcgis.maps.kotlin.toolkit.authentication)
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.example.snapandsolve
|
||||
|
||||
import MapViewModel
|
||||
import android.Manifest
|
||||
import android.app.Application
|
||||
import androidx.activity.compose.rememberLauncherForActivityResult
|
||||
@@ -7,17 +8,7 @@ import androidx.activity.result.PickVisualMediaRequest
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
import androidx.compose.foundation.Image
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.height
|
||||
import androidx.compose.foundation.layout.heightIn
|
||||
import androidx.compose.foundation.layout.offset
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.*
|
||||
import androidx.compose.foundation.lazy.grid.GridCells
|
||||
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
|
||||
import androidx.compose.foundation.lazy.grid.itemsIndexed
|
||||
@@ -26,110 +17,62 @@ import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.foundation.verticalScroll
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.filled.Add
|
||||
import androidx.compose.material.icons.filled.Filter
|
||||
import androidx.compose.material.icons.filled.FilterAlt
|
||||
import androidx.compose.material.icons.filled.Menu
|
||||
import androidx.compose.material.icons.filled.Person
|
||||
import androidx.compose.material3.BottomAppBar
|
||||
import androidx.compose.material3.Button
|
||||
import androidx.compose.material3.ButtonColors
|
||||
import androidx.compose.material3.Card
|
||||
import androidx.compose.material3.CardColors
|
||||
import androidx.compose.material3.ExperimentalMaterial3Api
|
||||
import androidx.compose.material3.FabPosition
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.IconButton
|
||||
import androidx.compose.material3.LargeFloatingActionButton
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.MediumTopAppBar
|
||||
import androidx.compose.material3.OutlinedButton
|
||||
import androidx.compose.material3.Scaffold
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.material3.TopAppBarDefaults
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.collectAsState
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.material3.*
|
||||
import androidx.compose.runtime.*
|
||||
import androidx.compose.runtime.saveable.rememberSaveable
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.layout.ContentScale
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.unit.dp
|
||||
// Hier holen wir die ArcGIS Klassen
|
||||
import com.arcgismaps.mapping.ArcGISMap
|
||||
import com.arcgismaps.mapping.BasemapStyle
|
||||
import com.arcgismaps.mapping.Viewpoint
|
||||
import com.arcgismaps.toolkit.geoviewcompose.MapView
|
||||
// Hier deine eigenen Klassen (Pfade prüfen!)
|
||||
import com.example.snapandsolve.camera.AlbumViewModel
|
||||
import com.example.snapandsolve.camera.AlbumViewState
|
||||
import com.example.snapandsolve.camera.Intent
|
||||
import com.example.snapandsolve.ui.theme.AppColor
|
||||
import com.example.snapandsolve.ui.theme.ButtonColor
|
||||
import com.example.snapandsolve.ui.theme.SideSlider
|
||||
import com.example.snapandsolve.ui.theme.SliderMenuItem
|
||||
import com.example.snapandsolve.ui.theme.WidgetColor
|
||||
import com.example.snapandsolve.ui.theme.setupLocationDisplay
|
||||
import com.example.snapandsolve.ui.theme.*
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
|
||||
|
||||
@Composable
|
||||
fun MainScreen(modifier: Modifier = Modifier, application: Application) {
|
||||
var showReport by rememberSaveable { mutableStateOf(false) }
|
||||
var sliderOpen by remember { mutableStateOf(false) }
|
||||
|
||||
// ViewModel Initialisierung
|
||||
val mapViewModel = remember { MapViewModel(application) }
|
||||
|
||||
Scaffold(
|
||||
modifier = Modifier.fillMaxSize(),
|
||||
topBar = {
|
||||
AppTopBar()
|
||||
},
|
||||
topBar = { AppTopBar() },
|
||||
bottomBar = {
|
||||
BottomAppBar(
|
||||
modifier = Modifier.height(120.dp),
|
||||
containerColor = AppColor,
|
||||
) {
|
||||
Row(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(start = 16.dp),
|
||||
verticalAlignment = Alignment.CenterVertically
|
||||
) {
|
||||
IconButton(
|
||||
onClick = {
|
||||
sliderOpen = !sliderOpen
|
||||
showReport = false
|
||||
},
|
||||
modifier = Modifier.padding(bottom = 8.dp)
|
||||
) {
|
||||
Icon(
|
||||
Icons.Default.Menu,
|
||||
contentDescription = "Menu",
|
||||
)
|
||||
}
|
||||
Spacer(Modifier.weight(1f))
|
||||
IconButton(onClick = { sliderOpen = !sliderOpen; showReport = false }) {
|
||||
Icon(Icons.Default.Menu, contentDescription = "Menu")
|
||||
}
|
||||
}
|
||||
},
|
||||
floatingActionButton = {
|
||||
LargeFloatingActionButton(
|
||||
onClick = {
|
||||
showReport = true
|
||||
sliderOpen = false
|
||||
},
|
||||
onClick = { showReport = true; sliderOpen = false },
|
||||
modifier = Modifier.offset(y = 64.dp),
|
||||
containerColor = ButtonColor
|
||||
) {
|
||||
Icon(Icons.Default.Add, contentDescription = "Add")
|
||||
}
|
||||
) { Icon(Icons.Default.Add, contentDescription = "Add") }
|
||||
},
|
||||
floatingActionButtonPosition = FabPosition.Center,
|
||||
) { innerPadding ->
|
||||
ContentScreen(
|
||||
modifier = Modifier.padding(innerPadding),
|
||||
application,
|
||||
mapViewModel = mapViewModel,
|
||||
showReport = showReport,
|
||||
sliderOpen = sliderOpen,
|
||||
onDismissReport = { showReport = false })
|
||||
@@ -139,54 +82,54 @@ fun MainScreen(modifier: Modifier = Modifier, application: Application) {
|
||||
@Composable
|
||||
fun ContentScreen(
|
||||
modifier: Modifier = Modifier,
|
||||
application: Application,
|
||||
mapViewModel: MapViewModel,
|
||||
showReport: Boolean,
|
||||
sliderOpen: Boolean,
|
||||
onDismissReport: () -> Unit
|
||||
) {
|
||||
val mapViewModel = remember { MapViewModel(application) }
|
||||
val albumViewModel = remember { AlbumViewModel(Dispatchers.Default) }
|
||||
|
||||
// ArcGIS Map erstellen
|
||||
val map = remember {
|
||||
createMap() //Funktion zur Erstellung der Map
|
||||
}
|
||||
|
||||
//Standortbestimmung aus locationHelper.kt
|
||||
val locationDisplay = setupLocationDisplay()
|
||||
|
||||
// VERBINDUNG ZUM VIEWMODEL (Hier wird das GPS-Werkzeug übergeben)
|
||||
LaunchedEffect(locationDisplay) {
|
||||
mapViewModel.locationDisplay = locationDisplay
|
||||
}
|
||||
|
||||
Box(modifier = modifier.fillMaxSize()) {
|
||||
// HINTERGRUND: Die Map
|
||||
MapView(
|
||||
modifier = Modifier.fillMaxSize(),
|
||||
arcGISMap = map,
|
||||
arcGISMap = mapViewModel.map,
|
||||
mapViewProxy = mapViewModel.mapViewProxy,
|
||||
locationDisplay = locationDisplay
|
||||
)
|
||||
|
||||
// VORDERGRUND: Das Overlay (wenn showReport = true)
|
||||
if (showReport) {
|
||||
ReportOverlay(
|
||||
onCancel = onDismissReport,
|
||||
onAdd = { /* später */ },
|
||||
onAdd = { beschreibung, typ ->
|
||||
mapViewModel.createFeatureAtCurrentLocation(
|
||||
beschreibung = beschreibung,
|
||||
typ = typ,
|
||||
photos = albumViewModel.viewStateFlow.value.selectedPictures
|
||||
)
|
||||
onDismissReport()
|
||||
},
|
||||
viewModel = albumViewModel
|
||||
)
|
||||
}
|
||||
|
||||
// RECHTSGRUND: Das Slider
|
||||
SideSlider(visible = sliderOpen) {
|
||||
Text(
|
||||
"Menü",
|
||||
style = MaterialTheme.typography.titleMedium,
|
||||
modifier = Modifier.padding(bottom = 12.dp)
|
||||
)
|
||||
|
||||
SliderMenuItem(
|
||||
text = "Schäden filtern",
|
||||
icon = Icons.Default.FilterAlt,
|
||||
onClick = {
|
||||
/* TODO */
|
||||
SliderMenuItem(text = "Schäden filtern", icon = Icons.Default.FilterAlt, onClick = {})
|
||||
}
|
||||
|
||||
if (mapViewModel.snackBarMessage.isNotEmpty()) {
|
||||
Snackbar(modifier = Modifier.align(Alignment.BottomCenter).padding(bottom = 80.dp)) {
|
||||
Text(mapViewModel.snackBarMessage)
|
||||
}
|
||||
LaunchedEffect(mapViewModel.snackBarMessage) {
|
||||
kotlinx.coroutines.delay(4000)
|
||||
mapViewModel.snackBarMessage = ""
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -210,12 +153,17 @@ fun AppTopBar(
|
||||
@Composable
|
||||
fun ReportOverlay(
|
||||
onCancel: () -> Unit,
|
||||
onAdd: () -> Unit,
|
||||
onAdd: (beschreibung: String, typ: String) -> Unit,
|
||||
viewModel: AlbumViewModel
|
||||
) {
|
||||
val viewState: AlbumViewState by viewModel.viewStateFlow.collectAsState()
|
||||
val currentContext = LocalContext.current
|
||||
|
||||
// State für Beschreibung und Typ
|
||||
var beschreibung by remember { mutableStateOf("") }
|
||||
var selectedTyp by remember { mutableStateOf("Schadenstyp wählen...") }
|
||||
var dropdownExpanded by remember { mutableStateOf(false) }
|
||||
|
||||
// Launcher für Bildauswahl aus Galerie
|
||||
val pickImageFromAlbumLauncher = rememberLauncherForActivityResult(
|
||||
ActivityResultContracts.PickMultipleVisualMedia(20)
|
||||
@@ -245,19 +193,13 @@ fun ReportOverlay(
|
||||
}
|
||||
}
|
||||
|
||||
// Funktion zum Starten der Kamera (prüft Berechtigung)
|
||||
// Funktion zum Starten der Kamera
|
||||
fun startCamera() {
|
||||
println("DEBUG: startCamera() aufgerufen")
|
||||
val hasPermission = currentContext.checkSelfPermission(Manifest.permission.CAMERA) ==
|
||||
android.content.pm.PackageManager.PERMISSION_GRANTED
|
||||
println("DEBUG: Hat Berechtigung? $hasPermission")
|
||||
if (hasPermission) {
|
||||
println("DEBUG: Erstelle tempFileUrl")
|
||||
// Berechtigung bereits erteilt -> direkt tempFileUrl erstellen
|
||||
viewModel.onReceive(Intent.OnPermissionGrantedWith(currentContext))
|
||||
} else {
|
||||
println("DEBUG: Frage Berechtigung an")
|
||||
// Berechtigung anfragen
|
||||
permissionLauncher.launch(Manifest.permission.CAMERA)
|
||||
}
|
||||
}
|
||||
@@ -269,7 +211,6 @@ fun ReportOverlay(
|
||||
}
|
||||
}
|
||||
|
||||
// leichter Dim-Hintergrund
|
||||
Box(
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
@@ -295,20 +236,39 @@ fun ReportOverlay(
|
||||
.verticalScroll(rememberScrollState()),
|
||||
verticalArrangement = Arrangement.spacedBy(16.dp)
|
||||
) {
|
||||
Text(
|
||||
"Schadensbeschreibung:",
|
||||
color = Color.Black
|
||||
)
|
||||
Text("Schadensbeschreibung:", color = Color.Black)
|
||||
|
||||
// Kamera-Buttons (über der weißen Box)
|
||||
// Typ-Auswahl (Dropdown)
|
||||
Box {
|
||||
OutlinedButton(
|
||||
onClick = { dropdownExpanded = true },
|
||||
modifier = Modifier.fillMaxWidth()
|
||||
) {
|
||||
Text("Typ: $selectedTyp")
|
||||
}
|
||||
DropdownMenu(
|
||||
expanded = dropdownExpanded,
|
||||
onDismissRequest = { dropdownExpanded = false }
|
||||
) {
|
||||
listOf("Straße", "Gehweg", "Fahrradweg", "Beleuchtung","Sonstiges").forEach { typ ->
|
||||
DropdownMenuItem(
|
||||
text = { Text(typ) },
|
||||
onClick = {
|
||||
selectedTyp = typ
|
||||
dropdownExpanded = false
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Kamera-Buttons
|
||||
Row(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
horizontalArrangement = Arrangement.spacedBy(8.dp)
|
||||
) {
|
||||
Button(
|
||||
onClick = {
|
||||
startCamera()
|
||||
},
|
||||
onClick = { startCamera() },
|
||||
modifier = Modifier.weight(1f)
|
||||
) {
|
||||
Text(text = "Foto aufnehmen")
|
||||
@@ -325,15 +285,21 @@ fun ReportOverlay(
|
||||
}
|
||||
}
|
||||
|
||||
// Die weiße Textfeld Box
|
||||
Box(
|
||||
// Textfeld für Beschreibung
|
||||
TextField(
|
||||
value = beschreibung,
|
||||
onValueChange = { beschreibung = it },
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.height(220.dp)
|
||||
.background(Color.White, RoundedCornerShape(12.dp))
|
||||
.height(220.dp),
|
||||
placeholder = { Text("Beschreibung eingeben...") },
|
||||
colors = TextFieldDefaults.colors(
|
||||
focusedContainerColor = Color.White,
|
||||
unfocusedContainerColor = Color.White
|
||||
)
|
||||
)
|
||||
|
||||
// Grid für ausgewählte Bilder (außerhalb der Box, aber in der Card)
|
||||
// Bilder-Grid
|
||||
if (viewState.selectedPictures.isNotEmpty()) {
|
||||
Text(
|
||||
text = "Ausgewählte Bilder (${viewState.selectedPictures.size})",
|
||||
@@ -357,6 +323,7 @@ fun ReportOverlay(
|
||||
}
|
||||
}
|
||||
|
||||
// Buttons
|
||||
Row(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
horizontalArrangement = Arrangement.SpaceBetween
|
||||
@@ -370,31 +337,22 @@ fun ReportOverlay(
|
||||
disabledContentColor = Color.White
|
||||
)
|
||||
) {
|
||||
Text(
|
||||
"Abbrechen",
|
||||
color = Color.Black
|
||||
)
|
||||
Text("Abbrechen", color = Color.Black)
|
||||
}
|
||||
Button(
|
||||
onClick = { onAdd(beschreibung, selectedTyp) },
|
||||
enabled = beschreibung.isNotBlank()
|
||||
) {
|
||||
Text("Hinzufügen")
|
||||
}
|
||||
Button(onClick = onAdd) { Text("Hinzufügen") }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
fun createMap(): ArcGISMap {
|
||||
|
||||
return ArcGISMap(BasemapStyle.ArcGISTopographic).apply {
|
||||
|
||||
initialViewpoint = Viewpoint(
|
||||
53.14,
|
||||
8.20,
|
||||
20000.0)
|
||||
|
||||
|
||||
|
||||
initialViewpoint = Viewpoint(53.14, 8.20, 20000.0)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,64 +1,182 @@
|
||||
package com.example.snapandsolve
|
||||
|
||||
import android.app.Application
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.graphics.ImageBitmap
|
||||
import androidx.compose.ui.graphics.asAndroidBitmap
|
||||
import androidx.lifecycle.AndroidViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.arcgismaps.LoadStatus
|
||||
import com.arcgismaps.data.ArcGISFeature
|
||||
import com.arcgismaps.data.CodedValueDomain
|
||||
import com.arcgismaps.data.ServiceFeatureTable
|
||||
import com.arcgismaps.geometry.GeometryEngine
|
||||
import com.arcgismaps.geometry.Point
|
||||
import com.arcgismaps.geometry.SpatialReference
|
||||
import com.arcgismaps.mapping.ArcGISMap
|
||||
import com.arcgismaps.mapping.BasemapStyle
|
||||
import com.arcgismaps.mapping.Viewpoint
|
||||
import com.arcgismaps.mapping.layers.FeatureLayer
|
||||
import com.arcgismaps.mapping.view.LocationDisplay
|
||||
import com.arcgismaps.toolkit.geoviewcompose.MapViewProxy
|
||||
import kotlinx.coroutines.launch
|
||||
import java.io.ByteArrayOutputStream
|
||||
|
||||
class MapViewModel(application: Application ): AndroidViewModel(application) {
|
||||
class MapViewModel(application: Application) : AndroidViewModel(application) {
|
||||
val map: ArcGISMap = ArcGISMap(BasemapStyle.OpenOsmStyle).apply {
|
||||
initialViewpoint = Viewpoint(53.14, 8.20, 20000.0)
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
ALLES UNTER DIESEM KOMMENTAR WIRD NICHT GENUTZT. Aber EVENTUELL nötig zum einbinden von
|
||||
Layer und Features.
|
||||
*/
|
||||
// Hold a reference to the selected feature.
|
||||
var selectedFeature: ArcGISFeature? by mutableStateOf(null)
|
||||
val mapViewProxy = MapViewProxy()
|
||||
|
||||
//var currentFeatureOperation by mutableStateOf(FeatureOperationType.CREATE)
|
||||
|
||||
lateinit var featureLayer: FeatureLayer
|
||||
|
||||
// Create a snackbar message to display the result of feature operations.
|
||||
var snackBarMessage: String by mutableStateOf("")
|
||||
|
||||
lateinit var serviceFeatureTable: ServiceFeatureTable
|
||||
|
||||
var currentDamageType by mutableStateOf("")
|
||||
|
||||
// The list of damage types to update the feature attribute.
|
||||
var damageTypeList: List<String> = mutableListOf()
|
||||
var locationDisplay: LocationDisplay? = null
|
||||
|
||||
init {
|
||||
viewModelScope.launch {
|
||||
serviceFeatureTable = ServiceFeatureTable("https://services9.arcgis.com/UVxdrlZq3S3gqt7w/ArcGIS/rest/services/StrassenSchaeden/FeatureServer/0")
|
||||
serviceFeatureTable = ServiceFeatureTable("https://services9.arcgis.com/UVxdrlZq3S3gqt7w/arcgis/rest/services/si_StrassenSchaeden/FeatureServer/0")
|
||||
serviceFeatureTable.load().onSuccess {
|
||||
// Get the field from the feature table that will be updated.
|
||||
val typeDamageField = serviceFeatureTable.fields.first { it.name == "Typ" }
|
||||
// Get the coded value domain for the field.
|
||||
val attributeDomain = typeDamageField.domain as CodedValueDomain
|
||||
// Add the damage types to the list.
|
||||
attributeDomain.codedValues.forEach {
|
||||
val typeDamageField = serviceFeatureTable.fields.firstOrNull { it.name == "Typ" }
|
||||
val attributeDomain = typeDamageField?.domain as? CodedValueDomain
|
||||
attributeDomain?.codedValues?.forEach {
|
||||
damageTypeList += it.name
|
||||
}
|
||||
println("DEBUG: ServiceFeatureTable erfolgreich geladen")
|
||||
}.onFailure {
|
||||
println("DEBUG: Fehler beim Laden der Tabelle: ${it.message}")
|
||||
}
|
||||
featureLayer = FeatureLayer.createWithFeatureTable(serviceFeatureTable)
|
||||
map.operationalLayers.add(featureLayer)
|
||||
}
|
||||
}
|
||||
|
||||
fun createFeatureAtCurrentLocation(
|
||||
beschreibung: String,
|
||||
typ: String,
|
||||
photos: List<ImageBitmap> = emptyList()
|
||||
) {
|
||||
viewModelScope.launch {
|
||||
println("DEBUG: createFeature gestartet") // Erscheint das in der Logcat?
|
||||
try {
|
||||
// 1. Location prüfen
|
||||
val locationData = locationDisplay?.location?.value
|
||||
val gpsPoint = locationData?.position
|
||||
|
||||
if (gpsPoint == null) {
|
||||
println("DEBUG: Standort ist NULL - GPS Signal fehlt!")
|
||||
snackBarMessage = "Kein GPS Signal. Bitte kurz warten oder nach draußen gehen."
|
||||
return@launch
|
||||
}
|
||||
|
||||
println("DEBUG: GPS gefunden: x=${gpsPoint.x}, y=${gpsPoint.y}")
|
||||
|
||||
// 2. Feature erstellen
|
||||
// Wichtig: explizit WGS84, falls es nicht schon hat
|
||||
val pointWgs84 = Point(gpsPoint.x, gpsPoint.y, SpatialReference.wgs84())
|
||||
|
||||
val feature = serviceFeatureTable.createFeature().apply {
|
||||
geometry = pointWgs84
|
||||
attributes["Typ"] = typ
|
||||
attributes["Beschreibung"] = beschreibung
|
||||
}
|
||||
|
||||
println("DEBUG: Feature lokal erstellt. Sende an Server...")
|
||||
|
||||
// 3. Hochladen
|
||||
serviceFeatureTable.addFeature(feature).onSuccess {
|
||||
println("DEBUG: addFeature erfolgreich")
|
||||
applyEditsWithPhotos(feature as ArcGISFeature, photos)
|
||||
}.onFailure {
|
||||
println("DEBUG: addFeature FEHLER: ${it.message}")
|
||||
snackBarMessage = "Fehler: ${it.message}"
|
||||
}
|
||||
|
||||
} catch (e: Exception) {
|
||||
println("DEBUG: CRASH in createFeature: ${e.message}")
|
||||
e.printStackTrace()
|
||||
snackBarMessage = "Fehler: ${e.message}"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun applyEditsWithPhotos(feature: ArcGISFeature, photos: List<ImageBitmap>) {
|
||||
serviceFeatureTable.applyEdits().onSuccess { editResults ->
|
||||
val result = editResults.firstOrNull()
|
||||
if (result != null && result.error == null) {
|
||||
val serverObjectId = result.objectId
|
||||
println("DEBUG: Server-Erfolg! Echte ObjectID: $serverObjectId")
|
||||
|
||||
if (photos.isNotEmpty()) {
|
||||
// Fix: erstellen eine Abfrage für die neue ID
|
||||
val queryParameters = com.arcgismaps.data.QueryParameters().apply {
|
||||
objectIds.add(serverObjectId)
|
||||
}
|
||||
|
||||
// laden das Feature neu vom Server
|
||||
serviceFeatureTable.queryFeatures(queryParameters).onSuccess { queryResult ->
|
||||
// Ein FeatureQueryResult ist ein Iterator, nehmen das erste Element
|
||||
val fetchedFeature = queryResult.firstOrNull() as? ArcGISFeature
|
||||
|
||||
if (fetchedFeature != null) {
|
||||
addPhotosToFeature(fetchedFeature, photos, serverObjectId)
|
||||
} else {
|
||||
println("DEBUG: Feature nach Query nicht gefunden")
|
||||
snackBarMessage = "Fehler: Feature-ID $serverObjectId nicht gefunden."
|
||||
}
|
||||
}.onFailure {
|
||||
println("DEBUG: Query fehlgeschlagen: ${it.message}")
|
||||
snackBarMessage = "Fotos konnten nicht zugeordnet werden."
|
||||
}
|
||||
} else {
|
||||
snackBarMessage = "Erfolgreich gemeldet! ID: $serverObjectId"
|
||||
}
|
||||
} else {
|
||||
println("DEBUG: Server-Fehler bei applyEdits: ${result?.error?.message}")
|
||||
snackBarMessage = "Serverfehler: ${result?.error?.message}"
|
||||
}
|
||||
}.onFailure {
|
||||
println("DEBUG: applyEdits total fehlgeschlagen: ${it.message}")
|
||||
snackBarMessage = "Senden fehlgeschlagen: ${it.message}"
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun addPhotosToFeature(
|
||||
feature: ArcGISFeature,
|
||||
photos: List<ImageBitmap>,
|
||||
objectId: Long?
|
||||
) {
|
||||
println("DEBUG: Füge ${photos.size} Fotos hinzu...")
|
||||
photos.forEachIndexed { index, imageBitmap ->
|
||||
try {
|
||||
val byteArray = imageBitmapToByteArray(imageBitmap)
|
||||
feature.addAttachment(
|
||||
name = "photo_$index.jpg",
|
||||
contentType = "image/jpeg",
|
||||
data = byteArray
|
||||
)
|
||||
} catch (e: Exception) {
|
||||
println("DEBUG: Attachment-Fehler bei Foto $index: ${e.message}")
|
||||
}
|
||||
}
|
||||
|
||||
serviceFeatureTable.updateFeature(feature).onSuccess {
|
||||
println("DEBUG: Feature mit Anhängen aktualisiert. Finales applyEdits...")
|
||||
serviceFeatureTable.applyEdits().onSuccess {
|
||||
snackBarMessage = "Gespeichert mit ${photos.size} Fotos! ID: $objectId"
|
||||
}
|
||||
}.onFailure {
|
||||
println("DEBUG: updateFeature für Anhänge fehlgeschlagen: ${it.message}")
|
||||
}
|
||||
}
|
||||
|
||||
private fun imageBitmapToByteArray(imageBitmap: ImageBitmap): ByteArray {
|
||||
val stream = ByteArrayOutputStream()
|
||||
imageBitmap.asAndroidBitmap().compress(android.graphics.Bitmap.CompressFormat.JPEG, 80, stream)
|
||||
return stream.toByteArray()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user