修复直传接收并优化文件选择提示
This commit is contained in:
@@ -79,6 +79,8 @@ class MainActivity : ComponentActivity() {
|
||||
private var connectedNetwork: Network? = null
|
||||
private var connectedBaseUrl: String? = null
|
||||
private val selectedFiles = mutableListOf<SelectedFile>()
|
||||
private var managingFilesForPair: PairingPayload? = null
|
||||
private var managingFilesForBaseUrl: String? = null
|
||||
private var sendStatusText: TextView? = null
|
||||
private var sendFilesText: TextView? = null
|
||||
private var sendProgress: ProgressBar? = null
|
||||
@@ -112,13 +114,18 @@ class MainActivity : ComponentActivity() {
|
||||
}
|
||||
|
||||
private val pickFiles = registerForActivityResult(ActivityResultContracts.OpenMultipleDocuments()) { uris ->
|
||||
selectedFiles.clear()
|
||||
var unknown = 0
|
||||
for (uri in uris) {
|
||||
val file = UploadClient.describeFile(contentResolver, uri)
|
||||
if (file != null) selectedFiles += file else unknown++
|
||||
if (file != null && selectedFiles.none { it.uri == file.uri }) selectedFiles += file else if (file == null) unknown++
|
||||
}
|
||||
renderSelectedFiles()
|
||||
val pair = managingFilesForPair
|
||||
val baseUrl = managingFilesForBaseUrl
|
||||
if (pair != null && baseUrl != null) {
|
||||
showFileSelection(pair, baseUrl)
|
||||
return@registerForActivityResult
|
||||
}
|
||||
if (unknown > 0) {
|
||||
sendStatusText?.text = "有 $unknown 个文件无法确定大小,已跳过"
|
||||
} else if (selectedFiles.isNotEmpty()) {
|
||||
@@ -147,6 +154,8 @@ class MainActivity : ComponentActivity() {
|
||||
downloadRunning = false
|
||||
downloadStatusText = null
|
||||
selectedFiles.clear()
|
||||
managingFilesForPair = null
|
||||
managingFilesForBaseUrl = null
|
||||
|
||||
val root = pageColumn()
|
||||
root.addView(text("QFT", 30f, Color.WHITE, true))
|
||||
@@ -172,6 +181,13 @@ class MainActivity : ComponentActivity() {
|
||||
body = "扫描电脑二维码后自动连接 QFT 本地网络:接收二维码会进入手机选文件上传;下发二维码会自动下载并保存到 Downloads/QFT。",
|
||||
buttonLabel = "扫描电脑二维码",
|
||||
) { requestScanner(ScannerMode.PAIR) })
|
||||
root.addView(space(16))
|
||||
root.addView(modeCard(
|
||||
title = "接收文件柜",
|
||||
badge = "Downloads/QFT · 图片/视频/PDF/Office/压缩包等",
|
||||
body = "查看 QFT 已接收的文件;点击后将使用手机中可用的阅读器或播放器打开。",
|
||||
buttonLabel = "打开文件柜",
|
||||
) { showReceivedFiles() })
|
||||
root.addView(space(22))
|
||||
root.addView(text("本机设备:$receiverDeviceLabel", 13f, MUTED))
|
||||
root.addView(text("QFT v0.4.0-rc2 · Android 7+ 到最新", 12f, MUTED))
|
||||
@@ -716,22 +732,25 @@ class MainActivity : ComponentActivity() {
|
||||
root.addView(space(14))
|
||||
root.addView(card().apply {
|
||||
addView(text("选择文件", 17f, Color.WHITE, true))
|
||||
sendFilesText = text("尚未选择文件", 14f, MUTED)
|
||||
sendFilesText = text("请先选择需要传输的文件", 14f, WARNING)
|
||||
addView(sendFilesText, matchWrap(top = 8))
|
||||
addView(button("选择文件(可多选)").apply {
|
||||
setOnClickListener { pickFiles.launch(arrayOf("*/*")) }
|
||||
setOnClickListener { showFileSelection(pair, baseUrl) }
|
||||
}, matchWrap(top = 12))
|
||||
})
|
||||
root.addView(space(14))
|
||||
root.addView(card().apply {
|
||||
addView(text("传输进度", 17f, Color.WHITE, true))
|
||||
sendStatusText = text("选择文件后点击开始传输", 14f, MUTED)
|
||||
sendStatusText = text("请先选择需要传输的文件,再点击“开始传输”", 14f, WARNING)
|
||||
addView(sendStatusText, matchWrap(top = 8))
|
||||
sendProgress = ProgressBar(this@MainActivity, null, android.R.attr.progressBarStyleHorizontal).apply { max = 1000; progress = 0 }
|
||||
addView(sendProgress, LinearLayout.LayoutParams(-1, dp(9)).apply { topMargin = dp(12) })
|
||||
sendButton = button("开始传输").apply {
|
||||
isEnabled = selectedFiles.isNotEmpty()
|
||||
setOnClickListener { startUploads(pair, baseUrl) }
|
||||
sendButton = button("选择传输文件").apply {
|
||||
isEnabled = connectedBaseUrl != null
|
||||
setOnClickListener {
|
||||
if (selectedFiles.isEmpty()) showFileSelection(pair, baseUrl)
|
||||
else startUploads(pair, baseUrl)
|
||||
}
|
||||
}
|
||||
addView(sendButton, matchWrap(top = 14))
|
||||
})
|
||||
@@ -800,10 +819,118 @@ class MainActivity : ComponentActivity() {
|
||||
}
|
||||
}
|
||||
|
||||
private fun showReceivedFiles(message: String? = null) {
|
||||
val root = pageColumn()
|
||||
root.addView(text("接收文件柜", 28f, Color.WHITE, true))
|
||||
root.addView(text("QFT 已保存到 Downloads/QFT 的文件", 14f, MUTED))
|
||||
message?.let { root.addView(text(it, 13f, WARNING), matchWrap(top = 10)) }
|
||||
root.addView(space(16))
|
||||
|
||||
val files = try {
|
||||
ReceivedFileCabinet.list(this)
|
||||
} catch (e: Exception) {
|
||||
emptyList()
|
||||
}
|
||||
if (files.isEmpty()) {
|
||||
root.addView(card().apply {
|
||||
addView(text("文件柜为空", 17f, Color.WHITE, true))
|
||||
addView(text("完成电脑下发或光学接收后,文件会自动出现在这里。", 14f, MUTED), matchWrap(top = 8))
|
||||
})
|
||||
} else {
|
||||
root.addView(text("共 ${files.size} 个文件", 13f, ACCENT, true))
|
||||
files.forEach { file ->
|
||||
val entry = card()
|
||||
entry.addView(text(file.name, 16f, Color.WHITE, true))
|
||||
entry.addView(
|
||||
text("${humanBytes(file.size)} · ${file.mimeType.ifBlank { "未知格式" }}", 12f, MUTED),
|
||||
matchWrap(top = 5),
|
||||
)
|
||||
entry.addView(button("打开文件").apply {
|
||||
setOnClickListener {
|
||||
try {
|
||||
ReceivedFileCabinet.open(this@MainActivity, file)
|
||||
} catch (e: Exception) {
|
||||
showReceivedFiles("无法打开 ${file.name}:请安装支持该格式的阅读器。")
|
||||
}
|
||||
}
|
||||
}, matchWrap(top = 10))
|
||||
root.addView(entry, matchWrap(bottom = 10))
|
||||
}
|
||||
}
|
||||
root.addView(space(12))
|
||||
root.addView(button("返回首页").apply { setOnClickListener { showHome() } })
|
||||
setContentView(ScrollView(this).apply { setBackgroundColor(BG); addView(root) })
|
||||
}
|
||||
|
||||
private fun showFileSelection(pair: PairingPayload, baseUrl: String) {
|
||||
managingFilesForPair = pair
|
||||
managingFilesForBaseUrl = baseUrl
|
||||
val root = pageColumn()
|
||||
root.addView(text("选择发送文件", 28f, Color.WHITE, true))
|
||||
root.addView(text("可多次添加文件,并在列表中单独移除。", 14f, MUTED))
|
||||
root.addView(space(14))
|
||||
val total = selectedFiles.sumOf { it.size }
|
||||
root.addView(card().apply {
|
||||
addView(text("已选 ${selectedFiles.size} 个 · ${humanBytes(total)}", 16f, ACCENT, true))
|
||||
if (selectedFiles.isEmpty()) {
|
||||
addView(text("请先选择需要传输的文件", 14f, WARNING), matchWrap(top = 8))
|
||||
} else {
|
||||
selectedFiles.toList().forEach { file ->
|
||||
val row = LinearLayout(this@MainActivity).apply {
|
||||
orientation = LinearLayout.HORIZONTAL
|
||||
gravity = Gravity.CENTER_VERTICAL
|
||||
}
|
||||
row.addView(
|
||||
text("${file.name}\n${humanBytes(file.size)}", 14f, Color.WHITE),
|
||||
LinearLayout.LayoutParams(0, -2, 1f).apply { marginEnd = dp(8) },
|
||||
)
|
||||
row.addView(button("移除").apply {
|
||||
setOnClickListener {
|
||||
selectedFiles.removeAll { it.uri == file.uri }
|
||||
showFileSelection(pair, baseUrl)
|
||||
}
|
||||
}, LinearLayout.LayoutParams(dp(82), dp(44)))
|
||||
addView(row, matchWrap(top = 10))
|
||||
}
|
||||
}
|
||||
})
|
||||
root.addView(space(14))
|
||||
root.addView(button("添加文件(可多选)").apply {
|
||||
setOnClickListener { pickFiles.launch(arrayOf("*/*")) }
|
||||
})
|
||||
if (selectedFiles.isNotEmpty()) {
|
||||
root.addView(space(10))
|
||||
root.addView(button("清空选择").apply {
|
||||
setOnClickListener {
|
||||
selectedFiles.clear()
|
||||
showFileSelection(pair, baseUrl)
|
||||
}
|
||||
})
|
||||
}
|
||||
root.addView(space(10))
|
||||
root.addView(button("完成并返回传输").apply {
|
||||
isEnabled = selectedFiles.isNotEmpty()
|
||||
setOnClickListener {
|
||||
managingFilesForPair = null
|
||||
managingFilesForBaseUrl = null
|
||||
showSendReady(pair, baseUrl)
|
||||
}
|
||||
})
|
||||
root.addView(space(10))
|
||||
root.addView(button("取消并返回传输").apply {
|
||||
setOnClickListener {
|
||||
managingFilesForPair = null
|
||||
managingFilesForBaseUrl = null
|
||||
showSendReady(pair, baseUrl)
|
||||
}
|
||||
})
|
||||
setContentView(ScrollView(this).apply { setBackgroundColor(BG); addView(root) })
|
||||
}
|
||||
|
||||
private fun renderSelectedFiles() {
|
||||
val total = selectedFiles.sumOf { it.size }
|
||||
sendFilesText?.text = if (selectedFiles.isEmpty()) {
|
||||
"尚未选择文件"
|
||||
"请先选择需要传输的文件"
|
||||
} else {
|
||||
buildString {
|
||||
selectedFiles.take(8).forEach { append("• ${it.name} ${humanBytes(it.size)}\n") }
|
||||
@@ -811,11 +938,17 @@ class MainActivity : ComponentActivity() {
|
||||
append("共 ${selectedFiles.size} 个 · ${humanBytes(total)}")
|
||||
}.trim()
|
||||
}
|
||||
sendButton?.isEnabled = selectedFiles.isNotEmpty() && connectedBaseUrl != null
|
||||
sendButton?.text = if (selectedFiles.isEmpty()) "选择传输文件" else "开始传输"
|
||||
sendButton?.isEnabled = connectedBaseUrl != null
|
||||
}
|
||||
|
||||
private fun startUploads(pair: PairingPayload, baseUrl: String) {
|
||||
if (selectedFiles.isEmpty()) return
|
||||
if (selectedFiles.isEmpty()) {
|
||||
sendStatusText?.setTextColor(WARNING)
|
||||
sendStatusText?.text = "请先选择需要传输的文件"
|
||||
showFileSelection(pair, baseUrl)
|
||||
return
|
||||
}
|
||||
val files = selectedFiles.toList()
|
||||
sendButton?.isEnabled = false
|
||||
transferExecutor.execute {
|
||||
|
||||
Reference in New Issue
Block a user