203 lines
6.6 KiB
Kotlin
203 lines
6.6 KiB
Kotlin
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
|
|
|
|
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
|
|
private lateinit var btnSave: Button
|
|
private lateinit var llConfigList: LinearLayout
|
|
|
|
private var configs = mutableListOf<APIConfig>()
|
|
private var editingId: Long? = null
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
setTheme(androidx.appcompat.R.style.Theme_AppCompat)
|
|
super.onCreate(savedInstanceState)
|
|
setContentView(R.layout.activity_second)
|
|
|
|
initViews()
|
|
loadConfigs()
|
|
displayConfigs()
|
|
|
|
val btnGoBack: Button = findViewById(R.id.btnGoBack)
|
|
btnGoBack.setOnClickListener {
|
|
finish()
|
|
}
|
|
|
|
btnSave.setOnClickListener {
|
|
if (editingId != null) {
|
|
updateConfig()
|
|
} else {
|
|
addConfig()
|
|
}
|
|
}
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
private fun loadConfigs() {
|
|
val sharedPrefs = getSharedPreferences("APIConfigs", MODE_PRIVATE)
|
|
val json = sharedPrefs.getString("configs", null)
|
|
if (json != null) {
|
|
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()
|
|
}
|
|
|
|
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()
|
|
|
|
// 生成唯一的 id
|
|
val id = System.currentTimeMillis()
|
|
// 创建新的配置项
|
|
val newConfig = APIConfig(id, name, url, key, secretKey, model)
|
|
// 添加配置项
|
|
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()
|
|
|
|
// 获取编辑的配置项 id
|
|
val id = editingId ?: return
|
|
// 更新配置项
|
|
val updatedConfig = APIConfig(id, name, url, key, secretKey, model)
|
|
val existingConfigIndex = configs.indexOfFirst { it.id == id }
|
|
if (existingConfigIndex != -1) {
|
|
configs[existingConfigIndex] = updatedConfig
|
|
}
|
|
// 保存配置
|
|
saveConfigs()
|
|
// 显示配置
|
|
displayConfigs()
|
|
// 清空输入框
|
|
clearInputs()
|
|
// 重置编辑状态
|
|
editingId = null
|
|
|
|
}
|
|
|
|
@SuppressLint("MissingInflatedId")
|
|
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)
|
|
|
|
// 获取并设置 URL 的 TextView 前景色和背景色
|
|
val tvUrl = configView.findViewById<TextView>(R.id.tvUrl)
|
|
tvUrl.text = "URL: ${config.url}"
|
|
tvUrl.setTextColor(Color.BLACK)
|
|
|
|
// 获取并设置 Key 的 TextView 前景色和背景色
|
|
val tvKey = configView.findViewById<TextView>(R.id.tvKey)
|
|
tvKey.text = "Key: ${config.key.take(4)}..."
|
|
tvKey.setTextColor(Color.BLACK)
|
|
|
|
|
|
// 获取并设置 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)
|
|
|
|
// 设置编辑按钮点击事件
|
|
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)
|
|
// 设置编辑状态
|
|
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()
|
|
btnSave.text = "保存配置"
|
|
}
|
|
}
|
|
|
|
data class APIConfig(
|
|
val id: Long,
|
|
val name: String,
|
|
val url: String,
|
|
val key: String,
|
|
val secretKey: String,
|
|
val model: String
|
|
) |