Skip to content

Commit

Permalink
Fixed many major/minor bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
sakithb committed Sep 4, 2021
1 parent 05f1381 commit 31dd0ad
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 21 deletions.
38 changes: 17 additions & 21 deletions extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ const Me = ExtensionUtils.getCurrentExtension();
const Mainloop = imports.mainloop;
const Main = imports.ui.main;

const { playerAction, getPlayers, getMetadata, getStatus } = Me.imports.utils;
const { playerAction, getPlayers, getMetadata, getStatus, isValidPlayer, hasMetadataChanged } =
Me.imports.utils;

let maxDisplayLength,
updateDelay,
Expand Down Expand Up @@ -56,8 +57,6 @@ let currentPlayer, currentMetadata, currentLabel, currentStatus;
let loopFinished, contentRemoved, mouseHovered;

const init = () => {
loopFinished = true;
contentRemoved = true;
playerIcons = ["chromium", "firefox"];
positions = {
left: "_leftBox",
Expand All @@ -69,6 +68,8 @@ const init = () => {
const enable = () => {
log("[Media-Controls] Enabling");
settings = ExtensionUtils.getSettings();
loopFinished = true;
contentRemoved = true;
currentMetadata = null;
currentPlayer = null;
currentStatus = null;
Expand Down Expand Up @@ -304,11 +305,8 @@ const mainLoop = async () => {
// log("Player is playing");
currentStatus = "Playing";
let metadata = await getMetadata(currentPlayer);
if (
metadata["title"] ||
(_metadata["id"] && _metadata["id"] !== "/org/mpris/MediaPlayer2/TrackList/NoTrack")
) {
if (Object.keys(metadata).every((key) => metadata[key] !== currentMetadata[key])) {
if (isValidPlayer(metadata)) {
if (hasMetadataChanged(metadata, currentMetadata)) {
log("Metadata is not equal, updating em");
currentMetadata = metadata;
currentLabel = currentMetadata["title"] || currentMetadata["id"];
Expand All @@ -317,6 +315,7 @@ const mainLoop = async () => {
updateToggleButtonIcon();
}
} else {
log("Not valid player");
currentPlayer = null;
}
} else {
Expand All @@ -331,17 +330,11 @@ const mainLoop = async () => {
}

if (currentPlayer) {
// log("not nulling player", currentPlayer);
log("not nulling player", currentPlayer);
currentStatus = _status;
_metadata = await getMetadata(currentPlayer);
if (
_metadata["title"] ||
(_metadata["id"] &&
_metadata["id"] !== "/org/mpris/MediaPlayer2/TrackList/NoTrack")
) {
if (
Object.keys(_metadata).every((key) => _metadata[key] !== currentMetadata[key])
) {
if (isValidPlayer(_metadata)) {
if (hasMetadataChanged(_metadata, currentMetadata)) {
log("_Metadata is not equal, updating em");
currentMetadata = _metadata;
currentLabel = currentMetadata["title"] || currentMetadata["id"];
Expand All @@ -350,18 +343,20 @@ const mainLoop = async () => {
updateToggleButtonIcon();
}
} else {
log("Not valid player");

currentPlayer = null;
}
}
}
} else {
log("Player not in list");
// log("Player not in list");
// log("New player/ new state");
let validPlayers = new Map();
let playingPlayers = [];
for (player of players) {
let { id, title, artist } = await getMetadata(player);
if (title || (id && id !== "/org/mpris/MediaPlayer2/TrackList/NoTrack")) {
if (isValidPlayer({ id, title })) {
let status = await getStatus(player);
if (status === "Playing") {
playingPlayers.push(player);
Expand All @@ -377,13 +372,14 @@ const mainLoop = async () => {
if (playingPlayers.length > 0) {
currentPlayer = playingPlayers[0];
currentStatus = "Playing";
// log("Playing player", currentPlayer);
log("Playing player", currentPlayer);
} else {
currentPlayer = validPlayers.keys().next().value;
currentStatus = "Paused";
// log("no playing players", currentPlayer);
log("no playing players", currentPlayer);
}
currentMetadata = validPlayers.get(currentPlayer);
log(currentMetadata["title"], currentMetadata["id"]);
currentLabel = currentMetadata["title"] || currentMetadata["id"];
addContent();
updateContent();
Expand Down
14 changes: 14 additions & 0 deletions utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,17 @@ var getStatus = async (player) => {
logError(error);
}
};

var isValidPlayer = ({ id, title }) => {
if (title || (id && id !== "/org/mpris/MediaPlayer2/TrackList/NoTrack")) {
return true;
}
return false;
};

var hasMetadataChanged = (metadata, _metadata) => {
if (Object.keys(metadata).every((key) => metadata[key] !== _metadata[key])) {
return true;
}
return false;
};

0 comments on commit 31dd0ad

Please sign in to comment.