From d18d4d4de06f18c87fa697ec0a8368d98062975e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BB=D0=B5=D0=BA=D1=81=D0=B5=D0=B9=20=D0=91=D1=83?= =?UTF-8?q?=D0=B4=D0=B0=D0=B5=D0=B2?= Date: Sun, 5 Apr 2026 14:56:29 +0800 Subject: [PATCH] Add API key settings with save/delete options --- .../java/com/mistral/chat/ui/MainActivity.kt | 68 ++++++++++++++++++- app/src/main/res/layout/dialog_api_key.xml | 26 +++++++ app/src/main/res/menu/main_menu.xml | 4 ++ app/src/main/res/values/strings.xml | 7 ++ 4 files changed, 103 insertions(+), 2 deletions(-) create mode 100644 app/src/main/res/layout/dialog_api_key.xml diff --git a/app/src/main/java/com/mistral/chat/ui/MainActivity.kt b/app/src/main/java/com/mistral/chat/ui/MainActivity.kt index 9d51756..bbcfea4 100644 --- a/app/src/main/java/com/mistral/chat/ui/MainActivity.kt +++ b/app/src/main/java/com/mistral/chat/ui/MainActivity.kt @@ -50,13 +50,14 @@ class MainActivity : AppCompatActivity() { private lateinit var prefs: SharedPreferences companion object { - private const val API_KEY = "YW0IjDBRLuyEBcgNjVeVUFlMI6fcZYLA" private const val PREFS_NAME = "mistral_chat_prefs" private const val KEY_USER_NAME = "user_name" private const val KEY_USER_BIO = "user_bio" private const val KEY_USER_PREFS = "user_preferences" private const val KEY_MESSAGES = "chat_messages" private const val KEY_PROFILE_HASH = "profile_hash" + private const val KEY_API_KEY = "api_key" + private const val DEFAULT_API_KEY = "YW0IjDBRLuyEBcgNjVeVUFlMI6fcZYLA" } override fun onCreate(savedInstanceState: Bundle?) { @@ -69,7 +70,7 @@ class MainActivity : AppCompatActivity() { gson = Gson() prefs = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE) - client = MistralClient(API_KEY) + client = MistralClient(getApiKey()) loadMessages() @@ -103,6 +104,10 @@ class MainActivity : AppCompatActivity() { popup.menuInflater.inflate(R.menu.main_menu, popup.menu) popup.setOnMenuItemClickListener { menuItem -> when (menuItem.itemId) { + R.id.action_api_key -> { + showApiKeyDialog() + true + } R.id.action_profile -> { showProfileDialog() true @@ -225,6 +230,65 @@ class MainActivity : AppCompatActivity() { prefs.edit().putString(KEY_MESSAGES, json).apply() } + private fun getApiKey(): String { + return prefs.getString(KEY_API_KEY, DEFAULT_API_KEY) ?: DEFAULT_API_KEY + } + + private fun saveApiKey(apiKey: String) { + prefs.edit().putString(KEY_API_KEY, apiKey).apply() + client = MistralClient(apiKey) + } + + private fun deleteApiKey() { + prefs.edit().remove(KEY_API_KEY).apply() + client = MistralClient(DEFAULT_API_KEY) + } + + private fun showApiKeyDialog() { + val currentKey = getApiKey() + val hasCustomKey = currentKey != DEFAULT_API_KEY && prefs.contains(KEY_API_KEY) + val displayKey = if (hasCustomKey && currentKey.length > 8) { + currentKey.take(4) + "*".repeat(currentKey.length - 8) + currentKey.takeLast(4) + } else if (hasCustomKey) { + "******" + } else { + getString(R.string.no_api_key) + } + + val dialogView = layoutInflater.inflate(R.layout.dialog_api_key, null) + val inputField = dialogView.findViewById(R.id.apiKeyInput) + + if (hasCustomKey) { + inputField.setText(currentKey) + } + + AlertDialog.Builder(this) + .setTitle(R.string.api_key_title) + .setMessage(getString(R.string.api_key_current, displayKey)) + .setView(dialogView) + .setPositiveButton(R.string.save) { _, _ -> + val newKey = inputField.text?.toString()?.trim() + if (!newKey.isNullOrEmpty()) { + saveApiKey(newKey) + showToast(getString(R.string.api_key_saved)) + } + } + .setNegativeButton(R.string.cancel, null) + .apply { + if (hasCustomKey) { + setNeutralButton(R.string.delete) { _, _ -> + deleteApiKey() + showToast(getString(R.string.api_key_deleted)) + } + } + } + .show() + } + + private fun showToast(message: String) { + android.widget.Toast.makeText(this, message, android.widget.Toast.LENGTH_SHORT).show() + } + private fun sendMessage(userInput: String) { val selectedModel = selectedModelName diff --git a/app/src/main/res/layout/dialog_api_key.xml b/app/src/main/res/layout/dialog_api_key.xml new file mode 100644 index 0000000..f9e89ab --- /dev/null +++ b/app/src/main/res/layout/dialog_api_key.xml @@ -0,0 +1,26 @@ + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/menu/main_menu.xml b/app/src/main/res/menu/main_menu.xml index af13832..d0dc9c6 100644 --- a/app/src/main/res/menu/main_menu.xml +++ b/app/src/main/res/menu/main_menu.xml @@ -2,6 +2,10 @@ + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 6486dbd..2095aec 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -25,4 +25,11 @@ Расскажите о себе... Профиль не установлен Настройки + Mistral API + Mistral API ключ + Введите API ключ + API ключ сохранён + API ключ удалён + API ключ не установлен + Текущий ключ: %s \ No newline at end of file