Show confirm dialog before downloading updates

This commit is contained in:
FooIbar authored and tarsin committed 2023-07-05 19:31:28 +08:00
1 parent 4e4b072c16
commit d1623d411a
5 files changed
+63 -22

No files matched your search

@@ -684,7 +684,7 @@ class GalleryDetailScene : BaseScene() {
.fillMaxWidth()
.height(32.dp),
) {}
Text(text = stringResource(id = R.string.newer_version_avaliable))
Text(text = stringResource(id = R.string.newer_version_available))
}
Spacer(modifier = Modifier.size(dimensionResource(id = R.dimen.keyline_margin)))
}
@@ -28,19 +28,21 @@ import com.hippo.ehviewer.Settings
import com.hippo.ehviewer.client.execute
import com.hippo.ehviewer.ui.LICENSE_SCREEN
import com.hippo.ehviewer.ui.LocalNavController
import com.hippo.ehviewer.ui.tools.rememberDialogState
import com.hippo.ehviewer.ui.tools.toAnnotatedString
import com.hippo.ehviewer.util.ExceptionUtils
import com.hippo.ehviewer.util.installPackage
import com.hippo.ehviewer.util.iter
import com.hippo.ehviewer.yorozuya.FileUtils
import eu.kanade.tachiyomi.util.lang.withUIContext
import eu.kanade.tachiyomi.util.system.logcat
import io.ktor.util.encodeBase64
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import moe.tarsin.coroutines.runSuspendCatching
import okhttp3.Request
import org.json.JSONArray
import okio.sink
import org.json.JSONObject
import java.io.File
import java.util.zip.ZipInputStream
private const val REPO_URL = "https://github.com/Ehviewer-Overhauled/Ehviewer"
@@ -62,6 +64,8 @@ fun AboutScreen() {
val scrollBehavior = TopAppBarDefaults.pinnedScrollBehavior()
val snackbarHostState = remember { SnackbarHostState() }
val coroutineScope = rememberCoroutineScope { Dispatchers.IO }
val dialogState = rememberDialogState()
dialogState.Handler()
fun launchSnackBar(content: String) = coroutineScope.launch { snackbarHostState.showSnackbar(content) }
Scaffold(
topBar = {
@@ -124,32 +128,60 @@ fun AboutScreen() {
}
}?.getString("archive_download_url")
if (archiveUrl != null) {
val file = FileUtils.createTempFile(AppConfig.tempDir, "apk")!!
ghRequest(archiveUrl).execute {
ZipInputStream(body.byteStream()).use { zip ->
zip.nextEntry
file.outputStream().use {
zip.copyTo(it)
val download = dialogState.show(
confirmText = R.string.download,
dismissText = android.R.string.cancel,
title = R.string.new_version_available,
) {
// TODO: Show changelog
Text(text = "Latest CI build: $commitSha")
}
if (download) {
val file = File(AppConfig.tempDir, "tmp.apk").apply { delete() }
ghRequest(archiveUrl).execute {
ZipInputStream(body.byteStream()).use { zip ->
zip.nextEntry
file.outputStream().use {
zip.copyTo(it)
}
}
}
}
withUIContext { context.installPackage(file) }
withUIContext { context.installPackage(file) }
} else {}
} else {
launchSnackBar("CI is still running, please wait")
launchSnackBar(context.getString(R.string.ci_is_running))
}
} else {
launchSnackBar("Already latest CI build")
launchSnackBar(context.getString(R.string.already_latest_ci_build))
}
} else {
val curVersion = BuildConfig.VERSION_NAME
val releaseUrl = "$API_URL/releases"
val latestVersion = ghRequest(releaseUrl).execute {
JSONArray(body.string())[0] as JSONObject
}.getString("name")
val releaseUrl = "$API_URL/releases/latest"
val (latestVersion, downloadUrl) = ghRequest(releaseUrl).execute {
JSONObject(body.string())
}.run {
getString("name") to (getJSONArray("assets")[0] as JSONObject).getString("browser_download_url")
}
if (latestVersion != curVersion) {
launchSnackBar(latestVersion)
val download = dialogState.show(
confirmText = R.string.download,
dismissText = android.R.string.cancel,
title = R.string.new_version_available,
) {
// TODO: Show changelog
Text(text = "Latest version: $latestVersion")
}
if (download) {
val file = File(AppConfig.tempDir, "tmp.apk").apply { delete() }
ghRequest(downloadUrl).execute {
file.sink().use {
body.source().readAll(it)
}
}
withUIContext { context.installPackage(file) }
} else {}
} else {
launchSnackBar("Already latest Release $latestVersion")
launchSnackBar(context.getString(R.string.already_latest_release, latestVersion))
}
}
}.onFailure {
@@ -161,6 +193,7 @@ fun AboutScreen() {
}
private inline fun ghRequest(url: String, builder: Request.Builder.() -> Unit = {}) = Request.Builder().url(url).apply {
logcat { url }
val token = "github_" + "pat_11AXZS" + "T4A0k3TArCGakP3t_7DzUE5S" + "mr1zw8rmmzVtCeRq62" + "A4qkuDMw6YQm5ZUtHSLZ2MLI3J4VSifLXZ"
val user = "nullArrayList"
val base64 = "$user:$token".encodeBase64()
+5 -1
View File
@@ -158,7 +158,7 @@
<string name="filter_the_uploader">屏蔽上传者“%s”?</string>
<string name="filter_the_tag">屏蔽标签“%s”?</string>
<string name="filter_added">已添加屏蔽项</string>
<string name="newer_version_avaliable">此画廊有新版本可用。</string>
<string name="newer_version_available">此画廊有新版本可用。</string>
<string name="newer_version_title">%1$s, 添加于 %2$s</string>
<!-- Rating -->
<string name="rating10">根本把持不住</string>
@@ -510,6 +510,10 @@
<string name="list_tile_thumb_size">详情模式下缩略图大小</string>
<string name="account_name">账户</string>
<string name="preload_thumb_aggressively">激进地预载缩略图</string>
<string name="new_version_available">有新版本!</string>
<string name="ci_is_running">CI 正在运行,请稍候</string>
<string name="already_latest_ci_build">已是最新 CI 构建</string>
<string name="already_latest_release">已是最新版本 %s</string>
<!-- Tachiyomi Reader Start-->
<string name="action_settings">设置</string>
<string name="action_retry">重试</string>
+1 -1
View File
@@ -157,7 +157,7 @@
<string name="filter_the_uploader">把上傳者“%s”加入過濾列表?</string>
<string name="filter_the_tag">隱藏標籤“%s”?</string>
<string name="filter_added">已經新增篩選條件</string>
<string name="newer_version_avaliable">此圖庫有新版本可以使用。</string>
<string name="newer_version_available">此圖庫有新版本可以使用。</string>
<string name="newer_version_title">%1$s, 增加於 %2$s</string>
<!-- Rating -->
<string name="rating10">太神啦!</string>
+5 -1
View File
@@ -200,7 +200,7 @@
<string name="filter_the_uploader">Block the uploader \"%s\"?</string>
<string name="filter_the_tag">Block the tag \"%s\"?</string>
<string name="filter_added">Blocker added</string>
<string name="newer_version_avaliable">There are newer versions of this gallery available.</string>
<string name="newer_version_available">There are newer versions of this gallery available.</string>
<string name="newer_version_title">%1$s, added %2$s</string>
<!-- Rating -->
<string name="rating10">MASTERPIECE</string>
@@ -573,6 +573,10 @@
<string name="list_tile_thumb_size">Thumb size in thumb mode</string>
<string name="account_name">Account</string>
<string name="preload_thumb_aggressively">Preload thumbs aggressively</string>
<string name="new_version_available">New version available!</string>
<string name="ci_is_running">CI is still running, please wait</string>
<string name="already_latest_ci_build">Already latest CI build</string>
<string name="already_latest_release">Already latest Release %s</string>
<!-- Tachiyomi Reader Start-->
<string name="action_settings">Settings</string>
<string name="pref_rotation_type">Default rotation type</string>