clippy judgement 2024

This commit is contained in:
Erica Z 2024-11-07 20:42:45 +01:00
parent 5d3c22aaad
commit 1c4f694157
5 changed files with 141 additions and 64 deletions

74
src/model/album.rs Normal file
View file

@ -0,0 +1,74 @@
mod imp {
use adw::prelude::*;
use gtk::subclass::prelude::*;
use gtk::{gdk, glib};
use std::cell::{Cell, RefCell};
#[derive(glib::Properties, Default)]
#[properties(wrapper_type = super::Album)]
pub struct Album {
#[property(get, set)]
id: RefCell<String>,
#[property(get, set)]
name: RefCell<String>,
#[property(get, set)]
artist: RefCell<Option<String>>,
}
#[glib::object_subclass]
impl ObjectSubclass for Album {
const NAME: &'static str = "AudreyModelAlbum";
type Type = super::Album;
}
}
use crate::subsonic;
use adw::{prelude::*, subclass::prelude::*};
use glib::Object;
use gtk::{gdk, glib};
use std::rc::Rc;
glib::wrapper! {
pub struct Album(ObjectSubclass<imp::Album>);
}
impl Album {
pub fn from_api(
album: &subsonic::schema::Child,
load_thumbnail: bool,
) -> Self {
let album: Album = Object::builder()
.property("id", &album.id)
.property("title", &album.title)
.property("artist", &album.artist)
.property("album", &album.album)
.property("genre", &album.genre)
.property("duration", album.duration as i64)
.property("track", album.track.map(i64::from).unwrap_or(-1))
.property("stream-url", api.stream_url(&album.id).as_str())
.build();
if load_thumbnail {
let api = Rc::clone(&api);
let id = album.id();
let album_weak = album.downgrade();
album.imp()
.thumbnail_loading
.replace(Some(glib::spawn_future_local(async move {
let bytes = api
.cover_art(&id, Some(50)) // TODO: WidgetExt::scale_factor
.await
.unwrap();
let album = match album_weak.upgrade() {
None => return,
Some(album) => album,
};
album.set_thumbnail(
gdk::Texture::from_bytes(&glib::Bytes::from_owned(bytes)).unwrap(),
);
})));
}
album
}
}

View file

