Add changelog functionality to AutoUpdaterService

This commit is contained in:
Shidorien
2023-11-24 06:59:44 +01:00
parent 4a0848fb41
commit ae2cf08c7d
+69 -1
View File
@@ -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<number>;
private i18nService: I18nService;
private modal: ModalService;
private readonly changelog: BehaviorSubject<Changelog>;
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<number>("update-download-progress").pipe(map(res => (res.success ? res.data : 0)));
}
@@ -45,4 +68,49 @@ export class AutoUpdaterService {
public getHaveBeenUpdated(): Observable<boolean>{
return this.ipcService.sendV2<boolean>("have-been-updated");
}
private async getChangelog(): Promise<Changelog> {
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<ChangelogVersion> {
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<string> {
return this.ipcService.sendV2<string>("current-version");
}
public async showChangelog(): Promise<void>{
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});
}
}
}