Skip to content
Merged
Show file tree
Hide file tree
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
83 changes: 55 additions & 28 deletions spotify_player/src/client/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ use tracing::Instrument;

use crate::{event::ClientRequest, state::*};

#[cfg(feature = "lyric-finder")]
use crate::utils::map_join;

/// starts the client's request handler
pub async fn start_client_handler(
state: SharedState,
Expand Down Expand Up @@ -104,46 +107,70 @@ pub async fn start_player_event_watchers(
}

// update the context state and request new data when moving to a new context page
if let PageState::Context {
id,
context_page_type,
state: page_state,
} = state.ui.lock().current_page_mut()
{
let expected_id = match context_page_type {
ContextPageType::Browsing(context_id) => Some(context_id.clone()),
ContextPageType::CurrentPlaying => state.player.read().playing_context_id(),
};
match state.ui.lock().current_page_mut() {
PageState::Context {
id,
context_page_type,
state: page_state,
} => {
let expected_id = match context_page_type {
ContextPageType::Browsing(context_id) => Some(context_id.clone()),
ContextPageType::CurrentPlaying => state.player.read().playing_context_id(),
};

if *id != expected_id {
tracing::info!("Current context ID ({:?}) is different from the expected ID ({:?}), update the context state", id, expected_id);
if *id != expected_id {
tracing::info!("Current context ID ({:?}) is different from the expected ID ({:?}), update the context state", id, expected_id);

*id = expected_id;
*id = expected_id;

// update the UI page state based on the context's type
match id {
Some(id) => {
*page_state = Some(match id {
ContextId::Album(_) => ContextPageUIState::new_album(),
ContextId::Artist(_) => ContextPageUIState::new_artist(),
ContextId::Playlist(_) => ContextPageUIState::new_playlist(),
ContextId::Tracks(_) => ContextPageUIState::new_tracks(),
});
// update the UI page state based on the context's type
match id {
Some(id) => {
*page_state = Some(match id {
ContextId::Album(_) => ContextPageUIState::new_album(),
ContextId::Artist(_) => ContextPageUIState::new_artist(),
ContextId::Playlist(_) => ContextPageUIState::new_playlist(),
ContextId::Tracks(_) => ContextPageUIState::new_tracks(),
});
}
None => {
*page_state = None;
}
}
None => {
*page_state = None;

// request new context's data if not found in memory
if let Some(id) = id {
if !state.data.read().caches.context.contains(&id.uri()) {
client_pub
.send(ClientRequest::GetContext(id.clone()))
.unwrap_or_default();
}
}
}
}
#[cfg(feature = "lyric-finder")]
PageState::Lyric {
track,
artists,
scroll_offset,
} => {
if let Some(current_track) = state.player.read().current_playing_track() {
if current_track.name != *track {
tracing::info!("Current playing track \"{}\" is different from the track \"{track}\" shown up in the lyric page. Updating the track and fetching its lyric...", current_track.name);
*track = current_track.name.clone();
*artists = map_join(&current_track.artists, |a| &a.name, ", ");
*scroll_offset = 0;

// request new context's data if not found in memory
if let Some(id) = id {
if !state.data.read().caches.context.contains(&id.uri()) {
client_pub
.send(ClientRequest::GetContext(id.clone()))
.send(ClientRequest::GetLyric {
track: track.clone(),
artists: artists.clone(),
})
.unwrap_or_default();
}
}
}
_ => {}
}
}
}
20 changes: 16 additions & 4 deletions spotify_player/src/ui/popup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ const COMMAND_TABLE_CONSTRAINTS: [Constraint; 3] = [
/// renders a popup (if any) to handle a command or show additional information
/// depending on the current popup state.
///
/// The function returns a rectangle area to render the main layout
/// and a boolean `is_active` determining whether the focus is **not** placed on the popup.
/// The function returns a rectangle area to render the main layout and
/// a boolean value determining whether the focus should be placed in the main layout.
pub fn render_popup(
frame: &mut Frame,
state: &SharedState,
Expand All @@ -44,7 +44,13 @@ pub fn render_popup(
PopupState::CommandHelp { .. } => {
let chunks = Layout::default()
.direction(Direction::Vertical)
.constraints([Constraint::Length(8), Constraint::Min(0)].as_ref())
.constraints(
[
Constraint::Length((state.app_config.playback_window_width + 2) as u16),
Constraint::Min(0),
]
.as_ref(),
)
.split(rect);

render_commands_help_popup(frame, state, ui, chunks[1]);
Expand All @@ -53,7 +59,13 @@ pub fn render_popup(
PopupState::Queue { .. } => {
let chunks = Layout::default()
.direction(Direction::Vertical)
.constraints([Constraint::Length(8), Constraint::Min(0)].as_ref())
.constraints(
[
Constraint::Length((state.app_config.playback_window_width + 2) as u16),
Constraint::Min(0),
]
.as_ref(),
)
.split(rect);

render_queue_popup(frame, state, ui, chunks[1]);
Expand Down