Files
flomo-ai/app/src/main/java/com/example/flomo_ai/SecondActivity.kt

262 lines
9.1 KiB
Kotlin
Raw Normal View History

2024-09-17 09:24:50 +08:00
package com.example.flomo_ai
import android.annotation.SuppressLint
import android.os.Bundle
import android.graphics.Color
import android.widget.Button
import android.widget.EditText
import android.widget.LinearLayout
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import androidx.core.content.ContextCompat
import android.content.Context
import android.util.Log
import androidx.core.content.res.ResourcesCompat
2024-09-17 09:24:50 +08:00
class SecondActivity : AppCompatActivity() {
private lateinit var etApiName: EditText
private lateinit var etApiUrl: EditText
private lateinit var etApiKey: EditText
private lateinit var etApiSecretKey: EditText
private lateinit var etApiModel: EditText
2024-09-17 09:24:50 +08:00
private lateinit var btnSave: Button
private lateinit var llConfigList: LinearLayout
2024-09-17 09:24:50 +08:00
private var configs = mutableListOf<APIConfig>()
private var editingId: Long? = null
private fun setButtonListeners() {
val buttonIds = listOf(
R.id.button_holo_red_light,
R.id.button_holo_green_light,
R.id.button_holo_blue_light,
R.id.button_holo_orange_light
)
buttonIds.forEach { buttonId ->
findViewById<Button>(buttonId).setOnClickListener {
val sharedPrefs = getSharedPreferences("APIConfigs", Context.MODE_PRIVATE)
val editor = sharedPrefs.edit()
// 获取颜色资源 ID
val colorResId = getColorForButtonId(buttonId)
// 使用正则表达式从资源名称中提取颜色值并存储
val colorValue = extractColorValue(buttonId)
editor.putString("buttonColor", colorValue)
editor.apply()
Log.d("SharedPrefsDebug", "Stored color value in SecondActivity: $colorValue")
}
}
}
private fun getColorForButtonId(buttonId: Int): Int {
return if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
// API 23 及以上使用 Context.getColor()
baseContext.getColor(findColorResourceId(buttonId))
} else {
// 低版本使用 ResourcesCompat.getColor()
ResourcesCompat.getColor(resources, findColorResourceId(buttonId), null)
}
}
private fun findColorResourceId(buttonId: Int): Int {
return when (buttonId) {
R.id.button_holo_red_light -> android.R.color.holo_red_light
R.id.button_holo_green_light -> android.R.color.holo_green_light
R.id.button_holo_blue_light -> android.R.color.holo_blue_light
R.id.button_holo_orange_light -> android.R.color.holo_orange_light
else -> -1
}
}
private fun extractColorValue(buttonId: Int): String {
val resourceName = resources.getResourceEntryName(buttonId)
val regex = Regex(".*_(.*)_light")
val matchResult = regex.find(resourceName)
return matchResult?.groupValues?.get(1)?: ""
}
2024-09-17 09:24:50 +08:00
override fun onCreate(savedInstanceState: Bundle?) {
setTheme(androidx.appcompat.R.style.Theme_AppCompat)
2024-09-17 09:24:50 +08:00
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_second)
2024-09-17 09:24:50 +08:00
initViews()
loadConfigs()
displayConfigs()
val btnGoBack: Button = findViewById(R.id.btnGoBack)
2024-09-17 09:24:50 +08:00
btnGoBack.setOnClickListener {
finish()
2024-09-17 09:24:50 +08:00
}
btnSave.setOnClickListener {
if (editingId != null) {
updateConfig()
} else {
addConfig()
}
}
//颜色按钮的监听
setButtonListeners()
}
2024-09-17 09:24:50 +08:00
private fun initViews() {
etApiName = findViewById(R.id.etApiName)
etApiUrl = findViewById(R.id.etApiUrl)
etApiKey = findViewById(R.id.etApiKey)
etApiSecretKey = findViewById(R.id.etApiSecretKey)
etApiModel = findViewById(R.id.etApiModel)
btnSave = findViewById(R.id.btnSave)
llConfigList = findViewById(R.id.llConfigList)
2024-09-17 09:24:50 +08:00
}
private fun loadConfigs() {
// 获取一个名为 "APIConfigs" 的共享偏好设置
2024-09-17 09:24:50 +08:00
val sharedPrefs = getSharedPreferences("APIConfigs", MODE_PRIVATE)
val json = sharedPrefs.getString("configs", null)
if (json != null) {
// 创建一个 TypeToken 的实例,用于表示一个包含 APIConfig 对象的列表类型
2024-09-17 09:24:50 +08:00
val type = object : TypeToken<List<APIConfig>>() {}.type
configs = Gson().fromJson(json, type)
}
}
private fun saveConfigs() {
val sharedPrefs = getSharedPreferences("APIConfigs", MODE_PRIVATE)
val json = Gson().toJson(configs)
sharedPrefs.edit().putString("configs", json).apply()
2024-09-17 09:24:50 +08:00
}
private fun addConfig() {
val name = etApiName.text.toString()
val url = etApiUrl.text.toString()
val key = etApiKey.text.toString()
val secretKey = etApiSecretKey.text.toString()
val model = etApiModel.text.toString()
2024-09-17 09:24:50 +08:00
// 生成唯一的 id
val id = System.currentTimeMillis()
// 创建新的配置项
val newConfig = APIConfig(id, name, url, key, secretKey, model)
2024-09-17 09:24:50 +08:00
// 添加配置项
configs.add(newConfig)
// 保存配置
saveConfigs()
// 显示配置
displayConfigs()
// 清空输入框
clearInputs()
}
private fun updateConfig() {
val name = etApiName.text.toString()
val url = etApiUrl.text.toString()
val key = etApiKey.text.toString()
val secretKey = etApiSecretKey.text.toString()
val model = etApiModel.text.toString()
2024-09-17 09:24:50 +08:00
// 获取编辑的配置项 id
val id = editingId ?: return
// 更新配置项
val updatedConfig = APIConfig(id, name, url, key, secretKey, model)
2024-09-17 09:24:50 +08:00
val existingConfigIndex = configs.indexOfFirst { it.id == id }
if (existingConfigIndex != -1) {
configs[existingConfigIndex] = updatedConfig
}
// 保存配置
saveConfigs()
// 显示配置
displayConfigs()
// 清空输入框
clearInputs()
// 重置编辑状态
editingId = null
}
@SuppressLint("MissingInflatedId")
2024-09-17 09:24:50 +08:00
private fun displayConfigs() {
llConfigList.removeAllViews()
for (config in configs) {
// 为每个配置项加载对应的布局文件
val configView = layoutInflater.inflate(R.layout.item_api_config, null)
// 设置各项文本信息
// 获取并设置 Name 的 TextView 前景色和背景色
val tvName = configView.findViewById<TextView>(R.id.tvName)
tvName.text = "Name: ${config.name}"
tvName.setTextColor(Color.BLACK)
2024-09-17 09:24:50 +08:00
// 获取并设置 URL 的 TextView 前景色和背景色
val tvUrl = configView.findViewById<TextView>(R.id.tvUrl)
tvUrl.text = "URL: ${config.url.take(32)}..."
tvUrl.setTextColor(Color.BLACK)
2024-09-17 09:24:50 +08:00
// 获取并设置 Key 的 TextView 前景色和背景色
val tvKey = configView.findViewById<TextView>(R.id.tvKey)
2024-09-17 09:24:50 +08:00
tvKey.text = "Key: ${config.key.take(4)}..."
tvKey.setTextColor(Color.BLACK)
2024-09-17 09:24:50 +08:00
// 获取并设置 SecretKey 的 TextView 前景色和背景色
val tvSecretKey = configView.findViewById<TextView>(R.id.tvSecretKey)
tvSecretKey.text = "Secret Key: ${config.secretKey.take(4)}..."
tvSecretKey.setTextColor(Color.BLACK)
// 获取并设置 model 的 TextView 前景色和背景色
val tvApiModel = configView.findViewById<TextView>(R.id.tvApiModel)
tvApiModel.text = "Model: ${config.model}"
tvApiModel.setTextColor(Color.BLACK)
2024-09-17 09:24:50 +08:00
// 设置编辑按钮点击事件
configView.findViewById<Button>(R.id.btnEdit).setOnClickListener {
editConfig(config)
}
// 设置删除按钮点击事件
configView.findViewById<Button>(R.id.btnDelete).setOnClickListener {
deleteConfig(config.id)
}
// 将包含配置信息的视图添加到父布局中
llConfigList.addView(configView)
}
}
private fun editConfig(config: APIConfig) {
etApiName.setText(config.name)
etApiUrl.setText(config.url)
etApiKey.setText(config.key)
etApiSecretKey.setText(config.secretKey)
etApiModel.setText(config.model)
2024-09-17 09:24:50 +08:00
// 设置编辑状态
editingId = config.id
btnSave.text = "更新配置"
}
private fun deleteConfig(id: Long) {
configs.removeAll { it.id == id }
saveConfigs()
displayConfigs()
}
private fun clearInputs() {
etApiName.text.clear()
etApiUrl.text.clear()
etApiKey.text.clear()
etApiSecretKey.text.clear()
etApiModel.text.clear()
2024-09-17 09:24:50 +08:00
btnSave.text = "保存配置"
}
2024-09-17 09:24:50 +08:00
}
data class APIConfig(
val id: Long,
val name: String,
val url: String,
val key: String,
val secretKey: String,
val model: String
2024-09-17 09:24:50 +08:00
)