Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion src/cards/ha-media_player-card.html
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,33 @@

playerObjChanged: function (playerObj) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a second argument to this function that could help with avoiding trying to catch the picture twice: oldPlayerObj. In that case you can avoid doing any image calculation/attr setting if the picture url is the same.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the backend PR there was a concert that the failure might be temporary. If we cache the URL the image won't come till the next song.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still support allowing the same URL to retry. Otherwise, the implication would be that any temporary interruption blocks retrying, and could lead to confusion, especially when someone is trying to debug a new media platform.

var picture = playerObj.stateObj.attributes.entity_picture;
var dummy;
if (picture) {
this.$.cover.style.backgroundImage = 'url(' + picture + ')';
dummy = document.createElement('IMG');

@emlove emlove Feb 13, 2017

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should wrap this in an `if (this.$.cover.style.backgroundImage != 'url(' + picture + ')'), or something along those lines, so that once the image loads successfully, it doesn't keep reloading it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like that 🔝

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, wouldn't this be cached too?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, yeah, you're right that the image would be cached. We would still be going through the code of creating the IMG element, but that might be inexpensive enough to justify keeping the code simpler.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about having a hidden iron-image in the template? The image src could just be a computed binding to the entity_picture. We can then listen to the loaded and error properties and update the background image when these events fire.

@andrey-git andrey-git Feb 13, 2017

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@balloob The image will be cached, so I don't think if is needed here.
@armills What is the advantage of iron-image? It will attached to the dom so might (not sure) affect performance more.
I don't think the code will be more compact.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The advantage would be that we aren't creating an element and registering events on every state change. I also think that as a high-level idea that making use of the polymer features when available will be more stable than writing our own javascript implementations. This instance should be pretty safe, but using polymer as a practice lets take advantage of their compatibility layer for various browsers.

It will probably be a little larger in LOC, but IMO moving the load/error handlers outside of the playerObjChanged handler will make it easier to quickly grok.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registering events is just setting properties, where with polymer it has to propagate through bindings.
I could extract the functions to separate functions here too. I don't think it is important for readability here.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine with not using iron-image. If we will need this in another place, we should extract this into a util function that returns a promise.

Oh and next time, you can use dummy = new Image();

dummy.onload = function () {
this.$.cover.style.backgroundImage = 'url(' + picture + ')';
dummy.onerror = dummy.onload = null;
dummy.src = '';
dummy = null;
}.bind(this);
dummy.onerror = function () {
this.$.cover.style.backgroundImage = '';
this.toggleClass('no-cover', true, this.$.cover.parentElement);
dummy.onerror = dummy.onload = null;
dummy.src = '';
dummy = null;
}.bind(this);
if (this._timeout_id) {
clearTimeout(this._timeout_id);
}
this._timeout_id = setTimeout(function () {
if (dummy) {
// Background load still inflight. Clear real image.
this.$.cover.style.backgroundImage = '';
}
this._timeout_id = null;
}, 5000);
dummy.src = picture;
} else {
this.$.cover.style.backgroundImage = '';
}
Expand Down