refresh button

This commit is contained in:
Erica Z 2024-11-08 09:39:22 +01:00
parent f66262b95f
commit a810842e68
2 changed files with 42 additions and 18 deletions

View file

@ -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;
}
}
}

View file

@ -21,7 +21,7 @@ mod imp {
#[template_child]
pub(super) play_queue: TemplateChild<crate::ui::PlayQueue>,
#[property(get, set, default = false)]
#[property(get, set)]
can_click_shuffle_all: Cell<bool>,
#[property(get, set, nullable)]
@ -70,6 +70,9 @@ mod imp {
time_pos_notify_timeout: Cell<Option<glib::SourceId>>,
mpris_player: RefCell<Option<InterfaceRef<mpris::Player>>>,
#[property(get, set)]
refreshing_albums: Cell<bool>,
}
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::<crate::model::Album>()
.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::<crate::model::Album>()
.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) {