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 { impl Drop for Song {
fn drop(&mut self) { 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(); .build();
if load_thumbnail { if load_thumbnail {
let api = Rc::clone(&api); let api = Rc::clone(api);
let id = song.id(); let id = song.id();
let song_weak = song.downgrade(); let song_weak = song.downgrade();
song.imp() song.imp()

View file

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

View file

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

View file

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