Skip to content
Open
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
49 changes: 44 additions & 5 deletions spotify_player/src/media_control.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
#![allow(unused_imports)]
use chrono::TimeDelta;
use rspotify::model::{
AlbumId, EpisodeId, Offset, PlayContextId, PlayableId, PlaylistId, ShowId, TrackId,
};
use souvlaki::MediaPosition;
use souvlaki::{MediaControlEvent, MediaControls, MediaMetadata, MediaPlayback, PlatformConfig};

use crate::state::{ContextId, Playback};
use crate::utils;
use crate::{
client::{ClientRequest, PlayerRequest},
Expand Down Expand Up @@ -127,11 +132,45 @@ pub fn start_event_watcher(
.send(ClientRequest::Player(PlayerRequest::PreviousTrack))
.unwrap_or_default();
}
MediaControlEvent::SetVolume(volume) => client_pub
.send(ClientRequest::Player(PlayerRequest::Volume(
(volume * 100.0) as u8,
)))
.unwrap_or_default(),
MediaControlEvent::SetVolume(volume) => {
client_pub
.send(ClientRequest::Player(PlayerRequest::Volume(
(volume * 100.0) as u8,
)))
.unwrap_or_default();
}
MediaControlEvent::OpenUri(uri) => {
let id = uri[uri.rfind(":").unwrap_or(0) + 1..uri.len()].to_string();
let uri_type = &uri[uri.find(":").unwrap_or(0) + 1..uri.rfind(":").unwrap_or(0)];
let playback: Option<Playback> = match uri_type {
"album" => {
let album_id = AlbumId::from_id(id).unwrap();
Some(Playback::Context(ContextId::Album(album_id), None))
}
"track" => {
let track_id = PlayableId::Track(TrackId::from_id(id).unwrap());
Some(Playback::URIs(vec![track_id], None))
}
"playlist" => {
let playlist_id = PlaylistId::from_id(id).unwrap();
Some(Playback::Context(ContextId::Playlist(playlist_id), None))
}
"show" => {
let show_id = ShowId::from_id(id).unwrap();
Some(Playback::Context(ContextId::Show(show_id), None))
}
"episode" => {
let episode_id = PlayableId::Episode(EpisodeId::from_id(id).unwrap());
Some(Playback::URIs(vec![episode_id], None))
}
_ => None,
};
if let Some(s) = playback {
client_pub
.send(ClientRequest::Player(PlayerRequest::StartPlayback(s, None)))
.unwrap_or_default();
}
}
Comment on lines +142 to +173
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 would implement it like this:

Suggested change
MediaControlEvent::OpenUri(uri) => {
let id = uri[uri.rfind(":").unwrap_or(0) + 1..uri.len()].to_string();
let uri_type = &uri[uri.find(":").unwrap_or(0) + 1..uri.rfind(":").unwrap_or(0)];
let playback: Option<Playback> = match uri_type {
"album" => {
let album_id = AlbumId::from_id(id).unwrap();
Some(Playback::Context(ContextId::Album(album_id), None))
}
"track" => {
let track_id = PlayableId::Track(TrackId::from_id(id).unwrap());
Some(Playback::URIs(vec![track_id], None))
}
"playlist" => {
let playlist_id = PlaylistId::from_id(id).unwrap();
Some(Playback::Context(ContextId::Playlist(playlist_id), None))
}
"show" => {
let show_id = ShowId::from_id(id).unwrap();
Some(Playback::Context(ContextId::Show(show_id), None))
}
"episode" => {
let episode_id = PlayableId::Episode(EpisodeId::from_id(id).unwrap());
Some(Playback::URIs(vec![episode_id], None))
}
_ => None,
};
if let Some(s) = playback {
client_pub
.send(ClientRequest::Player(PlayerRequest::StartPlayback(s, None)))
.unwrap_or_default();
}
}
MediaControlEvent::OpenUri(uri) => {
let mut split = uri.split(':');
let (Some("spotify"), Some(uri_type), Some(id)) =
(split.next(), split.next(), split.next())
else {
return;
};
let id = id.to_string();
let playback = match uri_type {
"album" => AlbumId::from_id(id)
.ok()
.map(|album_id| Playback::Context(ContextId::Album(album_id), None)),
"track" => TrackId::from_id(id)
.ok()
.map(|track_id| Playback::URIs(vec![PlayableId::Track(track_id)], None)),
"playlist" => PlaylistId::from_id(id).ok().map(|playlist_id| {
Playback::Context(ContextId::Playlist(playlist_id), None)
}),
"show" => ShowId::from_id(id)
.ok()
.map(|show_id| Playback::Context(ContextId::Show(show_id), None)),
"episode" => EpisodeId::from_id(id).ok().map(|episode_id| {
Playback::URIs(vec![PlayableId::Episode(episode_id)], None)
}),
_ => None,
};
if let Some(playback) = playback {
client_pub
.send(ClientRequest::Player(PlayerRequest::StartPlayback(
playback, None,
)))
.unwrap_or_default();
}
}

I'd argue that especially the parsing of the URI is more readable this way, plus it checks that the first part is actually 'spotify'.

The changes in the match statement get rid of the unwrap() calls you had, which is more safe.

_ => {}
}
})?;
Expand Down