Skip to content
This repository has been archived by the owner on Feb 2, 2021. It is now read-only.

Commit

Permalink
Cache update index so we can display the updates window even when off…
Browse files Browse the repository at this point in the history
…line. Part of issue #16
  • Loading branch information
mikehearn committed Dec 8, 2014
1 parent dbc975c commit c9607a8
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
4 changes: 4 additions & 0 deletions client/src/main/java/lighthouse/MainWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
import java.nio.file.Path;

import static javafx.beans.binding.Bindings.when;
import static lighthouse.protocol.LHUtils.unchecked;
import static lighthouse.threading.AffinityExecutor.UI_THREAD;
import static lighthouse.utils.GuiUtils.*;

Expand Down Expand Up @@ -412,6 +413,7 @@ public void invalidated(Observable x) {
NotificationBarPane.Item downloadingItem = Main.instance.notificationBar.displayNewItem(
"Downloading software update", updater.progressProperty());
updater.setOnSucceeded(ev -> {
UpdateFXWindow.saveCachedIndex(unchecked(updater::get).updates);
Button restartButton = new Button("Restart");
restartButton.setOnAction(ev2 -> Main.restart());
NotificationBarPane.Item newItem = Main.instance.notificationBar.createItem(
Expand All @@ -431,6 +433,8 @@ public void invalidated(Observable x) {
shown = true;
}
});
// Save the updates list to disk so we can still display the updates screen even if we're offline.
updater.setOnSucceeded(ev -> UpdateFXWindow.saveCachedIndex(unchecked(updater::get).updates));
// Don't bother the user if update check failed: assume some temporary server error that can be fixed silently.
updater.setOnFailed(ev -> log.error("Online update check failed", updater.getException()));
Thread thread = new Thread(updater, "Online update check");
Expand Down
37 changes: 36 additions & 1 deletion client/src/main/java/lighthouse/subwindows/UpdateFXWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
import lighthouse.files.AppDirectory;

import javax.annotation.Nullable;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand All @@ -30,11 +34,13 @@
import static java.lang.String.format;
import static java.nio.file.Files.exists;
import static lighthouse.utils.GuiUtils.informationalAlert;
import static lighthouse.utils.GuiUtils.log;

/**
* Lets the user select a version to pin themselves to.
*/
public class UpdateFXWindow {
public static final String CACHED_UPDATE_SUMMARY = "cached-update-summary";
public Main.OverlayUI<UpdateFXWindow> overlayUI;

@FXML Text descriptionLabel;
Expand Down Expand Up @@ -123,9 +129,19 @@ public void setUpdater(Updater updater) {
}

private void processUpdater(Updater updater) {
summary = Futures.getUnchecked(updater);
updates.clear();
updates.add(null); // Sentinel for "latest"
try {
summary = Futures.getUnchecked(updater);
} catch (Exception e) {
log.warn("Failed to get online updates index, trying to fall back to disk cache: {}", e.getMessage());
summary = loadCachedIndex();
if (summary == null) {
log.error("Not online and failed to load cached updates, showing blank window.");
pinBtn.setDisable(true);
return; // Not online and no cached updates, or some other issue, so give up.
}
}
List<UFXProtocol.Update> list = new ArrayList<>(summary.updates.getUpdatesList());
Collections.reverse(list);
for (UFXProtocol.Update update : list) {
Expand All @@ -137,4 +153,23 @@ private void processUpdater(Updater updater) {
}
}
}

// Returns an UpdateSummary object that we previously had downloaded, so we can still display update info if
// we are offline, or null if not found or some other error.
@Nullable
private UpdateSummary loadCachedIndex() {
try (InputStream is = Files.newInputStream(AppDirectory.dir().resolve(CACHED_UPDATE_SUMMARY))) {
return new UpdateSummary(Main.VERSION, UFXProtocol.Updates.parseDelimitedFrom(is));
} catch (IOException e) {
return null;
}
}

public static void saveCachedIndex(UFXProtocol.Updates updates) {
try (OutputStream os = Files.newOutputStream(AppDirectory.dir().resolve(CACHED_UPDATE_SUMMARY))) {
updates.writeDelimitedTo(os);
} catch (IOException e) {
log.error("Failed to save cached update index", e);
}
}
}

0 comments on commit c9607a8

Please sign in to comment.