Skip to content

Commit

Permalink
Save the sound volume
Browse files Browse the repository at this point in the history
  • Loading branch information
HadrienMP committed Mar 29, 2021
1 parent f86af8a commit 54e3121
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
7 changes: 5 additions & 2 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
<script src="/socket.io/socket.io.js"></script>
<script>

const app = Elm.Main.init({node: document.getElementById('elm'),});
const app = Elm.Main.init({
node: document.getElementById('elm'),
flags: JSON.parse(window.localStorage.getItem("preferences"))
});

let alarm = document.createElement("audio");
document.body.appendChild(alarm);
Expand All @@ -45,7 +48,7 @@
.finally(() => app.ports.events.send({name: 'Copied', value: ""}))
break;
case 'ChangeVolume':
// window.localStorage.setItem("preferences", JSON.stringify({volume: parseInt(command.value)}));
window.localStorage.setItem("preferences", JSON.stringify({volume: parseInt(command.value)}));
alarm.volume = parseInt(command.value) / 100.0;
break;
}
Expand Down
17 changes: 10 additions & 7 deletions src/elm/Main.elm
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import Svg.Attributes as Svg
import Task
import Time
import Url
import UserPreferences


port receiveEvent : (Json.Encode.Value -> msg) -> Sub msg
Expand All @@ -43,7 +44,7 @@ port receiveHistory : (List Json.Encode.Value -> msg) -> Sub msg
-- MAIN


main : Program () Model Msg
main : Program UserPreferences.Model Model Msg
main =
Browser.application
{ init = init
Expand Down Expand Up @@ -87,20 +88,23 @@ type alias Model =
}


init : () -> Url.Url -> Nav.Key -> ( Model, Cmd Msg )
init _ url key =
init : UserPreferences.Model -> Url.Url -> Nav.Key -> ( Model, Cmd Msg )
init preferences url key =
( { key = key
, url = url
, shared = Shared.init
, mobbersSettings = Mobbers.Settings.init
, clockSettings = Clock.Settings.init
, soundSettings = Sound.Settings.init 50
, soundSettings = Sound.Settings.init preferences.volume
, alarm = Standby
, now = Time.millisToPosix 0
, toasts = []
, tab = Main
}
, Task.perform TimePassed Time.now
, Cmd.batch
[ Task.perform TimePassed Time.now
, Js.Commands.send <| Js.Commands.ChangeVolume preferences.volume
]
)


Expand Down Expand Up @@ -247,15 +251,14 @@ update msg model =
(\a -> { model | clockSettings = a })
(Cmd.map GotClockSettingsMsg)

GotSoundSettingsMsg subMsg->
GotSoundSettingsMsg subMsg ->
Sound.Settings.update subMsg model.soundSettings
|> Tuple.mapBoth
(\a -> { model | soundSettings = a })
(Cmd.map GotSoundSettingsMsg)




-- SUBSCRIPTIONS


Expand Down
18 changes: 18 additions & 0 deletions src/elm/UserPreferences.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module UserPreferences exposing (..)

import Json.Encode


type alias Model =
{ volume : Int }


default : Model
default =
{ volume = 50 }


encode : Model -> Json.Encode.Value
encode model =
Json.Encode.object
[ ( "volume", Json.Encode.int model.volume ) ]

0 comments on commit 54e3121

Please sign in to comment.