make refresh button in carousel work

This commit is contained in:
Erica Z 2024-12-03 21:59:47 +01:00
parent c13a9701a3
commit a63ca1c556
2 changed files with 23 additions and 2 deletions

View file

@ -26,6 +26,8 @@ template $AudreyUiAlbumCarousel: Adw.Bin {
Button {
valign: center;
icon-name: "view-refresh-symbolic";
sensitive: bind template.refresh-button-inactive inverted,
clicked => $on_refresh_clicked() swapped;
styles [
"circular",

View file

@ -36,6 +36,9 @@ mod imp {
model: OnceCell<gio::ListStore>,
r#type: Cell<Type>,
client: RefCell<Option<crate::wrappers::Client>>,
refresh_button_inactive: Cell<bool>,
update_handle: Cell<Option<glib::JoinHandle<()>>>,
}
#[glib::object_subclass]
@ -127,7 +130,7 @@ mod imp {
Type::Frequent => AlbumListType::Frequent,
};
glib::spawn_future_local(async move {
let handle = glib::spawn_future_local(async move {
let album_list = match api.album_list(&type_, 50, 0).await {
Ok(album_list) => album_list,
Err(err) => {
@ -136,7 +139,6 @@ mod imp {
}
};
// TODO: handle to cancel on drop etc etc
let carousel = carousel.upgrade().unwrap();
for album in album_list {
carousel
@ -145,6 +147,10 @@ mod imp {
.append(&crate::model::Album::from_api(&album));
}
});
if let Some(previous_handle) = self.update_handle.replace(Some(handle)) {
previous_handle.abort();
}
}
#[template_callback]
@ -165,6 +171,19 @@ mod imp {
let child: super::Album = item.child().and_downcast().unwrap();
child.unbind();
}
#[template_callback]
fn on_refresh_clicked(&self) {
self.update_model();
}
}
impl Drop for AlbumCarousel {
fn drop(&mut self) {
if let Some(handle) = self.update_handle.take() {
handle.abort();
}
}
}
}