update check: use callback

Cleaner code, it caches and does require one intent less
This commit is contained in:
Moritz Zwerger 2023-12-22 16:15:35 +01:00
parent 6552c4e10c
commit f61a6a5981
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
2 changed files with 22 additions and 30 deletions

View File

@ -13,7 +13,6 @@
package de.bixilon.minosoft.terminal.commands 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.nodes.LiteralNode
import de.bixilon.minosoft.commands.stack.print.PrintTarget import de.bixilon.minosoft.commands.stack.print.PrintTarget
import de.bixilon.minosoft.config.profile.profiles.other.OtherProfileManager import de.bixilon.minosoft.config.profile.profiles.other.OtherProfileManager
@ -27,14 +26,7 @@ import de.bixilon.minosoft.updater.UpdateProgress
object UpdateCommand : Command { object UpdateCommand : Command {
override var node: LiteralNode = LiteralNode("update", executor = { it.print.check() }).addChild( override var node: LiteralNode = LiteralNode("update", executor = { it.print.check() }).addChild(
LiteralNode("notes", executor = { LiteralNode("notes", executor = {
val update = MinosoftUpdater.update MinosoftUpdater.check { update ->
if (update != null) {
update.printNotes(it.print)
return@LiteralNode
}
it.print.print("Fetching update release notes...")
DefaultThreadPool += {
val update = MinosoftUpdater.check()
if (update == null) { if (update == null) {
print("No update available!") print("No update available!")
} else { } else {
@ -43,24 +35,14 @@ object UpdateCommand : Command {
} }
}), }),
LiteralNode("update", executor = { LiteralNode("update", executor = {
val update = MinosoftUpdater.update MinosoftUpdater.check(false) { 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()
if (update == null) { if (update == null) {
print("No update available!") print("No update available!")
} else { }
DefaultThreadPool += { if (update != null) {
val progress = UpdateProgress(log = it.print) val progress = UpdateProgress(log = it.print)
MinosoftUpdater.download(update, progress) MinosoftUpdater.download(update, progress)
} return@check
} }
} }
}), }),
@ -78,13 +60,12 @@ object UpdateCommand : Command {
private fun PrintTarget.check() { private fun PrintTarget.check() {
print("Checking for updates...") print("Checking for updates...")
DefaultThreadPool += { MinosoftUpdater.check(true) {
val update = MinosoftUpdater.check() if (it == null) {
if (update == null) {
print("No update available!") print("No update available!")
} else { } else {
print("There is a new update available:") 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 notes\"").clickEvent(InternalCommandClickEvent("update notes")), " to see the release notes."))
print(BaseComponent("Run ", TextComponent("\"update update\"").clickEvent(InternalCommandClickEvent("update update")), " to download and update.")) print(BaseComponent("Run ", TextComponent("\"update update\"").clickEvent(InternalCommandClickEvent("update update")), " to download and update."))
} }

View File

@ -13,6 +13,7 @@
package de.bixilon.minosoft.updater package de.bixilon.minosoft.updater
import de.bixilon.kutil.concurrent.pool.DefaultThreadPool
import de.bixilon.kutil.observer.DataObserver.Companion.observed import de.bixilon.kutil.observer.DataObserver.Companion.observed
import de.bixilon.kutil.os.PlatformInfo import de.bixilon.kutil.os.PlatformInfo
import de.bixilon.kutil.string.StringUtil.formatPlaceholder import de.bixilon.kutil.string.StringUtil.formatPlaceholder
@ -42,8 +43,18 @@ object MinosoftUpdater {
throw IllegalStateException("Illegal protocol: $url") 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? { fun check(): MinosoftUpdate? {
if (!MinosoftProperties.canUpdate()) return null
val profile = OtherProfileManager.selected.updater val profile = OtherProfileManager.selected.updater
return check(profile.url, profile.channel) return check(profile.url, profile.channel)
} }