Add API key settings with save/delete options

This commit is contained in:
Алексей Будаев 2026-04-05 14:56:29 +08:00
parent 2228f10000
commit d18d4d4de0
4 changed files with 103 additions and 2 deletions

View file

@ -50,13 +50,14 @@ class MainActivity : AppCompatActivity() {
private lateinit var prefs: SharedPreferences private lateinit var prefs: SharedPreferences
companion object { companion object {
private const val API_KEY = "YW0IjDBRLuyEBcgNjVeVUFlMI6fcZYLA"
private const val PREFS_NAME = "mistral_chat_prefs" private const val PREFS_NAME = "mistral_chat_prefs"
private const val KEY_USER_NAME = "user_name" private const val KEY_USER_NAME = "user_name"
private const val KEY_USER_BIO = "user_bio" private const val KEY_USER_BIO = "user_bio"
private const val KEY_USER_PREFS = "user_preferences" private const val KEY_USER_PREFS = "user_preferences"
private const val KEY_MESSAGES = "chat_messages" private const val KEY_MESSAGES = "chat_messages"
private const val KEY_PROFILE_HASH = "profile_hash" 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?) { override fun onCreate(savedInstanceState: Bundle?) {
@ -69,7 +70,7 @@ class MainActivity : AppCompatActivity() {
gson = Gson() gson = Gson()
prefs = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE) prefs = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
client = MistralClient(API_KEY) client = MistralClient(getApiKey())
loadMessages() loadMessages()
@ -103,6 +104,10 @@ class MainActivity : AppCompatActivity() {
popup.menuInflater.inflate(R.menu.main_menu, popup.menu) popup.menuInflater.inflate(R.menu.main_menu, popup.menu)
popup.setOnMenuItemClickListener { menuItem -> popup.setOnMenuItemClickListener { menuItem ->
when (menuItem.itemId) { when (menuItem.itemId) {
R.id.action_api_key -> {
showApiKeyDialog()
true
}
R.id.action_profile -> { R.id.action_profile -> {
showProfileDialog() showProfileDialog()
true true
@ -225,6 +230,65 @@ class MainActivity : AppCompatActivity() {
prefs.edit().putString(KEY_MESSAGES, json).apply() 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<TextInputEditText>(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) { private fun sendMessage(userInput: String) {
val selectedModel = selectedModelName val selectedModel = selectedModelName

View file

@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="24dp">
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/apiKeyLayout"
style="@style/Widget.Material3.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/api_key_hint"
app:endIconMode="password_toggle">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/apiKeyInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:maxLines="1" />
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>

View file

@ -2,6 +2,10 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android" <menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"> xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_api_key"
android:title="@string/api_key" />
<item <item
android:id="@+id/action_profile" android:id="@+id/action_profile"
android:title="@string/profile" /> android:title="@string/profile" />

View file

@ -25,4 +25,11 @@
<string name="bio_hint">Расскажите о себе...</string> <string name="bio_hint">Расскажите о себе...</string>
<string name="no_profile">Профиль не установлен</string> <string name="no_profile">Профиль не установлен</string>
<string name="settings">Настройки</string> <string name="settings">Настройки</string>
<string name="api_key">Mistral API</string>
<string name="api_key_title">Mistral API ключ</string>
<string name="api_key_hint">Введите API ключ</string>
<string name="api_key_saved">API ключ сохранён</string>
<string name="api_key_deleted">API ключ удалён</string>
<string name="no_api_key">API ключ не установлен</string>
<string name="api_key_current">Текущий ключ: %s</string>
</resources> </resources>