From 3c24a6995f71412ee96c516387dcd1704d3c2fc2 Mon Sep 17 00:00:00 2001 From: Steven Vandevelde Date: Sat, 30 Mar 2019 20:07:01 +0100 Subject: [PATCH] Improve user data and fix auth issue --- src/Applications/Brain.elm | 11 +++++++ src/Applications/Brain/Authentication.elm | 14 ++++---- src/Applications/Brain/Core.elm | 1 + src/Applications/UI.elm | 12 +++++++ src/Applications/UI/Backdrop.elm | 2 +- src/Applications/UI/Core.elm | 1 + src/Applications/UI/Reply.elm | 1 + src/Applications/UI/UserData.elm | 40 ++++++++++++++--------- src/Library/Alien.elm | 7 ++++ src/Library/Authentication.elm | 39 +++++++++++++++++----- 10 files changed, 97 insertions(+), 31 deletions(-) diff --git a/src/Applications/Brain.elm b/src/Applications/Brain.elm index 1d2089db..31a52cc2 100644 --- a/src/Applications/Brain.elm +++ b/src/Applications/Brain.elm @@ -175,6 +175,13 @@ update msg model = |> hypaethralLenses.setFavourites model |> saveHypaethralData + SaveSettings value -> + value + |> Json.decodeValue (Json.map Just Authentication.settingsDecoder) + |> Result.withDefault model.hypaethralUserData.settings + |> hypaethralLenses.setSettings model + |> saveHypaethralData + SaveSources value -> value |> Json.decodeValue (Json.list Sources.decoder) @@ -274,6 +281,7 @@ updateTracks model sub = hypaethralLenses = { setFavourites = makeHypaethralLens (\h f -> { h | favourites = f }) + , setSettings = makeHypaethralLens (\h s -> { h | settings = s }) , setSources = makeHypaethralLens (\h s -> { h | sources = s }) , setTracks = makeHypaethralLens (\h t -> { h | tracks = t }) } @@ -354,6 +362,9 @@ translateAlienData event = Just Alien.SaveFavourites -> SaveFavourites event.data + Just Alien.SaveSettings -> + SaveSettings event.data + Just Alien.SaveSources -> SaveSources event.data diff --git a/src/Applications/Brain/Authentication.elm b/src/Applications/Brain/Authentication.elm index 7497a8fb..9bfc5f16 100644 --- a/src/Applications/Brain/Authentication.elm +++ b/src/Applications/Brain/Authentication.elm @@ -153,13 +153,15 @@ update msg model = HypaethralDataRetrieved json -> ( { model | performingSignIn = False } -- - , if model.performingSignIn then - json - |> Alien.broadcast Alien.AuthMethod - |> Ports.toCache + , case ( model.performingSignIn, model.method ) of + ( True, Just method ) -> + method + |> encodeMethod + |> Alien.broadcast Alien.AuthMethod + |> Ports.toCache - else - Cmd.none + _ -> + Cmd.none -- , Maybe.andThen (\method -> terminate <| Authenticated method json) diff --git a/src/Applications/Brain/Core.elm b/src/Applications/Brain/Core.elm index 1eeb1916..a8e116f4 100644 --- a/src/Applications/Brain/Core.elm +++ b/src/Applications/Brain/Core.elm @@ -51,5 +51,6 @@ type Msg | LoadHypaethralUserData Json.Value | SaveHypaethralData | SaveFavourites Json.Value + | SaveSettings Json.Value | SaveSources Json.Value | SaveTracks Json.Value diff --git a/src/Applications/UI.elm b/src/Applications/UI.elm index 027a6397..d99337f7 100644 --- a/src/Applications/UI.elm +++ b/src/Applications/UI.elm @@ -229,6 +229,14 @@ update msg model = |> Ports.toBrain |> R2.withModel model + Core.SaveSettings -> + model + |> UserData.gatherSettings + |> Authentication.encodeSettings + |> Alien.broadcast Alien.SaveSettings + |> Ports.toBrain + |> R2.withModel model + Core.SaveSources -> let updateEnabledSourceIdsOnTracks = @@ -373,6 +381,7 @@ update msg model = "diffuse.json" "application/json" ({ favourites = model.tracks.favourites + , settings = Just (UserData.gatherSettings model) , sources = model.sources.collection , tracks = model.tracks.collection.untouched } @@ -523,6 +532,9 @@ translateReply reply = Reply.SaveFavourites -> Core.SaveFavourites + Reply.SaveSettings -> + Core.SaveSettings + Reply.SaveSources -> Core.SaveSources diff --git a/src/Applications/UI/Backdrop.elm b/src/Applications/UI/Backdrop.elm index 8c5d3c24..1f9938e0 100644 --- a/src/Applications/UI/Backdrop.elm +++ b/src/Applications/UI/Backdrop.elm @@ -76,7 +76,7 @@ update msg model = Choose backdrop -> { model | chosen = Just backdrop } |> R2.withNoCmd - |> R3.withReply [ Reply.SaveEnclosedUserData ] + |> R3.withReply [ Reply.SaveSettings ] Load backdrop -> [ backdrop ] diff --git a/src/Applications/UI/Core.elm b/src/Applications/UI/Core.elm index b0da906d..301d896f 100644 --- a/src/Applications/UI/Core.elm +++ b/src/Applications/UI/Core.elm @@ -96,6 +96,7 @@ type Msg | ProcessSources | SaveEnclosedUserData | SaveFavourites + | SaveSettings | SaveSources | SaveTracks | SignOut diff --git a/src/Applications/UI/Reply.elm b/src/Applications/UI/Reply.elm index 5e752e2e..bcd55e9c 100644 --- a/src/Applications/UI/Reply.elm +++ b/src/Applications/UI/Reply.elm @@ -29,6 +29,7 @@ type Reply | ShiftQueue | SaveEnclosedUserData | SaveFavourites + | SaveSettings | SaveSources | SaveTracks | ToggleLoadingScreen Switch diff --git a/src/Applications/UI/UserData.elm b/src/Applications/UI/UserData.elm index d3f0193b..38055bfb 100644 --- a/src/Applications/UI/UserData.elm +++ b/src/Applications/UI/UserData.elm @@ -1,4 +1,4 @@ -module UI.UserData exposing (demo, encodedFavourites, encodedSources, encodedTracks, exportEnclosed, importEnclosed, importHypaethral) +module UI.UserData exposing (demo, encodedFavourites, encodedSources, encodedTracks, exportEnclosed, gatherSettings, importEnclosed, importHypaethral) import Authentication exposing (..) import Base64 @@ -6,6 +6,7 @@ import Common exposing (Switch(..)) import Json.Decode as Json import Json.Decode.Pipeline exposing (..) import Json.Encode +import Maybe.Extra as Maybe import Notifications import Replying as N5 exposing (R3D3) import Return3 as R3 @@ -43,11 +44,27 @@ encodedTracks { tracks } = Json.Encode.list Tracks.encodeTrack tracks.collection.untouched +gatherSettings : UI.Core.Model -> Settings +gatherSettings { backdrop } = + { backgroundImage = backdrop.chosen + } + + importHypaethral : Json.Value -> UI.Core.Model -> R3D3 UI.Core.Model UI.Core.Msg UI.Reply importHypaethral value model = case decodeHypaethral value of Ok data -> let + { backdrop } = + model + + backdropModel = + data.settings + |> Maybe.andThen .backgroundImage + |> Maybe.withDefault UI.Backdrop.default + |> Just + |> (\c -> { backdrop | chosen = c }) + ( sourcesModel, sourcesCmd, sourcesReply ) = importSources model.sources data @@ -55,7 +72,8 @@ importHypaethral value model = importTracks model.tracks data in ( { model - | sources = sourcesModel + | backdrop = backdropModel + , sources = sourcesModel , tracks = tracksModel } , Cmd.batch @@ -131,8 +149,7 @@ exportEnclosed model = } in encodeEnclosed - { backgroundImage = model.backdrop.chosen - , equalizerSettings = equalizerSettings + { equalizerSettings = equalizerSettings , onlyShowFavourites = model.tracks.favouritesOnly , repeat = model.queue.repeat , searchTerm = model.tracks.searchTerm @@ -145,17 +162,12 @@ exportEnclosed model = importEnclosed : Json.Value -> UI.Core.Model -> R3D3 UI.Core.Model UI.Core.Msg UI.Reply importEnclosed value model = let - { backdrop, equalizer, queue, tracks } = + { equalizer, queue, tracks } = model in case decodeEnclosed value of Ok data -> let - newBackDrop = - { backdrop - | chosen = Just (Maybe.withDefault UI.Backdrop.default data.backgroundImage) - } - newEqualizer = { equalizer | low = data.equalizerSettings.low @@ -179,8 +191,7 @@ importEnclosed value model = } in ( { model - | backdrop = newBackDrop - , equalizer = newEqualizer + | equalizer = newEqualizer , queue = newQueue , tracks = newTracks } @@ -189,8 +200,7 @@ importEnclosed value model = ) Err err -> - R3.withNothing - { model | backdrop = { backdrop | chosen = Just UI.Backdrop.default } } + R3.withNothing model @@ -214,7 +224,7 @@ mergeReplies list = --- DEMO +-- DEMO (TODO: Update this to the new structure) demo : Json.Value diff --git a/src/Library/Alien.elm b/src/Library/Alien.elm index d5132684..7b8014c1 100644 --- a/src/Library/Alien.elm +++ b/src/Library/Alien.elm @@ -30,6 +30,7 @@ type Tag | ProcessSources | SaveEnclosedUserData | SaveFavourites + | SaveSettings | SaveSources | SaveTracks | SignIn @@ -107,6 +108,9 @@ tagToString tag = SaveFavourites -> "SAVE_FAVOURITES" + SaveSettings -> + "SAVE_SETTINGS" + SaveSources -> "SAVE_SOURCES" @@ -183,6 +187,9 @@ tagFromString string = "SAVE_FAVOURITES" -> Just SaveFavourites + "SAVE_SETTINGS" -> + Just SaveSettings + "SAVE_SOURCES" -> Just SaveSources diff --git a/src/Library/Authentication.elm b/src/Library/Authentication.elm index 35e40ce4..47050a03 100644 --- a/src/Library/Authentication.elm +++ b/src/Library/Authentication.elm @@ -1,4 +1,4 @@ -module Authentication exposing (EnclosedUserData, HypaethralUserData, Method(..), decodeEnclosed, decodeHypaethral, decodeMethod, emptyHypaethralUserData, enclosedDecoder, encodeEnclosed, encodeHypaethral, encodeMethod, hypaethralDecoder, methodFromString, methodToString) +module Authentication exposing (EnclosedUserData, HypaethralUserData, Method(..), Settings, decodeEnclosed, decodeHypaethral, decodeMethod, emptyHypaethralUserData, enclosedDecoder, encodeEnclosed, encodeHypaethral, encodeMethod, encodeSettings, hypaethralDecoder, methodFromString, methodToString, settingsDecoder) import Equalizer import Json.Decode as Json @@ -22,8 +22,7 @@ type Method type alias EnclosedUserData = - { backgroundImage : Maybe String - , equalizerSettings : Equalizer.Settings + { equalizerSettings : Equalizer.Settings , onlyShowFavourites : Bool , repeat : Bool , searchTerm : Maybe String @@ -35,11 +34,17 @@ type alias EnclosedUserData = type alias HypaethralUserData = { favourites : List Tracks.Favourite + , settings : Maybe Settings , sources : List Sources.Source , tracks : List Tracks.Track } +type alias Settings = + { backgroundImage : Maybe String + } + + -- 🔱 ░░ METHOD @@ -89,21 +94,19 @@ decodeEnclosed = enclosedDecoder : Json.Decoder EnclosedUserData enclosedDecoder = Json.succeed EnclosedUserData - |> required "backgroundImage" (Json.maybe Json.string) |> optional "equalizerSettings" Equalizer.settingsDecoder Equalizer.defaultSettings |> optional "onlyShowFavourites" Json.bool False |> optional "repeat" Json.bool False - |> required "searchTerm" (Json.maybe Json.string) + |> optional "searchTerm" (Json.maybe Json.string) Nothing |> optional "shuffle" Json.bool False |> optional "sortBy" Tracks.sortByDecoder Tracks.Artist |> optional "sortDirection" Tracks.sortDirectionDecoder Tracks.Asc encodeEnclosed : EnclosedUserData -> Json.Value -encodeEnclosed { backgroundImage, equalizerSettings, onlyShowFavourites, repeat, searchTerm, shuffle, sortBy, sortDirection } = +encodeEnclosed { equalizerSettings, onlyShowFavourites, repeat, searchTerm, shuffle, sortBy, sortDirection } = Json.Encode.object - [ ( "backgroundImage", Maybe.unwrap Json.Encode.null Json.Encode.string backgroundImage ) - , ( "equalizerSettings", Equalizer.encodeSettings equalizerSettings ) + [ ( "equalizerSettings", Equalizer.encodeSettings equalizerSettings ) , ( "onlyShowFavourites", Json.Encode.bool onlyShowFavourites ) , ( "repeat", Json.Encode.bool repeat ) , ( "searchTerm", Maybe.unwrap Json.Encode.null Json.Encode.string searchTerm ) @@ -125,23 +128,41 @@ decodeHypaethral = emptyHypaethralUserData : HypaethralUserData emptyHypaethralUserData = { favourites = [] + , settings = Nothing , sources = [] , tracks = [] } encodeHypaethral : HypaethralUserData -> Json.Value -encodeHypaethral { favourites, sources, tracks } = +encodeHypaethral { favourites, settings, sources, tracks } = Json.Encode.object [ ( "favourites", Json.Encode.list Tracks.encodeFavourite favourites ) + , ( "settings", Maybe.unwrap Json.Encode.null encodeSettings settings ) , ( "sources", Json.Encode.list Sources.encode sources ) , ( "tracks", Json.Encode.list Tracks.encodeTrack tracks ) ] +encodeSettings : Settings -> Json.Value +encodeSettings settings = + Json.Encode.object + [ ( "backgroundImage" + , Maybe.unwrap Json.Encode.null Json.Encode.string settings.backgroundImage + ) + ] + + hypaethralDecoder : Json.Decoder HypaethralUserData hypaethralDecoder = Json.succeed HypaethralUserData |> optional "favourites" (Json.listIgnore Tracks.favouriteDecoder) [] + |> optional "settings" (Json.maybe settingsDecoder) Nothing |> optional "sources" (Json.listIgnore Sources.decoder) [] |> optional "tracks" (Json.listIgnore Tracks.trackDecoder) [] + + +settingsDecoder : Json.Decoder Settings +settingsDecoder = + Json.succeed Settings + |> required "backgroundImage" (Json.maybe Json.string) -- 2.51.2