1+ // --- START OF FILE ModelPreferences.kt ---
2+ package com.google.ai.sample.util
3+
4+ import android.content.Context
5+ import android.util.Log
6+
7+ /* *
8+ * Helper object to save and load the selected AI model name using SharedPreferences.
9+ */
10+ object ModelPreferences {
11+ private const val TAG = " ModelPreferences"
12+ private const val PREFS_NAME = " ai_model_prefs"
13+ private const val KEY_MODEL_NAME = " current_model_name"
14+
15+ // Definiere hier deine Standard- und High-Modellnamen konsistent
16+ const val LOW_REASONING_MODEL = " gemini-2.0-flash-lite"
17+ const val HIGH_REASONING_MODEL = " gemini-2.5-pro-exp-03-25"
18+
19+ // Das Standardmodell, das verwendet wird, wenn nichts gespeichert ist.
20+ private const val DEFAULT_MODEL = LOW_REASONING_MODEL
21+
22+ /* *
23+ * Saves the selected model name to SharedPreferences.
24+ * @param context Application context.
25+ * @param modelName The model name string to save.
26+ */
27+ fun saveModelName (context : Context , modelName : String ) {
28+ try {
29+ context.getSharedPreferences(PREFS_NAME , Context .MODE_PRIVATE )
30+ .edit()
31+ .putString(KEY_MODEL_NAME , modelName)
32+ .apply ()
33+ Log .i(TAG , " Saved model preference: $modelName " )
34+ } catch (e: Exception ) {
35+ Log .e(TAG , " Error saving model preference: $modelName " , e)
36+ }
37+ }
38+
39+ /* *
40+ * Loads the saved model name from SharedPreferences.
41+ * Returns the default model name if nothing is saved.
42+ * @param context Application context.
43+ * @return The loaded or default model name string.
44+ */
45+ fun loadModelName (context : Context ): String {
46+ return try {
47+ val name = context.getSharedPreferences(PREFS_NAME , Context .MODE_PRIVATE )
48+ .getString(KEY_MODEL_NAME , DEFAULT_MODEL ) ? : DEFAULT_MODEL
49+ Log .i(TAG , " Loaded model preference: $name " )
50+ name
51+ } catch (e: Exception ) {
52+ Log .e(TAG , " Error loading model preference, returning default: $DEFAULT_MODEL " , e)
53+ DEFAULT_MODEL
54+ }
55+ }
56+ }
57+ // --- END OF FILE ModelPreferences.kt ---
0 commit comments