feat: simplify version history api

This commit is contained in:
Thea Schöbl
2023-12-08 13:49:53 +00:00
committed by Rainer Killinger
parent d44204cf8d
commit d2c8120255
13 changed files with 240 additions and 117 deletions

View File

@@ -6,6 +6,7 @@ import {Capacitor} from '@capacitor/core';
import {ReleaseNotesComponent} from './release-notes.component';
import {SCAppVersionInfo} from '@openstapps/core';
import {App} from '@capacitor/app';
import {coerce} from 'semver';
export const RELEASE_NOTES_SHOWN_KEY = 'release_notes_shown';
@@ -17,14 +18,6 @@ export class AppVersionService {
private modalController: ModalController,
) {}
/**
* Get the release notes of the latest published version
*/
get publishedVersions() {
const platform = Capacitor.getPlatform() as 'android' | 'ios' | 'web';
return this.config.config.app.versionHistory?.filter(({published}) => published[platform] !== undefined);
}
/**
* Get the latest release notes that have not been presented yet
*/
@@ -32,29 +25,38 @@ export class AppVersionService {
if (Capacitor.getPlatform() === 'web') {
return;
}
const storedVersion = (await this.storage.has(RELEASE_NOTES_SHOWN_KEY))
? await this.storage.get<string>(RELEASE_NOTES_SHOWN_KEY)
: '';
const currentVersion = await App.getInfo().then(info => info.version);
return this.publishedVersions?.find(({version}) => {
const wasNotShown = version.localeCompare(storedVersion, undefined, {numeric: true}) === 1;
const isNotFutureVersion = version.localeCompare(currentVersion, undefined, {numeric: true}) <= 0;
return wasNotShown && isNotFutureVersion;
});
const storedVersion = coerce(
(await this.storage.has(RELEASE_NOTES_SHOWN_KEY))
? await this.storage.get<string>(RELEASE_NOTES_SHOWN_KEY)
: '0.0.0',
)!;
const currentVersion = coerce(await App.getInfo().then(info => info.version))!;
return this.config.config.app.versionHistory
?.filter(({version}) => {
const semanticVersion = coerce(version)!;
const wasNotShown = semanticVersion.compare(storedVersion) === 1;
const isNotFutureVersion = semanticVersion.compare(currentVersion) <= 0;
return wasNotShown && isNotFutureVersion;
})
?.sort((a, b) => coerce(a.version)!.compare(b.version));
}
/**
* Present release notes
*/
async presentReleaseNotes(version: SCAppVersionInfo) {
async presentReleaseNotes(versions: SCAppVersionInfo[]) {
if (!versions || versions.length === 0) {
return;
}
const modal = await this.modalController.create({
component: ReleaseNotesComponent,
componentProps: {
versionInfo: version,
versionInfos: versions,
},
});
await modal.present();
await modal.onDidDismiss();
await this.storage.put(RELEASE_NOTES_SHOWN_KEY, version.version);
await this.storage.put(RELEASE_NOTES_SHOWN_KEY, versions[0].version);
}
}