feat: replace notification window

This commit is contained in:
Zamitto
2024-10-29 23:33:01 -03:00
parent a45e06efa3
commit 5d304f9e13
12 changed files with 61 additions and 296 deletions

View File

@@ -14,6 +14,7 @@ import { achievementsLogger } from "../logger";
import { Cracker } from "@shared";
import { IsNull, Not } from "typeorm";
import { WindowManager } from "../window-manager";
import { publishNewAchievementBulkNotification } from "../notifications";
const fileStats: Map<string, number> = new Map();
const fltFiles: Map<string, Set<string>> = new Map();
@@ -249,10 +250,9 @@ export class AchievementWatcherManager {
0
);
WindowManager.notificationWindow?.webContents.send(
"on-combined-achievements-unlocked",
totalNewGamesWithAchievements,
totalNewAchievements
publishNewAchievementBulkNotification(
totalNewAchievements,
totalNewGamesWithAchievements
);
this.hasFinishedMergingWithRemote = true;

View File

@@ -8,6 +8,10 @@ import { HydraApi } from "../hydra-api";
import { getUnlockedAchievements } from "@main/events/user/get-unlocked-achievements";
import { Game } from "@main/entity";
import { achievementsLogger } from "../logger";
import {
publishNewAchievementBulkNotification as publishCombinedNewAchievementNotification,
publishNewAchievementNotification,
} from "../notifications";
const saveAchievementsOnLocal = async (
objectId: string,
@@ -82,6 +86,8 @@ export const mergeAchievements = async (
};
});
const mergedLocalAchievements = unlockedAchievements.concat(newAchievements);
if (
newAchievements.length &&
publishNotification &&
@@ -107,16 +113,22 @@ export const mergeAchievements = async (
};
});
WindowManager.notificationWindow?.webContents.send(
"on-achievement-unlocked",
game.objectID,
game.shop,
achievementsInfo
);
if (achievementsInfo.length > 1) {
publishCombinedNewAchievementNotification(
newAchievements.length,
1,
achievementsInfo[0].iconUrl
);
} else {
publishNewAchievementNotification({
displayName: achievementsInfo[0].displayName,
achievementIcon: achievementsInfo[0].iconUrl,
unlockedAchievementCount: mergedLocalAchievements.length,
totalAchievementCount: achievementsData.length,
});
}
}
const mergedLocalAchievements = unlockedAchievements.concat(newAchievements);
if (game.remoteId) {
await HydraApi.put("/profile/games/achievements", {
id: game.remoteId,

View File

@@ -10,6 +10,7 @@ import axios from "axios";
import path from "node:path";
import sound from "sound-play";
import { achievementSoundPath } from "@main/constants";
import icon from "@resources/icon.png?asset";
const getGameIconNativeImage = async (gameId: number) => {
try {
@@ -91,13 +92,46 @@ async function downloadImage(url: string) {
});
}
export const publishNewAchievementBulkNotification = async (
achievementCount,
gameCount,
achievementIcon?: string
) => {
const iconPath = achievementIcon
? await downloadImage(achievementIcon)
: icon;
new Notification({
title: "New achievement unlocked",
body: t("new_achievements_unlocked", {
ns: "achievement",
gameCount,
achievementCount,
}),
icon: iconPath,
silent: true,
toastXml: toXmlString({
title: "New achievement unlocked",
message: t("new_achievements_unlocked", {
ns: "achievement",
gameCount,
achievementCount,
}),
icon: iconPath,
silent: true,
}),
}).show();
sound.play(achievementSoundPath);
};
export const publishNewAchievementNotification = async (achievement: {
displayName: string;
icon: string;
achievementIcon: string;
unlockedAchievementCount: number;
totalAchievementCount: number;
}) => {
const iconPath = await downloadImage(achievement.icon);
const iconPath = await downloadImage(achievement.achievementIcon);
new Notification({
title: "New achievement unlocked",

View File

@@ -20,7 +20,6 @@ import UserAgent from "user-agents";
export class WindowManager {
public static mainWindow: Electron.BrowserWindow | null = null;
public static notificationWindow: Electron.BrowserWindow | null = null;
private static loadMainWindowURL(hash = "") {
// HMR for renderer base on electron-vite cli.
@@ -39,21 +38,6 @@ export class WindowManager {
}
}
private static loadNotificationWindowURL() {
if (is.dev && process.env["ELECTRON_RENDERER_URL"]) {
this.notificationWindow?.loadURL(
`${process.env["ELECTRON_RENDERER_URL"]}#/achievement-notification`
);
} else {
this.notificationWindow?.loadFile(
path.join(__dirname, "../renderer/index.html"),
{
hash: "achievement-notification",
}
);
}
}
public static createMainWindow() {
if (this.mainWindow) return;
@@ -151,32 +135,6 @@ export class WindowManager {
});
}
public static createNotificationWindow() {
this.notificationWindow = new BrowserWindow({
transparent: true,
maximizable: false,
autoHideMenuBar: true,
minimizable: false,
focusable: false,
skipTaskbar: true,
frame: false,
width: 350,
height: 104,
x: 0,
y: 0,
webPreferences: {
preload: path.join(__dirname, "../preload/index.mjs"),
sandbox: false,
},
});
this.notificationWindow.setIgnoreMouseEvents(true);
// this.notificationWindow.setVisibleOnAllWorkspaces(true, {
// visibleOnFullScreen: true,
// });
this.notificationWindow.setAlwaysOnTop(true, "screen-saver", 1);
this.loadNotificationWindowURL();
}
public static openAuthWindow() {
if (this.mainWindow) {
const authWindow = new BrowserWindow({