use crate::{ app::App, event::Key, handlers::{KeyEventHandler, handle_clear_errors}, matches_key, models::{ lidarr_models::Artist, servarr_data::lidarr::lidarr_data::{ActiveLidarrBlock, LIBRARY_BLOCKS}, stateful_table::SortOption, }, }; use super::handle_change_tab_left_right_keys; use crate::handlers::table_handler::{TableHandlingConfig, handle_table}; #[cfg(test)] #[path = "library_handler_tests.rs"] mod library_handler_tests; pub(super) struct LibraryHandler<'a, 'b> { key: Key, app: &'a mut App<'b>, active_lidarr_block: ActiveLidarrBlock, _context: Option, } impl<'a, 'b> KeyEventHandler<'a, 'b, ActiveLidarrBlock> for LibraryHandler<'a, 'b> { fn handle(&mut self) { let artists_table_handling_config = TableHandlingConfig::new(ActiveLidarrBlock::Artists.into()) .sorting_block(ActiveLidarrBlock::ArtistsSortPrompt.into()) .sort_options(artists_sorting_options()) .searching_block(ActiveLidarrBlock::SearchArtists.into()) .search_error_block(ActiveLidarrBlock::SearchArtistsError.into()) .search_field_fn(|artist| &artist.artist_name.text) .filtering_block(ActiveLidarrBlock::FilterArtists.into()) .filter_error_block(ActiveLidarrBlock::FilterArtistsError.into()) .filter_field_fn(|artist| &artist.artist_name.text); if !handle_table( self, |app| &mut app.data.lidarr_data.artists, artists_table_handling_config, ) { self.handle_key_event(); } } fn accepts(active_block: ActiveLidarrBlock) -> bool { LIBRARY_BLOCKS.contains(&active_block) } fn ignore_special_keys(&self) -> bool { self.app.ignore_special_keys_for_textbox_input } fn new( key: Key, app: &'a mut App<'b>, active_block: ActiveLidarrBlock, context: Option, ) -> LibraryHandler<'a, 'b> { LibraryHandler { key, app, active_lidarr_block: active_block, _context: context, } } fn get_key(&self) -> Key { self.key } fn is_ready(&self) -> bool { !self.app.is_loading && !self.app.data.lidarr_data.artists.is_empty() } fn handle_scroll_up(&mut self) {} fn handle_scroll_down(&mut self) {} fn handle_home(&mut self) {} fn handle_end(&mut self) {} fn handle_delete(&mut self) {} fn handle_left_right_action(&mut self) { if self.active_lidarr_block == ActiveLidarrBlock::Artists { handle_change_tab_left_right_keys(self.app, self.key); } } fn handle_submit(&mut self) {} fn handle_esc(&mut self) { handle_clear_errors(self.app); } fn handle_char_key_event(&mut self) { let key = self.key; if self.active_lidarr_block == ActiveLidarrBlock::Artists && matches_key!(refresh, key) { self.app.should_refresh = true; } } fn app_mut(&mut self) -> &mut App<'b> { self.app } fn current_route(&self) -> crate::models::Route { self.app.get_current_route() } } fn artists_sorting_options() -> Vec> { vec![ SortOption { name: "Name", cmp_fn: Some(|a, b| { a.artist_name .text .to_lowercase() .cmp(&b.artist_name.text.to_lowercase()) }), }, SortOption { name: "Type", cmp_fn: Some(|a, b| { a.artist_type .as_ref() .unwrap_or(&String::new()) .to_lowercase() .cmp( &b.artist_type .as_ref() .unwrap_or(&String::new()) .to_lowercase(), ) }), }, SortOption { name: "Status", cmp_fn: Some(|a, b| { a.status .to_string() .to_lowercase() .cmp(&b.status.to_string().to_lowercase()) }), }, SortOption { name: "Quality Profile", cmp_fn: Some(|a, b| a.quality_profile_id.cmp(&b.quality_profile_id)), }, SortOption { name: "Metadata Profile", cmp_fn: Some(|a, b| a.metadata_profile_id.cmp(&b.metadata_profile_id)), }, SortOption { name: "Albums", cmp_fn: Some(|a, b| { a.statistics .as_ref() .map_or(0, |stats| stats.album_count) .cmp(&b.statistics.as_ref().map_or(0, |stats| stats.album_count)) }), }, SortOption { name: "Tracks", cmp_fn: Some(|a, b| { a.statistics .as_ref() .map_or(0, |stats| stats.track_count) .cmp(&b.statistics.as_ref().map_or(0, |stats| stats.track_count)) }), }, SortOption { name: "Size", cmp_fn: Some(|a, b| { a.statistics .as_ref() .map_or(0, |stats| stats.size_on_disk) .cmp(&b.statistics.as_ref().map_or(0, |stats| stats.size_on_disk)) }), }, SortOption { name: "Monitored", cmp_fn: Some(|a, b| a.monitored.cmp(&b.monitored)), }, SortOption { name: "Tags", cmp_fn: Some(|a, b| { let a_str = a .tags .iter() .map(|tag| tag.as_i64().unwrap().to_string()) .collect::>() .join(","); let b_str = b .tags .iter() .map(|tag| tag.as_i64().unwrap().to_string()) .collect::>() .join(","); a_str.cmp(&b_str) }), }, ] }