Skip to content

Commit

Permalink
Suggested Changes (#547)
Browse files Browse the repository at this point in the history
Co-authored-by: so9010 <[email protected]>
  • Loading branch information
2 people authored and jacksongoode committed Oct 21, 2024
1 parent 0db015f commit 5f31315
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 75 deletions.
10 changes: 1 addition & 9 deletions psst-gui/src/data/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub use crate::data::{
user::{PublicUser, UserProfile},
utils::{Cached, Float64, Image, Page},
};
use crate::webapi::{TrackCredits, WebApi};
use crate::ui::credits::TrackCredits;

pub const ALERT_DURATION: Duration = Duration::from_secs(5);

Expand All @@ -88,7 +88,6 @@ pub struct AppState {
pub added_queue: Vector<QueueEntry>,
pub lyrics: Promise<Vector<TrackLines>>,
pub credits: Option<TrackCredits>,
pub webapi: Arc<WebApi>,
}

impl AppState {
Expand All @@ -112,12 +111,6 @@ impl AppState {
queue: Vector::new(),
volume: config.volume,
};
let webapi = Arc::new(WebApi::new(
SessionService::empty(),
None, // Remove config.proxy_url.as_deref()
None, // Remove config.cache_dir.clone()
config.paginated_limit,
));
Self {
session: SessionService::empty(),
nav: Nav::Home,
Expand Down Expand Up @@ -179,7 +172,6 @@ impl AppState {
finder: Finder::new(),
lyrics: Promise::Empty,
credits: None,
webapi,
}
}
}
Expand Down
55 changes: 45 additions & 10 deletions psst-gui/src/ui/credits.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,53 @@
use std::sync::Arc;

use crate::{
cmd,
data::{AppState, ArtistLink, Nav},
ui::theme,
webapi::{CreditArtist, RoleCredit},
};
use druid::{
widget::{Controller, CrossAxisAlignment, Flex, Label, LineBreaking, List, Painter, Scroll},
Cursor, Env, Event, EventCtx, LensExt, RenderContext, Target, Widget, WidgetExt, WindowDesc,
widget::{Controller, CrossAxisAlignment, Flex, Label, LineBreaking, List, Painter, Scroll}, Cursor, Data, Env, Event, EventCtx, Lens, LensExt, RenderContext, Target, Widget, WidgetExt, WindowDesc
};
use serde::Deserialize;


#[derive(Debug, Clone, Data, Lens, Deserialize)]
pub struct TrackCredits {
#[serde(rename = "trackUri")]
pub track_uri: String,
#[serde(rename = "trackTitle")]
pub track_title: String,
#[serde(rename = "roleCredits")]
pub role_credits: Arc<Vec<RoleCredit>>,
#[serde(rename = "extendedCredits")]
pub extended_credits: Arc<Vec<String>>,
#[serde(rename = "sourceNames")]
pub source_names: Arc<Vec<String>>,
}

#[derive(Debug, Clone, Data, Lens, Deserialize)]
pub struct RoleCredit {
#[serde(rename = "roleTitle")]
pub role_title: String,
pub artists: Arc<Vec<CreditArtist>>,
}

#[derive(Debug, Clone, Data, Lens, Deserialize)]
pub struct CreditArtist {
pub uri: Option<String>,
pub name: String,
#[serde(rename = "imageUri")]
pub image_uri: Option<String>,
#[serde(rename = "externalUrl")]
pub external_url: Option<String>,
#[serde(rename = "creatorUri")]
pub creator_uri: Option<String>,
#[serde(default)]
pub subroles: Arc<Vec<String>>,
#[serde(default)]
pub weight: f64,
}


pub fn credits_window(track_title: &str) -> WindowDesc<AppState> {
let win_size = (theme::grid(50.0), theme::grid(55.0));
Expand Down Expand Up @@ -36,13 +76,8 @@ fn credits_widget() -> impl Widget<AppState> {
),
)
.with_child(List::new(role_credit_widget).lens(AppState::credits.map(
|credits| {
credits
.as_ref()
.map(|c| c.role_credits.clone())
.unwrap_or_default()
},
|_, _| {},
|credits: &Option<TrackCredits>| credits.as_ref().map(|c| c.role_credits.clone()).unwrap_or_default(),
|_, _| ()
)))
.with_child(
Label::new(|data: &AppState, _: &_| {
Expand Down
2 changes: 1 addition & 1 deletion psst-gui/src/ui/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::data::config::SortCriteria;
use crate::data::Track;
use crate::error::Error;
use crate::webapi::TrackCredits;
use crate::{
cmd,
controller::{
Expand All @@ -16,6 +15,7 @@ use crate::{
icons, icons::SvgIcon, Border, Empty, MyWidgetExt, Overlay, ThemeScope, ViewDispatcher,
},
};
use credits::TrackCredits;
use druid::{
im::Vector,
widget::{CrossAxisAlignment, Either, Flex, Label, List, Scroll, Slider, Split, ViewSwitcher},
Expand Down
63 changes: 9 additions & 54 deletions psst-gui/src/webapi/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::{
use druid::{
im::Vector,
image::{self, ImageFormat},
Data, ImageBuf, Lens,
Data, ImageBuf,
};

use itertools::Itertools;
Expand All @@ -34,48 +34,10 @@ use crate::{
Playlist, PublicUser, Range, Recommendations, RecommendationsRequest, SearchResults,
SearchTopic, Show, SpotifyUrl, Track, TrackLines, UserProfile,
},
error::Error,
error::Error, ui::credits::TrackCredits,
};

use super::{cache::WebApiCache, local::LocalTrackManager};

#[derive(Debug, Clone, Data, Lens, Deserialize)]
pub struct TrackCredits {
#[serde(rename = "trackUri")]
pub track_uri: String,
#[serde(rename = "trackTitle")]
pub track_title: String,
#[serde(rename = "roleCredits")]
pub role_credits: Arc<Vec<RoleCredit>>,
#[serde(rename = "extendedCredits")]
pub extended_credits: Arc<Vec<String>>,
#[serde(rename = "sourceNames")]
pub source_names: Arc<Vec<String>>,
}

#[derive(Debug, Clone, Data, Lens, Deserialize)]
pub struct RoleCredit {
#[serde(rename = "roleTitle")]
pub role_title: String,
pub artists: Arc<Vec<CreditArtist>>,
}

#[derive(Debug, Clone, Data, Lens, Deserialize)]
pub struct CreditArtist {
pub uri: Option<String>,
pub name: String,
#[serde(rename = "imageUri")]
pub image_uri: Option<String>,
#[serde(rename = "externalUrl")]
pub external_url: Option<String>,
#[serde(rename = "creatorUri")]
pub creator_uri: Option<String>,
#[serde(default)]
pub subroles: Arc<Vec<String>>,
#[serde(default)]
pub weight: f64,
}

pub struct WebApi {
session: SessionService,
agent: Agent,
Expand Down Expand Up @@ -323,20 +285,6 @@ impl WebApi {
}
}

pub fn get_track_credits(&self, track_id: &str) -> Result<TrackCredits, Error> {
let request = self
.get(
format!("track-credits-view/v0/experimental/{}/credits", track_id),
Some("spclient.wg.spotify.com"),
)?
.set(
"client-token",
self.session.client_token().unwrap_or_default(),
);
let response = Self::with_retry(|| Ok(request.clone().call()?))?;
Ok(serde_json::from_str(&response.into_string()?)?)
}

fn load_and_return_home_section(&self, request: Request) -> Result<MixedView, Error> {
#[derive(Deserialize)]
pub struct Welcome {
Expand Down Expand Up @@ -999,6 +947,13 @@ impl WebApi {
Ok(result)
}

pub fn get_track_credits(&self, track_id: &str) -> Result<TrackCredits, Error> {
let request = self
.get(format!("track-credits-view/v0/experimental/{}/credits", track_id),Some("spclient.wg.spotify.com"))?;
let result: TrackCredits = self.load(request)?;
Ok(result)
}

pub fn get_lyrics(&self, track_id: String) -> Result<Vector<TrackLines>, Error> {
#[derive(Default, Debug, Clone, PartialEq, Deserialize, Data)]
#[serde(rename_all = "camelCase")]
Expand Down
1 change: 0 additions & 1 deletion psst-gui/src/webapi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@ mod client;
mod local;

pub use client::WebApi;
pub use client::{CreditArtist, RoleCredit, TrackCredits};

0 comments on commit 5f31315

Please sign in to comment.