Skip to content

Commit d691c6c

Browse files
Fix ViewModel initialization order and chat history
This commit addresses issues related to the initialization order in PhotoReasoningViewModel: 1. Automatic Initialization: - Added an `init` block to PhotoReasoningViewModel to automatically call `initializeViewModel()` upon creation. - `initializeViewModel()` loads the system message and chat history, then sets an `_isInitialized` flag. 2. Robust `reason()` Method: - The `reason()` method now checks if the ViewModel is initialized. - If not initialized, it launches a coroutine to initialize first, then proceeds with the reasoning logic. - If already initialized, it proceeds directly. - The core logic of `reason()` has been moved to a new private method `performReasoning()`. This ensures that: - The ViewModel attempts to initialize as soon as it's created. - Calls to `reason()` will always wait for initialization to complete if it hasn't happened yet. - The system message and database entries (loaded during `loadSystemMessage` which calls `loadChatHistory` which calls `rebuildChatHistory`) are consistently included in the chat history before any user interaction that triggers `reason()`.
1 parent 3030db5 commit d691c6c

1 file changed

Lines changed: 28 additions & 2 deletions

File tree

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

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,23 @@ class PhotoReasoningViewModel(
104104
private var commandProcessingJob: Job? = null
105105
private val stopExecutionFlag = AtomicBoolean(false)
106106

107-
fun reason(
107+
init {
108+
viewModelScope.launch {
109+
initializeViewModel()
110+
}
111+
}
112+
113+
private suspend fun initializeViewModel() {
114+
loadSystemMessage(MainActivity.getInstance()?.applicationContext!!)
115+
}
116+
117+
private fun performReasoning(
108118
userInput: String,
109119
selectedImages: List<Bitmap>,
110120
screenInfoForPrompt: String? = null,
111121
imageUrisForChat: List<String>? = null
112122
) {
113-
Log.d(TAG, "reason() called. User input: '$userInput', Image count: ${selectedImages.size}, ScreenInfo: ${screenInfoForPrompt != null}, ImageUris: ${imageUrisForChat != null}")
123+
Log.d(TAG, "performReasoning() called. User input: '$userInput', Image count: ${selectedImages.size}, ScreenInfo: ${screenInfoForPrompt != null}, ImageUris: ${imageUrisForChat != null}")
114124
_uiState.value = PhotoReasoningUiState.Loading
115125
Log.d(TAG, "Setting _showStopNotificationFlow to true")
116126
_showStopNotificationFlow.value = true
@@ -197,6 +207,22 @@ class PhotoReasoningViewModel(
197207
}
198208
}
199209

210+
fun reason(
211+
userInput: String,
212+
selectedImages: List<Bitmap>,
213+
screenInfoForPrompt: String? = null,
214+
imageUrisForChat: List<String>? = null
215+
) {
216+
if (!_isInitialized.value) {
217+
viewModelScope.launch {
218+
initializeViewModel()
219+
performReasoning(userInput, selectedImages, screenInfoForPrompt, imageUrisForChat)
220+
}
221+
return
222+
}
223+
performReasoning(userInput, selectedImages, screenInfoForPrompt, imageUrisForChat)
224+
}
225+
200226
fun onStopClicked() {
201227
_showStopNotificationFlow.value = false // Hide notification immediately on stop
202228
stopExecutionFlag.set(true)

0 commit comments

Comments
 (0)