Skip to content

Commit

Permalink
Flatten Track structure (martpie#794)
Browse files Browse the repository at this point in the history
* Flatten Track structure
* Move path up in the Track struct
  • Loading branch information
martpie authored and igorer88 committed Oct 30, 2024
1 parent 4afb65e commit e208f29
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 55 deletions.
29 changes: 10 additions & 19 deletions src-tauri/src/libs/track.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,17 @@ use uuid::Uuid;
pub struct Track {
#[natural_id]
pub _id: String,
pub path: PathBuf, // must be unique
pub title: String,
pub album: String,
pub artists: Vec<String>,
pub genres: Vec<String>,
pub year: Option<u32>,
pub duration: u32,
pub track: NumberOf,
pub disk: NumberOf,
pub path: PathBuf,
}

#[derive(Debug, Clone, Serialize, Deserialize, TS)]
#[ts(export, export_to = "../../src/generated/typings/index.ts")]
pub struct NumberOf {
pub no: Option<u32>,
pub of: Option<u32>,
pub track_no: Option<u32>,
pub track_of: Option<u32>,
pub disk_no: Option<u32>,
pub disk_of: Option<u32>,
}

/**
Expand Down Expand Up @@ -68,6 +63,7 @@ pub fn get_track_from_file(path: &PathBuf) -> Option<Track> {

Some(Track {
_id: id,
path: path.to_owned(),
title: tag
.get_string(&ItemKey::TrackTitle)
.unwrap_or("Unknown")
Expand All @@ -83,15 +79,10 @@ pub fn get_track_from_file(path: &PathBuf) -> Option<Track> {
.collect(),
year: tag.year(),
duration: u32::try_from(tagged_file.properties().duration().as_secs()).unwrap_or(0),
track: NumberOf {
no: tag.track(),
of: tag.track_total(),
},
disk: NumberOf {
no: tag.disk(),
of: tag.disk_total(),
},
path: path.to_owned(),
track_no: tag.track(),
track_of: tag.track_total(),
disk_no: tag.disk(),
disk_of: tag.disk_total(),
})
}
Err(err) => {
Expand Down
6 changes: 2 additions & 4 deletions src/generated/typings/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ export type DefaultView = "Library" | "Playlists";

export type IPCEvent = { "Unknown": string } | "PlaybackPlay" | "PlaybackPause" | "PlaybackStop" | "PlaybackPlayPause" | "PlaybackPrevious" | "PlaybackNext" | "PlaybackStart" | "LibraryScanProgress" | "GoToLibrary" | "GoToPlaylists" | "GoToSettings" | "JumpToPlayingTrack";

export type NumberOf = { no: number | null, of: number | null, };

/** ----------------------------------------------------------------------------
* Playlist
* represent a playlist, that has a name and a list of tracks
Expand All @@ -30,5 +28,5 @@ export type SortOrder = "Asc" | "Dsc";
/**
* Track
* represent a single track, id and path should be unique
*/
export type Track = { _id: string, title: string, album: string, artists: Array<string>, genres: Array<string>, year: number, duration: number, track: NumberOf, disk: NumberOf, path: string, };
*/
export type Track = { _id: string, path: string, title: string, album: string, artists: Array<string>, genres: Array<string>, year: number | null, duration: number, track_no: number | null, track_of: number | null, disk_no: number | null, disk_of: number | null, };
42 changes: 15 additions & 27 deletions src/lib/__tests__/utils-library.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,29 @@ import {
const TEST_TRACKS_ALBUM: Array<Track> = [
{
_id: '0_1',
path: '/tmp/unknown.mp3',
album: 'Album',
artists: ['Artist'],
track: {
no: 2,
of: 2,
},
disk: {
no: null,
of: null,
},
track_no: 2,
track_of: 2,
disk_no: null,
disk_of: null,
duration: 42,
genres: ['pop', 'rock'],
path: '/tmp/unknown.mp3',
title: 'Song 1',
year: 2000,
},
{
_id: '0_2',
path: '/tmp/unknown_2.mp3',
album: 'Album',
artists: ['Artist'],
track: {
no: 1,
of: 2,
},
disk: {
no: null,
of: null,
},
track_no: 1,
track_of: 2,
disk_no: null,
disk_of: null,
duration: 60,
genres: ['pop', 'rock'],
path: '/tmp/unknown_2.mp3',
title: 'Song 2',
year: 2000,
},
Expand All @@ -49,19 +41,15 @@ const TEST_TRACKS_ALBUM: Array<Track> = [
const TEST_TRACKS_PODCAST: Array<Track> = [
{
_id: '1_1',
path: '/tmp/unknown_2.mp3',
album: 'Another',
artists: ['Another Artist'],
track: {
no: 1,
of: 1,
},
disk: {
no: null,
of: null,
},
track_no: 1,
track_of: 1,
disk_no: null,
disk_of: null,
duration: 3600,
genres: ['podcast'],
path: '/tmp/unknown_2.mp3',
title: 'Song 2',
year: 2000,
},
Expand Down
10 changes: 5 additions & 5 deletions src/lib/utils-library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,11 @@ export type SortConfig = Array<TrackKeys | IterateeFunction>;

// Declarations
const SORT_ORDERS: Record<SortBy, SortConfig> = {
Artist: [ARTIST, ALBUM, 'disk.no', 'track.no'],
Title: [TITLE, ARTIST, ALBUM, 'disk.no', 'track.no'],
Duration: ['duration', ARTIST, ALBUM, 'disk.no', 'track.no'],
Album: [ALBUM, ARTIST, 'disk.no', 'track.no'],
Genre: [GENRE, ARTIST, ALBUM, 'disk.no', 'track.no'],
Artist: [ARTIST, ALBUM, 'disk_no', 'track_no'],
Title: [TITLE, ARTIST, ALBUM, 'disk_no', 'track_no'],
Duration: ['duration', ARTIST, ALBUM, 'disk_no', 'track_no'],
Album: [ALBUM, ARTIST, 'disk_no', 'track_no'],
Genre: [GENRE, ARTIST, ALBUM, 'disk_no', 'track_no'],
};

export function getSortOrder(sortBy: SortBy): SortConfig {
Expand Down

0 comments on commit e208f29

Please sign in to comment.