修自定义配置
null、导入、mixin ignore
This commit is contained in:
@@ -5,7 +5,10 @@ import android.content.ClipboardManager
|
||||
import android.content.Context
|
||||
import android.widget.Toast
|
||||
import androidx.activity.compose.BackHandler
|
||||
import androidx.activity.compose.rememberLauncherForActivityResult
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
import androidx.compose.foundation.border
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.layout.*
|
||||
import androidx.compose.foundation.rememberScrollState
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
@@ -27,10 +30,18 @@ import androidx.compose.ui.text.style.TextOverflow
|
||||
import androidx.compose.ui.text.withLink
|
||||
import androidx.compose.ui.unit.Dp
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.window.DialogProperties
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import net.bluemangoo.indpjsk.R
|
||||
import net.bluemangoo.indpjsk.data.MixinProfiles
|
||||
import net.bluemangoo.indpjsk.upload.*
|
||||
import org.json.JSONObject
|
||||
import java.net.HttpURLConnection
|
||||
import java.net.URL
|
||||
import java.util.concurrent.atomic.AtomicReference
|
||||
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
@Composable
|
||||
@@ -43,6 +54,7 @@ fun UploadSettingsScreen(onBack: () -> Unit) {
|
||||
if (target != null) {
|
||||
UploadEditScreen(
|
||||
initial = target.initial,
|
||||
isNew = target.index == null,
|
||||
takenIds = entries
|
||||
.filterIndexed { index, _ -> index != target.index }
|
||||
.map { it.profile.id }
|
||||
@@ -142,6 +154,17 @@ fun UploadSettingsScreen(onBack: () -> Unit) {
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun ImportAction(text: String, onClick: () -> Unit) {
|
||||
ListItem(
|
||||
headlineContent = {
|
||||
Text(text, style = MaterialTheme.typography.bodyMedium)
|
||||
},
|
||||
modifier = Modifier.clickable(onClick = onClick)
|
||||
)
|
||||
}
|
||||
|
||||
private data class UploadEditTarget(
|
||||
@@ -221,6 +244,7 @@ private fun UploadTableRow(
|
||||
@Composable
|
||||
private fun UploadEditScreen(
|
||||
initial: UploadEntry?,
|
||||
isNew: Boolean,
|
||||
takenIds: Set<String>,
|
||||
onCancel: () -> Unit,
|
||||
onSave: (UploadEntry) -> Unit,
|
||||
@@ -282,6 +306,91 @@ private fun UploadEditScreen(
|
||||
var importText by remember { mutableStateOf("") }
|
||||
var importError by remember { mutableStateOf<String?>(null) }
|
||||
|
||||
// 从 JSON 导入:URL / 文件 / 直接打开 JSON 编辑器
|
||||
var showImportMenu by remember { mutableStateOf(false) }
|
||||
var showUrlDialog by remember { mutableStateOf(false) }
|
||||
var urlText by remember { mutableStateOf("") }
|
||||
var loading by remember { mutableStateOf(false) }
|
||||
var loadError by remember { mutableStateOf<String?>(null) }
|
||||
|
||||
val scope = rememberCoroutineScope()
|
||||
val importJob = remember { AtomicReference<Job?>(null) }
|
||||
val importConnection = remember { AtomicReference<HttpURLConnection?>(null) }
|
||||
|
||||
fun openJsonEditor(text: String) {
|
||||
importText = text
|
||||
importError = null
|
||||
importDialog = true
|
||||
}
|
||||
|
||||
fun finishLoad(text: String) {
|
||||
if (text.isBlank()) {
|
||||
loadError = "内容为空"
|
||||
} else {
|
||||
openJsonEditor(text)
|
||||
}
|
||||
}
|
||||
|
||||
fun cancelImport() {
|
||||
importConnection.getAndSet(null)?.disconnect()
|
||||
importJob.getAndSet(null)?.cancel()
|
||||
loading = false
|
||||
}
|
||||
|
||||
fun loadFromUrl() {
|
||||
val url = urlText.trim()
|
||||
if (url.isEmpty()) {
|
||||
loadError = "请填写 URL"
|
||||
return
|
||||
}
|
||||
loading = true
|
||||
importJob.set(scope.launch {
|
||||
try {
|
||||
val result = withContext(Dispatchers.IO) {
|
||||
runCatching {
|
||||
val conn = URL(url).openConnection() as HttpURLConnection
|
||||
importConnection.set(conn)
|
||||
conn.instanceFollowRedirects = true
|
||||
conn.connectTimeout = 15_000
|
||||
conn.readTimeout = 20_000
|
||||
conn.inputStream.bufferedReader().use { it.readText() }
|
||||
}
|
||||
}
|
||||
result.fold(
|
||||
onSuccess = { finishLoad(it) },
|
||||
onFailure = { loadError = "从 URL 读取失败:${it.message}" },
|
||||
)
|
||||
} finally {
|
||||
importConnection.getAndSet(null)?.disconnect()
|
||||
loading = false
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
val filePicker = rememberLauncherForActivityResult(ActivityResultContracts.OpenDocument()) { uri ->
|
||||
if (uri != null) {
|
||||
loading = true
|
||||
importJob.set(scope.launch {
|
||||
try {
|
||||
val result = withContext(Dispatchers.IO) {
|
||||
runCatching {
|
||||
context.contentResolver.openInputStream(uri)
|
||||
?.bufferedReader()
|
||||
?.use { it.readText() }
|
||||
?: error("无法打开文件")
|
||||
}
|
||||
}
|
||||
result.fold(
|
||||
onSuccess = { finishLoad(it) },
|
||||
onFailure = { loadError = "读取文件失败:${it.message}" },
|
||||
)
|
||||
} finally {
|
||||
loading = false
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
val trimmedId = id.trim()
|
||||
val idError = when {
|
||||
trimmedId.isEmpty() -> "ID 不能为空"
|
||||
@@ -331,7 +440,7 @@ private fun UploadEditScreen(
|
||||
Scaffold(
|
||||
topBar = {
|
||||
TopAppBar(
|
||||
title = { Text(if (initial == null) "添加上传配置" else "编辑上传配置") },
|
||||
title = { Text(if (isNew) "添加上传配置" else "编辑上传配置") },
|
||||
navigationIcon = {
|
||||
IconButton(onClick = onCancel) {
|
||||
Icon(
|
||||
@@ -499,11 +608,7 @@ private fun UploadEditScreen(
|
||||
|
||||
Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) {
|
||||
OutlinedButton(
|
||||
onClick = {
|
||||
importText = ""
|
||||
importError = null
|
||||
importDialog = true
|
||||
},
|
||||
onClick = { showImportMenu = true },
|
||||
modifier = Modifier
|
||||
.weight(1f)
|
||||
.height(40.dp),
|
||||
@@ -566,14 +671,6 @@ private fun UploadEditScreen(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.heightIn(min = 160.dp),
|
||||
placeholder = {
|
||||
Text(
|
||||
text = "{\"type\":\"CUSTOM\",\"id\":\"my-upload\"," +
|
||||
"\"uploadUrl\":{\"mode\":\"general\",\"url\":\"https://example.com/upload\"}," +
|
||||
"\"headers\":{}}",
|
||||
style = MaterialTheme.typography.bodySmall
|
||||
)
|
||||
},
|
||||
textStyle = MaterialTheme.typography.bodySmall
|
||||
)
|
||||
importError?.let {
|
||||
@@ -625,6 +722,93 @@ private fun UploadEditScreen(
|
||||
)
|
||||
}
|
||||
|
||||
if (showImportMenu) {
|
||||
AlertDialog(
|
||||
onDismissRequest = { showImportMenu = false },
|
||||
title = { Text("导入 JSON") },
|
||||
text = {
|
||||
Column(verticalArrangement = Arrangement.spacedBy(2.dp)) {
|
||||
ImportAction("从 URL 导入") {
|
||||
showImportMenu = false
|
||||
urlText = ""
|
||||
showUrlDialog = true
|
||||
}
|
||||
ImportAction("选择文件") {
|
||||
showImportMenu = false
|
||||
filePicker.launch(
|
||||
arrayOf("application/json", "text/plain", "application/octet-stream")
|
||||
)
|
||||
}
|
||||
ImportAction("打开编辑器") {
|
||||
showImportMenu = false
|
||||
openJsonEditor("")
|
||||
}
|
||||
}
|
||||
},
|
||||
confirmButton = {},
|
||||
dismissButton = {
|
||||
TextButton(onClick = { showImportMenu = false }) { Text("取消") }
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
if (showUrlDialog) {
|
||||
AlertDialog(
|
||||
onDismissRequest = { showUrlDialog = false },
|
||||
title = { Text("从 URL 导入") },
|
||||
text = {
|
||||
OutlinedTextField(
|
||||
value = urlText,
|
||||
onValueChange = { urlText = it },
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
singleLine = true,
|
||||
label = { Text("URL") },
|
||||
placeholder = { Text("") },
|
||||
textStyle = MaterialTheme.typography.bodySmall
|
||||
)
|
||||
},
|
||||
confirmButton = {
|
||||
TextButton(onClick = {
|
||||
showUrlDialog = false
|
||||
loadFromUrl()
|
||||
}) { Text("导入") }
|
||||
},
|
||||
dismissButton = {
|
||||
TextButton(onClick = { showUrlDialog = false }) { Text("取消") }
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
if (loading) {
|
||||
AlertDialog(
|
||||
onDismissRequest = { cancelImport() },
|
||||
properties = DialogProperties(
|
||||
dismissOnBackPress = true,
|
||||
dismissOnClickOutside = false,
|
||||
),
|
||||
title = { Text("正在导入") },
|
||||
text = {
|
||||
Column(verticalArrangement = Arrangement.spacedBy(8.dp)) {
|
||||
LinearProgressIndicator(modifier = Modifier.fillMaxWidth())
|
||||
}
|
||||
},
|
||||
confirmButton = {
|
||||
TextButton(onClick = { cancelImport() }) { Text("取消") }
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
loadError?.let { message ->
|
||||
AlertDialog(
|
||||
onDismissRequest = { loadError = null },
|
||||
title = { Text("加载失败") },
|
||||
text = { Text(message) },
|
||||
confirmButton = {
|
||||
TextButton(onClick = { loadError = null }) { Text("确定") }
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
if (exportDialog) {
|
||||
val json = runCatching {
|
||||
UploadStore.encodeProfile(buildProfile()).toString(2)
|
||||
|
||||
Reference in New Issue
Block a user