diff --git a/src/main.rs b/src/main.rs index be8f1f9..cbf73a8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -416,6 +416,21 @@ impl cosmic::Application for App { self.process_key_press(key, modifiers) } Message::SongEditor(message) => { + debug!(?message); + match message { + song_editor::Message::UpdateSong(ref song) => { + debug!(?song); + if let Some(library) = self.library.as_mut() { + match library.update_song(song.clone()) { + Ok(_) => { + debug!("upated song") + } + Err(e) => error!(?e), + } + }; + } + _ => (), + }; self.song_editor.update(message).map(|m| { debug!(?m); cosmic::app::Message::App(Message::None) @@ -435,7 +450,7 @@ impl cosmic::Application for App { } Message::Library(message) => { - debug!(?message); + // debug!(?message); let (mut kind, mut index): (LibraryKind, i32) = (LibraryKind::Song, 0); let mut opened_item = false; diff --git a/src/ui/library.rs b/src/ui/library.rs index 47d54da..b70acbd 100644 --- a/src/ui/library.rs +++ b/src/ui/library.rs @@ -9,6 +9,7 @@ use cosmic::{ }, Element, Task, }; +use miette::{miette, Result}; use tracing::debug; use crate::core::{ @@ -32,6 +33,7 @@ pub(crate) struct Library { selected_item: Option<(LibraryKind, i32)>, hovered_item: Option<(LibraryKind, i32)>, dragged_item: Option<(LibraryKind, i32)>, + editing_item: Option<(LibraryKind, i32)>, } #[derive(Clone, Debug)] @@ -62,6 +64,7 @@ impl Library { selected_item: None, hovered_item: None, dragged_item: None, + editing_item: None, } } @@ -337,6 +340,23 @@ impl Library { }) .into() } + + pub(crate) fn update_song(&mut self, song: Song) -> Result<()> { + let Some((kind, index)) = self.editing_item else { + return Err(miette!("Not editing an item")); + }; + + if kind != LibraryKind::Song { + return Err(miette!("Not editing a song item")); + } + + if let Some(_) = self.song_library.items.get(index as usize) { + self.song_library.update_item(song, index); + Ok(()) + } else { + Err(miette!("Song not found")) + } + } } fn elide_text(text: String, width: f32) -> String { diff --git a/src/ui/song_editor.rs b/src/ui/song_editor.rs index 7fc2050..b87a8c1 100644 --- a/src/ui/song_editor.rs +++ b/src/ui/song_editor.rs @@ -156,9 +156,9 @@ impl SongEditor { Task::none() } Message::ChangeTitle(title) => { - debug!(title); self.title = title.clone(); if let Some(mut song) = self.song.clone() { + debug!(title); song.title = title; self.update(Message::UpdateSong(song)) } else {