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