Skip to content

Commit

Permalink
manager: Fix module changelog fetch failed. close #1084
Browse files Browse the repository at this point in the history
  • Loading branch information
tiann committed Oct 25, 2023
1 parent fb6565b commit 4c79ad7
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 21 deletions.
55 changes: 34 additions & 21 deletions manager/app/src/main/java/me/weishu/kernelsu/ui/screen/Module.kt
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ private fun ModuleList(
val changelogText = stringResource(R.string.module_changelog)
val downloadingText = stringResource(R.string.module_downloading)
val startDownloadingText = stringResource(R.string.module_start_downloading)
val fetchChangeLogFailed = stringResource(R.string.module_changelog_failed)

val dialogHost = LocalDialogHost.current
val snackBarHost = LocalSnackbarHost.current
Expand All @@ -161,36 +162,48 @@ private fun ModuleList(
downloadUrl: String,
fileName: String
) {
val changelog = dialogHost.withLoading {
val changelogResult = dialogHost.withLoading {
withContext(Dispatchers.IO) {
OkHttpClient().newCall(
okhttp3.Request.Builder().url(changelogUrl).build()
).execute().body!!.string()
runCatching {
OkHttpClient().newCall(
okhttp3.Request.Builder().url(changelogUrl).build()
).execute().body!!.string()
}
}
}

if (changelog.isNotEmpty()) {
// 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
val showToast: suspend (String) -> Unit = {msg->
withContext(Dispatchers.Main) {
Toast.makeText(
context,
msg,
Toast.LENGTH_SHORT
).show()
}
}

withContext(Dispatchers.Main) {
Toast.makeText(
context,
startDownloadingText.format(module.name),
Toast.LENGTH_SHORT
).show()
val changelog = changelogResult.getOrElse {
showToast(fetchChangeLogFailed.format(it.message))
return
}.ifBlank {
showToast(fetchChangeLogFailed.format(module.name))
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)
withContext(Dispatchers.IO) {
download(
Expand Down
1 change: 1 addition & 0 deletions manager/app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,5 @@
<string name="app_profile_template_sync">同步在线规则</string>
<string name="app_profile_template_save_failed">模版保存失败!</string>
<string name="app_profile_template_import_empty">剪切板为空!</string>
<string name="module_changelog_failed">获取更新日志失败:%s</string>
</resources>
1 change: 1 addition & 0 deletions manager/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,5 @@
<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_import_empty">Clipboard is empty!</string>
<string name="module_changelog_failed">Fetch changelog failed: %s</string>
</resources>

0 comments on commit 4c79ad7

Please sign in to comment.