From ccc5fb4fd9934c210bafa7c9706290fb401342f2 Mon Sep 17 00:00:00 2001 From: Majick Tek Date: Thu, 24 Feb 2022 01:06:40 -0500 Subject: [PATCH] Add callback to make sure progress displays correctly --- .../FileDownloaderDelegate.java | 2 ++ .../src/com/mt/minilauncher/LauncherWindow.java | 15 ++++++++------- .../mt/minilauncher/downloader/Downloader.java | 16 ++++++++++++++++ .../src/com/mt/minilauncher/util/Callback.java | 5 +++++ 4 files changed, 31 insertions(+), 7 deletions(-) create mode 100644 MiniCraftLauncher/src/com/mt/minilauncher/util/Callback.java diff --git a/MiniCraftLauncher/src/com/littlebigberry/httpfiledownloader/FileDownloaderDelegate.java b/MiniCraftLauncher/src/com/littlebigberry/httpfiledownloader/FileDownloaderDelegate.java index b07d638..da3b0b4 100644 --- a/MiniCraftLauncher/src/com/littlebigberry/httpfiledownloader/FileDownloaderDelegate.java +++ b/MiniCraftLauncher/src/com/littlebigberry/httpfiledownloader/FileDownloaderDelegate.java @@ -1,5 +1,7 @@ package com.littlebigberry.httpfiledownloader; +import com.mt.minilauncher.util.Callback; + public interface FileDownloaderDelegate { public void didStartDownload(FileDownloader fileDownloader); public void didProgressDownload(FileDownloader fileDownloader); diff --git a/MiniCraftLauncher/src/com/mt/minilauncher/LauncherWindow.java b/MiniCraftLauncher/src/com/mt/minilauncher/LauncherWindow.java index 5dda5b5..a859b0f 100644 --- a/MiniCraftLauncher/src/com/mt/minilauncher/LauncherWindow.java +++ b/MiniCraftLauncher/src/com/mt/minilauncher/LauncherWindow.java @@ -233,14 +233,15 @@ public void mouseClicked(MouseEvent e) { e1.printStackTrace(); } } else { - -// DownloadDialog dd = new DownloadDialog(); -// dd.download(selectedVersion.getURL(), Paths.get(Initializer.jarPath.toString(), selectedVersion.version + ".jar").toString()); -// dd.close(); - Downloader downloader = new Downloader(selectedVersion.getURL(), Paths.get(Initializer.jarPath.toString(), selectedVersion.version + ".jar").toString(), textArea); + Downloader downloader = new Downloader(selectedVersion.getURL(), + Paths.get(Initializer.jarPath.toString(), + selectedVersion.version + ".jar").toString(), + textArea, + () -> { + selectedVersion.isDownloaded = true; + list.updateUI(); + }); downloader.download(); - selectedVersion.isDownloaded = true; - list.updateUI(); } } } diff --git a/MiniCraftLauncher/src/com/mt/minilauncher/downloader/Downloader.java b/MiniCraftLauncher/src/com/mt/minilauncher/downloader/Downloader.java index 7e5f579..358b0b4 100644 --- a/MiniCraftLauncher/src/com/mt/minilauncher/downloader/Downloader.java +++ b/MiniCraftLauncher/src/com/mt/minilauncher/downloader/Downloader.java @@ -4,22 +4,34 @@ import com.littlebigberry.httpfiledownloader.FileDownloader; import com.littlebigberry.httpfiledownloader.FileDownloaderDelegate; +import com.mt.minilauncher.util.Callback; public class Downloader implements FileDownloaderDelegate{ String url, localLocation; JTextArea jta; + Callback callback; + public Downloader(String url, String localLocation) { this.url = url; this.localLocation = localLocation; jta = null; + callback = null; } public Downloader(String url, String localLocation, JTextArea jta) { this.url = url; this.localLocation = localLocation; this.jta = jta; + callback = null; + } + + public Downloader(String url, String localLocation, JTextArea jta, Callback onFinish) { + this.url = url; + this.localLocation = localLocation; + this.jta = jta; + callback = onFinish; } public void download() { @@ -48,10 +60,14 @@ public void didFinishDownload(FileDownloader fileDownloader) { if(jta != null) { jta.setText("Ready to play!"); } + if(callback != null) { + callback.call(); + } } @Override public void didFailDownload(FileDownloader fileDownloader) { System.err.println("Error"); } + } diff --git a/MiniCraftLauncher/src/com/mt/minilauncher/util/Callback.java b/MiniCraftLauncher/src/com/mt/minilauncher/util/Callback.java new file mode 100644 index 0000000..2bb204f --- /dev/null +++ b/MiniCraftLauncher/src/com/mt/minilauncher/util/Callback.java @@ -0,0 +1,5 @@ +package com.mt.minilauncher.util; + +public interface Callback { + public void call(); +}