diff --git a/resources/window.blp b/resources/window.blp index a708f66..9686a13 100644 --- a/resources/window.blp +++ b/resources/window.blp @@ -75,7 +75,9 @@ template $AudreyUiWindow: Adw.ApplicationWindow { [end] Box { Button { + sensitive: bind template.refreshing-albums inverted; icon-name: "view-refresh"; + clicked => $on_refresh_albums_clicked() swapped; } } } diff --git a/src/ui/window.rs b/src/ui/window.rs index cd5e837..046ca50 100644 --- a/src/ui/window.rs +++ b/src/ui/window.rs @@ -21,7 +21,7 @@ mod imp { #[template_child] pub(super) play_queue: TemplateChild, - #[property(get, set, default = false)] + #[property(get, set)] can_click_shuffle_all: Cell, #[property(get, set, nullable)] @@ -70,6 +70,9 @@ mod imp { time_pos_notify_timeout: Cell>, mpris_player: RefCell>>, + + #[property(get, set)] + refreshing_albums: Cell, } impl Default for Window { @@ -126,6 +129,7 @@ mod imp { time_pos_notify_timeout: Default::default(), mpris_player: Default::default(), + refreshing_albums: Cell::new(true), } } } @@ -582,6 +586,39 @@ mod imp { // make sure the seekbar looks full self.obj().notify("time-pos"); } + + #[template_callback] + pub(super) async fn on_refresh_albums_clicked(&self) { + self.obj().set_refreshing_albums(true); + self.albums_model.remove_all(); + + let api = Rc::clone(self.api.borrow().as_ref().unwrap()); + let albums_model = self.obj().albums_model().clone(); + let perform = async move { + use futures::TryStreamExt; + + let mut albums = + std::pin::pin!(api.album_list_full(crate::subsonic::AlbumListType::Newest)); + while let Some(album) = albums.try_next().await? { + albums_model.append( + &glib::Object::builder::() + .property("id", &album.id) + .property("name", &album.name) + .property("artist", &album.artist) + .build(), + ); + } + + Ok::<(), crate::subsonic::Error>(()) + }; + + match perform.await { + Ok(()) => {}, + Err(err) => event!(Level::ERROR, "could not refresh album list: {err}"), + } + + self.obj().set_refreshing_albums(false); + } } impl Drop for Window { @@ -664,23 +701,8 @@ impl Window { self.set_can_click_shuffle_all(true); - let api = Rc::clone(self.imp().api.borrow().as_ref().unwrap()); - let albums_model = self.albums_model().clone(); - glib::spawn_future_local(async move { - use futures::TryStreamExt; - - let mut albums = - std::pin::pin!(api.album_list_full(crate::subsonic::AlbumListType::Newest)); - while let Some(album) = albums.try_next().await.unwrap() { - albums_model.append( - &glib::Object::builder::() - .property("id", &album.id) - .property("name", &album.name) - .property("artist", &album.artist) - .build(), - ); - } - }); + let window = self.clone(); + glib::spawn_future_local(async move {window.imp().on_refresh_albums_clicked().await}); } pub fn playlist_next(&self) {