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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237module Brain.Sources.Processing.State exposing (..)
import Alienimport Brain.Common.State as Commonimport Brain.Sources.Processing.Common exposing (..)import Brain.Sources.Processing.Steps as Stepsimport Brain.Sources.Processing.Types as Processing exposing (..)import Brain.Tracks.State as Tracksimport Brain.Types exposing (..)import Dict.Ext as Dictimport Httpimport Json.Decode as Jsonimport Json.Encode as Encodeimport List.Extra as Listimport Return exposing (..)import Sources exposing (Source)import Sources.Processing exposing (..)import Sources.Processing.Encoding as Processing
-- 📣
update : Processing.Msg -> Managerupdate msg = case msg of Process a -> process a
NextInLine -> nextInLine
StopProcessing -> stopProcessing
----------------------------------------- -- Steps ----------------------------------------- PrepareStep a b -> prepareStep a b
TreeStep a b -> treeStep a b
TreeStepRemoveTracks a b -> treeStepRemoveTracks a b
TagsStep a -> tagsStep a
-- 🔱
process : Json.Value -> Managerprocess json = -- Only proceed to the processing if we got all the necessary data, -- otherwise report an error in the UI. case Json.decodeValue Processing.argumentsDecoder json of Ok arguments -> process_ arguments
Err err -> Common.reportUI Alien.ProcessSources (Json.errorToString err)
{- If already processing, do nothing. If there are no sources, do nothing. If there are sources, start processing the first source.-}
process_ : { origin : String, sources : List Source } -> Managerprocess_ { origin, sources } model = let tracks = model.hypaethralUserData.tracks
filter s = List.filter (.sourceId >> (==) s.id) tracks
all = sources |> List.sortBy (.data >> Dict.fetch "name" "") |> List.map (\s -> ( s, filter s )) in case ( isProcessing model.processingStatus || List.isEmpty sources , List.uncons all ) of ( False, Just ( ( s, t ), future ) ) -> return { model | origin = origin, processingStatus = Processing ( s, t ) future } (Steps.takeFirstStep origin model.currentTime s)
_ -> Return.singleton model
{- If not processing, do nothing. If there are no sources left, do nothing. If there are sources left, start processing the next source in line.-}
nextInLine : ManagernextInLine model = case model.processingStatus of Processing ( processedSource, _ ) (( source, tracks ) :: rest) -> source |> Steps.takeFirstStep model.origin model.currentTime |> return { model | processingStatus = Processing ( source, tracks ) rest } |> andThen (processedSource.id |> Encode.string |> Common.giveUI Alien.FinishedProcessingSource )
_ -> Common.nudgeUI Alien.FinishedProcessingSources { model | processingStatus = NotProcessing }
{- STOP! -}
stopProcessing : ManagerstopProcessing model = Return.singleton { model | processingStatus = NotProcessing }
-- PHASE 1------------ Prepare for processing.
prepareStep : Context -> Result Http.Error String -> ManagerprepareStep context result model = case result of Ok response -> model.currentTime |> Steps.takePrepareStep context response |> return model
Err err -> model |> nextInLine |> andThen (reportHttpError context.source err)
-- PHASE 2------------ Make a file list/tree.
treeStep : Context -> Result Http.Error String -> ManagertreeStep context result model = case result of Ok response -> case model.processingStatus of Processing ( _, tracks ) rest -> return { model | processingStatus = Processing ( context.source, tracks ) rest } (Steps.takeTreeStep context response tracks model.currentTime)
NotProcessing -> Return.singleton model
Err err -> model |> nextInLine |> andThen (reportHttpError context.source err)
treeStepRemoveTracks : String -> List String -> ManagertreeStepRemoveTracks sourceId filePaths model = let encodedData = Encode.object [ ( "filePaths", Encode.list Encode.string filePaths ) , ( "sourceId", Encode.string sourceId ) ] in model |> Common.giveUI Alien.RemoveTracksByPath encodedData |> andThen (Tracks.removeByPaths { sourceId = sourceId, paths = filePaths })
-- PHASE 3------------ Get the tags for each file in the file list.
tagsStep : ContextForTags -> ManagertagsStep tagsContext model = let maybeCmd = case model.processingStatus of Processing ( source, _ ) _ -> Steps.takeTagsStep model.currentTime tagsContext source
NotProcessing -> Just Cmd.none
tracksToAdd = tagsContext |> tracksFromTagsContext |> List.map (\track -> { track | insertedAt = model.currentTime })
amountLeft = List.length tagsContext.nextFilePaths
progressPercentage = 0.05 + 0.95 * (1 - toFloat amountLeft / toFloat tagsContext.amount)
progress = [ ( "progress", Encode.float progressPercentage ) , ( "sourceId", Encode.string tagsContext.sourceId ) ] in maybeCmd |> Maybe.map (return model) |> Maybe.withDefault (nextInLine model) |> andThen (Tracks.add tracksToAdd) |> andThen (Common.giveUI Alien.ReportProcessingProgress <| Encode.object progress)