mirror of
https://github.com/johrpan/musicus.git
synced 2025-10-26 11:47:25 +01:00
Implement program parameters
This commit is contained in:
parent
815dede141
commit
1788303bf3
6 changed files with 108 additions and 42 deletions
|
|
@ -5,7 +5,13 @@ use std::{
|
|||
|
||||
use anyhow::Result;
|
||||
use chrono::prelude::*;
|
||||
use diesel::{dsl::exists, prelude::*, QueryDsl, SqliteConnection};
|
||||
use diesel::{
|
||||
dsl::{exists, sql},
|
||||
prelude::*,
|
||||
sql_query,
|
||||
sql_types::BigInt,
|
||||
QueryDsl, SqliteConnection,
|
||||
};
|
||||
use gtk::{glib, glib::Properties, prelude::*, subclass::prelude::*};
|
||||
|
||||
use crate::{
|
||||
|
|
@ -414,7 +420,59 @@ impl MusicusLibrary {
|
|||
query = query.filter(album_recordings::album_id.eq(album_id));
|
||||
}
|
||||
|
||||
// TODO: Implement prefer_recently_added and prefer_least_recently_played.
|
||||
if program.prefer_recently_added() > 0.0 {
|
||||
let oldest_timestamp = sql_query(
|
||||
"SELECT CAST(STRFTIME('%s', MIN(created_at)) AS INTEGER) AS value FROM recordings",
|
||||
)
|
||||
.get_result::<IntegerValue>(connection)?
|
||||
.value;
|
||||
|
||||
let newest_timestamp = sql_query(
|
||||
"SELECT CAST(STRFTIME('%s', MAX(created_at)) AS INTEGER) AS value FROM recordings",
|
||||
)
|
||||
.get_result::<IntegerValue>(connection)?
|
||||
.value;
|
||||
|
||||
let range = newest_timestamp - oldest_timestamp;
|
||||
|
||||
if range >= 60 {
|
||||
let proportion = program.prefer_recently_added().max(1.0) * 0.9;
|
||||
let cutoff_timestamp =
|
||||
oldest_timestamp + (proportion * range as f64).floor() as i64;
|
||||
|
||||
query = query.filter(
|
||||
sql::<BigInt>("CAST(STRFTIME('%s', recordings.created_at) AS INTEGER)")
|
||||
.ge(cutoff_timestamp)
|
||||
.or(recordings::last_played_at.is_null()),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if program.prefer_least_recently_played() > 0.0 {
|
||||
let oldest_timestamp =
|
||||
sql_query("SELECT CAST(STRFTIME('%s', MIN(last_played_at)) AS INTEGER) AS value FROM recordings")
|
||||
.get_result::<IntegerValue>(connection)?
|
||||
.value;
|
||||
|
||||
let newest_timestamp =
|
||||
sql_query("SELECT CAST(STRFTIME('%s', MAX(last_played_at)) AS INTEGER) AS value FROM recordings")
|
||||
.get_result::<IntegerValue>(connection)?
|
||||
.value;
|
||||
|
||||
let range = newest_timestamp - oldest_timestamp;
|
||||
|
||||
if range >= 60 {
|
||||
let proportion = 1.0 - program.prefer_least_recently_played().max(1.0) * 0.9;
|
||||
let cutoff_timestamp =
|
||||
oldest_timestamp + (proportion * range as f64).floor() as i64;
|
||||
|
||||
query = query.filter(
|
||||
sql::<BigInt>("CAST(STRFTIME('%s', recordings.last_played_at) AS INTEGER)")
|
||||
.le(cutoff_timestamp)
|
||||
.or(recordings::last_played_at.is_null()),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
let row = query
|
||||
.order(random())
|
||||
|
|
@ -424,6 +482,31 @@ impl MusicusLibrary {
|
|||
Recording::from_table(row, &self.folder(), connection)
|
||||
}
|
||||
|
||||
pub fn track_played(&self, track_id: &str) -> Result<()> {
|
||||
let mut binding = self.imp().connection.borrow_mut();
|
||||
let connection = &mut *binding.as_mut().unwrap();
|
||||
|
||||
let now = Local::now().naive_local();
|
||||
|
||||
diesel::update(recordings::table)
|
||||
.filter(exists(
|
||||
tracks::table.filter(
|
||||
tracks::track_id
|
||||
.eq(track_id)
|
||||
.and(tracks::recording_id.eq(recordings::recording_id)),
|
||||
),
|
||||
))
|
||||
.set(recordings::last_played_at.eq(now))
|
||||
.execute(connection)?;
|
||||
|
||||
diesel::update(tracks::table)
|
||||
.filter(tracks::track_id.eq(track_id))
|
||||
.set(tracks::last_played_at.eq(now))
|
||||
.execute(connection)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn search_persons(&self, search: &str) -> Result<Vec<Person>> {
|
||||
let search = format!("%{}%", search);
|
||||
let mut binding = self.imp().connection.borrow_mut();
|
||||
|
|
@ -555,3 +638,9 @@ impl LibraryResults {
|
|||
&& self.albums.is_empty()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(QueryableByName)]
|
||||
pub struct IntegerValue {
|
||||
#[diesel(sql_type = diesel::sql_types::BigInt)]
|
||||
pub value: i64,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -90,6 +90,13 @@ mod imp {
|
|||
|
||||
self.current_index.set(index);
|
||||
item.set_is_playing(true);
|
||||
|
||||
self.library
|
||||
.borrow()
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.track_played(&item.track_id())
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -228,6 +235,7 @@ impl MusicusPlayer {
|
|||
Some(&performances),
|
||||
None,
|
||||
&tracks[0].path,
|
||||
&tracks[0].track_id,
|
||||
));
|
||||
} else {
|
||||
let mut tracks = tracks.into_iter();
|
||||
|
|
@ -254,6 +262,7 @@ impl MusicusPlayer {
|
|||
Some(&performances),
|
||||
Some(&track_title(&first_track, 1)),
|
||||
&first_track.path,
|
||||
&first_track.track_id,
|
||||
));
|
||||
|
||||
for (index, track) in tracks.enumerate() {
|
||||
|
|
@ -264,6 +273,7 @@ impl MusicusPlayer {
|
|||
// track number = track index + 1 (first track) + 1 (zero based)
|
||||
Some(&track_title(&track, index + 2)),
|
||||
&track.path,
|
||||
&track.track_id,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -27,6 +27,9 @@ mod imp {
|
|||
|
||||
#[property(get, construct_only)]
|
||||
pub path: OnceCell<PathBuf>,
|
||||
|
||||
#[property(get, construct_only)]
|
||||
pub track_id: OnceCell<String>,
|
||||
}
|
||||
|
||||
#[glib::object_subclass]
|
||||
|
|
@ -50,6 +53,7 @@ impl PlaylistItem {
|
|||
performers: Option<&str>,
|
||||
part_title: Option<&str>,
|
||||
path: impl AsRef<Path>,
|
||||
track_id: &str,
|
||||
) -> Self {
|
||||
glib::Object::builder()
|
||||
.property("is-title", is_title)
|
||||
|
|
@ -57,6 +61,7 @@ impl PlaylistItem {
|
|||
.property("performers", performers)
|
||||
.property("part-title", part_title)
|
||||
.property("path", path.as_ref())
|
||||
.property("track-id", track_id)
|
||||
.build()
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue