moved createID to MiscMethods.js

began work on playlist management.
This commit is contained in:
Paoda 2019-02-20 14:21:43 -06:00
parent 5fbc1a0ea1
commit 8d8dd3dfe6
6 changed files with 82 additions and 35 deletions

View File

@ -28,14 +28,14 @@ const filepath = new Filepath("C:\\Users\\Paoda\\Downloads");
song = await Song.getMetadata(song);
Song.setAlbumArt(song.metadata);
mp.load(song);
mp.loadSong(song);
// mp.play();
mp.element.onended = async () => {
let song = new Song(list[~~(Math.random() * list.length)]);
song = await Song.getMetadata(song);
Song.setAlbumArt(song.metadata);
mp.load(song);
mp.loadSong(song);
mp.play();
};
})();

View File

@ -29,12 +29,14 @@ export default class Body extends React.Component {
this.initialize();
}
shouldComponentUpdate(nextProps, state) {
if (this.state.table.id) return this.state.table.id !== state.table.id;
if (this.state.table) return this.state.table.id !== state.table.id;
else return false;
// On Feb 13 2019 Had problem wehre this.state.table.id was undefined.
// Unable to replicate issue, but it's a serous one. Probably should fix this yeah?
}
render() {
console.log("Render TableID: " + this.state.table.id);
if (this.state.table) console.log("Render TableID: " + this.state.table.id);
else console.warn("No Table Present");
return (
<div className="wrapper">
<div id="searchBar">

View File

@ -1,7 +1,7 @@
import React from "react";
import Song from "../../melodii/Song";
import MusicPlayer from "../../melodii/MusicPlayer";
import Misc from "../MiscMethods";
import Misc, { createID } from "../MiscMethods";
import Emitter from "../../melodii/Events";
import Filepath from "../../melodii/Filepath";
import Settings from 'electron-settings';
@ -10,8 +10,6 @@ import Settings from 'electron-settings';
var active = document.createElement("tr");
active.classList.toggle("active");
const usedTableIDs = [];
const mp = new MusicPlayer();
var JSXcache;
@ -152,7 +150,7 @@ export default class Table extends React.Component {
let filepath = e.currentTarget.dataset.filepath;
let song = new Song(filepath);
mp.load(song);
mp.loadSong(song);
mp.play();
song = await Song.getMetadata(song);
@ -173,7 +171,7 @@ export default class Table extends React.Component {
let filepath = e.currentTarget.dataset.filepath;
let song = new Song(filepath);
mp.load(song);
mp.loadSong(song);
mp.play();
song = await Song.getMetadata(song);
@ -243,29 +241,6 @@ export async function generate(path, template) {
res(table);
});
}
/**
* Creates a unique ID that is not UUID compliant.
* - used to distinguish table objects from one another.
* @param {Number} length
* @return {String}
*/
function createID(length) {
const chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
let id;
do {
id = "";
for (let i = 0; i < length; i++) id += chars[randInt(0, chars.length)];
} while(usedTableIDs.includes(id));
usedTableIDs.push(id);
return id;
function randInt(min, max) {
return ~~(Math.random() * (max - min) + min);
}
}
}
/**

View File

@ -1,5 +1,7 @@
import Song from "../melodii/Song";
const usedTableIDs = [];
/**
* - Every Method in this file must be Static and _probably_ Synchronous
* - The Methods contained in this class must only be methods that don't really fit anywhere else
@ -322,3 +324,26 @@ export default class MiscMethods {
return [stringA, stringB];
}
}
/**
* Creates a unique ID that is not UUID compliant.
* - used to distinguish table objects from one another.
* @param {Number} length
* @return {String}
*/
export function createID(length) {
const chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
let id;
do {
id = "";
for (let i = 0; i < length; i++) id += chars[randInt(0, chars.length)];
} while(usedTableIDs.includes(id));
usedTableIDs.push(id);
return id;
function randInt(min, max) {
return ~~(Math.random() * (max - min) + min);
}
}

View File

@ -1,6 +1,7 @@
import Emitter from "./Events";
import SongArchive from "./SongArchive";
import Song from './Song';
import Playlist from "./Playlist";
var mp = new Audio();
var archive = new SongArchive();
@ -9,6 +10,8 @@ export default class MusicPlayer {
constructor() {
this.element = mp;
this.isPaused = false;
this.playlist = null;
}
/** Pauses Music and sets currentTime to 0. */
@ -39,10 +42,28 @@ export default class MusicPlayer {
}
}
/**
*
* @param {Playlist} playlist
*/
load(playlist) {
const song = playlist.next();
let path = song.location;
try {
this.element.src = this.getURICompatible(path);
this.element.load();
console.log("'" + path + "'" + " from " + playlist.title + " was succesfully loaded");
} catch(e) {
console.error(path + " failed to load " + e.name + " from " + playlist.title);
}
}
/** Loads Song
* @param {Song} song
*/
load(song) {
loadSong(song) {
if (archive.getCurrentSong() !== undefined)
archive.add(archive.getCurrentSong());
let path = song.location;

View File

@ -1,5 +1,5 @@
import Filepath from "./Filepath";
import Misc from "../components/MiscMethods";
import Misc, { createID } from "../components/MiscMethods";
import Song from "./Song";
export default class Playlist {
@ -11,6 +11,22 @@ export default class Playlist {
*/
constructor(title, path) {
this.initialize(title, path);
this.id = createID(25);
this.songs = [];
this.currentSong;
this.songIndex = 0;
}
/**
* This method returns the "next" song in the playlist.
* @return {Song}
*/
next() {
return this.songs[this.songIndex++];
}
/**
@ -50,4 +66,12 @@ export default class Playlist {
res(content);
});
}
songs() {
return this.songs;
}
currentSong() {
return this.currentSong;
}
}