diff --git a/src/Applications/UI.elm b/src/Applications/UI.elm index 86df7dab..fd64ac7d 100644 --- a/src/Applications/UI.elm +++ b/src/Applications/UI.elm @@ -46,8 +46,9 @@ import UI.Queue.Types as Queue import UI.Reply.Translate as Reply import UI.Routing.State as Routing import UI.Services.State as Services -import UI.Sources as Sources import UI.Sources.ContextMenu as Sources +import UI.Sources.Form +import UI.Sources.State as Sources import UI.Tracks as Tracks import UI.Tracks.ContextMenu as Tracks import UI.Tracks.State as Tracks @@ -173,6 +174,15 @@ init flags url key = , repeat = False , shuffle = False + ----------------------------------------- + -- Sources + ----------------------------------------- + , processingContext = [] + , processingError = Nothing + , processingNotificationId = Nothing + , sources = [] + , sourceForm = UI.Sources.Form.initialModel + ----------------------------------------- -- 🦉 Nested ----------------------------------------- @@ -181,7 +191,6 @@ init flags url key = ----------------------------------------- -- Children (TODO) ----------------------------------------- - , sources = Sources.initialModel , tracks = Tracks.initialModel } |> Routing.transition page @@ -248,7 +257,7 @@ update msg = Audio.playPause ----------------------------------------- - -- Authentication + -- Authentication (TODO: Move) ----------------------------------------- AuthenticationBootFailure a -> Authentication.bootFailure a @@ -365,7 +374,7 @@ update msg = -- Routing ----------------------------------------- ChangeUrlUsingPage a -> - Routing.changeUrlUsingPage a + Common.changeUrlUsingPage a LinkClicked a -> Routing.linkClicked a @@ -386,7 +395,7 @@ update msg = Services.scrobble a ----------------------------------------- - -- Tracks + -- Tracks (TODO: Move) ----------------------------------------- DownloadTracksFinished -> Tracks.downloadTracksFinished @@ -421,15 +430,6 @@ update msg = KeyboardMsg a -> Adjunct.keyboardInput a - ----------------------------------------- - -- 📭 Other - ----------------------------------------- - SetCurrentTime a -> - Other.setCurrentTime a - - SetIsOnline a -> - Other.setIsOnline a - ----------------------------------------- -- 🦉 Nested ----------------------------------------- @@ -439,21 +439,21 @@ update msg = QueueMsg a -> Queue.update a + SourcesMsg a -> + Sources.update a + ----------------------------------------- - -- Children (TODO) + -- 📭 Other ----------------------------------------- - SourcesMsg sub -> - \model -> - Return3.wieldNested - Reply.translate - { mapCmd = SourcesMsg - , mapModel = \child -> { model | sources = child } - , update = Sources.update - } - { model = model.sources - , msg = sub - } + SetCurrentTime a -> + Other.setCurrentTime a + + SetIsOnline a -> + Other.setIsOnline a + ----------------------------------------- + -- Children (TODO) + ----------------------------------------- TracksMsg sub -> \model -> Return3.wieldNested @@ -496,6 +496,7 @@ subscriptions model = ----------------------------------------- -- Interface ----------------------------------------- + , Browser.Events.onResize Interface.onResize , Ports.indicateTouchDevice (\_ -> SetIsTouchDevice True) , Ports.preferredColorSchemaChanged PreferredColorSchemaChanged , Ports.showErrorNotification (Notifications.error >> ShowNotification) @@ -508,11 +509,6 @@ subscriptions model = , Ports.requestNext (\_ -> QueueMsg Queue.Shift) , Ports.requestPrevious (\_ -> QueueMsg Queue.Rewind) - ----------------------------------------- - -- Resize - ----------------------------------------- - , Browser.Events.onResize Interface.onResize - ----------------------------------------- -- Services ----------------------------------------- diff --git a/src/Applications/UI/Alien.elm b/src/Applications/UI/Alien.elm index f805cfcb..aaf37c35 100644 --- a/src/Applications/UI/Alien.elm +++ b/src/Applications/UI/Alien.elm @@ -5,7 +5,7 @@ import Common exposing (Switch(..)) import Json.Decode import Notifications import UI.Authentication.Types as Authentication -import UI.Sources as Sources +import UI.Sources.Types as Sources import UI.Tracks as Tracks import UI.Types exposing (..) import User.Layer exposing (..) @@ -46,6 +46,7 @@ translateAlienData event = Just Alien.FinishedProcessingSource -> event.data |> Json.Decode.decodeValue Json.Decode.string + |> Result.map (\id -> { sourceId = id }) |> Result.map (Sources.FinishedProcessingSource >> SourcesMsg) |> Result.withDefault Bypass diff --git a/src/Applications/UI/Common/State.elm b/src/Applications/UI/Common/State.elm index 8c67b43f..09245ab8 100644 --- a/src/Applications/UI/Common/State.elm +++ b/src/Applications/UI/Common/State.elm @@ -1,9 +1,12 @@ module UI.Common.State exposing (..) +import Browser.Navigation as Nav +import ContextMenu exposing (ContextMenu) import Monocle.Lens as Lens exposing (Lens) import Notifications exposing (Notification) -import Return +import Return exposing (return) import UI.Notifications +import UI.Page as Page exposing (Page) import UI.Reply exposing (Reply) import UI.Types as UI exposing (Manager) @@ -12,6 +15,27 @@ import UI.Types as UI exposing (Manager) -- 📣 +changeUrlUsingPage : Page -> Manager +changeUrlUsingPage page model = + page + |> Page.toString + |> Nav.pushUrl model.navKey + |> return model + + +dismissNotification : { id : Int } -> Manager +dismissNotification options model = + options + |> UI.Notifications.dismiss model.notifications + |> Return.map (\n -> { model | notifications = n }) + |> Return.mapCmd UI.Reply + + +showContextMenuWithModel : UI.Model -> ContextMenu Reply -> ( UI.Model, Cmd UI.Msg ) +showContextMenuWithModel model contextMenu = + Return.singleton { model | contextMenu = Just contextMenu } + + showNotification : Notification Reply -> Manager showNotification notification model = model.notifications diff --git a/src/Applications/UI/Other/State.elm b/src/Applications/UI/Other/State.elm index 0ed4469f..45d3b0ab 100644 --- a/src/Applications/UI/Other/State.elm +++ b/src/Applications/UI/Other/State.elm @@ -54,10 +54,7 @@ setIsOnline bool model = setCurrentTime : Time.Posix -> Manager setCurrentTime time model = - model - |> (\m -> { m | currentTime = time }) - |> Lens.modify Sources.lens (\s -> { s | currentTime = time }) - |> Return.singleton + Return.singleton { model | currentTime = time } diff --git a/src/Applications/UI/Playlists/State.elm b/src/Applications/UI/Playlists/State.elm index b82bd31d..aeaebd89 100644 --- a/src/Applications/UI/Playlists/State.elm +++ b/src/Applications/UI/Playlists/State.elm @@ -16,7 +16,6 @@ import UI.Page as Page import UI.Playlists.ContextMenu as Playlists import UI.Playlists.Page exposing (..) import UI.Ports as Ports -import UI.Routing.State as Routing import UI.Tracks as Tracks import UI.Types as UI exposing (..) @@ -230,4 +229,4 @@ showListMenu playlist mouseEvent model = redirectToIndexPage : Manager redirectToIndexPage = - Routing.changeUrlUsingPage (Page.Playlists Index) + Common.changeUrlUsingPage (Page.Playlists Index) diff --git a/src/Applications/UI/Queue/State.elm b/src/Applications/UI/Queue/State.elm index f593bc03..d3d525b4 100644 --- a/src/Applications/UI/Queue/State.elm +++ b/src/Applications/UI/Queue/State.elm @@ -87,7 +87,7 @@ changeActiveItem maybeItem model = |> Maybe.map (Queue.makeEngineItem model.currentTime - model.sources.collection + model.sources model.tracks.cached (if model.rememberProgress then model.progress @@ -168,7 +168,7 @@ preloadNext model = |> Tuple.second |> Queue.makeEngineItem model.currentTime - model.sources.collection + model.sources model.tracks.cached (if model.rememberProgress then model.progress diff --git a/src/Applications/UI/Reply.elm b/src/Applications/UI/Reply.elm index a6255ba8..a8410cb1 100644 --- a/src/Applications/UI/Reply.elm +++ b/src/Applications/UI/Reply.elm @@ -41,7 +41,6 @@ type Reply | ReplyViaContextMenu Reply | ShowMoreAuthenticationOptions Coordinates | ShowPlaylistListMenu Coordinates Playlist - | ShowSourceContextMenu Coordinates Source | ShowTracksContextMenu Coordinates { alt : Bool } (List IdentifiedTrack) | ShowTracksViewMenu Coordinates (Maybe Tracks.Grouping) ----------------------------------------- @@ -98,7 +97,6 @@ type Reply | RemoveSourceFromCollection { sourceId : String } | RemoveTracksFromCache (List Track) | RemoveTracksWithSourceId String - | ReplaceSourceInCollection Source | ScrollToNowPlaying | StoreTracksInCache (List Track) | ToggleCachedTracksOnly diff --git a/src/Applications/UI/Reply/Translate.elm b/src/Applications/UI/Reply/Translate.elm index 4426b1d6..96e4b37a 100644 --- a/src/Applications/UI/Reply/Translate.elm +++ b/src/Applications/UI/Reply/Translate.elm @@ -33,7 +33,7 @@ import UI.Audio.State as Audio import UI.Authentication.ContextMenu as Authentication import UI.Authentication.Types as Authentication import UI.Backdrop as Backdrop -import UI.Common.State exposing (showNotification, showNotificationWithModel) +import UI.Common.State as Common exposing (showNotification, showNotificationWithModel) import UI.Demo as Demo import UI.Interface.State as Interface import UI.Notifications @@ -45,10 +45,10 @@ import UI.Queue.ContextMenu as Queue import UI.Queue.State as Queue import UI.Queue.Types as Queue import UI.Reply as Reply exposing (Reply(..)) -import UI.Routing.State as Routing import UI.Settings as Settings -import UI.Sources as Sources import UI.Sources.ContextMenu as Sources +import UI.Sources.State as Sources +import UI.Sources.Types as Sources import UI.Tracks as Tracks import UI.Tracks.ContextMenu as Tracks import UI.Tracks.Scene.List @@ -77,7 +77,7 @@ translate reply model = |> return model GoToPage page -> - Routing.changeUrlUsingPage page model + Common.changeUrlUsingPage page model Reply.ToggleLoadingScreen a -> Interface.toggleLoadingScreen a model @@ -162,11 +162,10 @@ translate reply model = , shuffle = False -- - , sources = - { sources - | collection = [] - , isProcessing = [] - } + , processingContext = [] + , sources = [] + + -- , tracks = { tracks | collection = Tracks.emptyCollection @@ -205,9 +204,6 @@ translate reply model = Reply.ShowPlaylistListMenu coordinates playlist -> Return.singleton { model | contextMenu = Just (Playlists.listMenu playlist model.tracks.collection.identified model.confirmation coordinates) } - ShowSourceContextMenu coordinates source -> - Return.singleton { model | contextMenu = Just (Sources.sourceMenu source coordinates) } - ShowTracksContextMenu coordinates { alt } tracks -> let menuDependencies = @@ -217,7 +213,7 @@ translate reply model = , selectedPlaylist = model.tracks.selectedPlaylist , lastModifiedPlaylistName = model.lastModifiedPlaylist , showAlternativeMenu = alt - , sources = model.sources.collection + , sources = model.sources } in Return.singleton { model | contextMenu = Just (Tracks.trackMenu menuDependencies tracks coordinates) } @@ -309,7 +305,7 @@ translate reply model = directoryPlaylists = UI.Playlists.Directory.generate - model.sources.collection + model.sources model.tracks.collection.untouched in [ nonDirectoryPlaylists @@ -464,52 +460,8 @@ translate reply model = |> TracksMsg |> Return.performanceF model - ProcessSources [] -> - Return.singleton model - - ProcessSources sourcesToProcess -> - let - notification = - Notifications.stickyWarning "Processing sources ..." - - notificationId = - Notifications.id notification - - newNotifications = - List.filter - (\n -> Notifications.kind n /= Notifications.Error) - model.notifications - - sources = - model.sources - - isProcessing = - sourcesToProcess - |> List.sortBy (.data >> Dict.fetch "name" "") - |> List.map (\{ id } -> ( id, 0 )) - - newSources = - { sources - | isProcessing = isProcessing - , processingError = Nothing - , processingNotificationId = Just notificationId - } - - newModel = - { model | notifications = newNotifications, sources = newSources } - in - [ ( "origin" - , Json.Encode.string (Common.urlOrigin model.url) - ) - , ( "sources" - , Json.Encode.list Sources.encode sourcesToProcess - ) - ] - |> Json.Encode.object - |> Alien.broadcast Alien.ProcessSources - |> Ports.toBrain - |> return newModel - |> andThen (showNotification notification) + ProcessSources sources -> + Sources.process model RemoveSourceFromCollection args -> args @@ -548,18 +500,6 @@ translate reply model = |> Return.performanceF model |> Return.command cmd - ReplaceSourceInCollection source -> - let - sources = - model.sources - in - model.sources.collection - |> List.map (\s -> ifThenElse (s.id == source.id) source s) - |> (\c -> { sources | collection = c }) - |> (\s -> { model | sources = s }) - |> Return.singleton - |> andThen (translate SaveSources) - ScrollToNowPlaying -> Return.performance (TracksMsg Tracks.ScrollToNowPlaying) model @@ -592,7 +532,7 @@ translate reply model = , track |> Queue.makeTrackUrl model.currentTime - model.sources.collection + model.sources |> Json.Encode.string ) ] @@ -631,7 +571,7 @@ translate reply model = , playlists = List.filterNot .autoGenerated model.playlists , progress = model.progress , settings = Just (gatherSettings model) - , sources = model.sources.collection + , sources = model.sources , tracks = model.tracks.collection.untouched } |> encodeHypaethralData @@ -690,7 +630,7 @@ translate reply model = SaveSources -> let updateEnabledSourceIdsOnTracks = - model.sources.collection + model.sources |> Sources.enabledSourceIds |> Tracks.SetEnabledSourceIds |> TracksMsg @@ -699,7 +639,7 @@ translate reply model = ( updatedModel, updatedCmd ) = updateEnabledSourceIdsOnTracks model in - updatedModel.sources.collection + updatedModel.sources |> Json.Encode.list Sources.encode |> Alien.broadcast Alien.SaveSources |> Ports.toBrain diff --git a/src/Applications/UI/Routing/State.elm b/src/Applications/UI/Routing/State.elm index eb0c129f..e7a0a9c7 100644 --- a/src/Applications/UI/Routing/State.elm +++ b/src/Applications/UI/Routing/State.elm @@ -1,4 +1,4 @@ -module UI.Routing.State exposing (changeUrlUsingPage, linkClicked, resetUrl, transition, urlChanged) +module UI.Routing.State exposing (linkClicked, resetUrl, transition, urlChanged) import Browser exposing (UrlRequest) import Browser.Navigation as Nav @@ -9,10 +9,12 @@ import Return.Ext as Return import Sources import Sources.Services.Dropbox import Sources.Services.Google +import UI.Common.State as Common import UI.Page as Page exposing (Page) import UI.Sources.Form import UI.Sources.Page import UI.Sources.State as Sources +import UI.Sources.Types as Sources import UI.Types as UI exposing (Manager) import Url exposing (Url) @@ -21,14 +23,6 @@ import Url exposing (Url) -- 🔱 -changeUrlUsingPage : Page -> Manager -changeUrlUsingPage page model = - page - |> Page.toString - |> Nav.pushUrl model.navKey - |> return model - - linkClicked : UrlRequest -> Manager linkClicked urlRequest model = case urlRequest of @@ -80,9 +74,8 @@ transition page model = ----------------------------------------- Page.Sources (UI.Sources.Page.NewThroughRedirect service args) -> let - ( sources, form, defaultContext ) = - ( model.sources - , model.sources.form + ( form, defaultContext ) = + ( model.sourceForm , UI.Sources.Form.defaultContext ) in @@ -100,9 +93,8 @@ transition page model = , service = service } - |> (\c -> { form | context = c, step = UI.Sources.Form.By }) - |> (\f -> { sources | form = f }) - |> (\s -> { model | sources = s }) + |> (\c -> { form | context = c, step = Sources.By }) + |> (\f -> { model | sourceForm = f }) |> Return.singleton ----------------------------------------- @@ -144,7 +136,7 @@ loadSourceForForm sourceId model = model.isLoading maybeSource = - List.find (.id >> (==) sourceId) model.sources.collection + List.find (.id >> (==) sourceId) model.sources in case ( isLoading, maybeSource ) of ( False, Just source ) -> @@ -157,6 +149,6 @@ loadSourceForForm sourceId model = ( True, _ ) -> -- Redirect away from edit-source page - changeUrlUsingPage + Common.changeUrlUsingPage (Page.Sources UI.Sources.Page.Index) model diff --git a/src/Applications/UI/Sources.elm b/src/Applications/UI/Sources.elm deleted file mode 100644 index 5f747a8e..00000000 --- a/src/Applications/UI/Sources.elm +++ /dev/null @@ -1,516 +0,0 @@ -module UI.Sources exposing (Model, Msg(..), initialModel, sourcesToProcess, update, view) - -import Alien -import Chunky exposing (..) -import Conditional exposing (ifThenElse) -import Coordinates -import Css.Classes as C -import Dict.Ext as Dict -import Html exposing (Html, text) -import Html.Attributes exposing (href) -import Html.Events.Extra.Mouse as Mouse -import Json.Decode as Json -import List.Extra as List -import Material.Icons as Icons -import Material.Icons.Types exposing (Coloring(..)) -import Return3 as Return exposing (..) -import Sources exposing (..) -import Sources.Encoding -import Time -import Time.Ext as Time -import UI.Kit exposing (ButtonType(..)) -import UI.List -import UI.Navigation exposing (..) -import UI.Page as Page -import UI.Ports as Ports -import UI.Reply exposing (Reply(..)) -import UI.Sources.Form as Form -import UI.Sources.Page as Sources exposing (..) - - - --- 🌳 - - -type alias Model = - { collection : List Source - , currentTime : Time.Posix - , form : Form.Model - , isProcessing : List ( String, Float ) - , processingError : Maybe { error : String, sourceId : String } - , processingNotificationId : Maybe Int - } - - -initialModel : Model -initialModel = - { collection = [] - , currentTime = Time.default - , form = Form.initialModel - , isProcessing = [] - , processingError = Nothing - , processingNotificationId = Nothing - } - - - --- 📣 - - -type Msg - = Bypass - | FinishedProcessingSource String - | FinishedProcessing - | Process - | ReportProcessingError Json.Value - | ReportProcessingProgress Json.Value - | StopProcessing - ----------------------------------------- - -- Children - ----------------------------------------- - | FormMsg Form.Msg - ----------------------------------------- - -- Collection - ----------------------------------------- - | AddToCollection Source - | RemoveFromCollection { sourceId : String } - | UpdateSourceData Json.Value - ----------------------------------------- - -- Individual - ----------------------------------------- - | SourceContextMenu Source Mouse.Event - | ToggleActivation { sourceId : String } - | ToggleDirectoryPlaylists { sourceId : String } - - -update : Msg -> Model -> Return Model Msg Reply -update msg model = - case msg of - Bypass -> - return model - - FinishedProcessing -> - model.processingNotificationId - |> Maybe.map (\id -> [ DismissNotification { id = id } ]) - |> Maybe.withDefault [] - |> Return.repliesWithModel { model | isProcessing = [] } - - FinishedProcessingSource sourceId -> - return { model | isProcessing = List.filter (Tuple.first >> (/=) sourceId) model.isProcessing } - - Process -> - model - |> sourcesToProcess - |> ProcessSources - |> returnReplyWithModel model - - ReportProcessingError json -> - case Json.decodeValue (Json.dict Json.string) json of - Ok dict -> - let - args = - { error = Dict.fetch "error" "" dict - , sourceId = Dict.fetch "sourceId" "" dict - } - in - dict - |> Dict.fetch "error" "missingError" - |> ShowStickyErrorNotificationWithCode - ("Could not process the _" - ++ Dict.fetch "sourceName" "" dict - ++ "_ source. I got the following response from the source:" - ) - |> returnReplyWithModel - { model | processingError = Just args } - - Err _ -> - "Could not decode processing error" - |> ShowStickyErrorNotification - |> returnReplyWithModel model - - ReportProcessingProgress json -> - 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.isProcessing - |> List.map - (\( sid, pro ) -> - ifThenElse (sid == sourceId) - ( sid, progress ) - ( sid, pro ) - ) - |> (\isProcessing -> - { model | isProcessing = isProcessing } - ) - |> return - - Err _ -> - "Could not decode processing progress" - |> ShowStickyErrorNotification - |> returnReplyWithModel model - - StopProcessing -> - case model.processingNotificationId of - Just notificationId -> - Alien.StopProcessing - |> Alien.trigger - |> Ports.toBrain - |> returnCommandWithModel - { model - | isProcessing = [] - , processingNotificationId = Nothing - } - |> addReply (DismissNotification { id = notificationId }) - - Nothing -> - return model - - ----------------------------------------- - -- Children - ----------------------------------------- - FormMsg sub -> - model.form - |> Form.update sub - |> mapModel (\f -> { model | form = f }) - |> mapCmd FormMsg - - ----------------------------------------- - -- Collection - ----------------------------------------- - AddToCollection unsuitableSource -> - let - source = - setProperId - (List.length model.collection + 1) - model.currentTime - unsuitableSource - in - returnRepliesWithModel - { model | collection = model.collection ++ [ source ] } - [ UI.Reply.SaveSources - , UI.Reply.ProcessSources [ source ] - ] - - RemoveFromCollection { sourceId } -> - model.collection - |> List.filter (.id >> (/=) sourceId) - |> (\c -> { model | collection = c }) - |> return - |> addReplies - [ UI.Reply.SaveSources - , UI.Reply.RemoveTracksWithSourceId sourceId - ] - - UpdateSourceData json -> - json - |> Sources.Encoding.decode - |> Maybe.map - (\source -> - List.map - (\s -> - if s.id == source.id then - source - - else - s - ) - model.collection - ) - |> Maybe.map (\col -> { model | collection = col }) - |> Maybe.withDefault model - |> return - |> addReply UI.Reply.SaveSources - - ----------------------------------------- - -- Individual - ----------------------------------------- - SourceContextMenu source mouseEvent -> - returnRepliesWithModel - model - [ ShowSourceContextMenu - (Coordinates.fromTuple mouseEvent.clientPos) - source - ] - - ToggleActivation { sourceId } -> - model.collection - |> List.map - (\source -> - if source.id == sourceId then - { source | enabled = not source.enabled } - - else - source - ) - |> (\collection -> { model | collection = collection }) - |> return - |> addReply SaveSources - - ToggleDirectoryPlaylists { sourceId } -> - model.collection - |> List.map - (\source -> - if source.id == sourceId then - { source | directoryPlaylists = not source.directoryPlaylists } - - else - source - ) - |> (\collection -> { model | collection = collection }) - |> return - |> addReply SaveSources - |> addReply GenerateDirectoryPlaylists - - -sourcesToProcess : Model -> List Source -sourcesToProcess model = - List.filter (.enabled >> (==) True) model.collection - - - --- 🗺 - - -view : { amountOfTracks : Int } -> Sources.Page -> Model -> Html Msg -view { amountOfTracks } page model = - UI.Kit.receptacle - { scrolling = True } - (case page of - Index -> - index amountOfTracks model - - Edit sourceId -> - List.map (Html.map FormMsg) (Form.edit model.form) - - New -> - List.map (Html.map FormMsg) (Form.new { onboarding = False } model.form) - - NewOnboarding -> - List.map (Html.map FormMsg) (Form.new { onboarding = True } model.form) - - NewThroughRedirect _ _ -> - List.map (Html.map FormMsg) (Form.new { onboarding = False } model.form) - - Rename sourceId -> - List.map (Html.map FormMsg) (Form.rename model.form) - ) - - - --- INDEX - - -index : Int -> Model -> List (Html Msg) -index amountOfTracks model = - [ ----------------------------------------- - -- Navigation - ----------------------------------------- - if List.isEmpty model.collection then - UI.Navigation.local - [ ( Icon Icons.add - , Label "Add a new source" Shown - , NavigateToPage (Page.Sources New) - ) - ] - - else - UI.Navigation.local - [ ( Icon Icons.add - , Label "Add a new source" Shown - , NavigateToPage (Page.Sources New) - ) - - -- Process - ---------- - , if List.isEmpty model.isProcessing then - ( Icon Icons.sync - , Label "Process sources" Shown - , PerformMsg Process - ) - - else - ( Icon Icons.sync - , Label "Stop processing ..." Shown - , PerformMsg StopProcessing - ) - ] - - ----------------------------------------- - -- Content - ----------------------------------------- - , if List.isEmpty model.collection then - chunk - [ C.relative ] - [ chunk - [ C.absolute, C.left_0, C.top_0 ] - [ UI.Kit.canister [ UI.Kit.h1 "Sources" ] ] - ] - - else - UI.Kit.canister - [ UI.Kit.h1 "Sources" - - -- Intro - -------- - , intro amountOfTracks - - -- List - ------- - , model.collection - |> List.sortBy - (.data >> Dict.fetch "name" "") - |> List.map - (\source -> - { label = Html.text (Dict.fetch "name" "" source.data) - , actions = sourceActions model.isProcessing model.processingError source - , msg = Nothing - , isSelected = False - } - ) - |> UI.List.view UI.List.Normal - ] - - -- - , if List.isEmpty model.collection then - UI.Kit.centeredContent - [ slab - Html.a - [ href (Page.toString <| Page.Sources New) ] - [ C.block - , C.opacity_30 - , C.text_inherit - ] - [ Icons.music_note 64 Inherit ] - , slab - Html.a - [ href (Page.toString <| Page.Sources New) ] - [ C.block - , C.leading_normal - , C.mt_2 - , C.opacity_40 - , C.text_center - , C.text_inherit - ] - [ text "A source is a place where music is stored," - , lineBreak - , text "add one so you can play some music " - , inline - [ C.align_middle, C.inline_block, C.minus_mt_px ] - [ Icons.add 14 Inherit ] - ] - ] - - else - nothing - ] - - -intro : Int -> Html Msg -intro amountOfTracks = - [ text "A source is a place where your music is stored." - , lineBreak - , text "By connecting a source, the application will scan it and keep a list of all the music in it." - , lineBreak - , text "You currently have " - , text (String.fromInt amountOfTracks) - , text " " - , text (ifThenElse (amountOfTracks == 1) "track" "tracks") - , text " in your collection." - ] - |> raw - |> UI.Kit.intro - - -sourceActions : List ( String, Float ) -> Maybe { error : String, sourceId : String } -> Source -> List (UI.List.Action Msg) -sourceActions isProcessing processingError source = - let - processIndex = - List.findIndex (Tuple.first >> (==) source.id) isProcessing - - process = - Maybe.andThen (\idx -> List.getAt idx isProcessing) processIndex - in - List.append - (case ( process, processingError ) of - ( Just ( _, progress ), _ ) -> - [ { icon = - \_ _ -> - if progress < 0.05 then - inline - [ C.inline_block, C.opacity_70, C.px_1 ] - [ case processIndex of - Just 0 -> - Html.text "Listing" - - _ -> - Html.text "Waiting" - ] - - else - progress - |> (*) 100 - |> round - |> String.fromInt - |> (\s -> s ++ "%") - |> Html.text - |> List.singleton - |> inline [ C.inline_block, C.opacity_70, C.px_1 ] - , msg = Nothing - , title = "" - } - , { icon = Icons.sync - , msg = Nothing - , title = "Currently processing" - } - ] - - ( Nothing, Just { error, sourceId } ) -> - if sourceId == source.id then - [ { icon = \size _ -> Icons.error_outline size (Color UI.Kit.colors.error) - , msg = Nothing - , title = error - } - ] - - else - [] - - _ -> - [] - ) - [ { icon = - if source.enabled then - Icons.check - - else - Icons.block - , msg = - { sourceId = source.id } - |> ToggleActivation - |> always - |> Just - , title = - if source.enabled then - "Enabled (click to disable)" - - else - "Disabled (click to enable)" - } - - -- - , { icon = Icons.more_vert - , msg = Just (SourceContextMenu source) - , title = "Menu" - } - ] diff --git a/src/Applications/UI/Sources/Form.elm b/src/Applications/UI/Sources/Form.elm index 33040be4..0f8546c0 100644 --- a/src/Applications/UI/Sources/Form.elm +++ b/src/Applications/UI/Sources/Form.elm @@ -1,4 +1,4 @@ -module UI.Sources.Form exposing (FormStep(..), Model, Msg(..), defaultContext, edit, initialModel, new, rename, takeStepBackwards, takeStepForwards, update) +module UI.Sources.Form exposing (..) import Chunky exposing (..) import Common exposing (boolFromString, boolToString) @@ -22,25 +22,15 @@ import UI.Navigation exposing (..) import UI.Page as Page import UI.Reply exposing (Reply(..)) import UI.Sources.Page as Sources +import UI.Sources.Types exposing (..) +import UI.Types exposing (Model) -- 🌳 -type alias Model = - { step : FormStep - , context : Source - } - - -type FormStep - = Where - | How - | By - - -initialModel : Model +initialModel : Form initialModel = { step = Where , context = defaultContext @@ -63,133 +53,6 @@ defaultService = --- 📣 - - -type Msg - = AddSource - | Bypass - | EditSource - | RenameSource - | ReturnToIndex - | SelectService String - | SetData String String - | TakeStep - | TakeStepBackwards - - -update : Msg -> Model -> Return Model Msg Reply -update msg model = - case msg of - AddSource -> - let - context = - model.context - - cleanContext = - { context | data = Dict.map (always String.trim) context.data } - in - returnRepliesWithModel - { model | step = Where, context = defaultContext } - [ GoToPage (Page.Sources Sources.Index) - , AddSourceToCollection cleanContext - ] - - Bypass -> - return model - - EditSource -> - returnRepliesWithModel - { model | step = Where, context = defaultContext } - [ ReplaceSourceInCollection model.context - , ProcessSources [ model.context ] - , GoToPage (Page.Sources Sources.Index) - ] - - RenameSource -> - returnRepliesWithModel - { model | step = Where, context = defaultContext } - [ ReplaceSourceInCollection model.context - , GoToPage (Page.Sources Sources.Index) - ] - - ReturnToIndex -> - returnRepliesWithModel - model - [ GoToPage (Page.Sources Sources.Index) ] - - SelectService serviceKey -> - case Services.keyToType serviceKey of - Just service -> - let - ( context, data ) = - ( model.context - , Services.initialData service - ) - - newContext = - { context | data = data, service = service } - in - return { model | context = newContext } - - Nothing -> - return model - - SetData key value -> - let - context = - model.context - - updatedData = - Dict.insert key value context.data - - newContext = - { context | data = updatedData } - in - return { model | context = newContext } - - TakeStep -> - case ( model.step, model.context.service ) of - ( How, Dropbox ) -> - model.context.data - |> Sources.Services.Dropbox.authorizationUrl - |> ExternalSourceAuthorization - |> returnReplyWithModel model - - ( How, Google ) -> - model.context.data - |> Sources.Services.Google.authorizationUrl - |> ExternalSourceAuthorization - |> returnReplyWithModel model - - _ -> - return { model | step = takeStepForwards model.step } - - TakeStepBackwards -> - return { model | step = takeStepBackwards model.step } - - -takeStepForwards : FormStep -> FormStep -takeStepForwards currentStep = - case currentStep of - Where -> - How - - _ -> - By - - -takeStepBackwards : FormStep -> FormStep -takeStepBackwards currentStep = - case currentStep of - By -> - How - - _ -> - Where - - - -- NEW @@ -197,7 +60,7 @@ type alias Arguments = { onboarding : Bool } -new : Arguments -> Model -> List (Html Msg) +new : Arguments -> Form -> List (Html Msg) new args model = case model.step of Where -> @@ -210,7 +73,7 @@ new args model = newBy model -newWhere : Arguments -> Model -> List (Html Msg) +newWhere : Arguments -> Form -> List (Html Msg) newWhere { onboarding } { context } = [ ----------------------------------------- -- Navigation @@ -264,7 +127,7 @@ newWhere { onboarding } { context } = ] -newHow : Model -> List (Html Msg) +newHow : Form -> List (Html Msg) newHow { context } = [ ----------------------------------------- -- Navigation @@ -338,7 +201,7 @@ howNote = ] -newBy : Model -> List (Html Msg) +newBy : Form -> List (Html Msg) newBy { context } = [ ----------------------------------------- -- Navigation @@ -354,7 +217,7 @@ newBy { context } = -- Content ----------------------------------------- , (\h -> - form AddSource + form AddSourceUsingForm [ UI.Kit.canisterForm h ] ) [ UI.Kit.h2 "One last thing" @@ -376,7 +239,7 @@ newBy { context } = ] [ UI.Kit.textField [ name "name" - , onInput (SetData "name") + , onInput (SetFormData "name") , value nameValue ] ] @@ -442,7 +305,7 @@ corsWarning id = -- EDIT -edit : Model -> List (Html Msg) +edit : Form -> List (Html Msg) edit { context } = [ ----------------------------------------- -- Navigation @@ -458,7 +321,7 @@ edit { context } = -- Content ----------------------------------------- , (\h -> - form EditSource + form EditSourceUsingForm [ chunk [ C.text_left, C.w_full ] [ UI.Kit.canister h ] @@ -536,14 +399,14 @@ renderProperty context property = bool |> not |> boolToString - |> SetData property.key + |> SetFormData property.key } ] else UI.Kit.textField [ name property.key - , onInput (SetData property.key) + , onInput (SetFormData property.key) , placeholder property.placeholder , required (property.label |> String.toLower |> String.contains "optional" |> not) , type_ (ifThenElse property.password "password" "text") @@ -658,7 +521,7 @@ note service = -- RENAME -rename : Model -> List (Html Msg) +rename : Form -> List (Html Msg) rename { context } = [ ----------------------------------------- -- Navigation @@ -674,7 +537,7 @@ rename { context } = -- Content ----------------------------------------- , (\h -> - form RenameSource + form RenameSourceUsingForm [ UI.Kit.canisterForm h ] ) [ UI.Kit.h2 "Name your source" @@ -682,7 +545,7 @@ rename { context } = -- Input -------- , [ name "name" - , onInput (SetData "name") + , onInput (SetFormData "name") , value (Dict.fetch "name" "" context.data) ] |> UI.Kit.textField diff --git a/src/Applications/UI/Sources/State.elm b/src/Applications/UI/Sources/State.elm index 9ac96221..16b29f90 100644 --- a/src/Applications/UI/Sources/State.elm +++ b/src/Applications/UI/Sources/State.elm @@ -1,21 +1,519 @@ module UI.Sources.State exposing (..) +import Alien +import Common +import Conditional exposing (ifThenElse) +import Coordinates +import Dict +import Dict.Ext as Dict +import Html.Events.Extra.Mouse as Mouse +import Json.Decode as Json +import Json.Encode import Monocle.Lens as Lens exposing (Lens) +import Notifications +import Return exposing (andThen, return) +import Return.Ext as Return +import Sources exposing (..) +import Sources.Encoding as Sources +import Sources.Services as Services +import Sources.Services.Dropbox +import Sources.Services.Google +import UI.Common.State as Common +import UI.Page as Page +import UI.Ports as Ports +import UI.Reply as Reply +import UI.Sources.ContextMenu as Sources +import UI.Sources.Form as Form +import UI.Sources.Page as Sources +import UI.Sources.Types exposing (..) +import UI.Types as UI exposing (Manager, Model) +import UI.User.State.Export as User -- 🌳 -lens = - { get = .sources - , set = \sources m -> { m | sources = sources } +formLens = + { get = .sourceForm + , set = \form m -> { m | sourceForm = form } } -formLens = +formContextLens = Lens.compose - lens - { get = .form - , set = \form m -> { m | form = form } + formLens + { get = .context + , set = \context m -> { m | context = context } } + + +formStepLens = + Lens.compose + formLens + { get = .step + , set = \step m -> { m | step = step } + } + + + +-- 📣 + + +update : Msg -> Manager +update msg = + case msg of + Bypass -> + Return.singleton + + -- + FinishedProcessingSource a -> + finishedProcessingSource a + + FinishedProcessing -> + finishedProcessing + + Process -> + process + + 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 + + + +-- 🛠 + + +finishedProcessing : Manager +finishedProcessing model = + (case model.processingNotificationId of + Just id -> + Common.dismissNotification { id = id } + + Nothing -> + Return.singleton + ) + { model | processingContext = [] } + + +finishedProcessingSource : { sourceId : String } -> Manager +finishedProcessingSource { sourceId } model = + model.processingContext + |> List.filter (Tuple.first >> (/=) sourceId) + |> (\newContext -> { model | processingContext = newContext }) + |> Return.singleton + + +process : Manager +process model = + case sourcesToProcess model of + [] -> + Return.singleton model + + toProcess -> + let + notification = + Notifications.stickyWarning "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 -> Manager +reportProcessingError 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 -> Manager +reportProcessingProgress 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 : Manager +stopProcessing 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 -> Manager +addToCollection unsuitableSource model = + let + source = + setProperId + (List.length model.sources + 1) + model.currentTime + unsuitableSource + in + { model | sources = model.sources ++ [ source ] } + |> Return.performance (UI.Reply Reply.SaveSources) + |> andThen process + + +removeFromCollection : { sourceId : String } -> Manager +removeFromCollection { sourceId } model = + model.sources + |> List.filter (.id >> (/=) sourceId) + |> (\c -> { model | sources = c }) + |> Return.singleton + |> andThen (Return.performance <| UI.Reply Reply.SaveSources) + |> andThen (Return.performance <| UI.Reply <| Reply.RemoveTracksWithSourceId sourceId) + + +updateSourceData : Json.Value -> Manager +updateSourceData 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 + |> Return.performance (UI.Reply Reply.SaveSources) + + + +-- FORM + + +addSourceUsingForm : Manager +addSourceUsingForm 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 : Manager +editSourceUsingForm model = + model + |> formLens.set Form.initialModel + |> replaceSourceInCollection model.sourceForm.context + |> andThen process + |> andThen returnToIndex + + +renameSourceUsingForm : Manager +renameSourceUsingForm model = + model + |> formLens.set Form.initialModel + |> replaceSourceInCollection model.sourceForm.context + |> andThen returnToIndex + + +returnToIndex : Manager +returnToIndex = + Common.changeUrlUsingPage (Page.Sources Sources.Index) + + +selectService : String -> Manager +selectService 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 -> Manager +setFormData key value model = + model + |> Lens.modify + formContextLens + (\context -> + context.data + |> Dict.insert key value + |> (\data -> { context | data = data }) + ) + |> Return.singleton + + +takeStep : Manager +takeStep model = + let + form = + formLens.get model + in + case ( form.step, form.context.service ) of + ( How, Dropbox ) -> + form.context.data + |> Sources.Services.Dropbox.authorizationUrl + |> Reply.ExternalSourceAuthorization + |> UI.Reply + |> Return.performanceF model + + ( How, Google ) -> + form.context.data + |> Sources.Services.Google.authorizationUrl + |> Reply.ExternalSourceAuthorization + |> UI.Reply + |> Return.performanceF model + + _ -> + model + |> Lens.modify formStepLens takeStepForwards + |> Return.singleton + + +takeStepBackwards : Manager +takeStepBackwards = + Lens.modify formStepLens takeStepBackwards_ >> Return.singleton + + + +-- INDIVIDUAL + + +sourceContextMenu : Source -> Mouse.Event -> Manager +sourceContextMenu source mouseEvent model = + mouseEvent.clientPos + |> Coordinates.fromTuple + |> Sources.sourceMenu source + |> Common.showContextMenuWithModel model + + +toggleActivation : { sourceId : String } -> Manager +toggleActivation { sourceId } model = + model.sources + |> List.map + (\source -> + if source.id == sourceId then + { source | enabled = not source.enabled } + + else + source + ) + |> (\collection -> { model | sources = collection }) + |> Return.performance (UI.Reply Reply.SaveSources) + + +toggleDirectoryPlaylists : { sourceId : String } -> Manager +toggleDirectoryPlaylists { sourceId } model = + model.sources + |> List.map + (\source -> + if source.id == sourceId then + { source | directoryPlaylists = not source.directoryPlaylists } + + else + source + ) + |> (\collection -> { model | sources = collection }) + |> Return.performance (UI.Reply Reply.SaveSources) + |> andThen (Return.performance <| UI.Reply Reply.GenerateDirectoryPlaylists) + + + +-- ⚗️ + + +replaceSourceInCollection : Source -> Manager +replaceSourceInCollection source model = + model.sources + |> List.map (\s -> ifThenElse (s.id == source.id) source s) + |> (\s -> { model | sources = s }) + |> Return.performance (UI.Reply Reply.SaveSources) + + +sourcesToProcess : Model -> List Source +sourcesToProcess model = + List.filter (.enabled >> (==) True) model.sources + + +takeStepForwards : FormStep -> FormStep +takeStepForwards currentStep = + case currentStep of + Where -> + How + + _ -> + By + + +takeStepBackwards_ : FormStep -> FormStep +takeStepBackwards_ currentStep = + case currentStep of + By -> + How + + _ -> + Where diff --git a/src/Applications/UI/Sources/Types.elm b/src/Applications/UI/Sources/Types.elm new file mode 100644 index 00000000..f0f0f7cd --- /dev/null +++ b/src/Applications/UI/Sources/Types.elm @@ -0,0 +1,59 @@ +module UI.Sources.Types exposing (..) + +import Html.Events.Extra.Mouse as Mouse +import Json.Decode as Json +import Sources exposing (..) + + + +-- 🌳 + + +type alias Form = + { context : Source + , step : FormStep + } + + +type FormStep + = Where + | How + | By + + + +-- 📣 + + +type Msg + = Bypass + -- + | FinishedProcessingSource { sourceId : String } + | FinishedProcessing + | Process + | ReportProcessingError Json.Value + | ReportProcessingProgress Json.Value + | StopProcessing + ----------------------------------------- + -- Collection + ----------------------------------------- + | AddToCollection Source + | RemoveFromCollection { sourceId : String } + | UpdateSourceData Json.Value + ----------------------------------------- + -- Form + ----------------------------------------- + | AddSourceUsingForm + | EditSourceUsingForm + | RenameSourceUsingForm + | ReturnToIndex + | SelectService String + | SetFormData String String + | TakeStep + | TakeStepBackwards + ----------------------------------------- + -- Individual + ----------------------------------------- + | SourceContextMenu Source Mouse.Event + | ToggleActivation { sourceId : String } + | ToggleDirectoryPlaylists { sourceId : String } diff --git a/src/Applications/UI/Sources/View.elm b/src/Applications/UI/Sources/View.elm new file mode 100644 index 00000000..9826bc81 --- /dev/null +++ b/src/Applications/UI/Sources/View.elm @@ -0,0 +1,293 @@ +module UI.Sources.View exposing (view) + +import Alien +import Chunky exposing (..) +import Conditional exposing (ifThenElse) +import Coordinates +import Css.Classes as C +import Dict.Ext as Dict +import Html exposing (Html, text) +import Html.Attributes exposing (href) +import Html.Events.Extra.Mouse as Mouse +import Html.Lazy as Lazy +import Json.Decode as Json +import List.Extra as List +import Material.Icons as Icons +import Material.Icons.Types exposing (Coloring(..)) +import Return3 as Return exposing (..) +import Sources exposing (..) +import Sources.Encoding +import Time +import Time.Ext as Time +import UI.Kit exposing (ButtonType(..)) +import UI.List +import UI.Navigation exposing (..) +import UI.Page as Page +import UI.Ports as Ports +import UI.Reply exposing (Reply(..)) +import UI.Sources.Form as Form +import UI.Sources.Page as Sources exposing (..) +import UI.Sources.Types exposing (Msg(..)) +import UI.Types as UI exposing (Model) + + + +-- 🌳 + + +type alias PartialModel = + { processingContext : List ( String, Float ) + , processingError : Maybe { error : String, sourceId : String } + , sources : List Source + } + + + +-- 🗺 + + +view : Sources.Page -> Model -> Html UI.Msg +view page model = + Html.map UI.SourcesMsg + (case page of + Index -> + Lazy.lazy2 + (\a b -> receptacle <| index a b) + (List.length model.tracks.collection.untouched) + { processingContext = model.processingContext + , processingError = model.processingError + , sources = model.sources + } + + Edit sourceId -> + lazyForm model <| Form.edit + + New -> + lazyForm model <| Form.new { onboarding = False } + + NewOnboarding -> + lazyForm model <| Form.new { onboarding = True } + + NewThroughRedirect _ _ -> + lazyForm model <| Form.new { onboarding = False } + + Rename sourceId -> + lazyForm model <| Form.rename + ) + + +lazyForm model formView = + Lazy.lazy (formView >> receptacle) model.sourceForm + + +receptacle = + UI.Kit.receptacle { scrolling = True } + + + +-- INDEX + + +index : Int -> PartialModel -> List (Html Msg) +index amountOfTracks model = + [ ----------------------------------------- + -- Navigation + ----------------------------------------- + if List.isEmpty model.sources then + UI.Navigation.local + [ ( Icon Icons.add + , Label "Add a new source" Shown + , NavigateToPage (Page.Sources New) + ) + ] + + else + UI.Navigation.local + [ ( Icon Icons.add + , Label "Add a new source" Shown + , NavigateToPage (Page.Sources New) + ) + + -- Process + ---------- + , if List.isEmpty model.processingContext then + ( Icon Icons.sync + , Label "Process sources" Shown + , PerformMsg Process + ) + + else + ( Icon Icons.sync + , Label "Stop processing ..." Shown + , PerformMsg StopProcessing + ) + ] + + ----------------------------------------- + -- Content + ----------------------------------------- + , if List.isEmpty model.sources then + chunk + [ C.relative ] + [ chunk + [ C.absolute, C.left_0, C.top_0 ] + [ UI.Kit.canister [ UI.Kit.h1 "Sources" ] ] + ] + + else + UI.Kit.canister + [ UI.Kit.h1 "Sources" + + -- Intro + -------- + , intro amountOfTracks + + -- List + ------- + , model.sources + |> List.sortBy + (.data >> Dict.fetch "name" "") + |> List.map + (\source -> + { label = Html.text (Dict.fetch "name" "" source.data) + , actions = sourceActions model.processingContext model.processingError source + , msg = Nothing + , isSelected = False + } + ) + |> UI.List.view UI.List.Normal + ] + + -- + , if List.isEmpty model.sources then + UI.Kit.centeredContent + [ slab + Html.a + [ href (Page.toString <| Page.Sources New) ] + [ C.block + , C.opacity_30 + , C.text_inherit + ] + [ Icons.music_note 64 Inherit ] + , slab + Html.a + [ href (Page.toString <| Page.Sources New) ] + [ C.block + , C.leading_normal + , C.mt_2 + , C.opacity_40 + , C.text_center + , C.text_inherit + ] + [ text "A source is a place where music is stored," + , lineBreak + , text "add one so you can play some music " + , inline + [ C.align_middle, C.inline_block, C.minus_mt_px ] + [ Icons.add 14 Inherit ] + ] + ] + + else + nothing + ] + + +intro : Int -> Html Msg +intro amountOfTracks = + [ text "A source is a place where your music is stored." + , lineBreak + , text "By connecting a source, the application will scan it and keep a list of all the music in it." + , lineBreak + , text "You currently have " + , text (String.fromInt amountOfTracks) + , text " " + , text (ifThenElse (amountOfTracks == 1) "track" "tracks") + , text " in your collection." + ] + |> raw + |> UI.Kit.intro + + +sourceActions : List ( String, Float ) -> Maybe { error : String, sourceId : String } -> Source -> List (UI.List.Action Msg) +sourceActions processingContext processingError source = + let + processIndex = + List.findIndex (Tuple.first >> (==) source.id) processingContext + + process = + Maybe.andThen (\idx -> List.getAt idx processingContext) processIndex + in + List.append + (case ( process, processingError ) of + ( Just ( _, progress ), _ ) -> + [ { icon = + \_ _ -> + if progress < 0.05 then + inline + [ C.inline_block, C.opacity_70, C.px_1 ] + [ case processIndex of + Just 0 -> + Html.text "Listing" + + _ -> + Html.text "Waiting" + ] + + else + progress + |> (*) 100 + |> round + |> String.fromInt + |> (\s -> s ++ "%") + |> Html.text + |> List.singleton + |> inline [ C.inline_block, C.opacity_70, C.px_1 ] + , msg = Nothing + , title = "" + } + , { icon = Icons.sync + , msg = Nothing + , title = "Currently processing" + } + ] + + ( Nothing, Just { error, sourceId } ) -> + if sourceId == source.id then + [ { icon = \size _ -> Icons.error_outline size (Color UI.Kit.colors.error) + , msg = Nothing + , title = error + } + ] + + else + [] + + _ -> + [] + ) + [ { icon = + if source.enabled then + Icons.check + + else + Icons.block + , msg = + { sourceId = source.id } + |> ToggleActivation + |> always + |> Just + , title = + if source.enabled then + "Enabled (click to disable)" + + else + "Disabled (click to enable)" + } + + -- + , { icon = Icons.more_vert + , msg = Just (SourceContextMenu source) + , title = "Menu" + } + ] diff --git a/src/Applications/UI/Tracks.elm b/src/Applications/UI/Tracks.elm index ad92f1d4..b1c6fb3c 100644 --- a/src/Applications/UI/Tracks.elm +++ b/src/Applications/UI/Tracks.elm @@ -867,11 +867,11 @@ navigation maybeGrouping favouritesOnly searchTerm selectedPlaylist isOnIndexPag noTracksView : List String -> Int -> Int -> Int -> Html Msg -noTracksView isProcessing amountOfSources amountOfTracks amountOfFavourites = +noTracksView processingContext amountOfSources amountOfTracks amountOfFavourites = chunk [ C.flex, C.flex_grow ] [ UI.Kit.centeredContent - [ if List.length isProcessing > 0 then + [ if List.length processingContext > 0 then message "Processing Tracks" else if amountOfSources == 0 then diff --git a/src/Applications/UI/Types.elm b/src/Applications/UI/Types.elm index 118d307d..e23d5581 100644 --- a/src/Applications/UI/Types.elm +++ b/src/Applications/UI/Types.elm @@ -24,7 +24,7 @@ import Management import Notifications exposing (Notification) import Playlists exposing (Playlist, PlaylistTrack) import Queue -import Sources +import Sources exposing (Source) import Sources.Encoding as Sources import Time import Tracks @@ -35,8 +35,8 @@ import UI.Notifications import UI.Page as Page exposing (Page) import UI.Queue.Types as Queue import UI.Reply as Reply exposing (Reply(..)) -import UI.Sources as Sources import UI.Sources.ContextMenu as Sources +import UI.Sources.Types as Sources import UI.Tracks as Tracks import UI.Tracks.ContextMenu as Tracks import Url exposing (Protocol(..), Url) @@ -140,6 +140,15 @@ type alias Model = , repeat : Bool , shuffle : Bool + ----------------------------------------- + -- Sources + ----------------------------------------- + , processingContext : List ( String, Float ) + , processingError : Maybe { error : String, sourceId : String } + , processingNotificationId : Maybe Int + , sourceForm : Sources.Form + , sources : List Source + ----------------------------------------- -- 🦉 Nested ----------------------------------------- @@ -148,7 +157,6 @@ type alias Model = ----------------------------------------- -- Children (TODO) ----------------------------------------- - , sources : Sources.Model , tracks : Tracks.Model } @@ -238,7 +246,7 @@ type Msg | GotLastFmSession (Result Http.Error String) | Scrobble { duration : Int, timestamp : Int, trackId : String } ----------------------------------------- - -- Tracks + -- Tracks (TODO: Move to Tracks.Types) ----------------------------------------- | DownloadTracksFinished | FailedToStoreTracksInCache (List String) @@ -255,21 +263,18 @@ type Msg -- ⚗️ Adjunct ----------------------------------------- | KeyboardMsg Keyboard.Msg - ----------------------------------------- - -- 📭 Other - ----------------------------------------- - | SetCurrentTime Time.Posix - | SetIsOnline Bool ----------------------------------------- -- 🦉 Nested ----------------------------------------- | AuthenticationMsg Authentication.Msg | QueueMsg Queue.Msg - ----------------------------------------- - -- Children (TODO) - ----------------------------------------- | SourcesMsg Sources.Msg | TracksMsg Tracks.Msg + ----------------------------------------- + -- 📭 Other + ----------------------------------------- + | SetCurrentTime Time.Posix + | SetIsOnline Bool type alias Organizer model = diff --git a/src/Applications/UI/User/State/Import.elm b/src/Applications/UI/User/State/Import.elm index 89220eec..a414bdd1 100644 --- a/src/Applications/UI/User/State/Import.elm +++ b/src/Applications/UI/User/State/Import.elm @@ -20,8 +20,7 @@ import UI.Playlists.Directory import UI.Ports as Ports import UI.Reply exposing (..) import UI.Reply.Translate as Reply -import UI.Routing.State as Routing -import UI.Sources as Sources +import UI.Sources.State as Sources import UI.Tracks as Tracks import UI.Types as UI exposing (..) import Url.Ext as Url @@ -58,7 +57,7 @@ importJson json model = -- Clear tracks cache |> andThen (Reply.translate ClearTracksCache) -- Redirect to index page - |> andThen (Routing.changeUrlUsingPage Page.Index) + |> andThen (Common.changeUrlUsingPage Page.Index) ----------------------------- -- Save all the imported data ----------------------------- @@ -107,10 +106,7 @@ loadHypaethralUserData json model = |> andThen (\m -> if m.processAutomatically then - m.sources - |> Sources.sourcesToProcess - |> ProcessSources - |> Reply.translateWithModel m + Sources.process m else Return.singleton m @@ -135,9 +131,6 @@ importHypaethral value model = |> Maybe.withDefault Backdrop.default |> Just - sourcesModel = - { sources | collection = data.sources } - newPlaylistsCollection = List.append data.playlists @@ -155,8 +148,7 @@ importHypaethral value model = model.lastFm in ( { model - | sources = sourcesModel - , tracks = tracksModel + | tracks = tracksModel -- , chosenBackdrop = chosenBackdrop @@ -166,6 +158,7 @@ importHypaethral value model = , processAutomatically = Maybe.unwrap True .processAutomatically data.settings , progress = data.progress , rememberProgress = Maybe.unwrap True .rememberProgress data.settings + , sources = data.sources } -- , Cmd.map TracksMsg tracksCmd diff --git a/src/Applications/UI/View.elm b/src/Applications/UI/View.elm index 49d6caf7..79f45320 100644 --- a/src/Applications/UI/View.elm +++ b/src/Applications/UI/View.elm @@ -41,9 +41,9 @@ import UI.Queue.View as Queue import UI.Reply exposing (Reply(..)) import UI.Settings as Settings import UI.Settings.Page -import UI.Sources as Sources import UI.Sources.ContextMenu as Sources import UI.Sources.Page +import UI.Sources.View as Sources import UI.Svg.Elements import UI.Tracks as Tracks import UI.Tracks.ContextMenu as Tracks @@ -158,12 +158,12 @@ defaultScreen model = -- Main ----------------------------------------- , vessel - [ { amountOfSources = List.length model.sources.collection + [ { amountOfSources = List.length model.sources , bgColor = model.extractedBackdropColor , darkMode = model.darkMode , isOnIndexPage = model.page == Page.Index , isTouchDevice = model.isTouchDevice - , sourceIdsBeingProcessed = List.map Tuple.first model.sources.isProcessing + , sourceIdsBeingProcessed = List.map Tuple.first model.processingContext , viewport = model.viewport } |> Tracks.view model.tracks @@ -202,13 +202,7 @@ defaultScreen model = |> Html.map Reply Page.Sources subPage -> - let - amountOfTracks = - List.length model.tracks.collection.untouched - in - model.sources - |> Lazy.lazy3 Sources.view { amountOfTracks = amountOfTracks } subPage - |> Html.map SourcesMsg + Sources.view subPage model ] ----------------------------------------- diff --git a/src/Library/Lens/Ext.elm b/src/Library/Lens/Ext.elm index f01a8c2a..ee39cd28 100644 --- a/src/Library/Lens/Ext.elm +++ b/src/Library/Lens/Ext.elm @@ -3,6 +3,15 @@ module Lens.Ext exposing (..) import Monocle.Lens as Lens exposing (Lens) +{-| Flipped version of `Lens.modify`. +-} adjust : Lens a b -> a -> (b -> b) -> a adjust lens a fn = Lens.modify lens fn a + + +{-| Flipped version of `lens.set`. +-} +replace : Lens a b -> a -> b -> a +replace lens a b = + lens.set b a diff --git a/src/Library/Sources/Encoding.elm b/src/Library/Sources/Encoding.elm index a8a8bb12..722e008d 100644 --- a/src/Library/Sources/Encoding.elm +++ b/src/Library/Sources/Encoding.elm @@ -39,7 +39,8 @@ encodeData data = decode : Decode.Value -> Maybe Source decode value = - Decode.decodeValue decoder value + value + |> Decode.decodeValue decoder |> Result.toMaybe