From 26c23f3f0657369da9e6d15675ba21b436bd6864 Mon Sep 17 00:00:00 2001 From: Android PowerUser <88908510+Android-PowerUser@users.noreply.github.com> Date: Wed, 23 Apr 2025 21:04:45 +0200 Subject: [PATCH 1/2] Add files via upload --- .../ai/sample/GenerativeAiViewModelFactory.kt | 62 ++++++++++++++-- .../kotlin/com/google/ai/sample/MenuScreen.kt | 72 +++++++++++++++++++ 2 files changed, 128 insertions(+), 6 deletions(-) diff --git a/app/src/main/kotlin/com/google/ai/sample/GenerativeAiViewModelFactory.kt b/app/src/main/kotlin/com/google/ai/sample/GenerativeAiViewModelFactory.kt index 200f4fcd..e4638ae4 100644 --- a/app/src/main/kotlin/com/google/ai/sample/GenerativeAiViewModelFactory.kt +++ b/app/src/main/kotlin/com/google/ai/sample/GenerativeAiViewModelFactory.kt @@ -10,22 +10,45 @@ import com.google.ai.sample.feature.chat.ChatViewModel import com.google.ai.sample.feature.multimodal.PhotoReasoningViewModel import com.google.ai.sample.feature.text.SummarizeViewModel +// Model options +enum class ModelOption(val displayName: String, val modelName: String) { + GEMINI_FLASH_LITE("Gemini Flash Lite", "gemini-2.0-flash-lite"), + GEMINI_FLASH("Gemini Flash", "gemini-2.0-flash"), + GEMINI_FLASH_PREVIEW("Gemini 2.5 Flash Preview", "gemini-2.5-flash-preview-04-17"), + GEMINI_PRO("Gemini 2.5 Pro", "gemini-2.5-pro-exp-03-25") +} + val GenerativeViewModelFactory = object : ViewModelProvider.Factory { // Current selected model name - private var currentModelName = "gemini-2.0-flash-lite" + private var currentModelName = ModelOption.GEMINI_FLASH_LITE.modelName /** * Set the model to high reasoning capability (gemini-2.5-pro-preview-03-25) */ fun highReasoningModel() { - currentModelName = "gemini-2.5-pro-exp-03-25" + currentModelName = ModelOption.GEMINI_PRO.modelName } /** * Set the model to low reasoning capability (gemini-2.0-flash-lite) */ fun lowReasoningModel() { - currentModelName = "gemini-2.0-flash-lite" + currentModelName = ModelOption.GEMINI_FLASH_LITE.modelName + } + + /** + * Set the model to a specific model option + */ + fun setModel(modelOption: ModelOption) { + currentModelName = modelOption.modelName + } + + /** + * Get the current model option + */ + fun getCurrentModel(): ModelOption { + return ModelOption.values().find { it.modelName == currentModelName } + ?: ModelOption.GEMINI_FLASH_LITE } override fun create( @@ -93,13 +116,13 @@ val GenerativeViewModelFactory = object : ViewModelProvider.Factory { // Add companion object with static methods for easier access object GenerativeAiViewModelFactory { // Current selected model name - duplicated from GenerativeViewModelFactory - private var currentModelName = "gemini-2.0-flash-lite" + private var currentModelName = ModelOption.GEMINI_FLASH_LITE.modelName /** * Set the model to high reasoning capability (gemini-2.5-pro-preview-03-25) */ fun highReasoningModel() { - currentModelName = "gemini-2.5-pro-exp-03-25" + currentModelName = ModelOption.GEMINI_PRO.modelName // Also update the original factory to keep them in sync (GenerativeViewModelFactory as ViewModelProvider.Factory).apply { if (this is ViewModelProvider.Factory) { @@ -118,7 +141,26 @@ object GenerativeAiViewModelFactory { * Set the model to low reasoning capability (gemini-2.0-flash-lite) */ fun lowReasoningModel() { - currentModelName = "gemini-2.0-flash-lite" + currentModelName = ModelOption.GEMINI_FLASH_LITE.modelName + // Also update the original factory to keep them in sync + (GenerativeViewModelFactory as ViewModelProvider.Factory).apply { + if (this is ViewModelProvider.Factory) { + try { + val field = this.javaClass.getDeclaredField("currentModelName") + field.isAccessible = true + field.set(this, currentModelName) + } catch (e: Exception) { + // Fallback if reflection fails + } + } + } + } + + /** + * Set the model to a specific model option + */ + fun setModel(modelOption: ModelOption) { + currentModelName = modelOption.modelName // Also update the original factory to keep them in sync (GenerativeViewModelFactory as ViewModelProvider.Factory).apply { if (this is ViewModelProvider.Factory) { @@ -132,4 +174,12 @@ object GenerativeAiViewModelFactory { } } } + + /** + * Get the current model option + */ + fun getCurrentModel(): ModelOption { + return ModelOption.values().find { it.modelName == currentModelName } + ?: ModelOption.GEMINI_FLASH_LITE + } } diff --git a/app/src/main/kotlin/com/google/ai/sample/MenuScreen.kt b/app/src/main/kotlin/com/google/ai/sample/MenuScreen.kt index cad7dc70..33de9240 100644 --- a/app/src/main/kotlin/com/google/ai/sample/MenuScreen.kt +++ b/app/src/main/kotlin/com/google/ai/sample/MenuScreen.kt @@ -18,16 +18,24 @@ package com.google.ai.sample import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.padding import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.items import androidx.compose.material3.Button import androidx.compose.material3.Card +import androidx.compose.material3.DropdownMenu +import androidx.compose.material3.DropdownMenuItem import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Text import androidx.compose.material3.TextButton import androidx.compose.runtime.Composable +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.res.stringResource @@ -50,6 +58,12 @@ fun MenuScreen( MenuItem("photo_reasoning", R.string.menu_reason_title, R.string.menu_reason_description), MenuItem("chat", R.string.menu_chat_title, R.string.menu_chat_description) ) + + // Get current model + val currentModel = GenerativeViewModelFactory.getCurrentModel() + var selectedModel by remember { mutableStateOf(currentModel) } + var expanded by remember { mutableStateOf(false) } + LazyColumn( Modifier .padding(top = 16.dp, bottom = 16.dp) @@ -82,6 +96,64 @@ fun MenuScreen( } } + // Model Selection + item { + Card( + modifier = Modifier + .fillMaxWidth() + .padding(horizontal = 16.dp, vertical = 8.dp) + ) { + Column( + modifier = Modifier + .padding(all = 16.dp) + .fillMaxWidth() + ) { + Text( + text = "Model Selection", + style = MaterialTheme.typography.titleMedium + ) + + Spacer(modifier = Modifier.height(8.dp)) + + Text( + text = "Current model: ${selectedModel.displayName}", + style = MaterialTheme.typography.bodyMedium + ) + + Spacer(modifier = Modifier.height(8.dp)) + + Row( + modifier = Modifier.fillMaxWidth(), + verticalAlignment = Alignment.CenterVertically + ) { + Spacer(modifier = Modifier.weight(1f)) + + Button( + onClick = { expanded = true } + ) { + Text("Change Model") + } + + DropdownMenu( + expanded = expanded, + onDismissRequest = { expanded = false } + ) { + ModelOption.values().forEach { modelOption -> + DropdownMenuItem( + text = { Text(modelOption.displayName) }, + onClick = { + selectedModel = modelOption + GenerativeAiViewModelFactory.setModel(modelOption) + expanded = false + } + ) + } + } + } + } + } + } + // Menu Items items(menuItems) { menuItem -> Card( From 1bfd6157dfbea4096d4cb6e6c970ca36531b702e Mon Sep 17 00:00:00 2001 From: Android PowerUser <88908510+Android-PowerUser@users.noreply.github.com> Date: Wed, 23 Apr 2025 21:34:55 +0200 Subject: [PATCH 2/2] Add files via upload --- .../main/kotlin/com/google/ai/sample/MenuScreen.kt | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/app/src/main/kotlin/com/google/ai/sample/MenuScreen.kt b/app/src/main/kotlin/com/google/ai/sample/MenuScreen.kt index 33de9240..13aaa343 100644 --- a/app/src/main/kotlin/com/google/ai/sample/MenuScreen.kt +++ b/app/src/main/kotlin/com/google/ai/sample/MenuScreen.kt @@ -60,7 +60,7 @@ fun MenuScreen( ) // Get current model - val currentModel = GenerativeViewModelFactory.getCurrentModel() + val currentModel = GenerativeAiViewModelFactory.getCurrentModel() var selectedModel by remember { mutableStateOf(currentModel) } var expanded by remember { mutableStateOf(false) } @@ -138,7 +138,15 @@ fun MenuScreen( expanded = expanded, onDismissRequest = { expanded = false } ) { - ModelOption.values().forEach { modelOption -> + // Zeige die Modelle in der gewünschten Reihenfolge an + val orderedModels = listOf( + ModelOption.GEMINI_FLASH_LITE, + ModelOption.GEMINI_FLASH, + ModelOption.GEMINI_FLASH_PREVIEW, + ModelOption.GEMINI_PRO + ) + + orderedModels.forEach { modelOption -> DropdownMenuItem( text = { Text(modelOption.displayName) }, onClick = {