pub mod editing; use rfd::FileHandle; use rfd::AsyncFileDialog; use crate::load_song::load_song; use iced::Command; use crate::app::Message; pub use editing::Editing; // We allow a large enum variant here because this is only used in one place, so it's okay // to reserve the extra stack space for when FilePicker becomes Editing #[allow(clippy::large_enum_variant)] pub enum Model { FilePicker { tick: usize }, Editing(Editing), } const MAX_TICKS: usize = 900; impl Model { pub const DEFAULT: Self = Self::FilePicker { tick: 0 }; pub fn update(&mut self, message: Message) -> Command { match self { Self::FilePicker { tick } => { match message { Message::Tick => { *tick = (*tick + 1) % MAX_TICKS; Command::none() }, Message::FileOpened(path) => { println!("File opened! {}", path.display()); match Editing::new(&path) { Ok((editing, cmd)) => { *self = Self::Editing(editing); cmd }, Err(e) => { eprintln!("yikeys! Problem opening doodad: {}", e); //TODO! Report the error on the file loader Command::none() } } }, Message::PromptForFile => { let show_dialog = AsyncFileDialog::new() .add_filter("Song Files", &["mp3", "flac", "ogg", "opus", "wav", "mkv"]) .set_title("Select a song") .pick_file(); let to_message = |handle: Option| handle.map_or( Message::Null, |h| Message::FileOpened(h.path().to_owned()), ); Command::perform(show_dialog, to_message) }, _ => { Command::none() } } }, Self::Editing(model) => { model.update(message) }, } } }