修改了逻辑代码,取消所有的warning,增加一个分区配置界面的设置
This commit is contained in:
@@ -97,7 +97,7 @@ class MainActivity : AppCompatActivity() {
|
||||
private lateinit var submitToServerButton: Button
|
||||
private lateinit var statusText: TextView
|
||||
|
||||
@SuppressLint("MissingInflatedId")
|
||||
@SuppressLint("MissingInflatedId", "CutPasteId", "SetTextI18n")
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
setContentView(R.layout.activity_main)
|
||||
@@ -107,18 +107,16 @@ class MainActivity : AppCompatActivity() {
|
||||
|
||||
configButton = findViewById(R.id.configButton)
|
||||
// 状态栏目的信息
|
||||
statusText = findViewById<TextView>(R.id.statusTextView)
|
||||
statusText = findViewById(R.id.statusTextView)
|
||||
|
||||
submitToZhiPuAIButton = findViewById(R.id.submitToZhiPuAIButton)
|
||||
submitToZhiPuAIButton.setOnClickListener {
|
||||
// 创建 OkHttpClient,点击智谱AI分析返回标签
|
||||
val client = OkHttpClient.Builder()
|
||||
.addInterceptor(object : Interceptor {
|
||||
override fun intercept(chain: Interceptor.Chain): okhttp3.Response {
|
||||
.addInterceptor(Interceptor { chain ->
|
||||
val originalRequest = chain.request()
|
||||
val newRequest = originalRequest.newBuilder().build()
|
||||
return chain.proceed(newRequest)
|
||||
}
|
||||
chain.proceed(newRequest)
|
||||
})
|
||||
.build()
|
||||
// 这是一个 EditText 元素
|
||||
@@ -132,19 +130,19 @@ class MainActivity : AppCompatActivity() {
|
||||
// 从配置中读取 api_key
|
||||
val sharedPrefs = getSharedPreferences("APIConfigs", MODE_PRIVATE)
|
||||
val allConfigsJson = sharedPrefs.getString("configs", null)
|
||||
var api_key = ""
|
||||
var api_url = ""
|
||||
var api_model = "glm-4-flash"
|
||||
var apiKey = ""
|
||||
var apiUrl = ""
|
||||
var apiModel = "glm-4-flash"
|
||||
if (allConfigsJson != null) {
|
||||
val type = object : TypeToken<List<APIConfig>>() {}.type
|
||||
val allConfigs = Gson().fromJson<List<APIConfig>>(allConfigsJson, type)
|
||||
val zhipuConfig = allConfigs.find { it.name == "zhipu" }
|
||||
|
||||
if (zhipuConfig != null) {
|
||||
api_key = zhipuConfig.key
|
||||
api_url = zhipuConfig.url
|
||||
api_model = zhipuConfig.model
|
||||
statusText.text = "zhipuConfig配置文件$api_model"
|
||||
apiKey = zhipuConfig.key
|
||||
apiUrl = zhipuConfig.url
|
||||
apiModel = zhipuConfig.model
|
||||
statusText.text = "zhipuConfig配置文件$apiModel"
|
||||
|
||||
} else {
|
||||
statusText.text = "没找到zhipuConfig配置文件"
|
||||
@@ -155,7 +153,7 @@ class MainActivity : AppCompatActivity() {
|
||||
val jsonAdapter = moshi.adapter(ChatRequest::class.java)
|
||||
|
||||
val requestBody = ChatRequest(
|
||||
model = api_model.toString(),
|
||||
model = apiModel,
|
||||
messages = listOf(Message(role = "user", content = combinedText))
|
||||
)
|
||||
|
||||
@@ -165,9 +163,9 @@ class MainActivity : AppCompatActivity() {
|
||||
|
||||
// 创建请求
|
||||
val request = Request.Builder()
|
||||
.url("$api_url")
|
||||
.url(apiUrl)
|
||||
.post(body)
|
||||
.header("Authorization", "Bearer $api_key")
|
||||
.header("Authorization", "Bearer $apiKey")
|
||||
.header("Content-Type", "application/json")
|
||||
.build()
|
||||
|
||||
@@ -188,7 +186,7 @@ class MainActivity : AppCompatActivity() {
|
||||
print("return message is $responseBody")
|
||||
val labels = extractLabels(responseBody)
|
||||
labels?.let {
|
||||
if (labels != null && labels.size == 4) {
|
||||
if (labels.size == 4) {
|
||||
for (i in 0 until 4) {
|
||||
val tab = tabLayout.getTabAt(i)
|
||||
if (tab != null) {
|
||||
@@ -224,12 +222,10 @@ class MainActivity : AppCompatActivity() {
|
||||
submitToSparkAIButton.setOnClickListener {
|
||||
// 创建 OkHttpClient,点击星火大模型分析返回标签
|
||||
val client = OkHttpClient.Builder()
|
||||
.addInterceptor(object : Interceptor {
|
||||
override fun intercept(chain: Interceptor.Chain): okhttp3.Response {
|
||||
.addInterceptor(Interceptor { chain ->
|
||||
val originalRequest = chain.request()
|
||||
val newRequest = originalRequest.newBuilder().build()
|
||||
return chain.proceed(newRequest)
|
||||
}
|
||||
chain.proceed(newRequest)
|
||||
})
|
||||
.build()
|
||||
// 假设这是一个 EditText 元素
|
||||
@@ -243,19 +239,19 @@ class MainActivity : AppCompatActivity() {
|
||||
// 从配置中读取 api_key
|
||||
val sharedPrefs = getSharedPreferences("APIConfigs", MODE_PRIVATE)
|
||||
val allConfigsJson = sharedPrefs.getString("configs", null)
|
||||
var api_key = ""
|
||||
var api_url = ""
|
||||
var api_model = "general"
|
||||
var apiKey = ""
|
||||
var apiUrl = ""
|
||||
var apiModel = "general"
|
||||
if (allConfigsJson != null) {
|
||||
val type = object : TypeToken<List<APIConfig>>() {}.type
|
||||
val allConfigs = Gson().fromJson<List<APIConfig>>(allConfigsJson, type)
|
||||
val sparkConfig = allConfigs.find { it.name == "spark" }
|
||||
|
||||
if (sparkConfig != null) {
|
||||
api_key = sparkConfig.key
|
||||
api_url = sparkConfig.url
|
||||
api_model = sparkConfig.model
|
||||
statusText.text = "sparkConfig配置文件$api_model"
|
||||
apiKey = sparkConfig.key
|
||||
apiUrl = sparkConfig.url
|
||||
apiModel = sparkConfig.model
|
||||
statusText.text = "sparkConfig配置文件$apiModel"
|
||||
} else {
|
||||
statusText.text = "没找到sparkConfig配置文件"
|
||||
}
|
||||
@@ -265,7 +261,7 @@ class MainActivity : AppCompatActivity() {
|
||||
val jsonAdapter = moshi.adapter(ChatRequest::class.java)
|
||||
|
||||
val requestBody = ChatRequest(
|
||||
model = api_model.toString(),
|
||||
model = apiModel,
|
||||
messages = listOf(Message(role = "user", content = combinedText))
|
||||
)
|
||||
|
||||
@@ -275,9 +271,9 @@ class MainActivity : AppCompatActivity() {
|
||||
|
||||
// 创建请求
|
||||
val request = Request.Builder()
|
||||
.url("$api_url")
|
||||
.url(apiUrl)
|
||||
.post(body)
|
||||
.header("Authorization", "Bearer $api_key")
|
||||
.header("Authorization", "Bearer $apiKey")
|
||||
.header("Content-Type", "application/json")
|
||||
.build()
|
||||
|
||||
@@ -297,7 +293,7 @@ class MainActivity : AppCompatActivity() {
|
||||
// 处理响应 JSON 数据
|
||||
val labels = extractLabels(responseBody)
|
||||
labels?.let {
|
||||
if (labels != null && labels.size == 4) {
|
||||
if (labels.size == 4) {
|
||||
for (i in 0 until 4) {
|
||||
val tab = tabLayout.getTabAt(i)
|
||||
if (tab != null) {
|
||||
@@ -350,13 +346,14 @@ class MainActivity : AppCompatActivity() {
|
||||
val tabLayout = findViewById<TabLayout>(R.id.tabLayout)
|
||||
// 维持原来的创建标签按钮的代码
|
||||
(1..4).forEach { tabIndex ->
|
||||
val tabButton = tabLayout.newTab().apply {
|
||||
tabLayout.newTab().apply {
|
||||
text = "标签示例$tabIndex"
|
||||
tabLayout.addTab(this)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressLint("SetTextI18n")
|
||||
private fun submitToServer(content: String) {
|
||||
CoroutineScope(Dispatchers.Main).launch {
|
||||
statusText.text = "提交到flomo服务器..."
|
||||
@@ -376,7 +373,7 @@ class MainActivity : AppCompatActivity() {
|
||||
}
|
||||
|
||||
// 提交到笔记服务器,flomo服务器
|
||||
private suspend fun postDataToServer(content: String): Result {
|
||||
private fun postDataToServer(content: String): Result {
|
||||
return try {
|
||||
val client = OkHttpClient()
|
||||
val mediaType = "application/json".toMediaType()
|
||||
|
||||
@@ -12,6 +12,7 @@ import com.google.gson.Gson
|
||||
import com.google.gson.reflect.TypeToken
|
||||
|
||||
class SecondActivity : AppCompatActivity() {
|
||||
private lateinit var etApiButtonName: EditText
|
||||
private lateinit var etApiName: EditText
|
||||
private lateinit var etApiUrl: EditText
|
||||
private lateinit var etApiKey: EditText
|
||||
@@ -47,6 +48,7 @@ class SecondActivity : AppCompatActivity() {
|
||||
}
|
||||
|
||||
private fun initViews() {
|
||||
etApiButtonName = findViewById(R.id.etApiButtonName)
|
||||
etApiName = findViewById(R.id.etApiName)
|
||||
etApiUrl = findViewById(R.id.etApiUrl)
|
||||
etApiKey = findViewById(R.id.etApiKey)
|
||||
@@ -72,6 +74,7 @@ class SecondActivity : AppCompatActivity() {
|
||||
}
|
||||
|
||||
private fun addConfig() {
|
||||
val buttonname = etApiButtonName.text.toString()
|
||||
val name = etApiName.text.toString()
|
||||
val url = etApiUrl.text.toString()
|
||||
val key = etApiKey.text.toString()
|
||||
@@ -81,7 +84,7 @@ class SecondActivity : AppCompatActivity() {
|
||||
// 生成唯一的 id
|
||||
val id = System.currentTimeMillis()
|
||||
// 创建新的配置项
|
||||
val newConfig = APIConfig(id, name, url, key, secretKey, model)
|
||||
val newConfig = APIConfig(id, buttonname, name, url, key, secretKey, model)
|
||||
// 添加配置项
|
||||
configs.add(newConfig)
|
||||
// 保存配置
|
||||
@@ -93,6 +96,7 @@ class SecondActivity : AppCompatActivity() {
|
||||
}
|
||||
|
||||
private fun updateConfig() {
|
||||
val buttonname = etApiButtonName.text.toString()
|
||||
val name = etApiName.text.toString()
|
||||
val url = etApiUrl.text.toString()
|
||||
val key = etApiKey.text.toString()
|
||||
@@ -102,7 +106,7 @@ class SecondActivity : AppCompatActivity() {
|
||||
// 获取编辑的配置项 id
|
||||
val id = editingId ?: return
|
||||
// 更新配置项
|
||||
val updatedConfig = APIConfig(id, name, url, key, secretKey, model)
|
||||
val updatedConfig = APIConfig(id, buttonname, name, url, key, secretKey, model)
|
||||
val existingConfigIndex = configs.indexOfFirst { it.id == id }
|
||||
if (existingConfigIndex != -1) {
|
||||
configs[existingConfigIndex] = updatedConfig
|
||||
@@ -118,13 +122,19 @@ class SecondActivity : AppCompatActivity() {
|
||||
|
||||
}
|
||||
|
||||
@SuppressLint("MissingInflatedId")
|
||||
|
||||
@SuppressLint("MissingInflatedId", "SetTextI18n", "InflateParams")
|
||||
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)
|
||||
|
||||
// 获取并设置 Name 的 TextView 前景色和背景色
|
||||
val tvName = configView.findViewById<TextView>(R.id.tvName)
|
||||
tvName.text = "Name: ${config.name}"
|
||||
@@ -167,6 +177,7 @@ class SecondActivity : AppCompatActivity() {
|
||||
}
|
||||
|
||||
private fun editConfig(config: APIConfig) {
|
||||
etApiButtonName.setText(config.buttonname)
|
||||
etApiName.setText(config.name)
|
||||
etApiUrl.setText(config.url)
|
||||
etApiKey.setText(config.key)
|
||||
@@ -184,6 +195,7 @@ class SecondActivity : AppCompatActivity() {
|
||||
}
|
||||
|
||||
private fun clearInputs() {
|
||||
etApiButtonName.text.clear()
|
||||
etApiName.text.clear()
|
||||
etApiUrl.text.clear()
|
||||
etApiKey.text.clear()
|
||||
@@ -195,6 +207,7 @@ class SecondActivity : AppCompatActivity() {
|
||||
|
||||
data class APIConfig(
|
||||
val id: Long,
|
||||
var buttonname:String,
|
||||
val name: String,
|
||||
val url: String,
|
||||
val key: String,
|
||||
|
||||
@@ -13,6 +13,11 @@
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/tvButtonName"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/tvUrl"
|
||||
android:layout_width="wrap_content"
|
||||
|
||||
Reference in New Issue
Block a user