mirror of
https://github.com/MasterAcnolo/Freedom-Loader.git
synced 2026-07-29 18:25:47 +02:00
Refactor: auto-update functionality with user prompts and download progress
This commit is contained in:
@@ -1,47 +1,83 @@
|
||||
const { autoUpdater } = require("electron-updater");
|
||||
const { app, Notification, shell} = require("electron");
|
||||
const { dialog } = require("electron");
|
||||
const { logger } = require("../server/logger");
|
||||
|
||||
function AutoUpdater() {
|
||||
autoUpdater.autoDownload = false;
|
||||
autoUpdater.autoInstallOnAppQuit = false;
|
||||
|
||||
autoUpdater.on("update-available", (info) => {
|
||||
logger.info(`New Version Available : ${info.version}`);
|
||||
new Notification({
|
||||
title: "Freedom Loader",
|
||||
body: `New Version Available : ${info.version}. Application will restart`
|
||||
}).show();
|
||||
function initAutoUpdater(mainWindow) {
|
||||
autoUpdater.on("update-available", async (info) => {
|
||||
logger.info(`Update available: ${info.version}`);
|
||||
|
||||
const { response } = await dialog.showMessageBox(mainWindow, {
|
||||
type: "info",
|
||||
title: "Update Available",
|
||||
message: `Version ${info.version} is available.`,
|
||||
detail: "Would you like to download and install it now?",
|
||||
buttons: ["Install Update", "Maybe Later"],
|
||||
defaultId: 0,
|
||||
cancelId: 1,
|
||||
});
|
||||
|
||||
if (response === 0) {
|
||||
autoUpdater.downloadUpdate();
|
||||
} else {
|
||||
mainWindow?.webContents.executeJavaScript(
|
||||
`window.showUpdateBadge && window.showUpdateBadge("${info.version}")`
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
autoUpdater.on("update-downloaded", (info) => {
|
||||
logger.info(`Update Downloaded : ${info.version}`);
|
||||
new Notification({
|
||||
title: "Freedom Loader",
|
||||
body: `Update ${info.version} downloaded.`
|
||||
}).on("click", () =>
|
||||
shell.openExternal("https://github.com/MasterAcnolo/Freedom-Loader/releases/latest")
|
||||
).show();
|
||||
autoUpdater.on("download-progress", (progress) => {
|
||||
logger.info(`Download progress: ${Math.round(progress.percent)}%`);
|
||||
mainWindow?.webContents.send("update-progress", {
|
||||
percent: Math.round(progress.percent),
|
||||
speed: progress.bytesPerSecond,
|
||||
});
|
||||
});
|
||||
|
||||
setTimeout(() => {
|
||||
autoUpdater.quitAndInstall();
|
||||
}, 5000);
|
||||
autoUpdater.on("update-downloaded", async (info) => {
|
||||
logger.info(`Update downloaded: ${info.version}`);
|
||||
|
||||
const { response } = await dialog.showMessageBox(mainWindow, {
|
||||
type: "info",
|
||||
title: "Update Ready",
|
||||
message: `Version ${info.version} has been downloaded.`,
|
||||
detail: "The application will restart to apply the update.",
|
||||
buttons: ["Restart & Install", "Later"],
|
||||
defaultId: 0,
|
||||
cancelId: 1,
|
||||
});
|
||||
|
||||
if (response === 0) autoUpdater.quitAndInstall();
|
||||
});
|
||||
|
||||
autoUpdater.on("error", (err) => {
|
||||
logger.error("Auto Update Error :", err.message);
|
||||
new Notification({
|
||||
title: "Freedom Loader - Update Error",
|
||||
body: err.message
|
||||
}).show();
|
||||
logger.error("Auto update error:", err.message);
|
||||
dialog.showErrorBox("Update Error", err.message);
|
||||
});
|
||||
|
||||
app.whenReady().then(async () => {
|
||||
try {
|
||||
await autoUpdater.checkForUpdates();
|
||||
logger.info("Update check completed");
|
||||
} catch (err) {
|
||||
logger.error("Error during update check :", err.message);
|
||||
}
|
||||
});
|
||||
checkForUpdates();
|
||||
}
|
||||
|
||||
module.exports = { AutoUpdater };
|
||||
async function checkForUpdates() {
|
||||
try {
|
||||
await autoUpdater.checkForUpdates();
|
||||
} catch (err) {
|
||||
logger.error("Update check failed:", err.message);
|
||||
}
|
||||
}
|
||||
|
||||
async function downloadUpdate() {
|
||||
try {
|
||||
await autoUpdater.downloadUpdate();
|
||||
} catch (err) {
|
||||
logger.error("Download failed:", err.message);
|
||||
}
|
||||
}
|
||||
|
||||
function installUpdate() {
|
||||
autoUpdater.quitAndInstall();
|
||||
}
|
||||
|
||||
module.exports = { initAutoUpdater, downloadUpdate, installUpdate };
|
||||
@@ -1,10 +1,11 @@
|
||||
const { BrowserWindow } = require("electron");
|
||||
const { BrowserWindow, app } = require("electron");
|
||||
const path = require("path");
|
||||
|
||||
let splashWindow = null;
|
||||
|
||||
function createSplashWindow() {
|
||||
splashWindow = new BrowserWindow({
|
||||
|
||||
const splashOptions = {
|
||||
width: 400,
|
||||
height: 300,
|
||||
frame: false,
|
||||
@@ -16,9 +17,29 @@ function createSplashWindow() {
|
||||
nodeIntegration: false,
|
||||
contextIsolation: true,
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
splashWindow.loadFile(path.join(__dirname, "../public/splash.html"));
|
||||
splashWindow = new BrowserWindow(splashOptions);
|
||||
|
||||
const splashPath = path.join(__dirname, "../public/splash.html");
|
||||
splashWindow.loadFile(splashPath);
|
||||
|
||||
// Inject banner path for both dev and packaged app
|
||||
splashWindow.webContents.on('did-finish-load', () => {
|
||||
let bannerPath;
|
||||
|
||||
// Check if app is packaged
|
||||
if (app.isPackaged) {
|
||||
bannerPath = path.join(process.resourcesPath, 'banner.png');
|
||||
} else {
|
||||
// In dev, use build folder
|
||||
bannerPath = path.join(__dirname, '../build/banner.png');
|
||||
}
|
||||
|
||||
splashWindow.webContents.executeJavaScript(`
|
||||
document.querySelector('img[alt="Freedom Loader"]').src = 'file:///${bannerPath.replace(/\\/g, '/')}';
|
||||
`);
|
||||
});
|
||||
}
|
||||
|
||||
function closeSplashWindow() {
|
||||
|
||||
@@ -12,7 +12,7 @@ async function createMainWindow() {
|
||||
return mainWindow;
|
||||
}
|
||||
|
||||
mainWindow = new BrowserWindow({
|
||||
const windowOptions = {
|
||||
title: `Freedom Loader ${config.version}`,
|
||||
width: 750,
|
||||
height: 800,
|
||||
@@ -26,7 +26,9 @@ async function createMainWindow() {
|
||||
contextIsolation: true,
|
||||
preload: path.join(__dirname, "../preload.js"),
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
mainWindow = new BrowserWindow(windowOptions);
|
||||
|
||||
try {
|
||||
await mainWindow.loadURL(`http://localhost:${config.applicationPort}`);
|
||||
|
||||
Reference in New Issue
Block a user