From 74ddca5d54eacc2f48bb94594273fb2079a33ae5 Mon Sep 17 00:00:00 2001 From: comp500 Date: Sun, 13 Dec 2020 16:57:12 +0000 Subject: [PATCH] Remove unnecessary Futures from IUserInterface API --- .../infra/packwiz/installer/UpdateManager.kt | 53 +++---------------- .../packwiz/installer/ui/IUserInterface.kt | 12 ++--- .../packwiz/installer/ui/cli/CLIHandler.kt | 10 ++-- .../packwiz/installer/ui/gui/GUIHandler.kt | 15 +++--- 4 files changed, 19 insertions(+), 71 deletions(-) diff --git a/src/main/kotlin/link/infra/packwiz/installer/UpdateManager.kt b/src/main/kotlin/link/infra/packwiz/installer/UpdateManager.kt index d3c8245..ba0db0c 100644 --- a/src/main/kotlin/link/infra/packwiz/installer/UpdateManager.kt +++ b/src/main/kotlin/link/infra/packwiz/installer/UpdateManager.kt @@ -336,17 +336,7 @@ class UpdateManager internal constructor(private val opts: Options, val ui: IUse val failedTaskDetails = tasks.asSequence().map(DownloadTask::exceptionDetails).filterNotNull().toList() if (failedTaskDetails.isNotEmpty()) { errorsOccurred = true - val exceptionListResult: ExceptionListResult - exceptionListResult = try { - ui.showExceptions(failedTaskDetails, tasks.size, true).get() - } catch (e: InterruptedException) { // Interrupted means cancelled??? - ui.handleExceptionAndExit(e) - return - } catch (e: ExecutionException) { - ui.handleExceptionAndExit(e) - return - } - when (exceptionListResult) { + when (ui.showExceptions(failedTaskDetails, tasks.size, true)) { ExceptionListResult.CONTINUE -> {} ExceptionListResult.CANCEL -> { cancelled = true @@ -370,18 +360,9 @@ class UpdateManager internal constructor(private val opts: Options, val ui: IUse // If options changed, present all options again if (ui.optionsButtonPressed || optionTasks.any(DownloadTask::isNewOptional)) { // new ArrayList is required so it's an IOptionDetails rather than a DownloadTask list - val cancelledResult = ui.showOptions(ArrayList(optionTasks)) - try { - if (cancelledResult.get()) { - cancelled = true - // TODO: Should the UI be closed somehow?? - return - } - } catch (e: InterruptedException) { - // Interrupted means cancelled??? - ui.handleExceptionAndExit(e) - } catch (e: ExecutionException) { - ui.handleExceptionAndExit(e) + if (ui.showOptions(ArrayList(optionTasks))) { + cancelled = true + handleCancellation() } } ui.disableOptionsButton() @@ -445,18 +426,7 @@ class UpdateManager internal constructor(private val opts: Options, val ui: IUse val failedTasks2ElectricBoogaloo = nonFailedFirstTasks.asSequence().map(DownloadTask::exceptionDetails).filterNotNull().toList() if (failedTasks2ElectricBoogaloo.isNotEmpty()) { errorsOccurred = true - val exceptionListResult: ExceptionListResult - exceptionListResult = try { - ui.showExceptions(failedTasks2ElectricBoogaloo, tasks.size, false).get() - } catch (e: InterruptedException) { - // Interrupted means cancelled??? - ui.handleExceptionAndExit(e) - return - } catch (e: ExecutionException) { - ui.handleExceptionAndExit(e) - return - } - when (exceptionListResult) { + when (ui.showExceptions(failedTasks2ElectricBoogaloo, tasks.size, false)) { ExceptionListResult.CONTINUE -> {} ExceptionListResult.CANCEL -> cancelled = true ExceptionListResult.IGNORE -> cancelledStartGame = true @@ -465,18 +435,7 @@ class UpdateManager internal constructor(private val opts: Options, val ui: IUse } private fun showCancellationDialog() { - val cancellationResult: CancellationResult - cancellationResult = try { - ui.showCancellationDialog().get() - } catch (e: InterruptedException) { - // Interrupted means cancelled??? - ui.handleExceptionAndExit(e) - return - } catch (e: ExecutionException) { - ui.handleExceptionAndExit(e) - return - } - when (cancellationResult) { + when (ui.showCancellationDialog()) { CancellationResult.QUIT -> cancelled = true CancellationResult.CONTINUE -> cancelledStartGame = true } diff --git a/src/main/kotlin/link/infra/packwiz/installer/ui/IUserInterface.kt b/src/main/kotlin/link/infra/packwiz/installer/ui/IUserInterface.kt index 8909795..8a690e0 100644 --- a/src/main/kotlin/link/infra/packwiz/installer/ui/IUserInterface.kt +++ b/src/main/kotlin/link/infra/packwiz/installer/ui/IUserInterface.kt @@ -3,8 +3,6 @@ package link.infra.packwiz.installer.ui import link.infra.packwiz.installer.ui.data.ExceptionDetails import link.infra.packwiz.installer.ui.data.IOptionDetails import link.infra.packwiz.installer.ui.data.InstallProgress -import java.util.concurrent.CompletableFuture -import java.util.concurrent.Future import kotlin.system.exitProcess interface IUserInterface { @@ -19,16 +17,12 @@ interface IUserInterface { fun setTitle(title: String) {} fun submitProgress(progress: InstallProgress) // Return true if the installation was cancelled! - fun showOptions(options: List): Future + fun showOptions(options: List): Boolean - fun showExceptions(exceptions: List, numTotal: Int, allowsIgnore: Boolean): Future + fun showExceptions(exceptions: List, numTotal: Int, allowsIgnore: Boolean): ExceptionListResult fun disableOptionsButton() {} - fun showCancellationDialog(): Future { - return CompletableFuture().apply { - complete(CancellationResult.QUIT) - } - } + fun showCancellationDialog(): CancellationResult = CancellationResult.QUIT enum class ExceptionListResult { CONTINUE, CANCEL, IGNORE diff --git a/src/main/kotlin/link/infra/packwiz/installer/ui/cli/CLIHandler.kt b/src/main/kotlin/link/infra/packwiz/installer/ui/cli/CLIHandler.kt index 0885d89..5e01b02 100644 --- a/src/main/kotlin/link/infra/packwiz/installer/ui/cli/CLIHandler.kt +++ b/src/main/kotlin/link/infra/packwiz/installer/ui/cli/CLIHandler.kt @@ -5,8 +5,6 @@ import link.infra.packwiz.installer.ui.IUserInterface.ExceptionListResult import link.infra.packwiz.installer.ui.data.ExceptionDetails import link.infra.packwiz.installer.ui.data.IOptionDetails import link.infra.packwiz.installer.ui.data.InstallProgress -import java.util.concurrent.CompletableFuture -import java.util.concurrent.Future import kotlin.system.exitProcess class CLIHandler : IUserInterface { @@ -34,18 +32,16 @@ class CLIHandler : IUserInterface { println(sb.toString()) } - override fun showOptions(options: List): Future { + override fun showOptions(options: List): Boolean { for (opt in options) { opt.optionValue = true // TODO: implement option choice in the CLI? println("Warning: accepting option " + opt.name + " as option choosing is not implemented in the CLI") } - return CompletableFuture().apply { - complete(false) // Can't be cancelled! - } + return false // Can't be cancelled! } - override fun showExceptions(exceptions: List, numTotal: Int, allowsIgnore: Boolean): Future { + override fun showExceptions(exceptions: List, numTotal: Int, allowsIgnore: Boolean): ExceptionListResult { println("Failed to download modpack, the following errors were encountered:") for (ex in exceptions) { println(ex.name + ": ") diff --git a/src/main/kotlin/link/infra/packwiz/installer/ui/gui/GUIHandler.kt b/src/main/kotlin/link/infra/packwiz/installer/ui/gui/GUIHandler.kt index 042fb2b..782dec5 100644 --- a/src/main/kotlin/link/infra/packwiz/installer/ui/gui/GUIHandler.kt +++ b/src/main/kotlin/link/infra/packwiz/installer/ui/gui/GUIHandler.kt @@ -7,7 +7,6 @@ import link.infra.packwiz.installer.ui.data.IOptionDetails import link.infra.packwiz.installer.ui.data.InstallProgress import java.awt.EventQueue import java.util.concurrent.CompletableFuture -import java.util.concurrent.Future import javax.swing.JDialog import javax.swing.JOptionPane import javax.swing.UIManager @@ -86,9 +85,9 @@ class GUIHandler : IUserInterface { } } - override fun showOptions(options: List): Future { + override fun showOptions(options: List): Boolean { val future = CompletableFuture() - EventQueue.invokeLater { + EventQueue.invokeAndWait { if (options.isEmpty()) { JOptionPane.showMessageDialog(null, "This modpack has no optional mods!", @@ -101,10 +100,10 @@ class GUIHandler : IUserInterface { } } } - return future + return future.get() } - override fun showExceptions(exceptions: List, numTotal: Int, allowsIgnore: Boolean): Future { + override fun showExceptions(exceptions: List, numTotal: Int, allowsIgnore: Boolean): ExceptionListResult { val future = CompletableFuture() EventQueue.invokeLater { ExceptionListWindow(exceptions, future, numTotal, allowsIgnore, frmPackwizlauncher).apply { @@ -112,14 +111,14 @@ class GUIHandler : IUserInterface { isVisible = true } } - return future + return future.get() } override fun disableOptionsButton() = EventQueue.invokeLater { frmPackwizlauncher.disableOptionsButton() } - override fun showCancellationDialog(): Future { + override fun showCancellationDialog(): IUserInterface.CancellationResult { val future = CompletableFuture() EventQueue.invokeLater { val buttons = arrayOf("Quit", "Ignore") @@ -129,6 +128,6 @@ class GUIHandler : IUserInterface { JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, buttons, buttons[0]) future.complete(if (result == 0) IUserInterface.CancellationResult.QUIT else IUserInterface.CancellationResult.CONTINUE) } - return future + return future.get() } } \ No newline at end of file