Files
privacy.sexy/src/presentation/electron/UpdateProgressBar.ts
undergroundwires ddf417a16a Add new UX for optionally downloading updates
Before we used native method from electron for updating and notifying
(`checkForUpdatesAndNotify`). It simply checked if there's an update,
downloaded it, applied in the background and showed OS notification.

The flow is now updated. Updates will be checked, user will be asked to
confirm about whether to download and apply the updates, then a UI with
progress bar will be shown and user will be asked to restart the
application.

This commit also moves electron related logic to `/electron/` folder (as
there are now multiple files) to keep them structured. Also the electon
entrypoint `background.ts` is renamed to `main.ts`. The reason it was
named  `background.ts` by vue-cli-plugin-electron-builder was to remove
the confusion between `main.ts` of Vue itself. However, as they are
kept in different folders, but this is not the case for us.

Better than `checkForUpdatesAndNotify`.
Organizes electron desktop app logic in same folder to allow using
multiple files in a structured manner.
2021-09-11 11:04:08 +01:00

87 lines
2.8 KiB
TypeScript

import ProgressBar from 'electron-progressbar';
import { ProgressInfo } from 'electron-builder';
import { app } from 'electron';
import log from 'electron-log';
export class UpdateProgressBar {
private progressBar: ProgressBar;
private showingProgress = false;
public showIndeterminateState() {
this.progressBar?.close();
this.progressBar = progressBarFactory.createWithIndeterminateState();
}
public showProgress(progress: ProgressInfo) {
if (!this.showingProgress) { // First time showing progress
this.progressBar?.close();
this.showingProgress = true;
} else {
updateProgressBar(progress, this.progressBar);
}
}
public showError(e: Error) {
const reportUpdateError = () => {
this.progressBar.detail =
'An error occurred while fetching updates.\n'
+ (e && e.message ? e.message : e);
this.progressBar._window.setClosable(true);
};
if (this.progressBar._window) {
reportUpdateError();
} else {
this.progressBar.on('ready', () => reportUpdateError());
}
}
public close() {
if (!this.progressBar.isCompleted()) {
this.progressBar.close();
}
}
}
function getUpdatePercent(progress: ProgressInfo) {
let percent = progress.percent;
if (percent) {
percent = Math.round(percent * 100) / 100;
}
return percent;
}
const progressBarFactory = {
createWithIndeterminateState: () => {
return new ProgressBar({
title: `${app.name} Update`,
text: `Downloading ${app.name} update...`,
});
},
createWithPercentile: (initialProgress: ProgressInfo) => {
const percent = getUpdatePercent(initialProgress);
const progressBar = new ProgressBar({
indeterminate: false,
title: `${app.name} Update`,
text: `Downloading ${app.name} update...`,
detail: `${percent}% ...`,
initialValue: percent,
});
progressBar
.on('completed', () => {
progressBar.detail = 'Download completed.';
})
.on('aborted', (value) => {
log.info(`progress aborted... ${value}`);
})
.on('progress', (value) => {
progressBar.detail = `${value}% ...`;
})
.on('ready', () => {
// initialValue doesn't set the UI, so this is needed to render it correctly
progressBar.value = percent;
});
return progressBar;
},
};
function updateProgressBar(progress: ProgressInfo, progressBar: ProgressBar) {
progressBar.value = getUpdatePercent(progress);
}