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] [end]
Box { Box {
Button { Button {
sensitive: bind template.refreshing-albums inverted;
icon-name: "view-refresh"; icon-name: "view-refresh";
clicked => $on_refresh_albums_clicked() swapped;
} }
} }
} }

View file

@ -21,7 +21,7 @@ mod imp {
#[template_child] #[template_child]
pub(super) play_queue: TemplateChild<crate::ui::PlayQueue>, pub(super) play_queue: TemplateChild<crate::ui::PlayQueue>,
#[property(get, set, default = false)] #[property(get, set)]
can_click_shuffle_all: Cell<bool>, can_click_shuffle_all: Cell<bool>,
#[property(get, set, nullable)] #[property(get, set, nullable)]
@ -70,6 +70,9 @@ mod imp {
time_pos_notify_timeout: Cell<Option<glib::SourceId>>, time_pos_notify_timeout: Cell<Option<glib::SourceId>>,
mpris_player: RefCell<Option<InterfaceRef<mpris::Player>>>, mpris_player: RefCell<Option<InterfaceRef<mpris::Player>>>,
#[property(get, set)]
refreshing_albums: Cell<bool>,
} }
impl Default for Window { impl Default for Window {
@ -126,6 +129,7 @@ mod imp {
time_pos_notify_timeout: Default::default(), time_pos_notify_timeout: Default::default(),
mpris_player: Default::default(), mpris_player: Default::default(),
refreshing_albums: Cell::new(true),
} }
} }
} }
@ -582,6 +586,39 @@ mod imp {
// make sure the seekbar looks full // make sure the seekbar looks full
self.obj().notify("time-pos"); 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 { impl Drop for Window {
@ -664,23 +701,8 @@ impl Window {
self.set_can_click_shuffle_all(true); self.set_can_click_shuffle_all(true);
let api = Rc::clone(self.imp().api.borrow().as_ref().unwrap()); let window = self.clone();
let albums_model = self.albums_model().clone(); glib::spawn_future_local(async move {window.imp().on_refresh_albums_clicked().await});
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(),
);
}
});
} }
pub fn playlist_next(&self) { pub fn playlist_next(&self) {