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
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<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) {
val selectedModel = selectedModelName