Skip to content

Commit

Permalink
Fix flickering on reaload spotify page
Browse files Browse the repository at this point in the history
Add the first api call to the template of the page, so it is in one file
  • Loading branch information
robertdijk committed Jan 21, 2024
1 parent 7f0e406 commit f93310f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 27 deletions.
2 changes: 1 addition & 1 deletion narrowcast_content/spotify_now_playing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def index():
"""
Render the player page.
"""
return render_template("spotify_now_playing/index.jinja2")
return render_template("spotify_now_playing/index.jinja2", initial_data=currently_playing())


@spotify_now_playing.route("/currently_playing")
Expand Down
50 changes: 26 additions & 24 deletions narrowcast_content/spotify_now_playing/static/js/index.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,38 @@
function update_player(data) {
if (data.authorized === false) {
document.querySelector('.song').style.display = 'none';
document.querySelector('.unauthorized').style.display = 'grid';
document.querySelector('.not_playing').style.display = 'none';
} else if (data.anything_playing === false) {
document.querySelector('.song').style.display = 'none';
document.querySelector('.unauthorized').style.display = 'none';
document.querySelector('.not_playing').style.display = 'grid';
} else {
document.querySelector('.song').style.display = 'grid';
document.querySelector('.unauthorized').style.display = 'none';
document.querySelector('.not_playing').style.display = 'none';
document.querySelector('.title').textContent = data.title;
document.querySelector('.artist').textContent = data.artist;
document.querySelector('.album_cover img').src = data["album_cover"];
if (data.is_playing === true) {
document.querySelector('.playing_symbol').classList.add('playing');
} else {
document.querySelector('.playing_symbol').classList.remove('playing');
}
}
}

// Function to update the now playing information
function updateNowPlaying() {
fetch('currently_playing',)
.then(response => response.json())
.then(data => {
if (data.authorized === false) {
document.querySelector('.song').style.display = 'none';
document.querySelector('.unauthorized').style.display = 'grid';
document.querySelector('.not_playing').style.display = 'none';
} else if (data.anything_playing === false) {
document.querySelector('.song').style.display = 'none';
document.querySelector('.unauthorized').style.display = 'none';
document.querySelector('.not_playing').style.display = 'grid';
} else {
document.querySelector('.song').style.display = 'grid';
document.querySelector('.unauthorized').style.display = 'none';
document.querySelector('.not_playing').style.display = 'none';
document.querySelector('.title').textContent = data.title;
document.querySelector('.artist').textContent = data.artist;
document.querySelector('.album_cover img').src = data["album_cover"];
if (data.is_playing === true){
document.querySelector('.playing_symbol').classList.add('playing');
} else {
document.querySelector('.playing_symbol').classList.remove('playing');
}
}

update_player(data);
})
.catch(error => console.error('Error fetching data:', error));
}

// Call updateNowPlaying every 5 seconds
setInterval(updateNowPlaying, 5000);

// Initial call to populate the information
updateNowPlaying();
update_player(initial_data)
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
<link rel="stylesheet" href="{{ url_for('spotify_now_playing.static', filename='css/style.css') }}">
</head>
<body>
<script type="text/javascript">
const initial_data = {{ initial_data|tojson|safe }}
</script>

<div class="container song">
<div class="album_cover main_image">
<img alt="album cover">
Expand All @@ -20,13 +24,13 @@
<div class="title main_text">Title</div>
<div class="artist sub_text">Artist</div>
</div>
<div class="container unauthorized">
<div class="container unauthorized" style="display: none">
<div class="spotify_logo main_image">
<img src="{{ url_for('spotify_now_playing.static', filename='spotify_icon.svg') }}">
</div>
<div class="unauthorized_text main_text">Unauthorized</div>
</div>
<div class="container not_playing">
<div class="container not_playing" style="display: none">
<div class="spotify_logo main_image">
<img src="{{ url_for('spotify_now_playing.static', filename='spotify_icon.svg') }}">
</div>
Expand Down

0 comments on commit f93310f

Please sign in to comment.