add profile fix

This commit is contained in:
SaiD 2025-12-20 21:31:04 +05:30
parent 2b8aadfc73
commit a5b5c296c3
3 changed files with 19 additions and 8 deletions

View File

@ -1,5 +1,6 @@
package com.example.livingai.pages.addprofile
import android.util.Log
import androidx.compose.runtime.State
import androidx.compose.runtime.mutableStateMapOf
import androidx.compose.runtime.mutableStateOf
@ -107,10 +108,10 @@ class AddProfileViewModel(
.launchIn(viewModelScope)
}
fun saveAnimalDetails(): Boolean {
fun saveAnimalDetails(currentId: String): Boolean {
if (!validateInputs()) return false
val id = _currentAnimalId.value ?: return false
val id = currentId
val details = AnimalDetails(
animalId = id,
@ -121,9 +122,9 @@ class AddProfileViewModel(
calvingNumber = calvingNumber.value.toIntOrNull() ?: 0,
reproductiveStatus = reproductiveStatus.value ?: "",
description = description.value,
images = photos,
images = photos.toMap(),
video = _videoUri.value ?: "",
segmentedImages = segmentedImages,
segmentedImages = segmentedImages.toMap(),
name = "",
sex = "",
weight = 0

View File

@ -1,5 +1,6 @@
package com.example.livingai.pages.navigation
import android.util.Log
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
@ -62,6 +63,14 @@ fun NavGraph(
val currentId by viewModel.currentAnimalId
val videoUri by viewModel.videoUri
LaunchedEffect(currentId) {
when {
route.loadEntry && route.animalId != null -> viewModel.loadAnimal(route.animalId)
currentId != null -> viewModel.loadAnimal(currentId!!)
else -> viewModel.initializeNewProfileIfNeeded()
}
}
val newImageUri = backStackEntry.savedStateHandle.get<String>("newImageUri")
val newImageOrientation = backStackEntry.savedStateHandle.get<String>("newImageOrientation")
val newVideoUri = backStackEntry.savedStateHandle.get<String>("newVideoUri")
@ -94,10 +103,11 @@ fun NavGraph(
AddProfileScreen(
navController = navController,
viewModel = viewModel,
animalId = route.animalId,
loadEntry = route.loadEntry,
onSave = {
val isSaved = viewModel.saveAnimalDetails()
onSave = {
Log.d("AddProfileViewModel", "Current Id: $currentId")
if (currentId == null) return@AddProfileScreen
val isSaved = viewModel.saveAnimalDetails(currentId!!)
if (isSaved)
navController.popBackStack(Route.HomeScreen, inclusive = false)
},