Fix checkboxes and bulk download

This commit is contained in:
Geomitron
2023-12-25 10:29:57 -06:00
parent 5644ea2447
commit 199374b2e2
12 changed files with 66 additions and 126 deletions

View File

@@ -1,6 +1,8 @@
import { EventEmitter, Injectable, NgZone } from '@angular/core'
import { assign } from 'lodash'
import { ChartData } from 'src-shared/interfaces/search.interface'
import { removeStyleTags } from 'src-shared/UtilFunctions'
import { DownloadProgress } from '../../../../src-shared/interfaces/download.interface'
@@ -16,7 +18,7 @@ export class DownloadService {
window.electron.on.downloadQueueUpdate(download => zone.run(() => {
const downloadIndex = this.downloads.findIndex(d => d.md5 === download.md5)
if (download.type === 'cancel') {
this.downloads = this.downloads.filter(d => d.md5 !== this.downloads[downloadIndex].md5)
this.downloads = this.downloads.filter(d => d.md5 !== this.downloads[downloadIndex]?.md5)
} else if (downloadIndex === -1) {
this.downloads.push(download)
} else {
@@ -50,13 +52,16 @@ export class DownloadService {
return this.downloads.find(download => download.type === 'error') ? true : false
}
addDownload(md5: string, chartName: string) {
if (!this.downloads.find(d => d.md5 === md5)) { // Don't download something twice
addDownload(chart: ChartData) {
if (!this.downloads.find(d => d.md5 === chart.md5)) { // Don't download something twice
if (this.downloads.every(d => d.type === 'done')) { // Reset overall progress bar if it finished
this.downloads.forEach(d => d.stale = true)
}
const chartName = `${removeStyleTags(chart.artist ?? 'Unknown Artist')
} - ${removeStyleTags(chart.name ?? 'Unknown Name')
} (${removeStyleTags(chart.charter ?? 'Unknown Charter')})`
this.downloads.push({
md5,
md5: chart.md5,
chartName,
header: 'Waiting for other downloads to finish...',
body: '',
@@ -64,7 +69,7 @@ export class DownloadService {
type: 'good',
isPath: false,
})
window.electron.emit.download({ action: 'add', md5, chartName })
window.electron.emit.download({ action: 'add', md5: chart.md5, chartName })
}
this.downloadCountChanges.emit(this.downloadCount)
}

View File

@@ -18,7 +18,8 @@ export class SearchService {
public searchLoading = false
public songsResponse: Partial<SearchResult>
public currentPage = 1
public searchUpdated = new EventEmitter<Partial<SearchResult>>()
public newSearch = new EventEmitter<Partial<SearchResult>>()
public updateSearch = new EventEmitter<Partial<SearchResult>>()
public isDefaultSearch = true
public groupedSongs: ChartData[][]
@@ -115,7 +116,11 @@ export class SearchService {
.value()
)
this.searchUpdated.emit(response)
if (nextPage) {
this.updateSearch.emit(response)
} else {
this.newSearch.emit(response)
}
})
)
}
@@ -154,7 +159,7 @@ export class SearchService {
.value()
)
this.searchUpdated.emit(response)
this.newSearch.emit(response)
})
)
}

View File

@@ -2,34 +2,29 @@ import { EventEmitter, Injectable } from '@angular/core'
import { SearchService } from './search.service'
// Note: this class prevents event cycles by only emitting events if the checkbox changes
@Injectable({
providedIn: 'root',
})
export class SelectionService {
private allSelected = false
private selectAllChangedEmitter = new EventEmitter<boolean>()
public selections: { [groupId: number]: boolean | undefined } = {}
constructor(searchService: SearchService) {
searchService.searchUpdated.subscribe(() => {
searchService.newSearch.subscribe(() => {
this.selections = {}
this.deselectAll()
})
}
getSelectedResults() {
// TODO
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return [] as any[] // this.searchResults.filter(result => this.selections[result.id] === true)
}
onSelectAllChanged(callback: (selected: boolean) => void) {
this.selectAllChangedEmitter.subscribe(callback)
isAllSelected() {
return this.allSelected
}
deselectAll() {
this.allSelected = false
for (const groupId in this.selections) {
this.selections[groupId] = false
}
@@ -37,6 +32,7 @@ export class SelectionService {
}
selectAll() {
this.allSelected = true
for (const groupId in this.selections) {
this.selections[groupId] = true
}