From 2d94b4bf496e55f4ca5bbc9345a08f085c282b6f Mon Sep 17 00:00:00 2001 From: ThatGravyBoat Date: Mon, 6 May 2024 03:50:41 -0230 Subject: [PATCH] Add shuffle and repeat to current state api --- src/models/mediaInfo.ts | 2 ++ src/models/mediaPlayerInfo.ts | 8 +++++++ src/models/repeatState.ts | 5 +++++ src/preload.ts | 41 +++++++++++++++++++++++++++++++---- src/scripts/mediaInfo.ts | 11 ++++++++++ 5 files changed, 63 insertions(+), 4 deletions(-) create mode 100644 src/models/mediaPlayerInfo.ts create mode 100644 src/models/repeatState.ts diff --git a/src/models/mediaInfo.ts b/src/models/mediaInfo.ts index 7a87760..7fcd5e9 100644 --- a/src/models/mediaInfo.ts +++ b/src/models/mediaInfo.ts @@ -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; } diff --git a/src/models/mediaPlayerInfo.ts b/src/models/mediaPlayerInfo.ts new file mode 100644 index 0000000..70e57f6 --- /dev/null +++ b/src/models/mediaPlayerInfo.ts @@ -0,0 +1,8 @@ +import {RepeatState} from "./repeatState"; +import {MediaStatus} from "./mediaStatus"; + +export interface MediaPlayerInfo { + status: MediaStatus; + shuffle: boolean; + repeat: RepeatState; +} diff --git a/src/models/repeatState.ts b/src/models/repeatState.ts new file mode 100644 index 0000000..4b54619 --- /dev/null +++ b/src/models/repeatState.ts @@ -0,0 +1,5 @@ +export enum RepeatState { + off = "off", + all = "all", + single = "single", +} diff --git a/src/preload.ts b/src/preload.ts index 4127a7a..61187ec 100644 --- a/src/preload.ts +++ b/src/preload.ts @@ -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; 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 diff --git a/src/scripts/mediaInfo.ts b/src/scripts/mediaInfo.ts index 9cfada4..6e2b56e 100644 --- a/src/scripts/mediaInfo.ts +++ b/src/scripts/mediaInfo.ts @@ -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); }; /**