Refactor: separate Spotify API from player logic

The separation is not perfect yet, but it's a start and makes the entire codebase much easier to read.
This commit is contained in:
Henrik Friedrichsen
2021-08-23 22:15:54 +02:00
parent 3b0586b46a
commit faad362f55
15 changed files with 583 additions and 486 deletions

View File

@@ -38,7 +38,7 @@ impl ArtistView {
let library = library.clone();
thread::spawn(move || {
if let Some(id) = id {
if let Some(tracks) = spotify.artist_top_tracks(&id) {
if let Some(tracks) = spotify.api.artist_top_tracks(&id) {
top_tracks.write().unwrap().extend(tracks);
library.trigger_redraw();
}
@@ -53,7 +53,7 @@ impl ArtistView {
let library = library.clone();
thread::spawn(move || {
if let Some(id) = id {
if let Some(artists) = spotify.artist_related_artists(id) {
if let Some(artists) = spotify.api.artist_related_artists(id) {
related.write().unwrap().extend(artists);
library.trigger_redraw();
}
@@ -106,7 +106,7 @@ impl ArtistView {
) -> ListView<Album> {
if let Some(artist_id) = &artist.id {
let spotify = queue.get_spotify();
let albums_page = spotify.artist_albums(artist_id, Some(album_type));
let albums_page = spotify.api.artist_albums(artist_id, Some(album_type));
let view = ListView::new(albums_page.items.clone(), queue, library);
albums_page.apply_pagination(view.get_pagination());

View File

@@ -534,21 +534,27 @@ impl<I: ListItem + Clone> ViewExt for ListView<I> {
if let Some(url) = url {
let target: Option<Box<dyn ListItem>> = match url.uri_type {
UriType::Track => spotify
.api
.track(&url.id)
.map(|track| Track::from(&track).as_listitem()),
UriType::Album => spotify
.api
.album(&url.id)
.map(|album| Album::from(&album).as_listitem()),
UriType::Playlist => spotify
.api
.playlist(&url.id)
.map(|playlist| Playlist::from(&playlist).as_listitem()),
UriType::Artist => spotify
.api
.artist(&url.id)
.map(|artist| Artist::from(&artist).as_listitem()),
UriType::Episode => spotify
.api
.episode(&url.id)
.map(|episode| Episode::from(&episode).as_listitem()),
UriType::Show => spotify
.api
.get_show(&url.id)
.map(|show| Show::from(&show).as_listitem()),
};

View File

@@ -109,7 +109,7 @@ impl SearchResultsView {
_offset: usize,
_append: bool,
) -> u32 {
if let Some(results) = spotify.track(query) {
if let Some(results) = spotify.api.track(query) {
let t = vec![(&results).into()];
let mut r = tracks.write().unwrap();
*r = t;
@@ -126,7 +126,9 @@ impl SearchResultsView {
append: bool,
) -> u32 {
if let Some(SearchResult::Tracks(results)) =
spotify.search(SearchType::Track, query, 50, offset as u32)
spotify
.api
.search(SearchType::Track, query, 50, offset as u32)
{
let mut t = results.items.iter().map(|ft| ft.into()).collect();
let mut r = tracks.write().unwrap();
@@ -148,7 +150,7 @@ impl SearchResultsView {
_offset: usize,
_append: bool,
) -> u32 {
if let Some(results) = spotify.album(query) {
if let Some(results) = spotify.api.album(query) {
let a = vec![(&results).into()];
let mut r = albums.write().unwrap();
*r = a;
@@ -165,7 +167,9 @@ impl SearchResultsView {
append: bool,
) -> u32 {
if let Some(SearchResult::Albums(results)) =
spotify.search(SearchType::Album, query, 50, offset as u32)
spotify
.api
.search(SearchType::Album, query, 50, offset as u32)
{
let mut a = results.items.iter().map(|sa| sa.into()).collect();
let mut r = albums.write().unwrap();
@@ -187,7 +191,7 @@ impl SearchResultsView {
_offset: usize,
_append: bool,
) -> u32 {
if let Some(results) = spotify.artist(query) {
if let Some(results) = spotify.api.artist(query) {
let a = vec![(&results).into()];
let mut r = artists.write().unwrap();
*r = a;
@@ -204,7 +208,9 @@ impl SearchResultsView {
append: bool,
) -> u32 {
if let Some(SearchResult::Artists(results)) =
spotify.search(SearchType::Artist, query, 50, offset as u32)
spotify
.api
.search(SearchType::Artist, query, 50, offset as u32)
{
let mut a = results.items.iter().map(|fa| fa.into()).collect();
let mut r = artists.write().unwrap();
@@ -226,7 +232,7 @@ impl SearchResultsView {
_offset: usize,
_append: bool,
) -> u32 {
if let Some(result) = spotify.playlist(query).as_ref() {
if let Some(result) = spotify.api.playlist(query).as_ref() {
let pls = vec![result.into()];
let mut r = playlists.write().unwrap();
*r = pls;
@@ -243,7 +249,9 @@ impl SearchResultsView {
append: bool,
) -> u32 {
if let Some(SearchResult::Playlists(results)) =
spotify.search(SearchType::Playlist, query, 50, offset as u32)
spotify
.api
.search(SearchType::Playlist, query, 50, offset as u32)
{
let mut pls = results.items.iter().map(|sp| sp.into()).collect();
let mut r = playlists.write().unwrap();
@@ -265,7 +273,7 @@ impl SearchResultsView {
_offset: usize,
_append: bool,
) -> u32 {
if let Some(result) = spotify.get_show(query).as_ref() {
if let Some(result) = spotify.api.get_show(query).as_ref() {
let pls = vec![result.into()];
let mut r = shows.write().unwrap();
*r = pls;
@@ -282,7 +290,9 @@ impl SearchResultsView {
append: bool,
) -> u32 {
if let Some(SearchResult::Shows(results)) =
spotify.search(SearchType::Show, query, 50, offset as u32)
spotify
.api
.search(SearchType::Show, query, 50, offset as u32)
{
let mut pls = results.items.iter().map(|sp| sp.into()).collect();
let mut r = shows.write().unwrap();
@@ -304,7 +314,7 @@ impl SearchResultsView {
_offset: usize,
_append: bool,
) -> u32 {
if let Some(result) = spotify.episode(query).as_ref() {
if let Some(result) = spotify.api.episode(query).as_ref() {
let e = vec![result.into()];
let mut r = episodes.write().unwrap();
*r = e;
@@ -321,7 +331,9 @@ impl SearchResultsView {
append: bool,
) -> u32 {
if let Some(SearchResult::Episodes(results)) =
spotify.search(SearchType::Episode, query, 50, offset as u32)
spotify
.api
.search(SearchType::Episode, query, 50, offset as u32)
{
let mut e = results.items.iter().map(|se| se.into()).collect();
let mut r = episodes.write().unwrap();
@@ -378,7 +390,7 @@ impl SearchResultsView {
// check if API token refresh is necessary before commencing multiple
// requests to avoid deadlock, as the parallel requests might
// simultaneously try to refresh the token
self.spotify.refresh_token();
self.spotify.api.update_token();
// is the query a Spotify URI?
if let Some(uritype) = UriType::from_uri(&query) {

View File

@@ -23,7 +23,7 @@ impl ShowView {
let show = show.clone();
let list = {
let results = spotify.show_episodes(&show.id);
let results = spotify.api.show_episodes(&show.id);
let view = ListView::new(results.items.clone(), queue, library);
results.apply_pagination(view.get_pagination());