Skip to content

Commit

Permalink
feature: top favorites
Browse files Browse the repository at this point in the history
  • Loading branch information
punxaphil committed Sep 27, 2023
1 parent 04e6b8e commit 78b32f0
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 2 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ customThumbnailIfMissing:
mediaBrowserTitlesToIgnore:
- Local Media
- My Bad Playlist
topFavorites: # Show these favorites at the top of the list
- Legendary
- Country Rocks
- Kacey Musgraves Radio
```
## Using individual section cards
Expand Down
17 changes: 16 additions & 1 deletion src/editor/advanced-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,18 @@ export const ADVANCED_SCHEMA = [
name: 'mediaBrowserShowTitleForThumbnailIcons',
selector: { boolean: {} },
},
{
type: 'string',
name: 'topFavorites',
},
];

class AdvancedEditor extends BaseEditor {
protected render(): TemplateResult {
const topFavorites = this.store.config.topFavorites ?? [];
const data = { ...this.store.config, topFavorites: topFavorites.join(', ') };
return html`
<sonos-card-editor-form .schema=${ADVANCED_SCHEMA} .store=${this.store}></sonos-card-editor-form>
<sonos-card-editor-form .schema=${ADVANCED_SCHEMA} .store=${this.store} .data=${data} .changed=${this.changed}></sonos-card-editor-form>
<p>
The following needs to be configured using code (YAML):
<ul>
Expand All @@ -44,6 +50,15 @@ class AdvancedEditor extends BaseEditor {
</p>
`;
}
protected changed(ev: CustomEvent): void {
const changed = ev.detail.value;
this.config = {
...this.config,
...changed,
topFavorites: changed.topFavorites.split(/ *, */),
};
this.configChanged();
}
}

customElements.define('sonos-card-advanced-editor', AdvancedEditor);
15 changes: 14 additions & 1 deletion src/sections/media-browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,20 @@ export class MediaBrowser extends LitElement {
this.mediaPlayers,
this.config.mediaBrowserTitlesToIgnore,
);
allFavorites = allFavorites.sort((a, b) => a.title.localeCompare(b.title, 'en', { sensitivity: 'base' }));
allFavorites = allFavorites.sort((a, b) => {
const topFavorites = this.config.topFavorites ?? [];
const aIndex = topFavorites.indexOf(a.title);
const bIndex = topFavorites.indexOf(b.title);
if (aIndex > -1 && bIndex > -1) {
return aIndex - bIndex;
} else {
let result = bIndex - aIndex;
if (result === 0) {
result = a.title.localeCompare(b.title, 'en', { sensitivity: 'base' });
}
return result;
}
});
return [
...(this.config.customSources?.[this.activePlayer.id]?.map(MediaBrowser.createSource) || []),
...(this.config.customSources?.all?.map(MediaBrowser.createSource) || []),
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export interface CardConfig extends LovelaceCardConfig {
mediaBrowserTitlesToIgnore?: string[];
mediaBrowserItemsPerRow: number;
mediaBrowserShowTitleForThumbnailIcons?: boolean;
topFavorites?: string[];
}

export interface MediaArtworkOverride {
Expand Down

0 comments on commit 78b32f0

Please sign in to comment.