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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252module UI.Queue.Fill exposing (State, cleanAutoGenerated, ordered, queueLength, shuffled)
{-| These functions will return a new list for the `future` property.-}
import Arrayimport List.Extra as Listimport Maybe.Ext as Maybeimport Maybe.Extra as Maybeimport Queue exposing (Item, makeItem)import Random exposing (Generator, Seed)import Timeimport Tracks exposing (IdentifiedTrack)
-- ⛩
queueLength : IntqueueLength = 30
-- 🌳
type alias State = { activeItem : Maybe Item , future : List Item , ignored : List Item , past : List Item }
-- 🔱 ░░ ORDERED
ordered : Time.Posix -> List IdentifiedTrack -> State -> List Itemordered _ unfilteredTracks state = let tracks = state.ignored |> List.map itemTrackId |> Tuple.pair [] |> purifier unfilteredTracks |> Tuple.first
manualEntries = List.filter (.manualEntry >> (==) True) state.future
remaining = max (queueLength - List.length manualEntries) 0
focus = Maybe.preferFirst (List.last manualEntries) state.activeItem in case focus of Just item -> let maybeNowPlayingIndex = List.findIndex (Tracks.isNowPlaying item.identifiedTrack) tracks in maybeNowPlayingIndex |> Maybe.map (\idx -> List.drop (idx + 1) tracks) |> Maybe.withDefault tracks |> List.take remaining |> (\a -> let actualRemaining = remaining - List.length a
n = Maybe.withDefault (List.length tracks) maybeNowPlayingIndex in a ++ List.take (min n actualRemaining) tracks ) |> List.map (makeItem False) |> List.append manualEntries
Nothing -> tracks |> List.take remaining |> List.map (makeItem False) |> List.append manualEntries
-- 🔱 ░░ SHUFFLED
shuffled : Time.Posix -> List IdentifiedTrack -> State -> List Itemshuffled timestamp unfilteredTracks state = let idsToIgnoreWithoutPast = [ state.ignored , Maybe.unwrap [] List.singleton state.activeItem ] |> List.map (List.map itemTrackId) |> List.concat |> List.unique
( tracksWithPast, idsToIgnoreWithoutPastAfterFilter ) = purifier unfilteredTracks ( [], idsToIgnoreWithoutPast )
idsToIgnoreWithPast = List.unique (idsToIgnoreWithoutPastAfterFilter ++ List.map itemTrackId state.past)
( tracksWithoutPast, idsToIgnoreWithPastAfterFilter ) = purifier tracksWithPast ( [], idsToIgnoreWithPast )
idsToIgnoreWithFuture = List.unique (idsToIgnoreWithoutPastAfterFilter ++ List.map itemTrackId state.future)
( tracksList, _ ) = purifier (case tracksWithoutPast of [] -> tracksWithPast
t -> t ) ( [], idsToIgnoreWithFuture )
tracks = Array.fromList tracksList
amountOfTracks = Array.length tracks
generator = Random.int 0 (amountOfTracks - 1)
toAmount = max (queueLength - List.length state.future) 0
howMany = min toAmount amountOfTracks in if howMany > 0 then timestamp |> Time.posixToMillis |> Random.initialSeed |> generateIndexes generator howMany [] |> List.foldl (\idx acc -> case Array.get idx tracks of Just track -> makeItem False track :: acc
Nothing -> acc ) [] |> List.append state.future
else state.future
-- 🔱
cleanAutoGenerated : Bool -> String -> List Item -> List ItemcleanAutoGenerated shuffle trackId future = if shuffle then List.filterNot (\i -> i.manualEntry == False && itemTrackId i == trackId) future
else future
-- ㊙️
{-| Generated random indexes.
`squirrel` = accumulator, ie. collected indexes
-}generateIndexes : Generator Int -> Int -> List Int -> Seed -> List IntgenerateIndexes generator howMany squirrel seed = let ( index, newSeed ) = Random.step generator seed in if List.member index squirrel then generateIndexes generator howMany squirrel newSeed
else if howMany - 1 > 0 then generateIndexes generator (howMany - 1) (index :: squirrel) newSeed
else index :: squirrel
-- PURIFY
purifier : List IdentifiedTrack -> ( List IdentifiedTrack, List String ) -> ( List IdentifiedTrack, List String )purifier tracks ( acc, idsToIgnore ) = case idsToIgnore of [] -> -- Nothing more to ignore, -- stop here. ( acc ++ tracks, [] )
_ -> case tracks of [] -> -- No more tracks left, -- end of the road. ( acc, idsToIgnore )
(( _, track ) as identifiedTrack) :: rest -> case List.elemIndex track.id idsToIgnore of Just ignoreIdx -> -- It's a track to ignore, -- remove it from the ignore list and carry on. purifier rest ( acc, List.removeAt ignoreIdx idsToIgnore )
Nothing -> -- It's not a track to ignore, -- add it to the to-keep list and carry on. purifier rest ( identifiedTrack :: acc, idsToIgnore )
-- COMMON
itemTrackId : Item -> StringitemTrackId = .identifiedTrack >> Tuple.second >> .id