Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
kamiyaa committed Sep 2, 2024
1 parent c40e212 commit 4fbd62b
Show file tree
Hide file tree
Showing 21 changed files with 281 additions and 377 deletions.
16 changes: 8 additions & 8 deletions src/bin/client/commands/cursor_move.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,26 +72,26 @@ fn set_curr_dirlist_index(context: &mut AppContext, new_index: usize) {
pub fn get_playlist_index(context: &AppContext) -> Option<usize> {
context
.server_state_ref()
.player_ref()
.playlist_ref()
.player
.playlist
.get_cursor_index()
}
fn get_playlist_len(context: &AppContext) -> usize {
context.server_state_ref().player_ref().playlist_ref().len()
context.server_state_ref().player.playlist.len()
}
pub fn set_playlist_index(context: &mut AppContext, new_index: usize) {
let playlist_len = context.server_state_ref().player_ref().playlist_ref().len();
let playlist_len = context.server_state_ref().player.playlist.len();
if playlist_len <= new_index {
context
.server_state_mut()
.player_mut()
.playlist_mut()
.player
.playlist
.set_cursor_index(Some(safe_subtract(playlist_len, 1)));
} else {
context
.server_state_mut()
.player_mut()
.playlist_mut()
.player
.playlist
.set_cursor_index(Some(new_index));
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/bin/client/commands/goto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::config::option::WidgetType;
use crate::context::AppContext;

fn _directory_goto_playing(context: &mut AppContext) -> DiziResult {
let player_state = context.server_state_ref().player_ref();
let player_state = &context.server_state_ref().player;

if let Some(song) = player_state.song.clone() {
let file_path = song.file_path();
Expand All @@ -22,7 +22,7 @@ fn _directory_goto_playing(context: &mut AppContext) -> DiziResult {
}

fn _playlist_goto_playing(context: &mut AppContext) -> DiziResult {
let player_state = context.server_state_ref().player_ref();
let player_state = &context.server_state_ref().player;

match player_state.playlist_status {
PlaylistType::DirectoryListing => {
Expand Down
4 changes: 2 additions & 2 deletions src/bin/client/commands/open_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ pub fn file_browser_open(context: &mut AppContext) -> DiziResult {
pub fn playlist_open(context: &mut AppContext) -> DiziResult {
if let Some(index) = context
.server_state_ref()
.player_ref()
.playlist_ref()
.player
.playlist
.get_cursor_index()
{
let request = ClientRequest::PlaylistPlay { index: Some(index) };
Expand Down
4 changes: 2 additions & 2 deletions src/bin/client/commands/search_skim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ fn search_playlist_skim(

let items: Vec<DiziSkimItem> = context
.server_state_ref()
.player_ref()
.playlist_ref()
.player
.playlist
.list_ref()
.iter()
.enumerate()
Expand Down
14 changes: 1 addition & 13 deletions src/bin/client/context/server_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use dizi::player::PlayerState;

#[derive(Clone, Debug)]
pub struct ServerState {
player: PlayerState,
pub player: PlayerState,
}

impl ServerState {
Expand All @@ -11,16 +11,4 @@ impl ServerState {
player: PlayerState::new(),
}
}

pub fn set_player(&mut self, player: PlayerState) {
self.player = player;
}

pub fn player_ref(&self) -> &PlayerState {
&self.player
}

pub fn player_mut(&mut self) -> &mut PlayerState {
&mut self.player
}
}
111 changes: 47 additions & 64 deletions src/bin/client/event/process_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ pub fn process_server_event(context: &mut AppContext, s: &str) -> DiziResult {
ServerBroadcastEvent::ServerQuery { .. } => {}
ServerBroadcastEvent::ServerQueryAll { .. } => {}
ServerBroadcastEvent::PlayerState { mut state } => {
if !state.playlist_ref().is_empty() {
let old_state = context.server_state_ref().player_ref();
if !state.playlist.is_empty() {
let old_state = &context.server_state_ref().player;

let playlist_len = state.playlist_ref().len();
let playlist_len = state.playlist.len();
let new_cursor_index = old_state
.playlist_ref()
.playlist
.get_cursor_index()
.map(|s| {
if s < playlist_len {
Expand All @@ -84,19 +84,17 @@ pub fn process_server_event(context: &mut AppContext, s: &str) -> DiziResult {
}
})
.unwrap_or_else(|| 0);
state
.playlist_mut()
.set_cursor_index(Some(new_cursor_index));
state.playlist.set_cursor_index(Some(new_cursor_index));
}
context.server_state_mut().set_player(state);
context.server_state_mut().player = state;
}
ServerBroadcastEvent::PlaylistOpen { mut state } => {
if !state.playlist_ref().is_empty() {
let old_state = context.server_state_ref().player_ref();
if !state.playlist.is_empty() {
let old_state = &context.server_state_ref().player;

let playlist_len = state.playlist_ref().len();
let playlist_len = state.playlist.len();
let new_cursor_index = old_state
.playlist_ref()
.playlist
.get_cursor_index()
.map(|s| {
if s < playlist_len {
Expand All @@ -106,77 +104,60 @@ pub fn process_server_event(context: &mut AppContext, s: &str) -> DiziResult {
}
})
.unwrap_or_else(|| 0);
state
.playlist_mut()
.set_cursor_index(Some(new_cursor_index));
state.playlist.set_cursor_index(Some(new_cursor_index));
}
context.server_state_mut().set_player(state);
let len = context.server_state_ref().player_ref().playlist_ref().len();
context.server_state_mut().player = state;
let len = context.server_state_ref().player.playlist.len();
context
.message_queue_mut()
.push_success(format!("Loaded {} songs to playlist", len));
}
ServerBroadcastEvent::PlayerFilePlay { file: song } => {
context.server_state_mut().player_mut().set_song(Some(song));
context
.server_state_mut()
.player_mut()
.set_player_status(PlayerStatus::Playing);
context
.server_state_mut()
.player_mut()
.set_playlist_status(PlaylistType::DirectoryListing);
context.server_state_mut().player.song = Some(song);
context.server_state_mut().player.status = PlayerStatus::Playing;
context.server_state_mut().player.playlist_status = PlaylistType::DirectoryListing;
}
ServerBroadcastEvent::PlayerPause => {
context
.server_state_mut()
.player_mut()
.set_player_status(PlayerStatus::Paused);
context.server_state_mut().player.status = PlayerStatus::Paused;
}
ServerBroadcastEvent::PlayerResume => {
context
.server_state_mut()
.player_mut()
.set_player_status(PlayerStatus::Playing);
context.server_state_mut().player.status = PlayerStatus::Playing;
}
ServerBroadcastEvent::PlayerStop => {
context
.server_state_mut()
.player_mut()
.set_player_status(PlayerStatus::Stopped);
context.server_state_mut().player.status = PlayerStatus::Stopped;
}
ServerBroadcastEvent::PlayerShuffle { on } => {
context.server_state_mut().player_mut().set_shuffle(on);
context.server_state_mut().player.shuffle = on;
let setting = "Shuffle";
let status = if on { "ON" } else { "OFF" };
context
.message_queue_mut()
.push_success(format!("{} {}", setting, status));
}
ServerBroadcastEvent::PlayerRepeat { on } => {
context.server_state_mut().player_mut().set_repeat(on);
context.server_state_mut().player.repeat = on;
let setting = "Repeat";
let status = if on { "ON" } else { "OFF" };
context
.message_queue_mut()
.push_success(format!("{} {}", setting, status));
}
ServerBroadcastEvent::PlayerNext { on } => {
context.server_state_mut().player_mut().set_next(on);
context.server_state_mut().player.next = on;
let setting = "Next";
let status = if on { "ON" } else { "OFF" };
context
.message_queue_mut()
.push_success(format!("{} {}", setting, status));
}
ServerBroadcastEvent::PlayerVolumeUpdate { volume } => {
context.server_state_mut().player_mut().set_volume(volume);
context.server_state_mut().player.volume = volume;
}
ServerBroadcastEvent::PlayerProgressUpdate { elapsed } => {
context.server_state_mut().player_mut().set_elapsed(elapsed);
context.server_state_mut().player.elapsed = elapsed;
}
ServerBroadcastEvent::PlaylistSwapMove { index1, index2 } => {
let playlist = context.server_state_mut().player_mut().playlist_mut();
let playlist = &mut context.server_state_mut().player.playlist;
playlist.list_mut().swap(index1, index2);
playlist.set_cursor_index(Some(index2));
if let Some(playing_index) = playlist.get_playing_index() {
Expand All @@ -189,13 +170,12 @@ pub fn process_server_event(context: &mut AppContext, s: &str) -> DiziResult {
}
}
ServerBroadcastEvent::PlaylistClear => {
let playlist = context.server_state_mut().player_mut().playlist_mut();
let len = playlist.len();
playlist.clear();
let playlist_len = context.server_state_mut().player.playlist.len();
context.server_state_mut().player.playlist.clear();
context.set_view_widget(WidgetType::FileBrowser);
context
.message_queue_mut()
.push_success(format!("Removed {} songs from playlist", len));
.push_success(format!("Removed {playlist_len} songs from playlist"));
}
ServerBroadcastEvent::PlaylistAppend { audio_files } => {
let len = audio_files.len();
Expand All @@ -205,43 +185,46 @@ pub fn process_server_event(context: &mut AppContext, s: &str) -> DiziResult {
.collect();
context
.server_state_mut()
.player_mut()
.playlist_mut()
.player
.playlist
.list_mut()
.extend_from_slice(&entries);
if context
.server_state_ref()
.player_ref()
.playlist_ref()
.player
.playlist
.get_cursor_index()
.is_none()
{
context
.server_state_mut()
.player_mut()
.playlist_mut()
.player
.playlist
.set_cursor_index(Some(0));
}
context
.message_queue_mut()
.push_success(format!("Added {len} songs to playlist"));
}
ServerBroadcastEvent::PlaylistRemove { index } => {
let playlist = context.server_state_mut().player_mut().playlist_mut();
playlist.remove_song(index);
context
.server_state_mut()
.player
.playlist
.remove_song(index);
}
ServerBroadcastEvent::PlaylistPlay { index } => {
let len = context.server_state_ref().player_ref().playlist_ref().len();
let len = context.server_state_ref().player.playlist.len();
if index < len {
let player = context.server_state_mut().player_mut();
player.set_player_status(PlayerStatus::Playing);
player.set_playlist_status(PlaylistType::PlaylistFile);
let cursor_index = player.playlist_ref().get_cursor_index();
let playing_index = player.playlist_ref().get_playing_index();
let player = &mut context.server_state_mut().player;
player.status = PlayerStatus::Playing;
player.playlist_status = PlaylistType::PlaylistFile;
let cursor_index = player.playlist.get_cursor_index();
let playing_index = player.playlist.get_playing_index();
if playing_index == cursor_index {
player.playlist_mut().set_cursor_index(Some(index));
player.playlist.set_cursor_index(Some(index));
}
player.playlist_mut().set_playing_index(Some(index));
player.playlist.set_playing_index(Some(index));
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/bin/client/key_command/impl_appexecute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ pub fn execute_request(
}
}
ClientRequest::PlaylistPlay { index: None } => {
let playlist = context.server_state_ref().player_ref().playlist_ref();
let playlist = &context.server_state_ref().player.playlist;
if let Some(index) = playlist.get_cursor_index() {
let request = ClientRequest::PlaylistPlay { index: Some(index) };
send_client_request(context, &request)?;
Expand All @@ -144,7 +144,7 @@ pub fn execute_request(
if context.get_view_widget() != WidgetType::Playlist {
return Ok(());
}
let playlist = context.server_state_ref().player_ref().playlist_ref();
let playlist = &context.server_state_ref().player.playlist;
if let Some(index) = playlist.get_cursor_index() {
let request = ClientRequest::PlaylistRemove { index: Some(index) };
send_client_request(context, &request)?;
Expand All @@ -154,7 +154,7 @@ pub fn execute_request(
if context.get_view_widget() != WidgetType::Playlist {
return Ok(());
}
let playlist = context.server_state_ref().player_ref().playlist_ref();
let playlist = &context.server_state_ref().player.playlist;
if let Some(index) = playlist.get_cursor_index() {
let request = ClientRequest::PlaylistMoveUp { index: Some(index) };
send_client_request(context, &request)?;
Expand All @@ -164,7 +164,7 @@ pub fn execute_request(
if context.get_view_widget() != WidgetType::Playlist {
return Ok(());
}
let playlist = context.server_state_ref().player_ref().playlist_ref();
let playlist = &context.server_state_ref().player.playlist;
if let Some(index) = playlist.get_cursor_index() {
let request = ClientRequest::PlaylistMoveDown { index: Some(index) };
send_client_request(context, &request)?;
Expand Down
4 changes: 2 additions & 2 deletions src/bin/client/run/run_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ pub fn run_query(context: &mut AppContext, query: String) -> DiziResult {
break;
}
ServerBroadcastEvent::PlayerState { mut state } => {
if !state.playlist_ref().is_empty() {
state.playlist_mut().set_cursor_index(Some(0));
if !state.playlist.is_empty() {
state.playlist.set_cursor_index(Some(0));
}
let res = state.query(&query)?;
println!("{}", res);
Expand Down
4 changes: 2 additions & 2 deletions src/bin/client/run/run_query_all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ pub fn run_query_all(context: &mut AppContext) -> DiziResult {
break;
}
ServerBroadcastEvent::PlayerState { mut state } => {
if !state.playlist_ref().is_empty() {
state.playlist_mut().set_cursor_index(Some(0));
if !state.playlist.is_empty() {
state.playlist.set_cursor_index(Some(0));
}
let mut query_items = state.query_all();
let mut items_sorted: Vec<(String, String)> = query_items.drain().collect();
Expand Down
2 changes: 1 addition & 1 deletion src/bin/client/ui/views/tui_folder_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl<'a> Widget for TuiFolderView<'a> {

let config = self.context.config_ref();
let display_options = config.display_options_ref();
let currently_playing = self.context.server_state_ref().player_ref().song.as_ref();
let currently_playing = self.context.server_state_ref().player.song.as_ref();

// render current view
if let Some(list) = curr_list.as_ref() {
Expand Down
Loading

0 comments on commit 4fbd62b

Please sign in to comment.