From f61a6a59813e829bf1080280d2e0f7a202a81adf Mon Sep 17 00:00:00 2001 From: Moritz Zwerger Date: Fri, 22 Dec 2023 16:15:35 +0100 Subject: [PATCH] update check: use callback Cleaner code, it caches and does require one intent less --- .../terminal/commands/UpdateCommand.kt | 39 +++++-------------- .../minosoft/updater/MinosoftUpdater.kt | 13 ++++++- 2 files changed, 22 insertions(+), 30 deletions(-) diff --git a/src/main/java/de/bixilon/minosoft/terminal/commands/UpdateCommand.kt b/src/main/java/de/bixilon/minosoft/terminal/commands/UpdateCommand.kt index 7102e0378..d0fcf4e39 100644 --- a/src/main/java/de/bixilon/minosoft/terminal/commands/UpdateCommand.kt +++ b/src/main/java/de/bixilon/minosoft/terminal/commands/UpdateCommand.kt @@ -13,7 +13,6 @@ package de.bixilon.minosoft.terminal.commands -import de.bixilon.kutil.concurrent.pool.DefaultThreadPool import de.bixilon.minosoft.commands.nodes.LiteralNode import de.bixilon.minosoft.commands.stack.print.PrintTarget import de.bixilon.minosoft.config.profile.profiles.other.OtherProfileManager @@ -27,14 +26,7 @@ import de.bixilon.minosoft.updater.UpdateProgress object UpdateCommand : Command { override var node: LiteralNode = LiteralNode("update", executor = { it.print.check() }).addChild( LiteralNode("notes", executor = { - val update = MinosoftUpdater.update - if (update != null) { - update.printNotes(it.print) - return@LiteralNode - } - it.print.print("Fetching update release notes...") - DefaultThreadPool += { - val update = MinosoftUpdater.check() + MinosoftUpdater.check { update -> if (update == null) { print("No update available!") } else { @@ -43,24 +35,14 @@ object UpdateCommand : Command { } }), LiteralNode("update", executor = { - val update = MinosoftUpdater.update - if (update != null) { - DefaultThreadPool += { - val progress = UpdateProgress(log = it.print) - MinosoftUpdater.download(update, progress) - } - return@LiteralNode - } - it.print.print("Fetching update details...") - DefaultThreadPool += { - val update = MinosoftUpdater.check() + MinosoftUpdater.check(false) { update -> if (update == null) { print("No update available!") - } else { - DefaultThreadPool += { - val progress = UpdateProgress(log = it.print) - MinosoftUpdater.download(update, progress) - } + } + if (update != null) { + val progress = UpdateProgress(log = it.print) + MinosoftUpdater.download(update, progress) + return@check } } }), @@ -78,13 +60,12 @@ object UpdateCommand : Command { private fun PrintTarget.check() { print("Checking for updates...") - DefaultThreadPool += { - val update = MinosoftUpdater.check() - if (update == null) { + MinosoftUpdater.check(true) { + if (it == null) { print("No update available!") } else { print("There is a new update available:") - print("Version: ${update.name} (${update.id})") + print("Version: ${it.name} (${it.id})") print(BaseComponent("Run ", TextComponent("\"update notes\"").clickEvent(InternalCommandClickEvent("update notes")), " to see the release notes.")) print(BaseComponent("Run ", TextComponent("\"update update\"").clickEvent(InternalCommandClickEvent("update update")), " to download and update.")) } diff --git a/src/main/java/de/bixilon/minosoft/updater/MinosoftUpdater.kt b/src/main/java/de/bixilon/minosoft/updater/MinosoftUpdater.kt index abfea3c3d..4d91abb8d 100644 --- a/src/main/java/de/bixilon/minosoft/updater/MinosoftUpdater.kt +++ b/src/main/java/de/bixilon/minosoft/updater/MinosoftUpdater.kt @@ -13,6 +13,7 @@ package de.bixilon.minosoft.updater +import de.bixilon.kutil.concurrent.pool.DefaultThreadPool import de.bixilon.kutil.observer.DataObserver.Companion.observed import de.bixilon.kutil.os.PlatformInfo import de.bixilon.kutil.string.StringUtil.formatPlaceholder @@ -42,8 +43,18 @@ object MinosoftUpdater { throw IllegalStateException("Illegal protocol: $url") } + fun check(force: Boolean = false, callback: (MinosoftUpdate?) -> Unit) { + if (!MinosoftProperties.canUpdate()) return + if (!force) { + this.update?.let { callback.invoke(update); return } + } + DefaultThreadPool += { + val update = check() + callback.invoke(update) + } + } + fun check(): MinosoftUpdate? { - if (!MinosoftProperties.canUpdate()) return null val profile = OtherProfileManager.selected.updater return check(profile.url, profile.channel) }