Persist the media queue (#128)

This commit is contained in:
Lu 2023-05-25 16:24:36 +03:00 committed by GitHub
parent 246842a7da
commit 9af6ef8299
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 4 deletions

View File

@ -89,6 +89,7 @@ export interface StateDocument {
id: number;
since: number;
modified?: number;
mediaQueue: MediaItem [];
}
export interface NostrRelayDocument {
@ -436,6 +437,10 @@ export interface LNURLSuccessAction {
url?: string;
}
export declare interface OnInitialized {
initialize() : void;
}
// export interface ProfileView {

View File

@ -2,14 +2,16 @@ import { Injectable } from '@angular/core';
import { MatSnackBar } from '@angular/material/snack-bar';
import { SafeResourceUrl } from '@angular/platform-browser';
import { findIndex } from 'rxjs';
import { MediaItem } from './interfaces';
import { MediaItem, OnInitialized } from './interfaces';
import { OptionsService } from './options';
import { Utilities } from './utilities';
import { StorageService } from './storage';
import { ApplicationState } from './applicationstate';
@Injectable({
providedIn: 'root',
})
export class MediaService {
export class MediaService implements OnInitialized {
media: MediaItem[] = [];
audio?: HTMLAudioElement;
current?: MediaItem;
@ -27,7 +29,7 @@ export class MediaService {
return this.index < this.media.length - 1;
}
constructor(private options: OptionsService, private snackBar: MatSnackBar, private utilities: Utilities) {
constructor(private options: OptionsService, private snackBar: MatSnackBar, private utilities: Utilities, private storageService: StorageService, private appState: ApplicationState) {
navigator.mediaSession.setActionHandler('play', async () => {
if (!this.audio) {
return;
@ -66,6 +68,16 @@ export class MediaService {
this.next();
}
});
this.appState.initialized$.subscribe(() => {
this.initialize();
})
}
initialize(): void {
if (this.storageService.state.mediaQueue == null) {
return
}
this.media = this.storageService.state.mediaQueue;
}
exit() {
@ -100,6 +112,7 @@ export class MediaService {
// horizontalPosition: 'center',
// verticalPosition: 'bottom',
// });
this.save ()
}
dequeue(file: MediaItem) {
@ -108,6 +121,12 @@ export class MediaService {
return;
}
this.media.splice(index, 1);
this.save ()
}
async save() {
this.storageService.state.mediaQueue = this.media;
await this.storageService.saveState();
}
youtubeUrl?: SafeResourceUrl;

View File

@ -27,6 +27,7 @@ export class StorageService {
state = {
id: 1,
since: timeAgo,
mediaQueue: []
};
}
@ -41,7 +42,7 @@ export class StorageService {
const timeAgo = moment().subtract(10, 'minutes').unix();
this.state.since = timeAgo;
await this.storage.putState(this.state);
await this.saveState();
}, 60 * 1000);
// TODO: Remove, old code.
@ -65,6 +66,10 @@ export class StorageService {
}
}
async saveState() {
await this.storage.putState(this.state);
}
close() {
try {
this.storage.close();