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,15 +162,34 @@ 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) {
runCatching {
OkHttpClient().newCall( OkHttpClient().newCall(
okhttp3.Request.Builder().url(changelogUrl).build() okhttp3.Request.Builder().url(changelogUrl).build()
).execute().body!!.string() ).execute().body!!.string()
} }
} }
}
val showToast: suspend (String) -> Unit = {msg->
withContext(Dispatchers.Main) {
Toast.makeText(
context,
msg,
Toast.LENGTH_SHORT
).show()
}
}
val changelog = changelogResult.getOrElse {
showToast(fetchChangeLogFailed.format(it.message))
return
}.ifBlank {
showToast(fetchChangeLogFailed.format(module.name))
return
}
if (changelog.isNotEmpty()) {
// changelog is not empty, show it and wait for confirm // changelog is not empty, show it and wait for confirm
val confirmResult = dialogHost.showConfirm( val confirmResult = dialogHost.showConfirm(
changelogText, changelogText,
@@ -181,15 +201,8 @@ private fun ModuleList(
if (confirmResult != ConfirmResult.Confirmed) { if (confirmResult != ConfirmResult.Confirmed) {
return return
} }
}
withContext(Dispatchers.Main) { showToast(startDownloadingText.format(module.name))
Toast.makeText(
context,
startDownloadingText.format(module.name),
Toast.LENGTH_SHORT
).show()
}
val downloading = downloadingText.format(module.name) val downloading = downloadingText.format(module.name)
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {

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>