From 14d44f159ed16931dbd17dd4c5fa088b331a0629 Mon Sep 17 00:00:00 2001 From: Jan Steinke Date: Tue, 13 Feb 2024 21:58:44 +0200 Subject: [PATCH] update loading text, auto-refresh and store difficulty --- main.go | 3 +++ ui/menu.go | 64 +++++++++++++++++++++++++++++++++--------------------- 2 files changed, 42 insertions(+), 25 deletions(-) diff --git a/main.go b/main.go index 722d5bd..1c1dbd6 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,7 @@ import ( "flag" "log" "net/http" + "time" "github.com/jan-xyz/simon-says/ui" "github.com/maxence-charriere/go-app/v9/pkg/app" @@ -47,6 +48,8 @@ func main() { Icon: app.Icon{ Default: "/web/icon.png", }, + LoadingLabel: "Loading...", + AutoUpdateInterval: 15 * time.Minute, } if !*serve { h.Resources = app.GitHubPages("simon-says") diff --git a/ui/menu.go b/ui/menu.go index f5d25b2..f2ffe46 100644 --- a/ui/menu.go +++ b/ui/menu.go @@ -15,45 +15,59 @@ const ( Endless Difficulty = "endless" ) +const ( + localStorageDifficulty = "difficulty" +) + type menu struct { app.Compo selectedDifficulty Difficulty } func NewMenu() *menu { - return &menu{selectedDifficulty: Easy} + return &menu{} +} + +func (g *menu) OnMount(ctx app.Context) { + var d Difficulty + ctx.LocalStorage().Get(localStorageDifficulty, &d) + if d == "" { + d = Easy + } + g.selectedDifficulty = d } func (g *menu) Render() app.UI { - return app.Div().Body( + modes := []app.UI{} + for _, mode := range []Difficulty{Easy, Medium, Hard, Endless} { + input := app.Input().Type("radio").Name("difficulty-setting").ID("difficulty%d", mode).Value(mode).OnClick(g.storeValue) + label := app.Label().For("difficulty%d", mode).Body(app.Span().Text(mode)) + if g.selectedDifficulty == mode { + input.Checked(true) + } + modes = append(modes, input, label) + } + t := app.Div().Body( app.Button(). Class("simon-button", "new-game"). Body(app.Span().Text("New Game")). - OnClick(func(ctx app.Context, _ app.Event) { - ctx.NewActionWithValue(EventNewGame, g.selectedDifficulty) - }), + OnClick(g.startNewGame), app.Div().Class("difficulty").Body( - app.Input().Type("radio").Name("difficulty-setting").ID("difficulty%d", Easy).Value(Easy).Checked(true).OnClick(func(ctx app.Context, _ app.Event) { - val := ctx.JSSrc().Get("value").String() - g.selectedDifficulty = Difficulty(val) - }), - app.Label().For("difficulty%d", Easy).Body(app.Span().Text("easy")), - app.Input().Type("radio").Name("difficulty-setting").ID("difficulty%d", Medium).Value(Medium).OnClick(func(ctx app.Context, _ app.Event) { - val := ctx.JSSrc().Get("value").String() - g.selectedDifficulty = Difficulty(val) - }), - app.Label().For("difficulty%d", Medium).Body(app.Span().Text("medium")), - app.Input().Type("radio").Name("difficulty-setting").ID("difficulty%d", Hard).Value(Hard).OnClick(func(ctx app.Context, _ app.Event) { - val := ctx.JSSrc().Get("value").String() - g.selectedDifficulty = Difficulty(val) - }), - app.Label().For("difficulty%d", Hard).Body(app.Span().Text("hard")), - app.Input().Type("radio").Name("difficulty-setting").ID("difficulty%d", Endless).Value(Endless).OnClick(func(ctx app.Context, _ app.Event) { - val := ctx.JSSrc().Get("value").String() - g.selectedDifficulty = Difficulty(val) - }), - app.Label().For("difficulty%d", Endless).Body(app.Span().Text("endless")), + modes..., ), ) + return t +} + +func (g *menu) storeValue(ctx app.Context, _ app.Event) { + val := ctx.JSSrc().Get("value").String() + ctx.LocalStorage().Set(localStorageDifficulty, val) + g.selectedDifficulty = Difficulty(val) +} + +func (g *menu) startNewGame(ctx app.Context, _ app.Event) { + d := Easy + ctx.LocalStorage().Get(localStorageDifficulty, d) + ctx.NewActionWithValue(EventNewGame, d) }