Skip to content

Commit

Permalink
Add callback to make sure progress displays correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
MajickTek committed Feb 24, 2022
1 parent 5accb2b commit ccc5fb4
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
15 changes: 8 additions & 7 deletions MiniCraftLauncher/src/com/mt/minilauncher/LauncherWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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");
}

}
5 changes: 5 additions & 0 deletions MiniCraftLauncher/src/com/mt/minilauncher/util/Callback.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.mt.minilauncher.util;

public interface Callback {
public void call();
}

0 comments on commit ccc5fb4

Please sign in to comment.