use uuid::Uuid; use xmpd_cache::DlStatus; use xmpd_manifest::{song::Song, store::BaseStore}; use crate::{components::{left_nav::LeftNav, toast::ToastType, CompGetter, CompUi}, windows::WindowId}; use super::SongList; #[derive(Debug, Default)] pub struct Header { pub search_text: String, } component_register!(Header); impl CompUi for Header { fn draw(ui: &mut egui::Ui, state: &mut crate::GuiState) -> crate::Result<()> { let theme = xmpd_settings::Settings::get()?.theme.clone(); let pid = {LeftNav::get()?.selected_playlist_id.clone()}; ui.horizontal(|ui| { let search_icon = egui::Image::new(crate::data::SEARCH_ICON) .fit_to_exact_size(egui::Vec2::new(16.0, 16.0)) .tint(theme.accent_color); ui.add(search_icon); { ui.text_edit_singleline(&mut handle_error_ui!(Header::get()).search_text); } ui.with_layout(egui::Layout::right_to_left(egui::Align::RIGHT), |ui| { let download_all = ui.add( egui::Image::new(crate::data::DL_ICON) .tint(theme.accent_color) .sense(egui::Sense::click()) .fit_to_exact_size(egui::Vec2::new(16.0, 16.0)) ); let add_song = ui.add( egui::Image::new(crate::data::PLUS_ICON) .tint(theme.accent_color) .sense(egui::Sense::click()) .fit_to_exact_size(egui::Vec2::new(16.0, 16.0)) ); if download_all.clicked() { let songs: Vec<_>; match pid { Some(pid) => { songs = state.manifest.store().get_playlist(&pid).unwrap().songs().to_vec(); } None => { songs = state.manifest.store().get_songs().keys().cloned().collect(); } } for sid in handle_error_ui!(Self::get_songs_to_download(&songs)) { if let Some(song) = state.manifest.store().get_song(&sid) { handle_error_ui!(xmpd_cache::Cache::get()).download_song_to_cache(sid.clone(), song.clone()) } } let mut toast = handle_error_ui!(crate::components::toast::Toast::get()); toast.show_toast( "Downloading Songs", &format!("Started downloading {} songs", songs.len()), ToastType::Info ); } if add_song.clicked() { state.windows.toggle(&WindowId::AddSongToPl, true); } }); }); Ok(()) } } impl Header { fn get_songs_to_download(songs: &Vec) -> crate::Result> { let mut songs2 = Vec::new(); for sid in songs { if let None = xmpd_cache::Cache::get()?.get_cached_song_status(&sid) { songs2.push(sid.clone()); } } Ok(songs2) } }