第一个版本的客户端
This commit is contained in:
189
app/src/main/java/com/example/flomo_ai/SecondActivity.kt
Normal file
189
app/src/main/java/com/example/flomo_ai/SecondActivity.kt
Normal file
@@ -0,0 +1,189 @@
|
||||
package com.example.flomo_ai
|
||||
|
||||
import android.os.Bundle
|
||||
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 java.util.UUID
|
||||
|
||||
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 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)
|
||||
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()
|
||||
|
||||
// 生成唯一的 id
|
||||
val id = System.currentTimeMillis()
|
||||
// 创建新的配置项
|
||||
val newConfig = APIConfig(id, name, url, key, secretKey)
|
||||
// 添加配置项
|
||||
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()
|
||||
|
||||
// 获取编辑的配置项 id
|
||||
val id = editingId ?: return
|
||||
// 更新配置项
|
||||
val updatedConfig = APIConfig(id, name, url, key, secretKey)
|
||||
val existingConfigIndex = configs.indexOfFirst { it.id == id }
|
||||
if (existingConfigIndex != -1) {
|
||||
configs[existingConfigIndex] = updatedConfig
|
||||
}
|
||||
// 保存配置
|
||||
saveConfigs()
|
||||
// 显示配置
|
||||
displayConfigs()
|
||||
// 清空输入框
|
||||
clearInputs()
|
||||
// 重置编辑状态
|
||||
editingId = null
|
||||
|
||||
}
|
||||
|
||||
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.setTextColor(resources.getColor(R.color.background_color))
|
||||
tvName.text = "Name: ${config.name}"
|
||||
|
||||
// 获取并设置 URL 的 TextView 前景色和背景色
|
||||
val tvUrl = configView.findViewById<TextView>(R.id.tvUrl)
|
||||
tvUrl.setTextColor(resources.getColor(R.color.background_color))
|
||||
tvUrl.text = "URL: ${config.url}"
|
||||
|
||||
// 获取并设置 Key 的 TextView 前景色和背景色
|
||||
val tvKey = configView.findViewById<TextView>(R.id.tvKey).also {
|
||||
it.setTextColor(resources.getColor(R.color.background_color))
|
||||
}
|
||||
tvKey.text = "Key: ${config.key.take(4)}..."
|
||||
|
||||
// 获取并设置 SecretKey 的 TextView 前景色和背景色
|
||||
val tvSecretKey = configView.findViewById<TextView>(R.id.tvSecretKey)
|
||||
tvSecretKey.setTextColor(resources.getColor(R.color.background_color))
|
||||
tvSecretKey.text = "Secret Key: ${config.secretKey.take(4)}..."
|
||||
|
||||
// 设置编辑按钮点击事件
|
||||
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)
|
||||
// 设置编辑状态
|
||||
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()
|
||||
btnSave.text = "保存配置"
|
||||
}
|
||||
}
|
||||
|
||||
data class APIConfig(
|
||||
val id: Long,
|
||||
val name: String,
|
||||
val url: String,
|
||||
val key: String,
|
||||
val secretKey: String
|
||||
)
|
||||
Reference in New Issue
Block a user