From 5252441b542f36791b6613b25e4a4be4885870d4 Mon Sep 17 00:00:00 2001 From: Johnson Sun Date: Sat, 7 Dec 2024 15:33:15 -0500 Subject: [PATCH] feat: Set default endpoint on first use Hotfix for PR https://github.com/j3soon/whisper-to-input/pull/44 --- .../java/com/example/whispertoinput/MainActivity.kt | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/android/app/src/main/java/com/example/whispertoinput/MainActivity.kt b/android/app/src/main/java/com/example/whispertoinput/MainActivity.kt index ff98e22..f9dac32 100644 --- a/android/app/src/main/java/com/example/whispertoinput/MainActivity.kt +++ b/android/app/src/main/java/com/example/whispertoinput/MainActivity.kt @@ -160,6 +160,7 @@ class MainActivity : AppCompatActivity() { inner class SettingText( private val viewId: Int, private val preferenceKey: Preferences.Key, + private val defaultValue: String = "" ): SettingItem() { override fun setup(): Job { return CoroutineScope(Dispatchers.Main).launch { @@ -172,11 +173,13 @@ class MainActivity : AppCompatActivity() { btnApply.isEnabled = true } - // Read data - val value: String? = readSetting(preferenceKey) - if (!value.isNullOrEmpty()) { - editText.setText(value) + // Read data. If none, apply default value. + val settingValue: String? = readSetting(preferenceKey) + val value: String = settingValue ?: defaultValue + if (settingValue == null) { + writeSetting(preferenceKey, defaultValue) } + editText.setText(value) editText.isEnabled = true } } @@ -249,7 +252,7 @@ class MainActivity : AppCompatActivity() { getString(R.string.settings_option_openai_api) to true, getString(R.string.settings_option_whisper_webservice) to false, )), - SettingText(R.id.field_endpoint, ENDPOINT), + SettingText(R.id.field_endpoint, ENDPOINT, getString(R.string.settings_option_openai_api_default_endpoint)), SettingText(R.id.field_language_code, LANGUAGE_CODE), SettingText(R.id.field_api_key, API_KEY), SettingDropdown(R.id.spinner_auto_recording_start, AUTO_RECORDING_START, hashMapOf(