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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159module Tracks.Sorting exposing (sort)
import Maybe.Extra as Maybeimport Tracks exposing (..)
-- 🔱
sort : SortBy -> SortDirection -> List IdentifiedTrack -> List IdentifiedTracksort property direction list = let sortFn = case property of Album -> sortByAlbum
Artist -> sortByArtist
PlaylistIndex -> sortByPlaylistIndex
Title -> sortByTitle
dirFn = if direction == Desc then List.reverse
else identity in list |> List.sortWith sortFn |> dirFn
-- BY
sortByAlbum : IdentifiedTrack -> IdentifiedTrack -> OrdersortByAlbum ( x, a ) ( y, b ) = EQ |> andThenCompareBools isMissing x y |> andThenCompare album a b |> andThenCompare parentDir x y |> andThenCompare disc a b |> andThenCompare nr a b |> andThenCompare artist a b |> andThenCompare title a b
sortByArtist : IdentifiedTrack -> IdentifiedTrack -> OrdersortByArtist ( x, a ) ( y, b ) = EQ |> andThenCompareBools isMissing x y |> andThenCompare artist a b |> andThenCompare album a b |> andThenCompare parentDir x y |> andThenCompare disc a b |> andThenCompare nr a b |> andThenCompare title a b
sortByTitle : IdentifiedTrack -> IdentifiedTrack -> OrdersortByTitle ( _, a ) ( _, b ) = EQ |> andThenCompare title a b |> andThenCompare artist a b |> andThenCompare album a b
sortByPlaylistIndex : IdentifiedTrack -> IdentifiedTrack -> OrdersortByPlaylistIndex ( a, _ ) ( b, _ ) = andThenCompare (.indexInPlaylist >> Maybe.withDefault 0) a b EQ
-- TAGS
album : Track -> Stringalbum = .tags >> .album >> Maybe.unwrap fallbackAlbum low
artist : Track -> Stringartist = .tags >> .artist >> Maybe.unwrap fallbackArtist low
title : Track -> Stringtitle = .tags >> .title >> low
disc : Track -> Intdisc = .tags >> .disc
nr : Track -> Intnr = .tags >> .nr
isMissing : Identifiers -> BoolisMissing = .isMissing
parentDir : Identifiers -> StringparentDir = .parentDirectory >> low
-- COMMON
andThenCompare : (ctx -> comparable) -> ctx -> ctx -> Order -> OrderandThenCompare fn a b order = if order == EQ then compare (fn a) (fn b)
else order
andThenCompareBools : (ctx -> Bool) -> ctx -> ctx -> Order -> OrderandThenCompareBools fn a b order = if order == EQ then let af = fn a
bf = fn b in if af == bf then EQ
else if af == False then GT
else LT
else order
low : String -> Stringlow = String.trim >> String.toLower