From 493a930d179db367e8610aca6e7ede98c604cd3c Mon Sep 17 00:00:00 2001 From: Steven Vandevelde Date: Wed, 26 Dec 2018 13:39:03 +0100 Subject: [PATCH] Split up user data & add sources collection --- src/Applications/Brain.elm | 20 +++++-- src/Applications/Brain/Authentication.elm | 66 +++++++++++++++++++---- src/Applications/Brain/Reply.elm | 3 +- src/Applications/UI.elm | 31 +++++++++-- src/Applications/UI/Core.elm | 4 +- src/Applications/UI/Kit.elm | 2 +- src/Applications/UI/List.elm | 39 ++++++++++++++ src/Applications/UI/Ports.elm | 5 +- src/Applications/UI/Reply.elm | 9 +++- src/Applications/UI/Sources.elm | 40 +++++++++++--- src/Applications/UI/Sources/Form.elm | 57 +++++++++++++++++--- src/Javascript/index.js | 9 ++++ src/Library/Alien.elm | 38 +++++++++++-- src/Library/Replying.elm | 7 +++ src/README.md | 1 + 15 files changed, 288 insertions(+), 43 deletions(-) create mode 100644 src/Applications/UI/List.elm diff --git a/src/Applications/Brain.elm b/src/Applications/Brain.elm index 83a7e3c8..97476ca8 100644 --- a/src/Applications/Brain.elm +++ b/src/Applications/Brain.elm @@ -81,14 +81,22 @@ translateReply reply = Chill -> Bypass + ----------------------------------------- + -- To UI + ----------------------------------------- HideLoadingScreen -> Alien.HideLoadingScreen |> Alien.trigger |> NotifyUI - LoadUserData data -> + LoadEnclosedUserData data -> data - |> Alien.broadcast Alien.LoadUserData + |> Alien.broadcast Alien.LoadEnclosedUserData + |> NotifyUI + + LoadHypaethralUserData data -> + data + |> Alien.broadcast Alien.LoadHypaethralUserData |> NotifyUI @@ -112,11 +120,17 @@ translateAlienEvent : Alien.Event -> Msg translateAlienEvent event = case Alien.tagFromString event.tag of Just Alien.AuthAnonymous -> - AuthenticationMsg (Brain.Authentication.DataRetrieved event.data) + AuthenticationMsg (Brain.Authentication.HypaethralDataRetrieved event.data) Just Alien.AuthMethod -> AuthenticationMsg (Brain.Authentication.MethodRetrieved event.data) + Just Alien.SaveEnclosedUserData -> + AuthenticationMsg (Brain.Authentication.SaveEnclosedData event.data) + + Just Alien.SaveHypaethralUserData -> + AuthenticationMsg (Brain.Authentication.SaveHypaethralData event.data) + Just Alien.SignIn -> AuthenticationMsg (Brain.Authentication.PerformSignIn event.data) diff --git a/src/Applications/Brain/Authentication.elm b/src/Applications/Brain/Authentication.elm index bfecd58f..b6602601 100644 --- a/src/Applications/Brain/Authentication.elm +++ b/src/Applications/Brain/Authentication.elm @@ -4,8 +4,8 @@ module Brain.Authentication exposing (Model, Msg(..), initialCommand, initialMod Involves: - 1. Local UserData (enable shuffle, EQ settings, etc.) - 2. Remote UserData (sources, tracks, favourites, etc.) + 1. Enclosed UserData (enable shuffle, EQ settings, ... ~> device) + 2. Hypaethral UserData (sources, tracks, favourites, ... ~> account) Methods: @@ -16,7 +16,7 @@ Methods: Steps: 1. Get active method (if none, we're signed out) - 2. Get data + 2. Get unrestricted data -} @@ -46,7 +46,10 @@ initialModel = initialCommand : Cmd Msg initialCommand = - do RetrieveMethod + Cmd.batch + [ do RetrieveMethod + , do RetrieveEnclosedData + ] @@ -60,8 +63,13 @@ type Msg | RetrieveMethod | MethodRetrieved J.Value -- 2. Data - | RetrieveData - | DataRetrieved J.Value + | RetrieveHypaethralData + | HypaethralDataRetrieved J.Value + -- x. Data + | RetrieveEnclosedData + | EnclosedDataRetrieved J.Value + | SaveEnclosedData J.Value + | SaveHypaethralData J.Value update : Msg -> Model -> R3D3 Model Msg Reply @@ -75,7 +83,7 @@ update msg model = Just method -> ( { model | method = Just method } , Cmd.batch - [ do RetrieveData + [ do RetrieveHypaethralData , json |> Alien.broadcast Alien.AuthMethod |> Ports.toCache @@ -115,7 +123,7 @@ update msg model = -- 🚀 Just method -> ( { model | method = Just method } - , do RetrieveData + , do RetrieveHypaethralData , Nothing ) @@ -129,7 +137,7 @@ update msg model = ----------------------------------------- -- # 2 ----------------------------------------- - RetrieveData -> + RetrieveHypaethralData -> ( model , case model.method of -- 🚀 @@ -142,12 +150,48 @@ update msg model = , Nothing ) - DataRetrieved json -> + HypaethralDataRetrieved json -> ( model , noCmd , terminate (Authenticated json) ) + ----------------------------------------- + -- DATA + ----------------------------------------- + RetrieveEnclosedData -> + ( model + , Ports.requestCache (Alien.trigger Alien.AuthEnclosedData) + , Nothing + ) + + EnclosedDataRetrieved json -> + ( model + , noCmd + , Just [ LoadEnclosedUserData json ] + ) + + SaveEnclosedData json -> + ( model + , json + |> Alien.broadcast Alien.AuthEnclosedData + |> Ports.toCache + , Nothing + ) + + SaveHypaethralData json -> + ( model + , case model.method of + -- 🚀 + Just Local -> + Ports.toCache (Alien.broadcast Alien.AuthAnonymous json) + + -- ✋ + Nothing -> + Cmd.none + , Nothing + ) + noCmd : Cmd msg noCmd = @@ -179,7 +223,7 @@ terminate : Termination -> Maybe (List Reply) terminate t = case t of Authenticated json -> - Just [ LoadUserData json ] + Just [ LoadHypaethralUserData json ] NotAuthenticated -> Just [ HideLoadingScreen ] diff --git a/src/Applications/Brain/Reply.elm b/src/Applications/Brain/Reply.elm index cb6fea60..a7149590 100644 --- a/src/Applications/Brain/Reply.elm +++ b/src/Applications/Brain/Reply.elm @@ -11,4 +11,5 @@ type Reply = Chill -- UI | HideLoadingScreen - | LoadUserData Json.Value + | LoadEnclosedUserData Json.Value + | LoadHypaethralUserData Json.Value diff --git a/src/Applications/UI.elm b/src/Applications/UI.elm index c33d7148..2a4cb0cf 100644 --- a/src/Applications/UI.elm +++ b/src/Applications/UI.elm @@ -7,7 +7,7 @@ import Browser.Navigation as Nav import Chunky exposing (..) import Color import Html exposing (Html, div, section, text) -import Html.Attributes exposing (style) +import Html.Attributes exposing (id, style) import Html.Lazy import Json.Encode import Replying exposing (return) @@ -83,7 +83,12 @@ update msg model = , Cmd.none ) - LoadUserData json -> + LoadEnclosedUserData json -> + ( model + , Cmd.none + ) + + LoadHypaethralUserData json -> ( { model | isAuthenticated = True, isLoading = False } , Cmd.none ) @@ -143,6 +148,11 @@ update msg model = ----------------------------------------- -- URL ----------------------------------------- + ChangeUrlUsingPage page -> + ( model + , Nav.pushUrl model.navKey (Page.toString page) + ) + LinkClicked urlRequest -> case urlRequest of Browser.Internal url -> @@ -171,9 +181,15 @@ update msg model = translateReply : Reply -> Msg translateReply reply = case reply of + AddSourceToCollection source -> + SourcesMsg (UI.Sources.AddToCollection source) + Chill -> Bypass + GoToPage page -> + ChangeUrlUsingPage page + updateChild = Replying.updateChild update translateReply @@ -194,8 +210,11 @@ translateAlienEvent event = Just Alien.HideLoadingScreen -> ToggleLoadingScreen Off - Just Alien.LoadUserData -> - LoadUserData event.data + Just Alien.LoadEnclosedUserData -> + LoadEnclosedUserData event.data + + Just Alien.LoadHypaethralUserData -> + LoadHypaethralUserData event.data _ -> Bypass @@ -286,7 +305,9 @@ defaultScreen model = root : List (Html msg) -> Html msg root = section - [ style "color" (Color.toCssString UI.Kit.colors.text) ] + [ id "diffuse" + , style "color" (Color.toCssString UI.Kit.colors.text) + ] content : List (Html msg) -> Html msg diff --git a/src/Applications/UI/Core.elm b/src/Applications/UI/Core.elm index c7cb6d36..19ec8e5d 100644 --- a/src/Applications/UI/Core.elm +++ b/src/Applications/UI/Core.elm @@ -44,7 +44,8 @@ type alias Model = type Msg = Bypass - | LoadUserData Json.Value + | LoadEnclosedUserData Json.Value + | LoadHypaethralUserData Json.Value | ToggleLoadingScreen Switch ----------------------------------------- -- Children @@ -59,6 +60,7 @@ type Msg ----------------------------------------- -- URL ----------------------------------------- + | ChangeUrlUsingPage Page | LinkClicked Browser.UrlRequest | UrlChanged Url diff --git a/src/Applications/UI/Kit.elm b/src/Applications/UI/Kit.elm index 0134872a..f3fa29ca 100644 --- a/src/Applications/UI/Kit.elm +++ b/src/Applications/UI/Kit.elm @@ -205,7 +205,7 @@ h3 text = [ T.f4 , T.fw7 , T.lh_title - , T.mb5 + , T.mb4 ] [ Html.text text ] diff --git a/src/Applications/UI/List.elm b/src/Applications/UI/List.elm new file mode 100644 index 00000000..28cc8c76 --- /dev/null +++ b/src/Applications/UI/List.elm @@ -0,0 +1,39 @@ +module UI.List exposing (view) + +import Chunky exposing (..) +import Color +import Html exposing (Html) +import Html.Attributes exposing (style) +import Tachyons.Classes as T +import UI.Kit + + + +-- 🌳 + + +type alias Item msg = + { label : String + , actions : List (Html msg) + } + + + +-- ⛩ + + +view : List (Item msg) -> Html msg +view = + List.map item >> chunk [ T.f6, T.lh_copy ] + + + +-- 🚯 + + +item : Item msg -> Html msg +item { label } = + block + [ style "border-bottom" ("1px solid " ++ Color.toCssString UI.Kit.colors.subtleBorder) ] + [] + [ Html.text label ] diff --git a/src/Applications/UI/Ports.elm b/src/Applications/UI/Ports.elm index c4a9eb55..68a50631 100644 --- a/src/Applications/UI/Ports.elm +++ b/src/Applications/UI/Ports.elm @@ -1,4 +1,4 @@ -port module UI.Ports exposing (fromBrain, toBrain) +port module UI.Ports exposing (fromBrain, removeFocus, toBrain) import Alien @@ -7,6 +7,9 @@ import Alien -- 📣 +port removeFocus : () -> Cmd msg + + port toBrain : Alien.Event -> Cmd msg diff --git a/src/Applications/UI/Reply.elm b/src/Applications/UI/Reply.elm index faca6359..db4bb16a 100644 --- a/src/Applications/UI/Reply.elm +++ b/src/Applications/UI/Reply.elm @@ -1,7 +1,14 @@ module UI.Reply exposing (Reply(..)) +import Sources exposing (Source) +import UI.Page exposing (Page) + + + -- 🌳 type Reply - = Chill + = AddSourceToCollection Source + | Chill + | GoToPage Page diff --git a/src/Applications/UI/Sources.elm b/src/Applications/UI/Sources.elm index c39f35a4..74b0d7b8 100644 --- a/src/Applications/UI/Sources.elm +++ b/src/Applications/UI/Sources.elm @@ -1,20 +1,21 @@ module UI.Sources exposing (Model, Msg(..), initialModel, update, view) import Chunky exposing (..) +import Dict.Ext as Dict import Html exposing (Html, text) import Material.Icons.Content as Icons import Material.Icons.Navigation as Icons import Material.Icons.Notification as Icons import Replying exposing (R3D3) -import Return2 import Return3 import Sources exposing (..) import Sources.Services as Services import Tachyons.Classes as T import UI.Kit exposing (ButtonType(..), select) +import UI.List import UI.Navigation exposing (..) import UI.Page as Page -import UI.Reply exposing (Reply(..)) +import UI.Reply exposing (Reply) import UI.Sources.Form as Form @@ -23,12 +24,15 @@ import UI.Sources.Form as Form type alias Model = - { form : Form.Model } + { collection : List Source + , form : Form.Model + } initialModel : Model initialModel = - { form = Form.initialModel + { collection = [] + , form = Form.initialModel } @@ -38,6 +42,10 @@ initialModel = type Msg = Bypass + ----------------------------------------- + -- Collection + ----------------------------------------- + | AddToCollection Source ----------------------------------------- -- Children ----------------------------------------- @@ -51,15 +59,24 @@ update msg model = model |> Return3.withNothing + ----------------------------------------- + -- Collection + ----------------------------------------- + AddToCollection source -> + source + |> List.singleton + |> List.append model.collection + |> (\c -> { model | collection = c }) + |> Return3.withNothing + ----------------------------------------- -- Children ----------------------------------------- FormMsg sub -> model.form |> Form.update sub - |> Return2.mapModel (\f -> { model | form = f }) - |> Return2.mapCmd FormMsg - |> Return3.withNoReply + |> Return3.mapModel (\f -> { model | form = f }) + |> Return3.mapCmd FormMsg @@ -103,6 +120,9 @@ index model = ----------------------------------------- , UI.Kit.canister [ UI.Kit.h1 "Sources" + + -- Intro + -------- , [ 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." @@ -111,5 +131,11 @@ index model = ] |> Html.span [] |> UI.Kit.intro + + -- List + ------- + , model.collection + |> List.map (\s -> { label = Dict.fetch "name" "" s.data, actions = [] }) + |> UI.List.view ] ] diff --git a/src/Applications/UI/Sources/Form.elm b/src/Applications/UI/Sources/Form.elm index a238c96b..5f868855 100644 --- a/src/Applications/UI/Sources/Form.elm +++ b/src/Applications/UI/Sources/Form.elm @@ -2,12 +2,15 @@ module UI.Sources.Form exposing (FormStep(..), Model, Msg(..), initialModel, new import Chunky exposing (..) import Conditional exposing (..) +import Dict import Dict.Ext as Dict import Html exposing (Html, strong, text) import Html.Attributes exposing (for, name, placeholder, type_, value) +import Html.Events exposing (onInput) import List.Extra as List import Material.Icons.Alert as Icons import Material.Icons.Navigation as Icons +import Replying exposing (R3D3) import Sources exposing (..) import Sources.Services as Services import Sources.Services.Common @@ -15,6 +18,8 @@ import Tachyons.Classes as T import UI.Kit exposing (ButtonType(..), select) import UI.Navigation exposing (..) import UI.Page as Page +import UI.Ports +import UI.Reply exposing (Reply) @@ -56,17 +61,26 @@ defaultService = type Msg - = SelectService String + = AddSource + | Bypass + | SelectService String + | SetData String String | TakeStep | TakeStepBackwards -update : Msg -> Model -> ( Model, Cmd Msg ) +update : Msg -> Model -> R3D3 Model Msg Reply update msg model = ( ----------------------------------------- -- Model ----------------------------------------- case msg of + AddSource -> + model + + Bypass -> + model + SelectService serviceKey -> case Services.keyToType serviceKey of Just service -> @@ -81,6 +95,16 @@ update msg model = Nothing -> model + SetData key value -> + let + context = + model.context + + updatedData = + Dict.insert key value context.data + in + { model | context = { context | data = updatedData } } + TakeStep -> { model | step = takeStepForwards model.step } @@ -89,7 +113,24 @@ update msg model = ----------------------------------------- -- Command ----------------------------------------- - , Cmd.none + , case msg of + TakeStepBackwards -> + UI.Ports.removeFocus () + + _ -> + Cmd.none + ----------------------------------------- + -- Reply + ----------------------------------------- + , case msg of + AddSource -> + Just + [ UI.Reply.GoToPage (Page.Sources Sources.Index) + , UI.Reply.AddSourceToCollection model.context + ] + + _ -> + Nothing ) @@ -202,7 +243,7 @@ newHow { context } = List.splitAt (ceiling dividingPoint) properties in chunk - [ T.flex ] + [ T.flex, T.pt3 ] [ chunk [ T.flex_grow_1, T.pr3 ] (List.map (renderProperty context) listA) @@ -253,6 +294,7 @@ newBy { context } = [ T.flex, T.mt4, T.justify_center, T.w_100 ] [ UI.Kit.textField [ name "name" + , onInput (SetData "name") , value nameValue ] ] @@ -260,7 +302,7 @@ newBy { context } = -- Note ------- , chunk - [ T.f6, T.flex, T.lh_title, T.mt5, T.o_50, T.tc ] + [ T.f6, T.flex, T.lh_title, T.mt4, T.o_50, T.tc ] [ slab Html.span [] @@ -271,7 +313,7 @@ newBy { context } = [ text "Make sure CORS is enabled" ] ] , chunk - [ T.f6, T.lh_title, T.mb5, T.mt2, T.o_50, T.tc ] + [ T.f6, T.lh_title, T.mb4, T.mt2, T.o_50, T.tc ] [ text "You can find the instructions over " , UI.Kit.link { label = "here", url = "/about#CORS" } ] @@ -280,7 +322,7 @@ newBy { context } = --------- , UI.Kit.button WithText - TakeStep + AddSource (text "Add source") ] ] @@ -297,6 +339,7 @@ renderProperty context property = [ UI.Kit.label [ for property.k ] property.l , UI.Kit.textField [ name property.k + , onInput (SetData property.k) , placeholder property.h , type_ (ifThenElse property.p "password" "text") , value (Dict.fetch property.k "" context.data) diff --git a/src/Javascript/index.js b/src/Javascript/index.js index fb22d95f..db60c4b7 100644 --- a/src/Javascript/index.js +++ b/src/Javascript/index.js @@ -81,3 +81,12 @@ const orchestrion = { // unstallAudio(orchestrion.audio) // } // }) + + + +// Other +// ----- + +app.ports.removeFocus.subscribe(_ => { + if (document.activeElement) document.activeElement.blur() +}) diff --git a/src/Library/Alien.elm b/src/Library/Alien.elm index 87029f1b..8d243769 100644 --- a/src/Library/Alien.elm +++ b/src/Library/Alien.elm @@ -20,13 +20,17 @@ type alias Event = type Tag = AuthAnonymous + | AuthEnclosedData | AuthMethod -- from UI + | SaveEnclosedUserData + | SaveHypaethralUserData | SignIn | SignOut -- to UI | HideLoadingScreen - | LoadUserData + | LoadEnclosedUserData + | LoadHypaethralUserData @@ -66,9 +70,18 @@ tagToString tag = AuthMethod -> "AUTH_METHOD" + AuthEnclosedData -> + "AUTH_ENCLOSED_DATA" + ----------------------------------------- -- From UI ----------------------------------------- + SaveEnclosedUserData -> + "SAVE_ENCLOSED_USER_DATA" + + SaveHypaethralUserData -> + "SAVE_HYPAETHRAL_USER_DATA" + SignIn -> "SIGN_IN" @@ -81,8 +94,11 @@ tagToString tag = HideLoadingScreen -> "HIDE_LOADING_SCREEN" - LoadUserData -> - "LOAD_USER_DATA" + LoadEnclosedUserData -> + "LOAD_ENCLOSED_USER_DATA" + + LoadHypaethralUserData -> + "LOAD_HYPAETHRAL_USER_DATA" tagFromString : String -> Maybe Tag @@ -94,9 +110,18 @@ tagFromString string = "AUTH_METHOD" -> Just AuthMethod + "AUTH_ENCLOSED_DATA" -> + Just AuthEnclosedData + ----------------------------------------- -- From UI ----------------------------------------- + "SAVE_ENCLOSED_USER_DATA" -> + Just SaveEnclosedUserData + + "SAVE_HYPAETHRAL_USER_DATA" -> + Just SaveHypaethralUserData + "SIGN_IN" -> Just SignIn @@ -109,8 +134,11 @@ tagFromString string = "HIDE_LOADING_SCREEN" -> Just HideLoadingScreen - "LOAD_USER_DATA" -> - Just LoadUserData + "LOAD_ENCLOSED_USER_DATA" -> + Just LoadEnclosedUserData + + "LOAD_HYPAETHRAL_USER_DATA" -> + Just LoadHypaethralUserData _ -> Nothing diff --git a/src/Library/Replying.elm b/src/Library/Replying.elm index d81c0279..cc36a390 100644 --- a/src/Library/Replying.elm +++ b/src/Library/Replying.elm @@ -16,6 +16,11 @@ type alias R3D3 model msg reply = -- ⚡️ +{-| Handle the state of a child. + + NOTE: Replies are performed from left to right. + +-} updateChild : (msg -> model -> ( model, Cmd msg )) -> (reply -> msg) @@ -34,6 +39,8 @@ updateChild update translateReply context data = |> reducto update translateReply +{-| Convenience function for returning the standard ( model, Cmd msg ) tuple. +-} return : model -> Cmd msg -> ( model, Cmd msg ) return model msg = ( model, msg ) diff --git a/src/README.md b/src/README.md index b0e3389c..ae81cf7c 100644 --- a/src/README.md +++ b/src/README.md @@ -21,4 +21,5 @@ Elm directories: 🧠 Brain ⚡️ Functions +🚯 Private ``` -- knotmirror 0.0.1