修改了逻辑代码,取消所有的warning,增加一个分区配置界面的设置

This commit is contained in:
2024-09-25 21:08:06 +08:00
parent d639b149b7
commit 1339121826
3 changed files with 56 additions and 41 deletions

View File

@@ -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 {
val originalRequest = chain.request()
val newRequest = originalRequest.newBuilder().build()
return chain.proceed(newRequest)
}
.addInterceptor(Interceptor { chain ->
val originalRequest = chain.request()
val newRequest = originalRequest.newBuilder().build()
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 {
val originalRequest = chain.request()
val newRequest = originalRequest.newBuilder().build()
return chain.proceed(newRequest)
}
.addInterceptor(Interceptor { chain ->
val originalRequest = chain.request()
val newRequest = originalRequest.newBuilder().build()
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()