Download Selected button, Various small improvements

This commit is contained in:
Geomitron
2020-02-11 23:52:22 -05:00
parent db083d573a
commit 7d4d339018
17 changed files with 192 additions and 58 deletions

View File

@@ -0,0 +1,22 @@
import { IPCInvokeHandler } from '../shared/IPCHandler'
import Database from '../shared/Database'
import { VersionResult } from '../shared/interfaces/songDetails.interface'
export default class BatchSongDetailsHandler implements IPCInvokeHandler<'batch-song-details'> {
event: 'batch-song-details' = 'batch-song-details'
// TODO: add method documentation
async handler(songIDs: number[]) {
const db = await Database.getInstance()
return db.sendQuery(this.getVersionQuery(songIDs)) as Promise<VersionResult[]>
}
private getVersionQuery(songIDs: number[]) {
return `
SELECT *
FROM VersionMetaFull
WHERE songID IN (${songIDs.join(',')});
`
}
}

View File

@@ -0,0 +1,11 @@
import InitSettingsHandler from '../ipc/InitSettingsHandler.ipc'
import { basename } from 'path'
/**
* @param absoluteFilepath The absolute filepath to a folder
* @returns The relative filepath from the scanned folder to <absoluteFilepath>
*/
export async function getRelativeFilepath(absoluteFilepath: string) {
const settings = await InitSettingsHandler.getSettings()
return basename(settings.libraryPath) + absoluteFilepath.substring(settings.libraryPath.length)
}

View File

@@ -1,5 +1,5 @@
import { red } from 'cli-color'
import { getRelativeFilepath } from './UtilFunctions'
import { getRelativeFilepath } from './ElectronUtilFunctions'
// TODO: add better error reporting (through the UI)

View File

@@ -7,6 +7,7 @@ import { Download, NewDownload, DownloadProgress } from './interfaces/download.i
import { DownloadHandler } from '../ipc/download/DownloadHandler'
import { Settings } from './Settings'
import InitSettingsHandler from '../ipc/InitSettingsHandler.ipc'
import BatchSongDetailsHandler from '../ipc/BatchSongDetailsHandler.ipc'
/**
* To add a new IPC listener:
@@ -21,6 +22,7 @@ export function getIPCInvokeHandlers(): IPCInvokeHandler<keyof IPCInvokeEvents>[
new InitSettingsHandler(),
new SearchHandler(),
new SongDetailsHandler(),
new BatchSongDetailsHandler(),
new AlbumArtHandler()
]
}
@@ -42,6 +44,10 @@ export type IPCInvokeEvents = {
input: SongResult['id']
output: VersionResult[]
}
'batch-song-details': {
input: number[]
output: VersionResult[]
}
}
export interface IPCInvokeHandler<E extends keyof IPCInvokeEvents> {

View File

@@ -1,16 +1,5 @@
let sanitize = require('sanitize-filename')
/**
* @param absoluteFilepath The absolute filepath to a folder
* @returns The relative filepath from the scanned folder to <absoluteFilepath>
*/
export function getRelativeFilepath(absoluteFilepath: string) {
// TODO: figure out how these functions should use <settings> (like an async initialization script that
// loads everything and connects to the database, etc...)
// return basename(scanSettings.songsFolderPath) + absoluteFilepath.substring(scanSettings.songsFolderPath.length)
return absoluteFilepath
}
/**
* @returns A random UUID
*/
@@ -53,4 +42,23 @@ export function sanitizeFilename(filename: string): string {
*/
export function interpolate(val: number, fromA: number, fromB: number, toA: number, toB: number) {
return ((val - fromA) / (fromB - fromA)) * (toB - toA) + toA
}
/**
* Splits <objectList> into multiple arrays, grouping by matching <key> values.
* @param objectList A list of objects.
* @param key A key from the list of objects.
*/
export function groupBy<T>(objectList: T[], key: keyof T) {
const results: T[][] = []
for (const object of objectList) {
const matchingGroup = results.find(result => result[0][key] == object[key])
if (matchingGroup != undefined) {
matchingGroup.push(object)
} else {
results.push([object])
}
}
return results
}