Something went wrong. Try again.
A music player that connects to your cloud/distributed storage. forked from tokono.ma/diffuse
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470module Tracks exposing (..)
import Base64import List.Extra as Listimport Maybe.Extra as Maybeimport Playlists exposing (Playlist, PlaylistTrackWithoutMetadata)import String.Ext as Stringimport Timeimport Time.Ext as Time
-- 🌳
type alias Track = { id : String , insertedAt : Time.Posix , path : String , sourceId : String , tags : Tags }
-- PIECES
type alias Tags = { disc : Int , nr : Int
-- Main , album : Maybe String , artist : Maybe String , title : String
-- Extra , genre : Maybe String , picture : Maybe String , year : Maybe Int }
-- DERIVATIVES & SUPPLEMENTS
type alias Cover = { group : String , identifiedTrackCover : IdentifiedTrack , key : String , sameAlbum : Bool , sameArtist : Bool , trackIds : List String , tracks : List IdentifiedTrack , variousArtists : Bool }
type alias Favourite = { artist : Maybe String , title : String }
type alias IdentifiedTrack = ( Identifiers, Track )
type alias Identifiers = { isFavourite : Bool , isMissing : Bool
-- , filename : String , group : Maybe { name : String, firstInGroup : Bool } , indexInList : Int , indexInPlaylist : Maybe Int , parentDirectory : String }
-- COLLECTIONS
type alias Collection = { untouched : List Track
-- `Track`s with `Identifiers` , identified : List IdentifiedTrack
-- Sorted, grouped and filtered by playlist (if not auto-generated) , arranged : List IdentifiedTrack
-- Filtered by search results, favourites, etc. , harvested : List IdentifiedTrack
-- Contexts ----------- , scrollContext : String }
type alias CollectionDependencies = { cached : List String , cachedOnly : Bool , enabledSourceIds : List String , favourites : List Favourite , favouritesOnly : Bool , grouping : Maybe Grouping , hideDuplicates : Bool , selectedPlaylist : Maybe Playlist , searchResults : Maybe (List String) , sortBy : SortBy , sortDirection : SortDirection }
type alias Parcel = ( CollectionDependencies, Collection )
type alias CoverCollection = { arranged : List Cover , harvested : List Cover }
-- GROUPING & SORTING
type Grouping = AddedOn | Directory | FirstAlphaCharacter | TrackYear
type SortBy = Artist | Album | PlaylistIndex | Title
type SortDirection = Asc | Desc
-- VIEW
type Scene = Covers | List
-- 🔱
emptyTrack : TrackemptyTrack = { id = "" , insertedAt = Time.default , path = "" , sourceId = "" , tags = emptyTags }
emptyTags : TagsemptyTags = { disc = 1 , nr = 0 , album = Nothing , artist = Nothing , title = "Empty" , genre = Nothing , picture = Nothing , year = Nothing }
emptyIdentifiedTrack : IdentifiedTrackemptyIdentifiedTrack = ( emptyIdentifiers , emptyTrack )
emptyIdentifiers : IdentifiersemptyIdentifiers = { isFavourite = False , isMissing = False
-- , filename = "" , group = Nothing , indexInList = 0 , indexInPlaylist = Nothing , parentDirectory = "" }
emptyCollection : CollectionemptyCollection = { untouched = [] , identified = [] , arranged = [] , harvested = []
-- Contexts ----------- , scrollContext = "" }
{-| If a track doesn't fit into a group, where does it go?-}fallbackCoverGroup : StringfallbackCoverGroup = "MISSING_TRACK_INFO"
{-| This value is used as a fallback in the UI if the album is missing.-}fallbackAlbum : StringfallbackAlbum = ""
{-| This value is used as a fallback in the UI if the artist is missing.-}fallbackArtist : StringfallbackArtist = ""
-- MORE STUFF
coverGroup : SortBy -> IdentifiedTrack -> StringcoverGroup sort ( identifiers, { tags } as track ) = if identifiers.isMissing then "MISSING_TRACKS"
else case sort of Artist -> Maybe.unwrap fallbackCoverGroup (String.trim >> String.toLower) tags.artist
Album -> -- There is the possibility of albums with the same name, -- such as "Greatests Hits". -- To make sure we treat those as different albums, -- we prefix the album by its parent directory. case tags.album of Just album -> (identifiers.parentDirectory ++ album) |> String.trim |> String.toLower
Nothing -> fallbackCoverGroup
PlaylistIndex -> ""
Title -> tags.title
coverKey : Bool -> Track -> StringcoverKey isVariousArtists { tags } = if isVariousArtists then Maybe.withDefault "?" tags.album
else Maybe.withDefault "?" tags.artist ++ " --- " ++ Maybe.withDefault "?" tags.album
isNowPlaying : IdentifiedTrack -> IdentifiedTrack -> BoolisNowPlaying ( a, b ) ( x, y ) = a.indexInPlaylist == x.indexInPlaylist && b.id == y.id
makeTrack : String -> ( String, Tags ) -> TrackmakeTrack sourceId ( path, tags ) = { id = (sourceId ++ "//" ++ path) |> Base64.encode |> String.chopEnd "=" , insertedAt = Time.default , path = path , sourceId = sourceId , tags = tags }
matchesAutoGeneratedPlaylist : Playlist -> Track -> BoolmatchesAutoGeneratedPlaylist playlist track = case playlist.autoGenerated of Just { level } -> track.path |> String.split "/" |> List.drop (max 0 (level - 1)) |> List.head |> (==) (Just playlist.name)
Nothing -> False
missingId : StringmissingId = "<missing>"
pathParts : Track -> { filename : String, parentDirectory : String }pathParts { path } = let s = String.split "/" path
l = List.length s in case List.drop (max 0 <| l - 2) s of [ p, f ] -> { filename = f, parentDirectory = p }
[ f ] -> { filename = f, parentDirectory = "" }
_ -> { filename = "", parentDirectory = "" }
{-| Given a collection of tracks, pick out the tracks by id in order.Note that track ids in the ids list may occur multiple times.-}pick : List String -> List Track -> List Trackpick ids collection = collection |> List.foldr (\track -> List.map (\picking -> case picking of PickId id -> if id == track.id then PickTrack track
else PickId id
p -> p ) ) (List.map PickId ids) |> List.foldr (\picking acc -> case picking of PickId _ -> acc
PickTrack track -> track :: acc ) []
removeByPaths : { sourceId : String, paths : List String } -> List Track -> { kept : List Track, removed : List Track }removeByPaths { sourceId, paths } tracks = tracks |> List.foldr (\t ( kept, removed, remainingPathsToRemove ) -> if t.sourceId == sourceId && List.member t.path remainingPathsToRemove then ( kept, t :: removed, List.remove t.path remainingPathsToRemove )
else ( t :: kept, removed, remainingPathsToRemove ) ) ( [], [], paths ) |> (\( k, r, _ ) -> { kept = k, removed = r } )
removeBySourceId : String -> List Track -> { kept : List Track, removed : List Track }removeBySourceId removedSourceId tracks = tracks |> List.foldr (\t ( kept, removed ) -> if t.sourceId == removedSourceId then ( kept, t :: removed )
else ( t :: kept, removed ) ) ( [], [] ) |> (\( k, r ) -> { kept = k, removed = r } )
removeFromPlaylist : List IdentifiedTrack -> Playlist -> PlaylistremoveFromPlaylist tracks playlist = playlist.tracks |> List.indexedFoldr (\idx t ( acc, remaining ) -> case List.partition ((==) (Just idx)) remaining of ( _ :: _, rem ) -> ( acc, rem )
( _, rem ) -> ( t :: acc, rem ) ) ( [] , List.map (Tuple.first >> .indexInPlaylist) tracks ) |> (\( t, _ ) -> { playlist | tracks = t })
shouldNoteProgress : { duration : Float } -> BoolshouldNoteProgress { duration } = duration >= 30 * 60
shouldRenderGroup : Identifiers -> BoolshouldRenderGroup identifiers = identifiers.group |> Maybe.map (.firstInGroup >> (==) True) |> Maybe.withDefault False
playlistTrackFromTrack : Track -> PlaylistTrackWithoutMetadataplaylistTrackFromTrack track = { album = track.tags.album , artist = track.tags.artist , title = track.tags.title }
sortByIndexInPlaylist : List IdentifiedTrack -> List IdentifiedTracksortByIndexInPlaylist = List.sortBy (\( i, t ) -> Maybe.withDefault (t.tags.disc * 1000 + t.tags.nr) i.indexInPlaylist)
toPlaylistTracks : List IdentifiedTrack -> List PlaylistTrackWithoutMetadatatoPlaylistTracks = List.map (Tuple.second >> playlistTrackFromTrack)
-- ㊙️
type Pick = PickId String | PickTrack Track