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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582module UI.Sources.State exposing (..)
import Alienimport Browser.Navigation as Navimport Commonimport Conditional exposing (ifThenElse)import Coordinatesimport Dictimport Dict.Ext as Dictimport Html.Events.Extra.Mouse as Mouseimport Json.Decode as Jsonimport Json.Encodeimport Monocle.Lens as Lensimport Notificationsimport Return exposing (andThen, return)import Sources exposing (..)import Sources.Encoding as Sourcesimport Sources.Services as Servicesimport Sources.Services.Dropboximport Sources.Services.Googleimport Tracks.Collectionimport UI.Common.State as Commonimport UI.Page as Pageimport UI.Ports as Portsimport UI.Sources.ContextMenu as Sourcesimport UI.Sources.Form as Formimport UI.Sources.Page as Sourcesimport UI.Sources.Queryimport UI.Sources.Types exposing (..)import UI.Tracks.State as Tracksimport UI.Types as UI exposing (Manager, Model)import UI.User.State.Export as User
-- 🌳
formLens = { get = .sourceForm , set = \form m -> { m | sourceForm = form } }
formContextLens = Lens.compose formLens { get = .context , set = \context m -> { m | context = context } }
formStepLens = Lens.compose formLens { get = .step , set = \step m -> { m | step = step } }
-- 📣
update : Msg -> Managerupdate msg = case msg of Bypass -> Return.singleton
-- FinishedProcessingSource a -> finishedProcessingSource a
FinishedProcessing -> finishedProcessing
Process -> process
ProcessSpecific a -> processSpecific a
ReportProcessingError a -> reportProcessingError a
ReportProcessingProgress a -> reportProcessingProgress a
StopProcessing -> stopProcessing
----------------------------------------- -- Collection ----------------------------------------- AddToCollection a -> addToCollection a
RemoveFromCollection a -> removeFromCollection a
UpdateSourceData a -> updateSourceData a
----------------------------------------- -- Form ----------------------------------------- AddSourceUsingForm -> addSourceUsingForm
EditSourceUsingForm -> editSourceUsingForm
RenameSourceUsingForm -> renameSourceUsingForm
ReturnToIndex -> returnToIndex
SelectService a -> selectService a
SetFormData a b -> setFormData a b
TakeStep -> takeStep
TakeStepBackwards -> takeStepBackwards
----------------------------------------- -- Individual ----------------------------------------- SourceContextMenu a b -> sourceContextMenu a b
ToggleActivation a -> toggleActivation a
ToggleDirectoryPlaylists a -> toggleDirectoryPlaylists a
ToggleProcessAutomatically -> toggleProcessAutomatically
-- 🔱
addSourcesFromUrl : ManageraddSourcesFromUrl model = case UI.Sources.Query.sourcesFromUrl model.url of [] -> Return.singleton model
sources -> sources |> List.foldl (\s -> andThen <| addToCollection s) (Return.singleton model) |> Return.command (Nav.replaceUrl model.navKey (model.url.path ++ Page.toString model.page) )
finishedProcessing : ManagerfinishedProcessing model = (case model.processingNotificationId of Just id -> Common.dismissNotification { id = id }
Nothing -> Return.singleton ) { model | processingContext = [] }
finishedProcessingSource : Json.Value -> ManagerfinishedProcessingSource json model = case Json.decodeValue Json.string json of Ok sourceId -> model.processingContext |> List.filter (Tuple.first >> (/=) sourceId) |> (\newContext -> { model | processingContext = newContext }) |> Return.singleton
Err _ -> Return.singleton model
process : Managerprocess model = case sourcesToProcess model of [] -> Return.singleton model
toProcess -> if model.isOnline then processSpecific toProcess model
else toProcess |> List.filter Sources.worksOffline |> (\s -> case s of [] -> Return.singleton model
_ -> processSpecific s model )
processSpecific : List Source -> ManagerprocessSpecific toProcess model = let notification = Notifications.stickyCasual "Processing sources ..."
notificationId = Notifications.id notification
newNotifications = List.filter (\n -> Notifications.kind n /= Notifications.Error) model.notifications
processingContext = toProcess |> List.sortBy (.data >> Dict.fetch "name" "") |> List.map (\{ id } -> ( id, 0 ))
newModel = { model | notifications = newNotifications , processingContext = processingContext , processingError = Nothing , processingNotificationId = Just notificationId } in [ ( "origin" , Json.Encode.string (Common.urlOrigin model.url) ) , ( "sources" , Json.Encode.list Sources.encode toProcess ) ] |> Json.Encode.object |> Alien.broadcast Alien.ProcessSources |> Ports.toBrain |> return newModel |> andThen (Common.showNotification notification)
reportProcessingError : Json.Value -> ManagerreportProcessingError json model = case Json.decodeValue (Json.dict Json.string) json of Ok dict -> let args = { error = Dict.fetch "error" "" dict , sourceId = Dict.fetch "sourceId" "" dict } in [] |> Notifications.errorWithCode ("Could not process the _" ++ Dict.fetch "sourceName" "" dict ++ "_ source. I got the following response from the source:" ) (Dict.fetch "error" "missingError" dict) |> Common.showNotificationWithModel { model | processingError = Just args }
Err _ -> "Could not decode processing error" |> Notifications.stickyError |> Common.showNotificationWithModel model
reportProcessingProgress : Json.Value -> ManagerreportProcessingProgress json model = case Json.decodeValue (Json.map2 (\p s -> { progress = p , sourceId = s } ) (Json.field "progress" Json.float) (Json.field "sourceId" Json.string) ) json of Ok { progress, sourceId } -> model.processingContext |> List.map (\( sid, pro ) -> ifThenElse (sid == sourceId) ( sid, progress ) ( sid, pro ) ) |> (\processingContext -> { model | processingContext = processingContext } ) |> Return.singleton
Err _ -> "Could not decode processing progress" |> Notifications.stickyError |> Common.showNotificationWithModel model
stopProcessing : ManagerstopProcessing model = case model.processingNotificationId of Just notificationId -> Alien.StopProcessing |> Alien.trigger |> Ports.toBrain |> return { model | processingContext = [] , processingNotificationId = Nothing } |> andThen (Common.dismissNotification { id = notificationId })
Nothing -> Return.singleton model
-- COLLECTION
addToCollection : Source -> ManageraddToCollection unsuitableSource model = let source = setProperId (List.length model.sources + 1) model.currentTime unsuitableSource in { model | sources = model.sources ++ [ source ] } |> User.saveSources |> andThen (processSpecific [ source ])
removeFromCollection : { sourceId : String } -> ManagerremoveFromCollection { sourceId } model = model.sources |> List.filter (.id >> (/=) sourceId) |> (\c -> { model | sources = c }) |> Return.singleton |> andThen User.saveSources |> andThen (Tracks.removeBySourceId sourceId)
updateSourceData : Json.Value -> ManagerupdateSourceData json model = json |> Sources.decode |> Maybe.map (\source -> List.map (\s -> if s.id == source.id then source
else s ) model.sources ) |> Maybe.map (\col -> { model | sources = col }) |> Maybe.withDefault model |> User.saveSources
-- FORM
addSourceUsingForm : ManageraddSourceUsingForm model = let context = model.sourceForm.context
cleanContext = { context | data = Dict.map (always String.trim) context.data } in model |> formLens.set Form.initialModel |> addToCollection cleanContext |> andThen returnToIndex
editSourceUsingForm : ManagereditSourceUsingForm model = model |> formLens.set Form.initialModel |> replaceSourceInCollection model.sourceForm.context |> andThen process |> andThen returnToIndex
renameSourceUsingForm : ManagerrenameSourceUsingForm model = model |> formLens.set Form.initialModel |> replaceSourceInCollection model.sourceForm.context |> andThen returnToIndex
returnToIndex : ManagerreturnToIndex = Common.changeUrlUsingPage (Page.Sources Sources.Index)
selectService : String -> ManagerselectService serviceKey model = case Services.keyToType serviceKey of Just service -> model |> Lens.modify formContextLens (\c -> { c | data = Services.initialData service , service = service } ) |> Return.singleton
Nothing -> Return.singleton model
setFormData : String -> String -> ManagersetFormData key value model = model |> Lens.modify formContextLens (\context -> context.data |> Dict.insert key value |> (\data -> { context | data = data }) ) |> Return.singleton
takeStep : ManagertakeStep model = let form = formLens.get model in case ( form.step, form.context.service ) of ( How, Dropbox ) -> form.context.data |> Sources.Services.Dropbox.authorizationUrl |> externalAuthorization model
( How, Google ) -> form.context.data |> Sources.Services.Google.authorizationUrl |> externalAuthorization model
_ -> model |> Lens.modify formStepLens takeStepForwards |> Return.singleton
takeStepBackwards : ManagertakeStepBackwards = Lens.modify formStepLens takeStepBackwards_ >> Return.singleton
-- INDIVIDUAL
sourceContextMenu : Source -> Mouse.Event -> ManagersourceContextMenu source mouseEvent model = mouseEvent.clientPos |> Coordinates.fromTuple |> Sources.sourceMenu source |> Common.showContextMenuWithModel model
toggleActivation : { sourceId : String } -> ManagertoggleActivation { sourceId } model = model.sources |> List.map (\source -> if source.id == sourceId then { source | enabled = not source.enabled }
else source ) |> (\collection -> { model | sources = collection }) |> Tracks.reviseCollection Tracks.Collection.identify |> andThen Common.forceTracksRerender |> andThen Common.generateDirectoryPlaylists |> andThen User.saveSources
toggleDirectoryPlaylists : { sourceId : String } -> ManagertoggleDirectoryPlaylists { sourceId } model = model.sources |> List.map (\source -> if source.id == sourceId then { source | directoryPlaylists = not source.directoryPlaylists }
else source ) |> (\collection -> { model | sources = collection }) |> User.saveSources |> andThen Common.generateDirectoryPlaylists
toggleProcessAutomatically : ManagertoggleProcessAutomatically model = User.saveSettings { model | processAutomatically = not model.processAutomatically }
-- ⚗️
externalAuthorization : Model -> (String -> String) -> ( Model, Cmd UI.Msg )externalAuthorization model urlBuilder = model.url |> Common.urlOrigin |> urlBuilder |> Nav.load |> return model
replaceSourceInCollection : Source -> ManagerreplaceSourceInCollection source model = model.sources |> List.map (\s -> ifThenElse (s.id == source.id) source s) |> (\s -> { model | sources = s }) |> User.saveSources
sourcesToProcess : Model -> List SourcesourcesToProcess model = List.filter (.enabled >> (==) True) model.sources
takeStepForwards : FormStep -> FormSteptakeStepForwards currentStep = case currentStep of Where -> How
_ -> By
takeStepBackwards_ : FormStep -> FormSteptakeStepBackwards_ currentStep = case currentStep of By -> How
_ -> Where