Skip to content

Commit

Permalink
Rewrite application state's data model (#19)
Browse files Browse the repository at this point in the history
## Brief description of changes

- add `state/model.rs` module to application's Spotify model structures
- separate the application's state and data
  + Previously, application data such as caches, user data, etc are stored inside the player state.
  + Now, `state::data::AppData` is used to store those the application's data.
- add some cleanups, documentation/naming changes, and code refactors while making changes for the new data/state structure
  • Loading branch information
aome510 authored Oct 26, 2021
1 parent 1ac3068 commit f4139a4
Show file tree
Hide file tree
Showing 16 changed files with 1,071 additions and 987 deletions.
327 changes: 130 additions & 197 deletions spotify_player/src/client/mod.rs

Large diffs are not rendered by default.

31 changes: 12 additions & 19 deletions spotify_player/src/event/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ fn handle_key_event(
&mut ui,
)?
}
PageState::Searching(..) => window::handle_key_sequence_for_search_window(
PageState::Searching { .. } => window::handle_key_sequence_for_search_window(
&key_sequence,
send,
state,
Expand Down Expand Up @@ -211,27 +211,26 @@ fn handle_global_command(
}
}
Command::OpenCommandHelp => {
ui.popup = Some(PopupState::CommandHelp(0));
ui.popup = Some(PopupState::CommandHelp { offset: 0 });
}
Command::RefreshPlayback => {
send.send(ClientRequest::GetCurrentPlayback)?;
}
Command::ShowActionsOnCurrentTrack => {
if let Some(track) = state.player.read().unwrap().current_playing_track() {
ui.popup = Some(PopupState::ActionList(
Item::Track(track.clone().into()),
new_list_state(),
));
let item = Item::Track(track.clone().into());
let actions = item.actions();
ui.popup = Some(PopupState::ActionList(item, actions, new_list_state()));
}
}
Command::BrowsePlayingContext => {
ui.new_page(PageState::CurrentPlaying);
ui.create_new_page(PageState::CurrentPlaying);
}
Command::BrowseUserPlaylists => {
send.send(ClientRequest::GetUserPlaylists)?;
ui.popup = Some(PopupState::UserPlaylistList(
PlaylistPopupAction::Browse,
state.player.read().unwrap().user_playlists.to_vec(),
state.data.read().unwrap().user_data.playlists.to_vec(),
new_list_state(),
));
}
Expand All @@ -244,17 +243,11 @@ fn handle_global_command(
ui.popup = Some(PopupState::UserSavedAlbumList(new_list_state()));
}
Command::SearchPage => {
ui.new_page(PageState::Searching(
"".to_owned(),
Box::new(SearchResults::default()),
));
ui.window = WindowState::Search(
new_list_state(),
new_list_state(),
new_list_state(),
new_list_state(),
SearchFocusState::Input,
);
ui.create_new_page(PageState::Searching {
input: "".to_owned(),
current_query: "".to_owned(),
});
ui.window = WindowState::new_search_state();
}
Command::PreviousPage => {
if ui.history.len() > 1 {
Expand Down
62 changes: 37 additions & 25 deletions spotify_player/src/event/popup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub fn handle_key_sequence_for_popup(
ui: &mut UIStateGuard,
) -> Result<bool> {
match ui.popup.as_ref().unwrap() {
PopupState::ContextSearch(_) => {
PopupState::Search { .. } => {
handle_key_sequence_for_search_popup(key_sequence, send, state, ui)
}
PopupState::ArtistList(..) => handle_key_sequence_for_list_popup(
Expand All @@ -30,7 +30,7 @@ pub fn handle_key_sequence_for_popup(

let context_id = ContextId::Artist(artists[id].id.clone());
send.send(ClientRequest::GetContext(context_id.clone()))?;
ui.new_page(PageState::Browsing(context_id));
ui.create_new_page(PageState::Browsing(context_id));

Ok(())
},
Expand Down Expand Up @@ -79,10 +79,11 @@ pub fn handle_key_sequence_for_popup(
// when adding a new track to a playlist, we need to remove
// the cache for that playlist
state
.player
.data
.write()
.unwrap()
.context_cache
.caches
.context
.pop(&playlists[id].id.uri());

send.send(ClientRequest::AddTrackToPlaylist(
Expand All @@ -100,9 +101,12 @@ pub fn handle_key_sequence_for_popup(
}
}
PopupState::UserFollowedArtistList(_) => {
let player = state.player.read().unwrap();
let artist_uris = player
.user_followed_artists
let artist_uris = state
.data
.read()
.unwrap()
.user_data
.followed_artists
.iter()
.map(|a| a.id.uri())
.collect::<Vec<_>>();
Expand All @@ -117,9 +121,12 @@ pub fn handle_key_sequence_for_popup(
)
}
PopupState::UserSavedAlbumList(_) => {
let player = state.player.read().unwrap();
let album_uris = player
.user_saved_albums
let album_uris = state
.data
.read()
.unwrap()
.user_data
.saved_albums
.iter()
.map(|a| a.id.uri())
.collect::<Vec<_>>();
Expand Down Expand Up @@ -181,10 +188,10 @@ pub fn handle_key_sequence_for_popup(
},
)
}
PopupState::CommandHelp(_) => {
PopupState::CommandHelp { .. } => {
handle_key_sequence_for_command_help_popup(key_sequence, state, ui)
}
PopupState::ActionList(ref item, _) => {
PopupState::ActionList(item, ..) => {
handle_key_sequence_for_action_list_popup(item.actions(), key_sequence, send, state, ui)
}
}
Expand All @@ -198,7 +205,7 @@ fn handle_key_sequence_for_search_popup(
ui: &mut UIStateGuard,
) -> Result<bool> {
let query = match ui.popup {
Some(PopupState::ContextSearch(ref mut query)) => query,
Some(PopupState::Search { ref mut query }) => query,
_ => unreachable!(),
};
if key_sequence.keys.len() == 1 {
Expand Down Expand Up @@ -290,7 +297,7 @@ fn handle_key_sequence_for_context_browsing_list_popup(

send.send(ClientRequest::GetContext(context_id.clone()))?;

ui.new_page(PageState::Browsing(context_id));
ui.create_new_page(PageState::Browsing(context_id));

Ok(())
},
Expand Down Expand Up @@ -369,7 +376,7 @@ fn handle_key_sequence_for_command_help_popup(
};

let offset = match ui.popup {
Some(PopupState::CommandHelp(ref mut offset)) => offset,
Some(PopupState::CommandHelp { ref mut offset }) => offset,
_ => unreachable!(),
};
match command {
Expand Down Expand Up @@ -405,7 +412,7 @@ fn handle_key_sequence_for_action_list_popup(
|_, _| {},
|ui: &mut UIStateGuard, id: usize| -> Result<()> {
let item = match ui.popup {
Some(PopupState::ActionList(ref item, _)) => item,
Some(PopupState::ActionList(ref item, ..)) => item,
_ => unreachable!(),
};

Expand All @@ -416,7 +423,7 @@ fn handle_key_sequence_for_action_list_popup(
let uri = album.id.uri();
let context_id = ContextId::Album(AlbumId::from_uri(&uri)?);
send.send(ClientRequest::GetContext(context_id.clone()))?;
ui.new_page(PageState::Browsing(context_id));
ui.create_new_page(PageState::Browsing(context_id));
}
}
Action::BrowseArtist => {
Expand All @@ -426,10 +433,11 @@ fn handle_key_sequence_for_action_list_popup(
));
}
Action::AddTrackToPlaylist => {
let player = state.player.read().unwrap();
if let Some(ref user) = player.user {
let playlists = player
.user_playlists
let data = state.data.read().unwrap();
if let Some(ref user) = data.user_data.user {
let playlists = data
.user_data
.playlists
.iter()
.filter(|p| p.owner.1 == user.id)
.cloned()
Expand All @@ -449,8 +457,10 @@ fn handle_key_sequence_for_action_list_popup(
Action::BrowseRecommendations => {
let seed = SeedItem::Track(track.clone());
send.send(ClientRequest::GetRecommendations(seed.clone()))?;
ui.new_page(PageState::Recommendations(seed, None));
ui.window = WindowState::Recommendations(new_table_state());
ui.create_new_page(PageState::Recommendations(seed));
ui.window = WindowState::Recommendations {
track_table: new_table_state(),
};
}
},
Item::Album(album) => match actions[id] {
Expand All @@ -474,8 +484,10 @@ fn handle_key_sequence_for_action_list_popup(
Action::BrowseRecommendations => {
let seed = SeedItem::Artist(artist.clone());
send.send(ClientRequest::GetRecommendations(seed.clone()))?;
ui.new_page(PageState::Recommendations(seed, None));
ui.window = WindowState::Recommendations(new_table_state());
ui.create_new_page(PageState::Recommendations(seed));
ui.window = WindowState::Recommendations {
track_table: new_table_state(),
};
}
_ => {}
},
Expand Down
Loading

0 comments on commit f4139a4

Please sign in to comment.