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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585module UI.Queue.State exposing (..)
import Coordinatesimport Debouncer.Basic as Debouncerimport Dictimport Html.Events.Extra.Mouse as Mouseimport List.Extra as Listimport Notificationsimport Queue exposing (..)import Return exposing (andThen)import Return.Ext as Returnimport Tracks exposing (..)import UI.Audio.Types exposing (AudioLoadingState(..))import UI.Common.State as Commonimport UI.Ports as Portsimport UI.Queue.ContextMenu as Queueimport UI.Queue.Fill as Fillimport UI.Queue.Types as Queue exposing (..)import UI.Types exposing (..)import UI.User.State.Export exposing (..)
-- 📣
update : Queue.Msg -> Managerupdate msg = case msg of Clear -> clear
PreloadNext -> preloadNext
Reset -> reset
ResetIgnored -> resetIgnored
Rewind -> rewind
Shift -> shift
Select a -> select a
ShowFutureMenu a b c -> showFutureMenu a b c
ShowFutureNavigationMenu a -> showFutureNavigationMenu a
ShowHistoryMenu a b -> showHistoryMenu a b
ToggleRepeat -> toggleRepeat
ToggleShuffle -> toggleShuffle
------------------------------------ -- Future ------------------------------------ AddTracks a -> addTracks a
InjectFirst a b -> injectFirst a b
InjectLast a b -> injectLast a b
InjectFirstAndPlay a -> injectFirstAndPlay a
MoveItemToFirst a -> moveItemToFirst a
MoveItemToLast a -> moveItemToLast a
RemoveItem a -> removeItem a
-- 🛠
changeActiveItem : Maybe Item -> ManagerchangeActiveItem maybeItem model = let maybeNowPlaying = Maybe.map (\item -> { coverLoaded = False , duration = Nothing , isPlaying = False , item = item , loadingState = Loading , playbackPosition = 0 } ) maybeItem in maybeItem |> Maybe.map (.identifiedTrack >> Tuple.second) |> Maybe.map (Queue.makeEngineItem False model.currentTime model.sources model.cachedTracks (if model.rememberProgress then model.progress
else Dict.empty ) ) |> Maybe.map insertTrack |> Maybe.withDefault Return.singleton |> (\fn -> fn { model | nowPlaying = maybeNowPlaying }) |> andThen fill
clear : Managerclear model = fill { model | playingNext = [] }
fill : Managerfill model = let ( availableTracks, timestamp ) = ( case ( model.selectedCover, model.coverSelectionReducesPool ) of ( Just cover, True ) -> Tuple.first <| List.foldl coverTracksHarvester ( [], cover.trackIds ) model.tracks.harvested
_ -> model.tracks.harvested -- , model.currentTime )
nonMissingTracks = List.filter (Tuple.second >> .id >> (/=) Tracks.missingId) availableTracks in model |> (\m -> -- Empty the ignored list when we are ignoring all the tracks if List.length model.dontPlay == List.length nonMissingTracks then { m | dontPlay = [] }
else m ) |> (\m -> if m.shuffle && List.length model.playingNext >= Fill.queueLength then m
else let fillState = { activeItem = Maybe.map .item m.nowPlaying , future = m.playingNext , ignored = m.dontPlay , past = m.playedPreviously } in -- Fill using the appropiate method if m.shuffle then { m | playingNext = Fill.shuffled timestamp nonMissingTracks fillState }
else { m | playingNext = Fill.ordered timestamp nonMissingTracks fillState } ) |> Return.communicate (Queue.PreloadNext |> QueueMsg |> Debouncer.provideInput |> AudioPreloadDebounce |> Return.task )
insertTrack : EngineItem -> ManagerinsertTrack item model = item |> (\engineItem -> if List.any (\a -> engineItem.trackId == a.trackId) model.audioElements then List.map (\a -> if engineItem.trackId == a.trackId then { a | isPreload = False }
else a ) model.audioElements
else model.audioElements ++ [ engineItem ] ) |> List.filter (\a -> if item.isPreload then True
else if a.trackId /= item.trackId && not a.isPreload then False
else True ) |> (\a -> { model | audioElements = a }) |> Return.singleton |> Return.effect_ (\m -> Ports.renderAudioElements { items = m.audioElements , play = if item.isPreload then Nothing
else Just item.trackId , volume = m.eqSettings.volume } )
preloadNext : ManagerpreloadNext model = case List.head model.playingNext of Just item -> item |> .identifiedTrack |> Tuple.second |> Queue.makeEngineItem True model.currentTime model.sources model.cachedTracks (if model.rememberProgress then model.progress
else Dict.empty ) |> (\engineItem -> insertTrack engineItem model )
Nothing -> Return.singleton model
rewind : Managerrewind model = changeActiveItem (List.last model.playedPreviously) { model | playingNext = model.nowPlaying |> Maybe.map (\{ item } -> item :: model.playingNext) |> Maybe.withDefault model.playingNext , playedPreviously = model.playedPreviously |> List.init |> Maybe.withDefault [] }
{-| Renew the queue, meaning that the auto-generated items in the queue are removed and new items are added.-}reset : Managerreset model = model.playingNext |> List.filter (.manualEntry >> (==) True) |> (\f -> { model | playingNext = f }) |> fill
resetIgnored : ManagerresetIgnored model = fill { model | dontPlay = [] }
select : Item -> Managerselect item model = Return.singleton { model | selectedQueueItem = Just item }
shift : Managershift model = changeActiveItem (List.head model.playingNext) { model | playingNext = model.playingNext |> List.drop 1 , playedPreviously = model.nowPlaying |> Maybe.map (.item >> List.singleton) |> Maybe.map (List.append model.playedPreviously) |> Maybe.withDefault model.playedPreviously }
showFutureMenu : Item -> { index : Int } -> Mouse.Event -> ManagershowFutureMenu item { index } mouseEvent model = mouseEvent.clientPos |> Coordinates.fromTuple |> Queue.futureMenu { cached = model.cachedTracks , cachingInProgress = model.cachingTracksInProgress , itemIndex = index } item |> Just |> (\c -> { model | contextMenu = c }) |> Return.singleton
showFutureNavigationMenu : Mouse.Event -> ManagershowFutureNavigationMenu mouseEvent model = mouseEvent.clientPos |> Coordinates.fromTuple |> Queue.futureNavigationMenu { manualEntries = List.filter .manualEntry model.playingNext } |> Just |> (\c -> { model | contextMenu = c }) |> Return.singleton
showHistoryMenu : Item -> Mouse.Event -> ManagershowHistoryMenu item mouseEvent model = mouseEvent.clientPos |> Coordinates.fromTuple |> Queue.historyMenu { cached = model.cachedTracks , cachingInProgress = model.cachingTracksInProgress } item |> Just |> (\c -> { model | contextMenu = c }) |> Return.singleton
toggleRepeat : ManagertoggleRepeat model = saveEnclosedUserData { model | repeat = not model.repeat }
toggleShuffle : ManagertoggleShuffle model = { model | shuffle = not model.shuffle } |> reset |> andThen saveEnclosedUserData
-- 🛠 ░░ FUTURE
addTracks : { inFront : Bool, tracks : List IdentifiedTrack } -> ManageraddTracks { inFront, tracks } = (if inFront then injectFirst
else injectLast ) { showNotification = True } tracks
{-| Add an item in front of the queue.-}injectFirst : { showNotification : Bool } -> List IdentifiedTrack -> ManagerinjectFirst { showNotification } identifiedTracks model = let ( items, tracks ) = ( List.map (makeItem True) identifiedTracks , List.map Tuple.second identifiedTracks )
cleanedFuture = List.foldl (.id >> Fill.cleanAutoGenerated model.shuffle) model.playingNext tracks
notification = case tracks of [ t ] -> ("__" ++ t.tags.title ++ "__ will be played next") |> Notifications.casual
list -> list |> List.length |> String.fromInt |> (\s -> "__" ++ s ++ " tracks__ will be played next") |> Notifications.casual in { model | playingNext = items ++ cleanedFuture } |> (if showNotification then Common.showNotification notification
else Return.singleton ) |> andThen fill
injectFirstAndPlay : IdentifiedTrack -> ManagerinjectFirstAndPlay identifiedTrack model = model |> injectFirst { showNotification = False } [ identifiedTrack ] |> andThen shift
{-| Add an item after the last manual entry(ie. after the last injected item).-}injectLast : { showNotification : Bool } -> List IdentifiedTrack -> ManagerinjectLast { showNotification } identifiedTracks model = let ( items, tracks ) = ( List.map (makeItem True) identifiedTracks , List.map Tuple.second identifiedTracks )
cleanedFuture = List.foldl (.id >> Fill.cleanAutoGenerated model.shuffle) model.playingNext tracks
manualItems = cleanedFuture |> List.filter (.manualEntry >> (==) True) |> List.length
newFuture = [] ++ List.take manualItems cleanedFuture ++ items ++ List.drop manualItems cleanedFuture
notification = case tracks of [ t ] -> ("__" ++ t.tags.title ++ "__ was added to the queue") |> Notifications.casual
list -> list |> List.length |> String.fromInt |> (\s -> "__" ++ s ++ " tracks__ were added to the queue") |> Notifications.casual in { model | playingNext = newFuture } |> (if showNotification then Common.showNotification notification
else Return.singleton ) |> andThen fill
moveItemToFirst : { index : Int } -> ManagermoveItemToFirst { index } model = model.playingNext |> moveItem { from = index, to = 0, shuffle = model.shuffle } |> (\f -> { model | playingNext = f }) |> fill
moveItemToLast : { index : Int } -> ManagermoveItemToLast { index } model = let to = model.playingNext |> List.filter (.manualEntry >> (==) True) |> List.length in model.playingNext |> moveItem { from = index, to = to, shuffle = model.shuffle } |> (\f -> { model | playingNext = f }) |> fill
removeItem : { index : Int, item : Item } -> ManagerremoveItem { index, item } model = let newFuture = List.removeAt index model.playingNext
newIgnored = if item.manualEntry then model.dontPlay
else item :: model.dontPlay in fill { model | playingNext = newFuture, dontPlay = newIgnored }
-- ⚗️
coverTracksHarvester : IdentifiedTrack -> ( List IdentifiedTrack, List String ) -> ( List IdentifiedTrack, List String )coverTracksHarvester ( i, t ) ( acc, coverTrackIds ) = case List.findIndex ((==) t.id) coverTrackIds of Just idx -> ( acc ++ [ ( i, t ) ] , List.removeAt idx coverTrackIds )
Nothing -> ( acc , coverTrackIds )
moveItem : { from : Int, to : Int, shuffle : Bool } -> List Item -> List ItemmoveItem { from, to, shuffle } collection = let subjectItem = collection |> List.getAt from |> Maybe.map (\s -> { s | manualEntry = True })
fixedTarget = if to > from then to - 1
else to in collection |> List.removeAt from |> List.indexedFoldr (\idx existingItem acc -> if idx == fixedTarget then case subjectItem of Just itemToInsert -> List.append [ itemToInsert, existingItem ] acc
Nothing -> existingItem :: acc
else if idx < fixedTarget then { existingItem | manualEntry = True } :: acc
else existingItem :: acc ) [] |> (if shuffle then identity
else List.filter (.manualEntry >> (==) True) )