@@ -85,6 +85,7 @@ class MainActivity : ComponentActivity() {
8585 var currentDelay by remember { mutableStateOf(2000L ) } // 当前延迟时间(毫秒)
8686 var syncRecords by remember { mutableStateOf(listOf<SyncRecord >()) } // 同步记录
8787 var showIpHistory by remember { mutableStateOf(false ) } // 是否显示 IP 历史列表
88+ var updateResult by remember { mutableStateOf<UpdateChecker .UpdateResult ?>(null ) } // 更新检查结果
8889 val scope = rememberCoroutineScope()
8990
9091 val dateFormat = SimpleDateFormat (" HH:mm:ss" , Locale .getDefault())
@@ -142,9 +143,16 @@ class MainActivity : ComponentActivity() {
142143 // 检查更新按钮
143144 OutlinedButton (
144145 onClick = {
146+ logMessage = " 正在检查更新..."
145147 scope.launch {
146- checkForUpdate { updateResult ->
147- logMessage = updateResult
148+ val result = UpdateChecker .check(BuildConfig .VERSION_NAME )
149+ if (result == null ) {
150+ logMessage = " 检查更新失败,请检查网络连接"
151+ } else if (result.hasUpdate) {
152+ updateResult = result
153+ logMessage = " 发现新版本: v${result.latestVersion} "
154+ } else {
155+ logMessage = " 已是最新版本 v${result.currentVersion} "
148156 }
149157 }
150158 },
@@ -648,51 +656,54 @@ class MainActivity : ComponentActivity() {
648656 }
649657 }
650658 }
651- }
652- }
653- }
654-
655- /* *
656- * 检查应用更新
657- */
658- private suspend fun checkForUpdate (onMessage : (String ) -> Unit ) {
659- onMessage(" 正在检查更新..." )
660-
661- val result = UpdateChecker .check(BuildConfig .VERSION_NAME )
662-
663- if (result == null ) {
664- onMessage(" 检查更新失败,请检查网络连接" )
665- return
666- }
667-
668- if (result.hasUpdate) {
669- onMessage(" 发现新版本: v${result.latestVersion} " )
670- // 在主线程显示对话框
671- runOnUiThread {
672- androidx.appcompat.app.AlertDialog .Builder (this )
673- .setTitle(" 发现新版本 v${result.latestVersion} " )
674- .setMessage(
675- buildString {
676- append(" 当前版本:v${result.currentVersion} \n " )
677- append(" 最新版本:v${result.latestVersion} \n\n " )
678- if (result.releaseNotes.isNotBlank()) {
679- append(" 更新说明:\n ${result.releaseNotes} \n\n " )
659+
660+ // 更新对话框
661+ updateResult?.let { result ->
662+ AlertDialog (
663+ onDismissRequest = { updateResult = null },
664+ title = { Text (" 发现新版本 v${result.latestVersion} " ) },
665+ text = {
666+ Column {
667+ Text (" 当前版本:v${result.currentVersion} " )
668+ Text (" 最新版本:v${result.latestVersion} " )
669+ if (result.releaseNotes.isNotBlank()) {
670+ Spacer (modifier = Modifier .height(8 .dp))
671+ Text (" 更新说明:" , style = MaterialTheme .typography.bodyMedium)
672+ Text (
673+ result.releaseNotes.take(200 ) + if (result.releaseNotes.length > 200 ) " ..." else " " ,
674+ style = MaterialTheme .typography.bodySmall
675+ )
676+ }
677+ Spacer (modifier = Modifier .height(8 .dp))
678+ Text (
679+ " ⚠️ 如安装时提示「签名不一致」,请先卸载旧版本再安装。\n (设置不会丢失)" ,
680+ style = MaterialTheme .typography.bodySmall,
681+ color = MaterialTheme .colorScheme.onSurfaceVariant
682+ )
683+ }
684+ },
685+ confirmButton = {
686+ Button (
687+ onClick = {
688+ val intent = android.content.Intent (
689+ android.content.Intent .ACTION_VIEW ,
690+ android.net.Uri .parse(result.downloadUrl)
691+ )
692+ startActivity(intent)
693+ updateResult = null
694+ }
695+ ) {
696+ Text (" 下载更新" )
697+ }
698+ },
699+ dismissButton = {
700+ TextButton (onClick = { updateResult = null }) {
701+ Text (" 稍后再说" )
680702 }
681- append(" ⚠️ 如安装时提示「签名不一致」,请先卸载旧版本再安装。\n (设置不会丢失)" )
682703 }
683704 )
684- .setPositiveButton(" 下载更新" ) { _, _ ->
685- val intent = android.content.Intent (
686- android.content.Intent .ACTION_VIEW ,
687- android.net.Uri .parse(result.downloadUrl)
688- )
689- startActivity(intent)
690- }
691- .setNegativeButton(" 稍后再说" , null )
692- .show()
705+ }
693706 }
694- } else {
695- onMessage(" 已是最新版本 v${result.currentVersion} " )
696707 }
697708 }
698709
0 commit comments