Skip to content

Commit e453500

Browse files
Update PhotoReasoningViewModel.kt
1 parent 8641b72 commit e453500

1 file changed

Lines changed: 19 additions & 13 deletions

File tree

app/src/main/kotlin/com/google/ai/sample/feature/multimodal/PhotoReasoningViewModel.kt

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ class PhotoReasoningViewModel(
4040
private var currentGenerativeModel: GenerativeModel = initialGenerativeModel
4141
private set
4242

43+
// --- NEU: StateFlow für den aktuellen Modellnamen ---
44+
private val _currentModelName = MutableStateFlow(initialGenerativeModel.modelName)
45+
val currentModelName: StateFlow<String> = _currentModelName.asStateFlow()
46+
47+
// --- ENTFERNT: Separater Trigger nicht mehr nötig ---
48+
// private val _modelUpdateTrigger = MutableStateFlow(0)
49+
// val modelUpdateTrigger: StateFlow<Int> = _modelUpdateTrigger.asStateFlow()
50+
4351
private val _uiState: MutableStateFlow<PhotoReasoningUiState> =
4452
MutableStateFlow(PhotoReasoningUiState.Initial)
4553
val uiState: StateFlow<PhotoReasoningUiState> =
@@ -62,10 +70,6 @@ class PhotoReasoningViewModel(
6270
private val _commandExecutionStatus = MutableStateFlow<String>("")
6371
val commandExecutionStatus: StateFlow<String> = _commandExecutionStatus.asStateFlow()
6472

65-
// --- NEU: Trigger für UI-Update nach Modellwechsel ---
66-
private val _modelUpdateTrigger = MutableStateFlow(0) // Einfacher Trigger-State
67-
val modelUpdateTrigger: StateFlow<Int> = _modelUpdateTrigger.asStateFlow()
68-
6973
// System message state
7074
private val _systemMessage = MutableStateFlow<String>("")
7175
val systemMessage: StateFlow<String> = _systemMessage.asStateFlow()
@@ -87,7 +91,8 @@ class PhotoReasoningViewModel(
8791

8892
init {
8993
Log.i(TAG, "ViewModel initialized with model: ${initialGenerativeModel.modelName}")
90-
// Context wird jetzt in loadSystemMessage übergeben, das von der Route aufgerufen wird
94+
// Setze den initialen Namen im StateFlow (redundant, da schon im Konstruktor, aber sicher)
95+
_currentModelName.value = initialGenerativeModel.modelName
9196
}
9297

9398

@@ -111,9 +116,7 @@ class PhotoReasoningViewModel(
111116
text = userInput,
112117
participant = PhotoParticipant.USER,
113118
isPending = false,
114-
// TODO: Hier müssten die URIs der 'selectedImages' übergeben werden,
115-
// wenn sie im Chat angezeigt werden sollen. Aktuell werden Bitmaps übergeben.
116-
imageUris = emptyList() // Platzhalter
119+
imageUris = emptyList() // Placeholder
117120
)
118121
_chatState.addMessage(userMessage)
119122
_chatMessagesFlow.value = _chatState.messages // Update Flow mit neuer Liste
@@ -414,7 +417,6 @@ class PhotoReasoningViewModel(
414417

415418
if (history.isNotEmpty()) {
416419
try {
417-
// Verwende das AKTUELLE Modell zum Starten des Chats mit History
418420
chat = currentGenerativeModel.startChat(history = history)
419421
Log.d(TAG, "Chat history rebuilt successfully with ${history.size} items using model ${currentGenerativeModel.modelName}.")
420422
} catch (e: Exception) {
@@ -472,15 +474,16 @@ class PhotoReasoningViewModel(
472474
* @param newModelName The name of the new model to use.
473475
*/
474476
fun updateGenerativeModel(newModelName: String) {
475-
if (currentGenerativeModel.modelName == newModelName) {
477+
// --- GEÄNDERT: Verwende den StateFlow für die Prüfung ---
478+
if (_currentModelName.value == newModelName) {
476479
Log.i(TAG, "Model $newModelName is already active. No update needed.")
477480
_commandExecutionStatus.value = "Modell '$newModelName' ist bereits aktiv."
478481
MainActivity.getInstance()?.updateStatusMessage("Modell '$newModelName' ist bereits aktiv.", false)
479482
return
480483
}
481484

482485
viewModelScope.launch {
483-
Log.i(TAG, "Updating GenerativeModel from ${currentGenerativeModel.modelName} to: $newModelName")
486+
Log.i(TAG, "Updating GenerativeModel from ${_currentModelName.value} to: $newModelName")
484487
val context = MainActivity.getInstance()?.applicationContext
485488
if (context == null) {
486489
Log.e(TAG, "Cannot update model, application context is null.")
@@ -501,6 +504,9 @@ class PhotoReasoningViewModel(
501504

502505
ModelPreferences.saveModelName(context, newModelName)
503506

507+
// --- NEU: Aktualisiere den StateFlow mit dem neuen Namen ---
508+
_currentModelName.value = newModelName
509+
504510
_chatState.addMessage(
505511
PhotoReasoningMessage(
506512
text = "Chat-Modell wurde zu '$newModelName' gewechselt.",
@@ -511,8 +517,8 @@ class PhotoReasoningViewModel(
511517
_chatMessagesFlow.value = _chatState.messages // Update UI Flow
512518
saveChatHistory(context) // Speichern nach UI-Update
513519

514-
// <<< NEU: Trigger für die UI aktualisieren >>>
515-
_modelUpdateTrigger.value++ // Ändere den Wert, um die UI zu triggern
520+
// --- ENTFERNT: Trigger nicht mehr nötig ---
521+
// _modelUpdateTrigger.value++
516522

517523
_commandExecutionStatus.value = "Modell erfolgreich zu '$newModelName' gewechselt."
518524

0 commit comments

Comments
 (0)