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

@@ -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) {