Merge pull request #396 from ThatGravyBoat/api/shuffle-repeat-state

Api Feature: Add shuffle and repeat state to API
This commit is contained in:
Rick van Lieshout 2024-05-14 22:50:59 +02:00 committed by GitHub
commit 5e3583534b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 63 additions and 4 deletions

View File

@ -1,4 +1,5 @@
import { MediaStatus } from "./mediaStatus";
import { MediaPlayerInfo } from "./mediaPlayerInfo";
export interface MediaInfo {
title: string;
@ -13,4 +14,5 @@ export interface MediaInfo {
durationInSeconds?: number;
image: string;
favorite: boolean;
player: MediaPlayerInfo;
}

View File

@ -0,0 +1,8 @@
import { RepeatState } from "./repeatState";
import { MediaStatus } from "./mediaStatus";
export interface MediaPlayerInfo {
status: MediaStatus;
shuffle: boolean;
repeat: RepeatState;
}

View File

@ -0,0 +1,5 @@
export enum RepeatState {
off = "off",
all = "all",
single = "single",
}

View File

@ -19,6 +19,7 @@ import { downloadFile } from "./scripts/download";
import { addHotkey } from "./scripts/hotkeys";
import { settingsStore } from "./scripts/settings";
import { setTitle } from "./scripts/window-functions";
import { RepeatState } from "./models/repeatState";
const notificationPath = `${app.getPath("userData")}/notification.jpg`;
const appName = "TIDAL Hi-Fi";
@ -29,6 +30,8 @@ let currentListenBrainzDelayId: ReturnType<typeof setTimeout>;
let scrobbleWaitingForDelay = false;
let currentlyPlaying = MediaStatus.paused;
let currentRepeatState: RepeatState = RepeatState.off;
let currentShuffleState = false;
let currentMediaInfo: Options;
let currentNotification: Electron.Notification;
@ -348,6 +351,23 @@ function getCurrentlyPlayingStatus() {
return status;
}
function getCurrentShuffleState() {
const shuffle = elements.get("shuffle");
return shuffle?.getAttribute("aria-checked") === "true";
}
function getCurrentRepeatState() {
const repeat = elements.get("repeat");
switch (repeat?.getAttribute("data-type")) {
case "button__repeatAll":
return RepeatState.all;
case "button__repeatSingle":
return RepeatState.single;
default:
return RepeatState.off;
}
}
/**
* Convert the duration from MM:SS to seconds
* @param {*} duration
@ -511,14 +531,21 @@ setInterval(function () {
const titleOrArtistsChanged = currentSong !== songDashArtistTitle;
const current = elements.getText("current");
const currentStatus = getCurrentlyPlayingStatus();
const shuffleState = getCurrentShuffleState();
const repeatState = getCurrentRepeatState();
const playStateChanged = currentStatus != currentlyPlaying;
const shuffleStateChanged = shuffleState != currentShuffleState;
const repeatStateChanged = repeatState != currentRepeatState;
const hasStateChanged = playStateChanged || shuffleStateChanged || repeatStateChanged;
// update info if song changed or was just paused/resumed
if (titleOrArtistsChanged || playStateChanged) {
if (playStateChanged) {
currentlyPlaying = currentStatus;
}
if (titleOrArtistsChanged || hasStateChanged) {
if (playStateChanged) currentlyPlaying = currentStatus;
if (shuffleStateChanged) currentShuffleState = shuffleState;
if (repeatStateChanged) currentRepeatState = repeatState;
skipArtistsIfFoundInSkippedArtistsList(artistsArray);
const album = elements.getAlbumName();
@ -537,6 +564,12 @@ setInterval(function () {
image: "",
icon: "",
favorite: elements.isFavorite(),
player: {
status: currentStatus,
shuffle: shuffleState,
repeat: repeatState,
},
};
// update title, url and play info with new info

View File

@ -1,5 +1,6 @@
import { MediaInfo } from "../models/mediaInfo";
import { MediaStatus } from "../models/mediaStatus";
import { RepeatState } from "../models/repeatState";
export const mediaInfo = {
title: "",
@ -14,6 +15,12 @@ export const mediaInfo = {
durationInSeconds: 0,
image: "tidal-hifi-icon",
favorite: false,
player: {
status: MediaStatus.paused as string,
shuffle: false,
repeat: RepeatState.off as string,
}
};
export const updateMediaInfo = (arg: MediaInfo) => {
@ -29,6 +36,10 @@ export const updateMediaInfo = (arg: MediaInfo) => {
mediaInfo.durationInSeconds = arg.durationInSeconds ?? 0;
mediaInfo.image = propOrDefault(arg.image);
mediaInfo.favorite = arg.favorite;
mediaInfo.player.status = propOrDefault(arg.player?.status);
mediaInfo.player.shuffle = arg.player.shuffle;
mediaInfo.player.repeat = propOrDefault(arg.player?.repeat);
};
/**