Add playlists

This commit is contained in:
Myx
2025-03-21 23:00:06 +01:00
committed by Myx
parent b5658ce37f
commit 50a69c9598
14 changed files with 418 additions and 3 deletions

View File

@@ -6,6 +6,7 @@ import { resolveChartFolderName } from 'src-shared/UtilFunctions'
import { DownloadProgress } from '../../../../src-shared/interfaces/download.interface'
import { SettingsService } from './settings.service'
import { PlaylistService } from './playlist.service'
@Injectable({
providedIn: 'root',
@@ -15,7 +16,7 @@ export class DownloadService {
public downloadCountChanges = new EventEmitter<number>()
public downloads: DownloadProgress[] = []
constructor(zone: NgZone, private settingsService: SettingsService) {
constructor(zone: NgZone, private settingsService: SettingsService, private playlistService: PlaylistService) {
window.electron.on.downloadQueueUpdate(download => zone.run(() => {
const downloadIndex = this.downloads.findIndex(d => d.md5 === download.md5)
if (download.type === 'cancel') {
@@ -68,6 +69,9 @@ export class DownloadService {
if (this.downloads.every(d => d.type === 'done')) { // Reset overall progress bar if it finished
this.downloads.forEach(d => d.stale = true)
}
this.playlistService.playlistAdd(chart)
const newChart = {
name: chart.name ?? 'Unknown Name',
artist: chart.artist ?? 'Unknown Artist',

View File

@@ -0,0 +1,92 @@
import { Injectable, Injector } from '@angular/core'
import { BehaviorSubject } from 'rxjs'
import { ChartData } from 'src-shared/interfaces/search.interface'
import { DownloadService } from './download.service'
@Injectable({
providedIn: 'root',
})
export class PlaylistService {
private _tracks = new BehaviorSubject<ChartData[]>([])
tracks$ = this._tracks.asObservable()
private _selectedSongs = new BehaviorSubject<ChartData[]>([])
selectedSongs$ = this._selectedSongs.asObservable()
private _downloadService: DownloadService | null = null
constructor(private injector: Injector) {
const playlist = localStorage.getItem('playlist')
if (playlist) {
this._tracks.next(JSON.parse(playlist))
}
}
private get downloadService(): DownloadService {
if (!this._downloadService) {
this._downloadService = this.injector.get(DownloadService)
}
return this._downloadService
}
getPlaylist() {
return this._tracks.value
}
playlistAdd(chart: ChartData) {
const updatedTracks = [...this._tracks.value, chart]
this._tracks.next(updatedTracks)
localStorage.setItem('playlist', JSON.stringify(updatedTracks))
}
downloadPlaylist(songs: ChartData[]) {
songs.forEach(track => {
if (!this._tracks.value.includes(track)) {
this.downloadService.addDownload(track)
}
})
}
storePlaylist() {
const fakeLink = document.createElement('a')
const file = new Blob([JSON.stringify(this._tracks.value)], { type: 'application/json' })
fakeLink.href = URL.createObjectURL(file)
fakeLink.download = 'songs.setlist'
fakeLink.click()
}
storeSelectedSongs() {
const fakeLink = document.createElement('a')
const file = new Blob([JSON.stringify(this._selectedSongs.value)], { type: 'application/json' })
fakeLink.href = URL.createObjectURL(file)
fakeLink.download = 'selected.setlist'
fakeLink.click()
}
addToSelectedSongs(song: ChartData) {
const updatedSelectedSongs = [...this._selectedSongs.value, song]
this._selectedSongs.next(updatedSelectedSongs)
}
removeFromSelectedSongs(song: ChartData) {
const updatedSelectedSongs = this._selectedSongs.value.filter(selectedSong => selectedSong !== song)
this._selectedSongs.next(updatedSelectedSongs)
}
clearSelectedSongs() {
this._selectedSongs.next([])
}
removeFromPlaylist() {
this._selectedSongs.value.forEach(selectedSong => {
const updatedTracks = this._tracks.value.filter(track => track !== selectedSong)
this._tracks.next(updatedTracks)
localStorage.setItem('playlist', JSON.stringify(updatedTracks))
})
}
clearPlaylist() {
this._tracks.next([])
localStorage.removeItem('playlist')
}
}