manager: Fix module changelog fetch failed. close #1084

This commit is contained in:
weishu
2023-10-25 11:11:44 +08:00
parent fb6565bd19
commit 4c79ad7a81
3 changed files with 36 additions and 21 deletions

View File

@@ -150,6 +150,7 @@ private fun ModuleList(
val changelogText = stringResource(R.string.module_changelog) val changelogText = stringResource(R.string.module_changelog)
val downloadingText = stringResource(R.string.module_downloading) val downloadingText = stringResource(R.string.module_downloading)
val startDownloadingText = stringResource(R.string.module_start_downloading) val startDownloadingText = stringResource(R.string.module_start_downloading)
val fetchChangeLogFailed = stringResource(R.string.module_changelog_failed)
val dialogHost = LocalDialogHost.current val dialogHost = LocalDialogHost.current
val snackBarHost = LocalSnackbarHost.current val snackBarHost = LocalSnackbarHost.current
@@ -161,36 +162,48 @@ private fun ModuleList(
downloadUrl: String, downloadUrl: String,
fileName: String fileName: String
) { ) {
val changelog = dialogHost.withLoading { val changelogResult = dialogHost.withLoading {
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
OkHttpClient().newCall( runCatching {
okhttp3.Request.Builder().url(changelogUrl).build() OkHttpClient().newCall(
).execute().body!!.string() okhttp3.Request.Builder().url(changelogUrl).build()
).execute().body!!.string()
}
} }
} }
if (changelog.isNotEmpty()) { val showToast: suspend (String) -> Unit = {msg->
// changelog is not empty, show it and wait for confirm withContext(Dispatchers.Main) {
val confirmResult = dialogHost.showConfirm( Toast.makeText(
changelogText, context,
content = changelog, msg,
markdown = true, Toast.LENGTH_SHORT
confirm = updateText, ).show()
)
if (confirmResult != ConfirmResult.Confirmed) {
return
} }
} }
withContext(Dispatchers.Main) { val changelog = changelogResult.getOrElse {
Toast.makeText( showToast(fetchChangeLogFailed.format(it.message))
context, return
startDownloadingText.format(module.name), }.ifBlank {
Toast.LENGTH_SHORT showToast(fetchChangeLogFailed.format(module.name))
).show() return
} }
// changelog is not empty, show it and wait for confirm
val confirmResult = dialogHost.showConfirm(
changelogText,
content = changelog,
markdown = true,
confirm = updateText,
)
if (confirmResult != ConfirmResult.Confirmed) {
return
}
showToast(startDownloadingText.format(module.name))
val downloading = downloadingText.format(module.name) val downloading = downloadingText.format(module.name)
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
download( download(

View File

@@ -101,4 +101,5 @@
<string name="app_profile_template_sync">同步在线规则</string> <string name="app_profile_template_sync">同步在线规则</string>
<string name="app_profile_template_save_failed">模版保存失败!</string> <string name="app_profile_template_save_failed">模版保存失败!</string>
<string name="app_profile_template_import_empty">剪切板为空!</string> <string name="app_profile_template_import_empty">剪切板为空!</string>
<string name="module_changelog_failed">获取更新日志失败:%s</string>
</resources> </resources>

View File

@@ -103,4 +103,5 @@
<string name="app_profile_template_sync">Sync online templates</string> <string name="app_profile_template_sync">Sync online templates</string>
<string name="app_profile_template_save_failed">Failed to save template</string> <string name="app_profile_template_save_failed">Failed to save template</string>
<string name="app_profile_template_import_empty">Clipboard is empty!</string> <string name="app_profile_template_import_empty">Clipboard is empty!</string>
<string name="module_changelog_failed">Fetch changelog failed: %s</string>
</resources> </resources>