From 839ab49c4c46d60b2a8b74db609f7f1fa4784741 Mon Sep 17 00:00:00 2001 From: Matthias Date: Wed, 25 Oct 2023 11:36:34 +0200 Subject: [PATCH] flatten localstorage --- elm.json.base | 1 + src/Config/UserSettings.elm | 27 +++++++++++++++++++-------- src/Effect.elm | 3 ++- src/Init.elm | 4 +++- src/Model.elm | 2 +- src/Ports.elm | 2 +- src/main.js | 31 +++++++++++++------------------ 7 files changed, 40 insertions(+), 30 deletions(-) diff --git a/elm.json.base b/elm.json.base index d57ba854..ecebd0a1 100644 --- a/elm.json.base +++ b/elm.json.base @@ -43,6 +43,7 @@ "elm-community/easing-functions": "2.0.0", "elm-community/html-extra": "3.4.0", "elm-community/intdict": "3.0.0", + "elm-community/json-extra": "4.3.0", "elm-community/list-extra": "8.6.0", "elm-community/maybe-extra": "5.3.0", "elm-community/result-extra": "2.4.0", diff --git a/src/Config/UserSettings.elm b/src/Config/UserSettings.elm index fadd7e31..469073fe 100644 --- a/src/Config/UserSettings.elm +++ b/src/Config/UserSettings.elm @@ -2,6 +2,7 @@ module Config.UserSettings exposing (..) import Config.Graph exposing (AddressLabelType(..), TxLabelType(..)) import Json.Decode as Decode exposing (Decoder, bool, nullable, string) +import Json.Decode.Extra import Json.Decode.Pipeline exposing (optional, required) import Json.Encode import Model.Currency exposing (Currency(..)) @@ -116,18 +117,28 @@ stringToEdgeLabel s = Value +fromString : Decoder a -> Decoder a +fromString dec = + Decode.string + |> Decode.andThen + (Decode.decodeString dec + >> Result.mapError Decode.errorToString + >> Json.Decode.Extra.fromResult + ) + + decoder : Decoder UserSettings decoder = Decode.succeed UserSettings |> required "selectedLanguage" string - |> optional "lightMode" (nullable bool) Nothing - |> optional "valueDetail" (nullable (Decode.string |> Decode.map stringToValueDetail)) Nothing - |> optional "valueDenomination" (nullable (Decode.string |> Decode.map stringToCurrency)) Nothing - |> optional "addressLabel" (nullable (Decode.string |> Decode.map stringToAddressLabel)) Nothing - |> optional "edgeLabel" (nullable (Decode.string |> Decode.map stringToEdgeLabel)) Nothing - |> optional "showAddressShadowLinks" (nullable bool) Nothing - |> optional "showClusterShadowLinks" (nullable bool) Nothing - |> optional "showDatesInUserLocale" (nullable bool) Nothing + |> optional "lightMode" (nullable bool |> fromString) Nothing + |> optional "valueDetail" (Decode.string |> Decode.map stringToValueDetail |> nullable) Nothing + |> optional "valueDenomination" (Decode.string |> Decode.map stringToCurrency |> nullable) Nothing + |> optional "addressLabel" (Decode.string |> Decode.map stringToAddressLabel |> nullable) Nothing + |> optional "edgeLabel" (Decode.string |> Decode.map stringToEdgeLabel |> nullable) Nothing + |> optional "showAddressShadowLinks" (nullable bool |> fromString) Nothing + |> optional "showClusterShadowLinks" (nullable bool |> fromString) Nothing + |> optional "showDatesInUserLocale" (nullable bool |> fromString) Nothing encoder : UserSettings -> Json.Encode.Value diff --git a/src/Effect.elm b/src/Effect.elm index 5dcf8b26..ffa1acef 100644 --- a/src/Effect.elm +++ b/src/Effect.elm @@ -71,7 +71,8 @@ perform plugins key statusbarToken apiKey effect = Ports.setDirty False SaveUserSettingsEffect model -> - Ports.saveToLocalStorage ( "gs_user_settings", Config.UserSettings.encoder model ) + Config.UserSettings.encoder model + |> Ports.saveToLocalStorage ApiEffect eff -> Effect.Api.perform apiKey (BrowserGotResponseWithHeaders statusbarToken) eff diff --git a/src/Init.elm b/src/Init.elm index 450b5d31..be8aa207 100644 --- a/src/Init.elm +++ b/src/Init.elm @@ -20,7 +20,9 @@ init : Plugins -> Flags -> Url -> key -> ( Model key, List Effect ) init plugins flags url key = let settings = - Json.Decode.decodeValue Config.UserSettings.decoder flags.settings |> Result.withDefault Config.UserSettings.default + flags.localStorage + |> Json.Decode.decodeValue Config.UserSettings.decoder + |> Result.withDefault Config.UserSettings.default ( locale, localeEffect ) = Locale.init settings diff --git a/src/Model.elm b/src/Model.elm index 391cb8ce..1ce9b280 100644 --- a/src/Model.elm +++ b/src/Model.elm @@ -28,7 +28,7 @@ import Url exposing (Url) type alias Flags = - { settings : Json.Encode.Value + { localStorage : Json.Encode.Value , now : Int , width : Int , height : Int diff --git a/src/Ports.elm b/src/Ports.elm index 30b2d5f8..017cb14c 100644 --- a/src/Ports.elm +++ b/src/Ports.elm @@ -33,7 +33,7 @@ port copyToClipboard : String -> Cmd msg port setDirty : Bool -> Cmd msg -port saveToLocalStorage : ( String, Value ) -> Cmd msg +port saveToLocalStorage : Value -> Cmd msg diff --git a/src/main.js b/src/main.js index aa14f31d..fffe5af8 100644 --- a/src/main.js +++ b/src/main.js @@ -15,15 +15,6 @@ const getNavigatorLanguage = () => { const locale = getNavigatorLanguage().split('-')[0] -let userSettings = localStorage.getItem("gs_user_settings"); - -let settings; -if (typeof userSettings !== 'undefined' && userSettings !== null){ - settings = JSON.parse(userSettings) -} else { - settings = { "selectedLanguage": locale} -} - const docElem = document.documentElement const body = document.getElementsByTagName('body')[0] const width = window.innerWidth || docElem.clientWidth || body.clientWidth @@ -37,7 +28,15 @@ for (const plugin in plugins) { pluginFlags[plugin] = plugins[plugin].flags() } -const app = Elm.Main.init({ flags: { settings, width, height, now, pluginFlags } }) +const app = Elm.Main.init( + { flags: + { localStorage: {...localStorage} + , width + , height + , now + , pluginFlags + } + }) let isDirty = false @@ -167,12 +166,8 @@ app.ports.setDirty.subscribe(dirty => { isDirty = dirty }); -app.ports.saveToLocalStorage.subscribe(data_tuple => { - let k, v; - [k, v] = data_tuple; - localStorage.setItem(k, JSON.stringify(v)); +app.ports.saveToLocalStorage.subscribe(data => { + for(let k in data) { + localStorage.setItem(k, data[k]); + } }); - -// app.ports.loadFromLocalStorage.subscribe((key, value) => { -// app.ports.load.send(localStorage.getItem(key, value)); -// });