Skip to content

Commit

Permalink
persist settings in browser storage
Browse files Browse the repository at this point in the history
  • Loading branch information
hall committed Apr 1, 2023
1 parent 0aba004 commit ac08568
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- toggle playback with space bar
- persist settings in browser storage

## [0.1.1] - 2023-03-31

Expand Down
9 changes: 7 additions & 2 deletions src/app/home/home.component.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ChangeDetectorRef, Component, HostListener, OnInit, ViewChild } from '@angular/core';
import { ChangeDetectorRef, Component, HostListener, OnDestroy, OnInit, ViewChild } from '@angular/core';
import { IonContent } from '@ionic/angular';
import { OpenSheetMusicDisplay } from 'opensheetmusicdisplay';

Expand Down Expand Up @@ -31,7 +31,12 @@ export class HomePageComponent implements OnInit {
if (event.key == ' ') {
if (this.running) this.osmdCursorStop();
else this.osmdPractice();
} else alert(JSON.stringify(event));
}
}

@HostListener('window:beforeunload', ['$event'])
onUnload(): void {
this.settings.persist();
}

constructor(
Expand Down
21 changes: 18 additions & 3 deletions src/app/services/settings.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,28 @@ export class SettingsService {
speed: number = 100;
backgroundColor: string = '#ffffff';

constructor(public translate: TranslateService) {
this.translate.use(this.language);
constructor(public _translate: TranslateService) {
this._translate.use(this.language);

// initialize values if they were previously persisted
for (let p in this) {
if (p.startsWith('_')) continue;
let store = localStorage.getItem(p);
if (store !== null) this[p] = JSON.parse(store);
}
}

// save properties to local storage
persist() {
for (let p in this) {
if (p.startsWith('_')) continue;
localStorage.setItem(p, JSON.stringify(this[p]));
}
}

useLanguage(event: any): void {
this.language = event.detail.value;
this.translate.use(event.detail.value);
this._translate.use(event.detail.value);
}

updateZoom(value: number, osmd: OpenSheetMusicDisplay): void {
Expand Down

0 comments on commit ac08568

Please sign in to comment.