Skip to content

Commit

Permalink
chore: post release cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
XeroxDev authored Feb 14, 2024
2 parents cfca17a + 7a37596 commit 8f0a289
Show file tree
Hide file tree
Showing 16 changed files with 136 additions and 70 deletions.
4 changes: 4 additions & 0 deletions de.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@
"Name": "Shuffle",
"Tooltip": "Shuffle"
},
"fun.shiro.ytmdc.repeat": {
"Name": "Wiederholungsmodus",
"Tooltip": "Wechselt den Wiederholungsmodus"
},
"Localization": {
"PI": {
"HOST": "Host",
Expand Down
4 changes: 4 additions & 0 deletions en.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@
"Name": "Shuffle",
"Tooltip": "Shuffle"
},
"fun.shiro.ytmdc.repeat": {
"Name": "Repeat mode",
"Tooltip": "Change repeat mode"
},
"Localization": {
"PI": {
"HOST": "Host",
Expand Down
2 changes: 1 addition & 1 deletion property-inspector.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ <h3 class="error-title">Something went wrong</h3>
</div>
<div class="sdpi-item">
<div class="sdpi-item-label action-label">Action</div>
<select class="sdpi-item-value action-label" id="action">
<select class="sdpi-item-value" id="action">
<option class="toggle-label" value="TOGGLE" selected>Toggle</option>
<option class="pause-label" value="PAUSE">Pause</option>
<option class="play-label" value="PLAY">Play</option>
Expand Down
3 changes: 3 additions & 0 deletions scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ browserify({entries: ['src/ytmd-pi.ts'], plugin: [tsify]}).bundle((err, buf) =>
fs.copyFileSync('manifest.json', 'build/fun.shiro.ytmd.sdPlugin/manifest.json');
fs.copyFileSync('property-inspector.html', 'build/fun.shiro.ytmd.sdPlugin/property-inspector.html');
fs.copyFileSync('action.html', 'build/fun.shiro.ytmd.sdPlugin/action.html');
fs.copyFileSync('de.json', 'build/fun.shiro.ytmd.sdPlugin/de.json');
fs.copyFileSync('en.json', 'build/fun.shiro.ytmd.sdPlugin/en.json');
fs.copyFileSync('fr.json', 'build/fun.shiro.ytmd.sdPlugin/fr.json');
fs.cpSync('icons', 'build/fun.shiro.ytmd.sdPlugin/icons', {recursive: true});

// Run distribution tool
Expand Down
1 change: 1 addition & 0 deletions src/actions/default.action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export abstract class DefaultAction<Instance> extends StreamDeckAction<
this.socket = YTMD.COMPANION.socketClient;
this.rest = YTMD.COMPANION.restClient;
console.info(`Initialized ${actionName}`);
plugin.logMessage(`Initialized ${actionName}`);
}

abstract onContextAppear(event: WillAppearEvent): void;
Expand Down
2 changes: 2 additions & 0 deletions src/actions/like-dislike.action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,12 @@ export class LikeDislikeAction extends DefaultAction<LikeDislikeAction> {
onKeypressUp(event: KeyUpEvent): void {
if (this.likeStatus === LikeStatus.LIKE) this.rest.toggleLike().catch(reason => {
console.error(reason);
this.plugin.logMessage(`Error while like toggle. like status: ${this.likeStatus}, event: ${JSON.stringify(event)}, error: ${JSON.stringify(reason)}`);
this.plugin.showAlert(event.context)
});
else if (this.likeStatus === LikeStatus.DISLIKE) this.rest.toggleDislike().catch(reason => {
console.error(reason);
this.plugin.logMessage(`Error while dislike toggle. like status: ${this.likeStatus}, event: ${JSON.stringify(event)}, error: ${JSON.stringify(reason)}`);
this.plugin.showAlert(event.context)
});
}
Expand Down
42 changes: 34 additions & 8 deletions src/actions/mute.action.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import {KeyUpEvent, SDOnActionEvent, WillAppearEvent, WillDisappearEvent,} from 'streamdeck-typescript';
import {KeyUpEvent, SDOnActionEvent, StateType, WillAppearEvent, WillDisappearEvent,} from 'streamdeck-typescript';
import {YTMD} from '../ytmd';
import {DefaultAction} from './default.action';
import {StateOutput} from "ytmdesktop-ts-companion";

export class MuteAction extends DefaultAction<MuteAction> {
private events: { context: string, method: (state: StateOutput) => void }[] = [];
private muted: boolean = false;
private initialized = false;
private volume = 0;
private lastVolume = 0;

constructor(private plugin: YTMD, actionName: string) {
super(plugin, actionName);
Expand All @@ -20,7 +22,19 @@ export class MuteAction extends DefaultAction<MuteAction> {

found = {
context: context,
method: (state: StateOutput) => this.muted = state.player.volume === 0
method: (state: StateOutput) => {
if (!this.initialized) {
this.initialized = true;
this.volume = state.player.volume;
this.lastVolume = this.volume;
}
this.volume = state.player.volume;
if (this.volume > 0) {
this.lastVolume = this.volume;
}

this.plugin.setState(this.volume > 0 ? StateType.ON : StateType.OFF, context);
}
};

this.events.push(found);
Expand All @@ -41,12 +55,24 @@ export class MuteAction extends DefaultAction<MuteAction> {

@SDOnActionEvent('keyUp')
onKeypressUp(event: KeyUpEvent) {
this.muted = !this.muted;
this.muted ? this.rest.mute().catch(reason => {
console.error(reason);
this.plugin.showAlert(event.context)
}) : this.rest.unmute().catch(reason => {
if (this.volume <= 0) {
this.volume = this.lastVolume;

this.rest.setVolume(this.volume).catch(reason => {
console.error(reason);
this.plugin.logMessage(`Error while setting volume. volume: ${this.volume}, event: ${JSON.stringify(event)}, error: ${JSON.stringify(reason)}`);
this.plugin.showAlert(event.context)
});

return;
}

this.lastVolume = this.volume;
this.volume = 0;

this.rest.setVolume(this.volume).catch(reason => {
console.error(reason);
this.plugin.logMessage(`Error while setting volume. volume: ${this.volume}, event: ${JSON.stringify(event)}, error: ${JSON.stringify(reason)}`);
this.plugin.showAlert(event.context)
});
}
Expand Down
2 changes: 2 additions & 0 deletions src/actions/next-prev-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ export class NextPrevAction extends DefaultAction<NextPrevAction> {
onKeypressUp(event: KeyUpEvent) {
if (this.nextOrPrev === 'NEXT') this.rest.next().catch(reason => {
console.error(reason);
this.plugin.logMessage(`Error while next. event: ${JSON.stringify(event)}, error: ${JSON.stringify(reason)}`);
this.plugin.showAlert(event.context)
});
else this.rest.previous().catch(reason => {
console.error(reason);
this.plugin.logMessage(`Error while previous. event: ${JSON.stringify(event)}, error: ${JSON.stringify(reason)}`);
this.plugin.showAlert(event.context)
})
}
Expand Down
23 changes: 15 additions & 8 deletions src/actions/play-pause.action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class PlayPauseAction extends DefaultAction<PlayPauseAction> {
private trackState: TrackState = TrackState.UNKNOWN;
private currentTitle: string;
private firstTimes = 10;
private format = '{current}';
private contextFormat: { [key: string]: string } = {};
private events: {
context: string,
onTick: (state: StateOutput) => void,
Expand Down Expand Up @@ -113,6 +113,7 @@ export class PlayPauseAction extends DefaultAction<PlayPauseAction> {
if (!settings?.action) {
this.rest.playPause().catch(reason => {
console.error(reason);
this.plugin.logMessage(`Error while playPause toggle. context: ${JSON.stringify(context)}, error: ${JSON.stringify(reason)}`);
this.plugin.showAlert(context)
})
return;
Expand All @@ -121,18 +122,21 @@ export class PlayPauseAction extends DefaultAction<PlayPauseAction> {
case 'PLAY':
this.rest.play().catch(reason => {
console.error(reason);
this.plugin.logMessage(`Error while play. context: ${JSON.stringify(context)}, error: ${JSON.stringify(reason)}`);
this.plugin.showAlert(context)
});
break;
case 'PAUSE':
this.rest.pause().catch(reason => {
console.error(reason);
this.plugin.logMessage(`Error while pause. context: ${JSON.stringify(context)}, error: ${JSON.stringify(reason)}`);
this.plugin.showAlert(context)
});
break;
default:
this.rest.playPause().catch(reason => {
console.error(reason);
this.plugin.logMessage(`Error while playPause toggle. context: ${JSON.stringify(context)}, error: ${JSON.stringify(reason)}`);
this.plugin.showAlert(context)
});
break;
Expand All @@ -142,7 +146,7 @@ export class PlayPauseAction extends DefaultAction<PlayPauseAction> {
}

handlePlayerData(
{context}: WillAppearEvent,
{context, payload: {settings}}: WillAppearEvent<PlayPauseSettings>,
data: StateOutput
) {
if (Object.keys(data).length === 0) {
Expand All @@ -151,9 +155,9 @@ export class PlayPauseAction extends DefaultAction<PlayPauseAction> {
}
let current = Math.floor(data.player.videoProgress);
let duration = Math.floor(data.video?.durationSeconds ?? 0);
let remaining = Math.floor(data.player.videoProgress - (data.video?.durationSeconds ?? 0));
let remaining = duration - current;

const title = this.formatTitle(current, duration, remaining);
const title = this.formatTitle(current, duration, remaining, context, settings);

if (this.currentTitle !== title || this.firstTimes >= 1) {
this.firstTimes--;
Expand All @@ -170,31 +174,34 @@ export class PlayPauseAction extends DefaultAction<PlayPauseAction> {
}
}

private formatTitle(current: number, duration: number, remaining: number): string {
private formatTitle(current: number, duration: number, remaining: number, context: string, settings: PlayPauseSettings): string {
current = current ?? 0;
duration = duration ?? 0;
remaining = remaining ?? 0;
const varMapping: { [key: string]: string } = {
'current': PlayPauseAction.formatTime(current),
'current:H': PlayPauseAction.formatTime(current),
'current:S': current.toString(),
'duration': PlayPauseAction.formatTime(duration),
'duration:H': PlayPauseAction.formatTime(duration),
'duration:S': duration.toString(),
'remaining': PlayPauseAction.formatTime(remaining),
'remaining:H': PlayPauseAction.formatTime(remaining),
'remaining:S': remaining.toString()
};

let result = this.format;
let result = this.contextFormat[context] ?? settings.displayFormat ?? '{current}';

for (let varMappingKey in varMapping) {
const value = varMapping[varMappingKey];
result = result.replace(new RegExp(`\{${varMappingKey}\}`, 'g'), value);
result = result.replace(new RegExp(`\{${varMappingKey}\}`, 'gi'), value);
}

return result;
}

@SDOnActionEvent('didReceiveSettings')
private handleSettings(e: DidReceiveSettingsEvent<PlayPauseSettings>) {
this.format = e?.payload?.settings?.displayFormat ?? this.format;
this.contextFormat[e.context] = e.payload.settings?.displayFormat ?? this.contextFormat[e.context];
}
}
21 changes: 4 additions & 17 deletions src/actions/repeat.action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,6 @@ export class RepeatAction extends DefaultAction<RepeatAction> {

@SDOnActionEvent('willAppear')
onContextAppear(event: WillAppearEvent<any>): void {
// this.socket.onTick$
// .pipe(
// map((data) => data.player.repeatType),
// distinctUntilChanged(),
// takeUntil(this.destroy$)
// )
// .subscribe((repeatType) => {
// this.plugin.setImage(
// `data:image/png;base64,${this.icons[repeatType]}`,
// event.context
// );
// });

let found = this.events.find(e => e.context === event.context);
if (found) {
return;
Expand All @@ -44,11 +31,11 @@ export class RepeatAction extends DefaultAction<RepeatAction> {
found = {
context: event.context,
method: (state: StateOutput) => {
if (!state.player.queue?.repeatMode) {
if (state.player.queue?.repeatMode === undefined || state.player.queue.repeatMode === null || state.player.queue.repeatMode === this.currentMode) {
return;
}

const currentMode = state.player.queue.repeatMode;
this.currentMode = state.player.queue.repeatMode;

let mode: "NONE" | "ONE" | "ALL" = "NONE";
switch (this.currentMode) {
Expand Down Expand Up @@ -89,7 +76,7 @@ export class RepeatAction extends DefaultAction<RepeatAction> {

@SDOnActionEvent('keyUp')
onKeypressUp(event: KeyUpEvent<any>): void {
let mode: RepeatMode = this.currentMode;
let mode: RepeatMode;
switch (this.currentMode) {
case RepeatMode.ALL:
mode = RepeatMode.NONE;
Expand All @@ -101,9 +88,9 @@ export class RepeatAction extends DefaultAction<RepeatAction> {
mode = RepeatMode.ONE;
break;
}

this.rest.repeatMode(mode).catch(reason => {
console.error(reason);
this.plugin.logMessage(`Error while setting repeat mode. mode: ${mode}, event: ${JSON.stringify(event)}, error: ${JSON.stringify(reason)}`);
this.plugin.showAlert(event.context)
})
}
Expand Down
1 change: 1 addition & 0 deletions src/actions/shuffle.action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export class ShuffleAction extends DefaultAction<ShuffleAction> {
.then(() => this.plugin.showOk(event.context))
.catch(reason => {
console.error(reason);
this.plugin.logMessage(`Error while shuffle. event: ${JSON.stringify(event)}, error: ${JSON.stringify(reason)}`);
this.plugin.showAlert(event.context)
});
}
Expand Down
11 changes: 8 additions & 3 deletions src/actions/song-info.action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class SongInfoAction extends DefaultAction<SongInfoAction> {
private albumIndex = 0;
private currentAlbum: string;
private currentThumbnail: string;
private lastChange: Date;
private lastChange: {context: string, date:Date }[] = [];

constructor(private plugin: YTMD, actionName: string) {
super(plugin, actionName);
Expand All @@ -36,6 +36,7 @@ export class SongInfoAction extends DefaultAction<SongInfoAction> {
context: event.context,
method: (state: StateOutput) => this.handleSongInfo(event, state).catch(reason => {
console.error(reason);
this.plugin.logMessage(`Error while executing handleSongInfo. state: ${JSON.stringify(state)}, event: ${JSON.stringify(event)}, error: ${JSON.stringify(reason)}`);
this.plugin.showAlert(event.context)
})
};
Expand Down Expand Up @@ -70,6 +71,7 @@ export class SongInfoAction extends DefaultAction<SongInfoAction> {
public onKeypressUp(event: KeyUpEvent): void {
this.rest.playPause().catch(reason => {
console.error(reason);
this.plugin.logMessage(`Error while playPause toggle (song info). event: ${JSON.stringify(event)}, error: ${JSON.stringify(reason)}`);
this.plugin.showAlert(event.context)
});
}
Expand Down Expand Up @@ -109,8 +111,11 @@ export class SongInfoAction extends DefaultAction<SongInfoAction> {
}

private async handleSongInfo(event: WillAppearEvent, state: StateOutput) {
if (this.lastChange && new Date().getTime() - this.lastChange.getTime() < 250) return;
this.lastChange = new Date();
const lastChange = this.lastChange.find(l => l.context === event.context);
if (lastChange && new Date().getTime() - lastChange.date.getTime() < 450) return;
this.lastChange = this.lastChange.filter(l => l.context !== event.context);
this.lastChange.push({context: event.context, date: new Date()});

const {title, album, author, cover} = this.getSongData(state);

if (this.currentTitle !== title) this.titleIndex = 0;
Expand Down
1 change: 1 addition & 0 deletions src/actions/vol-change.action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export class VolChangeAction extends DefaultAction<VolChangeAction> {
this.currentVolume = newVolume;
this.rest.setVolume(newVolume <= 0 ? 0 : newVolume >= 100 ? 100 : newVolume).catch(reason => {
console.error(reason);
this.plugin.logMessage(`Error while setting volume. volume: ${newVolume}, context: ${JSON.stringify(context)}, error: ${JSON.stringify(reason)}`);
this.plugin.showAlert(context)
});
await this.wait(500);
Expand Down
Loading

0 comments on commit 8f0a289

Please sign in to comment.