@ -53,7 +53,9 @@ mod imp {
impl Drop for Song {
fn drop(&mut self) {
self.thumbnail_loading.take().map(|handle| handle.abort());
if let Some(handle) = self.thumbnail_loading.take() {
handle.abort();
}
}
}
}
@ -86,7 +88,7 @@ impl Song {
.build();
if load_thumbnail {
let api = Rc::clone(&api);
let api = Rc::clone(api);
let id = song.id();
let song_weak = song.downgrade();
song.imp()

View file

@ -131,9 +131,9 @@ impl Player {
object_server.at("/org/mpris/MediaPlayer2", player).await?;
Ok(object_server
object_server
.interface::<_, Self>("/org/mpris/MediaPlayer2")
.await?)
.await
/*
playbin.connect_new_track(glib::clone!(
@ -339,7 +339,7 @@ impl Player {
#[zbus(property(emits_changed_signal = "false"))]
fn position(&self) -> i64 {
(self.window().time_pos() * MICROSECONDS as f64) as i64
(self.window().time_pos() * MICROSECONDS) as i64
}
#[zbus(property)]

View file

@ -36,10 +36,7 @@ impl Client {
_ => todo!("{type_:?}"),
}
.map(|response| match response.album_list2.album {
Some(albums) => albums,
None => vec![],
})
.map(|response| response.album_list2.album.unwrap_or_default())
}
pub fn album_list_full(
@ -50,7 +47,7 @@ impl Client {
let type_ = type_.clone();
async move {
match self.album_list(&type_, 500, offset as u32).await {
Ok(albums) if albums.len() == 0 => Ok(None),
Ok(albums) if albums.is_empty() => Ok(None),
Ok(albums) => {
let next_offset = offset + albums.len();
Ok(Some((

View file

@ -291,15 +291,11 @@ mod imp {
}
fn volume(&self) -> i64 {
self.mpv
.get_property::<i64>("volume")
.unwrap()
.try_into()
.unwrap()
self.mpv.get_property("volume").unwrap()
}
fn set_volume(&self, volume: i64) {
self.mpv.set_property("volume", volume as i64).unwrap();
self.mpv.set_property("volume", volume).unwrap();
}
fn mute(&self) -> bool {
@ -365,11 +361,7 @@ mod imp {
}
fn playlist_count(&self) -> i64 {
self.mpv
.get_property::<i64>("playlist-count")
.unwrap()
.try_into()
.unwrap()
self.mpv.get_property::<i64>("playlist-count").unwrap()
}
fn song(&self) -> Option<Song> {
@ -389,8 +381,7 @@ mod imp {
fn buffering_start(&self) {
let started_buffering = std::time::Instant::now();
let window = self.obj().downgrade();
self.buffering_timeout
.replace(Some(glib::timeout_add_local(
if let Some(source) = self.buffering_timeout.replace(Some(glib::timeout_add_local(
std::time::Duration::from_millis(100),
move || {
match window.upgrade() {
@ -405,12 +396,15 @@ mod imp {
}
}
},
)))
.map(|source| source.remove());
))) {
source.remove()
}
}
fn buffering_end(&self) {
self.buffering_timeout.take().map(|source| source.remove());
if let Some(source) = self.buffering_timeout.take() {
source.remove();
}
self.playbar.set_show_pulse_bar(false);
}
@ -459,7 +453,7 @@ mod imp {
assert_eq!(path, self.obj().song().unwrap().stream_url())
}
Err(err) if err.is_property_unavailable() => {}
Err(err) => Err(err).unwrap(),
Err(err) => panic!("{err:?}"),
}
}
@ -474,7 +468,8 @@ mod imp {
let window = self.obj().clone();
let song_id = window.song().unwrap().id();
self.loading_cover_handle
if let Some(handle) = self
.loading_cover_handle
.replace(Some(glib::spawn_future_local(async move {
let api = window.imp().api.borrow().as_ref().unwrap().clone();
let bytes = api
@ -496,7 +491,9 @@ mod imp {
}
}
})))
.map(|handle| handle.abort());
{
handle.abort();
}
// make sure this is reported as 0
self.obj().notify("time-pos");
@ -589,10 +586,12 @@ mod imp {
self.mpv_event_loop_handle.take().unwrap().abort();
self.zbus_executor_loop_handle.take().unwrap().abort();
self.time_pos_notify_timeout
.take()
.map(|source| source.remove());
self.buffering_timeout.take().map(|source| source.remove());
if let Some(source) = self.time_pos_notify_timeout.take() {
source.remove();
}
if let Some(source) = self.buffering_timeout.take() {
source.remove();
}
}
}
}
@ -664,7 +663,7 @@ impl Window {
let mut count = 0;
let mut albums =
std::pin::pin!(api.album_list_full(crate::subsonic::AlbumListType::Newest));
while let Some(_) = albums.try_next().await.unwrap() {
while albums.try_next().await.unwrap().is_some() {
count += 1;
}
println!("gathered {count} albums");
@ -705,7 +704,9 @@ impl Window {
.command(["playlist-move", &from.to_string(), &to.to_string()])
.unwrap();
if from < to {
use std::cmp::Ordering;
match from.cmp(&to) {
Ordering::Less => {
// F1234T -> 1234FT
let mut spliced = Vec::with_capacity((to - from) as usize);
for i in from + 1..to {
@ -715,7 +716,8 @@ impl Window {
self.playlist_model()
.splice(from, spliced.len() as u32, &spliced);
} else if to < from {
}
Ordering::Greater => {
// T1234F -> FT1234
let mut spliced = Vec::with_capacity((from - to + 1) as usize);
spliced.push(self.playlist_model().item(from).unwrap());
@ -727,5 +729,7 @@ impl Window {
self.playlist_model()
.splice(to, spliced.len() as u32, &spliced);
}
Ordering::Equal => {}
}
}
}