Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Playlist pagination #1010

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
43 changes: 34 additions & 9 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,20 @@ impl<T> ScrollableResultPages<T> {
}
}

pub fn index(&self) -> usize {
self.index
}

pub fn next_page(&mut self) {
if self.index + 1 < self.pages.len() {
self.index += 1;
}
}

pub fn previous_page(&mut self) {
self.index -= 1;
}

pub fn get_results(&self, at_index: Option<usize>) -> Option<&T> {
self.pages.get(at_index.unwrap_or(self.index))
}
Expand All @@ -67,10 +81,17 @@ impl<T> ScrollableResultPages<T> {
}

pub fn add_pages(&mut self, new_pages: T) {
self.pages.push(new_pages);
// Whenever a new page is added, set the active index to the end of the vector
self.pages.push(new_pages);
self.index = self.pages.len() - 1;
}

pub fn set_page_at_index(&mut self, new_pages: T, at_index: usize) {
self.pages.push(new_pages);
self.pages.swap_remove(at_index);
//When a page is modified, the following pages must be invalidated
self.pages.truncate(at_index + 1);
}
}

#[derive(Default)]
Expand Down Expand Up @@ -284,11 +305,12 @@ pub struct App {
pub saved_show_ids_set: HashSet<String>,
pub large_search_limit: u32,
pub library: Library,
pub playlist_offset: u32,
pub playlist_track_offset: u32,
pub made_for_you_offset: u32,
pub playlist_tracks: Option<Page<PlaylistTrack>>,
pub made_for_you_tracks: Option<Page<PlaylistTrack>>,
pub playlists: Option<Page<SimplifiedPlaylist>>,
pub playlists: ScrollableResultPages<Page<SimplifiedPlaylist>>,
pub playlist_offset: u32,
pub recently_played: SpotifyResultAndSelectedIndex<Option<CursorBasedPage<PlayHistory>>>,
pub recommended_tracks: Vec<FullTrack>,
pub recommendations_seed: String,
Expand Down Expand Up @@ -339,6 +361,7 @@ impl Default for App {
artists: vec![],
artist: None,
user_config: UserConfig::new(),
playlist_offset: 0,
saved_album_tracks_index: 0,
recently_played: Default::default(),
size: Rect::default(),
Expand Down Expand Up @@ -367,11 +390,11 @@ impl Default for App {
input: vec![],
input_idx: 0,
input_cursor_position: 0,
playlist_offset: 0,
playlist_track_offset: 0,
made_for_you_offset: 0,
playlist_tracks: None,
made_for_you_tracks: None,
playlists: None,
playlists: ScrollableResultPages::new(),
recommended_tracks: vec![],
recommendations_context: None,
recommendations_seed: "".to_string(),
Expand Down Expand Up @@ -1022,10 +1045,12 @@ impl App {
}

pub fn user_unfollow_playlist(&mut self) {
if let (Some(playlists), Some(selected_index), Some(user)) =
(&self.playlists, self.selected_playlist_index, &self.user)
{
let selected_playlist = &playlists.items[selected_index];
if let (Some(selected_index), Some(user), Some(playlist_page)) = (
self.selected_playlist_index,
&self.user,
&self.playlists.get_results(None),
) {
let selected_playlist = &playlist_page.items[selected_index];
let selected_id = selected_playlist.id.clone();
let user_id = user.id.clone();
self.dispatch(IoEvent::UserUnfollowPlaylist(user_id, selected_id))
Expand Down
16 changes: 10 additions & 6 deletions src/cli/cli_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,14 @@ impl<'a> CliApp<'a> {
}
}
Type::Playlist => {
self.net.handle_network_event(IoEvent::GetPlaylists).await;
if let Some(playlists) = &self.net.app.lock().await.playlists {
playlists
//TODO:This will only return the playlists in the first page
self
.net
.handle_network_event(IoEvent::GetPlaylists(None))
.await;
let playlists = &self.net.app.lock().await.playlists;
match playlists.get_results(Some(0)) {
Some(playlists) => playlists
.items
.iter()
.map(|p| {
Expand All @@ -212,9 +217,8 @@ impl<'a> CliApp<'a> {
)
})
.collect::<Vec<String>>()
.join("\n")
} else {
"No playlists found".to_string()
.join("\n"),
None => "No playlist found".to_string(),
}
}
Type::Liked => {
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/made_for_you.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub fn handler(key: Key, app: &mut App) {
&app.made_for_you_index,
) {
app.track_table.context = Some(TrackTableContext::MadeForYou);
app.playlist_offset = 0;
app.playlist_track_offset = 0;
if let Some(selected_playlist) = playlists.items.get(selected_playlist_index.to_owned()) {
app.made_for_you_offset = 0;
let playlist_id = selected_playlist.id.to_owned();
Expand Down
84 changes: 53 additions & 31 deletions src/handlers/playlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,26 @@ use crate::network::IoEvent;
pub fn handler(key: Key, app: &mut App) {
match key {
k if common_key_events::right_event(k) => common_key_events::handle_right_event(app),
k if common_key_events::down_event(k) => {
match &app.playlists {
Some(p) => {
if let Some(selected_playlist_index) = app.selected_playlist_index {
let next_index =
common_key_events::on_down_press_handler(&p.items, Some(selected_playlist_index));
app.selected_playlist_index = Some(next_index);
}
}
None => {}
};
}
k if common_key_events::up_event(k) => {
match &app.playlists {
Some(p) => {
k if common_key_events::down_event(k) => match &app.playlists.get_results(None) {
Some(p) => {
if let Some(selected_playlist_index) = app.selected_playlist_index {
let next_index =
common_key_events::on_up_press_handler(&p.items, app.selected_playlist_index);
common_key_events::on_down_press_handler(&p.items, Some(selected_playlist_index));
app.selected_playlist_index = Some(next_index);
}
None => {}
};
}
}
None => {}
},
k if common_key_events::up_event(k) => match &app.playlists.get_results(None) {
Some(playlist_page) => {
let next_index =
common_key_events::on_up_press_handler(&playlist_page.items, app.selected_playlist_index);
app.selected_playlist_index = Some(next_index);
}
None => {}
},
k if common_key_events::high_event(k) => {
match &app.playlists {
match &app.playlists.get_results(None) {
Some(_p) => {
let next_index = common_key_events::on_high_press_handler();
app.selected_playlist_index = Some(next_index);
Expand All @@ -41,7 +37,7 @@ pub fn handler(key: Key, app: &mut App) {
};
}
k if common_key_events::middle_event(k) => {
match &app.playlists {
match &app.playlists.get_results(None) {
Some(p) => {
let next_index = common_key_events::on_middle_press_handler(&p.items);
app.selected_playlist_index = Some(next_index);
Expand All @@ -50,7 +46,7 @@ pub fn handler(key: Key, app: &mut App) {
};
}
k if common_key_events::low_event(k) => {
match &app.playlists {
match &app.playlists.get_results(None) {
Some(p) => {
let next_index = common_key_events::on_low_press_handler(&p.items);
app.selected_playlist_index = Some(next_index);
Expand All @@ -59,25 +55,51 @@ pub fn handler(key: Key, app: &mut App) {
};
}
Key::Enter => {
if let (Some(playlists), Some(selected_playlist_index)) =
(&app.playlists, &app.selected_playlist_index)
{
if let (Some(playlists), Some(selected_playlist_index)) = (
&app.playlists.get_results(None),
&app.selected_playlist_index,
) {
app.active_playlist_index = Some(selected_playlist_index.to_owned());
app.track_table.context = Some(TrackTableContext::MyPlaylists);
app.playlist_offset = 0;
app.playlist_track_offset = 0;
if let Some(selected_playlist) = playlists.items.get(selected_playlist_index.to_owned()) {
let playlist_id = selected_playlist.id.to_owned();
app.dispatch(IoEvent::GetPlaylistTracks(playlist_id, app.playlist_offset));
app.dispatch(IoEvent::GetPlaylistTracks(
playlist_id,
app.playlist_track_offset,
));
}
};
}

k if k == app.user_config.keys.previous_page => {
if app.playlists.index() > 0 {
app.playlists.previous_page();
app.selected_playlist_index = Some(0)
}
}
k if k == app.user_config.keys.next_page => {
match app.playlists.get_results(Some(app.playlists.index() + 1)) {
Some(_) => {
app.playlists.next_page();
app.selected_playlist_index = Some(0);
}
None => {
if let Some(saved_playlists) = &app.playlists.get_results(None) {
let offset = Some(saved_playlists.offset + saved_playlists.limit);
app.dispatch(IoEvent::GetPlaylists(offset));
}
}
}
}
Key::Char('D') => {
if let (Some(playlists), Some(selected_index)) = (&app.playlists, app.selected_playlist_index)
{
let selected_playlist = &playlists.items[selected_index].name;
if let (Some(playlists), Some(selected_index)) = (
&app.playlists.get_results(None),
&app.selected_playlist_index,
) {
let selected_playlist = &playlists.items[selected_index.to_owned()].name;
app.dialog = Some(selected_playlist.clone());
app.confirm = false;

app.push_navigation_stack(
RouteId::Dialog,
ActiveBlock::Dialog(DialogContext::PlaylistWindow),
Expand Down
4 changes: 2 additions & 2 deletions src/handlers/search_results.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ fn handle_enter_event_on_selected_block(app: &mut App) {
// Go to playlist tracks table
app.track_table.context = Some(TrackTableContext::PlaylistSearch);
let playlist_id = playlist.id.to_owned();
app.dispatch(IoEvent::GetPlaylistTracks(playlist_id, app.playlist_offset));
app.dispatch(IoEvent::GetPlaylistTracks(playlist_id, app.playlist_track_offset));
};
}
}
Expand Down Expand Up @@ -495,7 +495,7 @@ pub fn handler(key: Key, app: &mut App) {
Key::Enter => match app.search_results.selected_block {
SearchResultBlock::Empty => handle_enter_event_on_hovered_block(app),
SearchResultBlock::PlaylistSearch => {
app.playlist_offset = 0;
app.playlist_track_offset = 0;
handle_enter_event_on_selected_block(app);
}
_ => handle_enter_event_on_selected_block(app),
Expand Down
Loading