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

264 lines
9.6 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.app.Activity
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Color
import android.net.Uri
import android.os.Bundle
import android.provider.MediaStore
import android.util.Log
import android.widget.Button
import android.widget.EditText
import android.widget.LinearLayout
import android.widget.TextView
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import java.io.InputStream
2024-09-17 09:24:50 +08:00
class SecondActivity : AppCompatActivity() {
private lateinit var etApiButtonName: EditText
2024-09-17 09:24:50 +08:00
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
private var configs = mutableListOf<APIConfig>()
private var editingId: Long? = null
private lateinit var imagePickerLauncher: ActivityResultLauncher<Intent>
companion object {
private const val REQUEST_CODE_PICK_IMAGE = 1000
}
2024-09-17 09:24:50 +08:00
override fun onCreate(savedInstanceState: Bundle?) {
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 {
val intent = Intent(this, MainActivity::class.java);
startActivity(intent);
finish();
2024-09-17 09:24:50 +08:00
}
btnSave.setOnClickListener {
if (editingId != null) {
updateConfig()
} else {
addConfig()
}
}
// 创建背景图
setContentView(R.layout.activity_second)
imagePickerLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == Activity.RESULT_OK && result.data != null) {
val selectedImageUri: Uri? = result.data?.data
selectedImageUri?.let { uri ->
val bitmap = getBitmapFromUri(uri)
// 使用bitmap更新背景或其他操作
}
}
}
val chooseButton: Button = findViewById(R.id.chooseButton)
chooseButton.setOnClickListener {
val intent = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
intent.type = "image/*"
imagePickerLauncher.launch(intent)
}
2024-09-17 09:24:50 +08:00
}
private fun getBitmapFromUri(uri: Uri): Bitmap? {
val inputStream: InputStream? = contentResolver.openInputStream(uri)
return BitmapFactory.decodeStream(inputStream)
}
2024-09-17 09:24:50 +08:00
private fun initViews() {
etApiName = findViewById(R.id.etApiName) ?: throw IllegalStateException("etApiName is not found")
etApiUrl = findViewById(R.id.etApiUrl) ?: throw IllegalStateException("etApiUrl is not found")
etApiKey = findViewById(R.id.etApiKey) ?: throw IllegalStateException("etApiKey is not found")
etApiSecretKey = findViewById(R.id.etApiSecretKey) ?: throw IllegalStateException("etApiSecretKey is not found")
etApiModel = findViewById(R.id.etApiModel) ?: throw IllegalStateException("etApiModel is not found")
etApiButtonName = findViewById(R.id.etApiButtonName) ?: throw IllegalStateException("etApiButtonName is not found")
btnSave = findViewById(R.id.btnSave) ?: throw IllegalStateException("btnSave is not found")
llConfigList = findViewById(R.id.llConfigList) ?: throw IllegalStateException("llConfigList is not found")
2024-09-17 09:24:50 +08:00
}
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 editor = sharedPrefs.edit()
if (editor != null) {
val gson = Gson()
val json = gson.toJson(configs)
Log.d("ConfigDebug", "Saving configs: $json")
editor.putString("configs", json)
editor.apply()
} else {
Log.e("ConfigDebug", "Failed to get SharedPreferences.Editor")
}
2024-09-17 09:24:50 +08:00
}
private fun addConfig() {
val buttonname = etApiButtonName.text.toString()
2024-09-17 09:24:50 +08:00
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, buttonname, name, url, key, secretKey, model)
2024-09-17 09:24:50 +08:00
// 添加配置项
configs.add(newConfig)
// 保存配置
saveConfigs()
// 显示配置
displayConfigs()
// 清空输入框
clearInputs()
}
private fun updateConfig() {
val buttonname = etApiButtonName.text.toString()
2024-09-17 09:24:50 +08:00
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, buttonname, 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", "SetTextI18n", "InflateParams")
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 tvButtonName = configView.findViewById<TextView>(R.id.tvButtonName)
tvButtonName.text = "按钮名称: ${config.buttonname}"
tvButtonName.setTextColor(Color.BLACK)
2024-09-17 09:24:50 +08:00
// 获取并设置 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}"
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) {
etApiButtonName.setText(config.buttonname)
2024-09-17 09:24:50 +08:00
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() {
etApiButtonName.text.clear()
2024-09-17 09:24:50 +08:00
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 = "保存配置"
}
}
data class APIConfig(
val id: Long,
var buttonname:String,
2024-09-17 09:24:50 +08:00
val name: String,
val url: String,
val key: String,
val secretKey: String,
val model: String
2024-09-17 09:24:50 +08:00
)