Remove unnecessary Futures from IUserInterface API

This commit is contained in:
comp500 2020-12-13 16:57:12 +00:00
parent 0df48d19a9
commit 74ddca5d54
4 changed files with 19 additions and 71 deletions

View File

@ -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
}

View File

@ -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<IOptionDetails>): Future<Boolean>
fun showOptions(options: List<IOptionDetails>): Boolean
fun showExceptions(exceptions: List<ExceptionDetails>, numTotal: Int, allowsIgnore: Boolean): Future<ExceptionListResult>
fun showExceptions(exceptions: List<ExceptionDetails>, numTotal: Int, allowsIgnore: Boolean): ExceptionListResult
fun disableOptionsButton() {}
fun showCancellationDialog(): Future<CancellationResult> {
return CompletableFuture<CancellationResult>().apply {
complete(CancellationResult.QUIT)
}
}
fun showCancellationDialog(): CancellationResult = CancellationResult.QUIT
enum class ExceptionListResult {
CONTINUE, CANCEL, IGNORE

View File

@ -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<IOptionDetails>): Future<Boolean> {
override fun showOptions(options: List<IOptionDetails>): 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<Boolean>().apply {
complete(false) // Can't be cancelled!
}
return false // Can't be cancelled!
}
override fun showExceptions(exceptions: List<ExceptionDetails>, numTotal: Int, allowsIgnore: Boolean): Future<ExceptionListResult> {
override fun showExceptions(exceptions: List<ExceptionDetails>, numTotal: Int, allowsIgnore: Boolean): ExceptionListResult {
println("Failed to download modpack, the following errors were encountered:")
for (ex in exceptions) {
println(ex.name + ": ")

View File

@ -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<IOptionDetails>): Future<Boolean> {
override fun showOptions(options: List<IOptionDetails>): Boolean {
val future = CompletableFuture<Boolean>()
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<ExceptionDetails>, numTotal: Int, allowsIgnore: Boolean): Future<ExceptionListResult> {
override fun showExceptions(exceptions: List<ExceptionDetails>, numTotal: Int, allowsIgnore: Boolean): ExceptionListResult {
val future = CompletableFuture<ExceptionListResult>()
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<IUserInterface.CancellationResult> {
override fun showCancellationDialog(): IUserInterface.CancellationResult {
val future = CompletableFuture<IUserInterface.CancellationResult>()
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()
}
}