From ae2cf08c7df6c670b543f58c595a0f3021220c25 Mon Sep 17 00:00:00 2001 From: Shidorien Date: Fri, 24 Nov 2023 06:59:44 +0100 Subject: [PATCH] Add changelog functionality to AutoUpdaterService --- src/renderer/services/auto-updater.service.ts | 70 ++++++++++++++++++- 1 file changed, 69 insertions(+), 1 deletion(-) diff --git a/src/renderer/services/auto-updater.service.ts b/src/renderer/services/auto-updater.service.ts index d9ae43fe..883906fe 100644 --- a/src/renderer/services/auto-updater.service.ts +++ b/src/renderer/services/auto-updater.service.ts @@ -1,8 +1,22 @@ -import { Observable, lastValueFrom } from "rxjs"; +import { BehaviorSubject, Observable, lastValueFrom } from "rxjs"; import { map } from "rxjs/operators"; import { IpcService } from "./ipc.service"; import { ProgressBarService } from "./progress-bar.service"; +import { I18nService } from "./i18n.service"; +import { ModalService } from "renderer/services/modale.service"; +import { ChangelogModal } from "renderer/components/modal/modal-types/changelog-modal.component"; +import { error } from "console"; + +export interface Changelog { + [version: string]: ChangelogVersion; +} +export interface ChangelogVersion { + htmlBody: string; + title: string; + timestampPublished: number; + version: string; +} export class AutoUpdaterService { private static instance: AutoUpdaterService; @@ -11,6 +25,12 @@ export class AutoUpdaterService { private downloadProgress$: Observable; + private i18nService: I18nService; + + private modal: ModalService; + + private readonly changelog: BehaviorSubject; + public static getInstance(): AutoUpdaterService { if (!AutoUpdaterService.instance) { AutoUpdaterService.instance = new AutoUpdaterService(); @@ -21,7 +41,10 @@ export class AutoUpdaterService { private constructor() { this.progressService = ProgressBarService.getInstance(); this.ipcService = IpcService.getInstance(); + this.i18nService = I18nService.getInstance(); + this.modal = ModalService.getInstance(); + //this.getChangelog().then(changelog => {this.changelog.next(changelog);}); this.downloadProgress$ = this.ipcService.watch("update-download-progress").pipe(map(res => (res.success ? res.data : 0))); } @@ -45,4 +68,49 @@ export class AutoUpdaterService { public getHaveBeenUpdated(): Observable{ return this.ipcService.sendV2("have-been-updated"); } + + private async getChangelog(): Promise { + const path = `https://raw.githubusercontent.com/Zagrios/bs-manager/feature/add-changelog-modal/178/assets/jsons/changelogs/${this.i18nService.currentLanguage.split("-")[0]}.json` + const response = await fetch(path); + if (!response.ok) { + return undefined; + } + + const data = await response.json(); + if (!data) { + return undefined; + } + return data; + } + + private async getChangelogVersion(version:string): Promise { + const changelogs = await this.getChangelog(); + if (!changelogs) { + throw new Error("No changelog found"); + } + + const changelogVersion = changelogs[version]; + if (!changelogVersion) { + throw new Error("No changelog found for this version"); + } + + + return changelogVersion; + } + + public getAppVersion() : Observable { + return this.ipcService.sendV2("current-version"); + } + + public async showChangelog(): Promise{ + try{ + const currentVersion = await lastValueFrom(this.getAppVersion()); + const changelog = await this.getChangelogVersion(currentVersion); + + this.modal.openModal(ChangelogModal, changelog); + } + catch(error){ + this.ipcService.sendLazy("log-error", {args: error}); + } + } }