From a291d8a38b40569fdd1f00125ca0b29e4b9264f2 Mon Sep 17 00:00:00 2001 From: Frédéric Guillot Date: Wed, 18 Jul 2018 22:30:05 -0700 Subject: Improve themes handling - Store user theme in session - Logged out users will keep their theme - Add theme background color to web manifest and meta tag --- model/app_session.go | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++ model/session.go | 58 --------------------------------------------------- model/theme.go | 12 +++++++++++ 3 files changed, 71 insertions(+), 58 deletions(-) create mode 100644 model/app_session.go delete mode 100644 model/session.go (limited to 'model') diff --git a/model/app_session.go b/model/app_session.go new file mode 100644 index 0000000..e9ee06e --- /dev/null +++ b/model/app_session.go @@ -0,0 +1,59 @@ +// Copyright 2017 Frédéric Guillot. All rights reserved. +// Use of this source code is governed by the Apache 2.0 +// license that can be found in the LICENSE file. + +package model + +import ( + "database/sql/driver" + "encoding/json" + "errors" + "fmt" +) + +// SessionData represents the data attached to the session. +type SessionData struct { + CSRF string `json:"csrf"` + OAuth2State string `json:"oauth2_state"` + FlashMessage string `json:"flash_message"` + FlashErrorMessage string `json:"flash_error_message"` + Language string `json:"language"` + Theme string `json:"Theme"` + PocketRequestToken string `json:"pocket_request_token"` +} + +func (s SessionData) String() string { + return fmt.Sprintf(`CSRF=%q, "OAuth2State=%q, FlashMsg=%q, FlashErrorMsg=%q, Lang=%q, Theme=%q`, + s.CSRF, s.OAuth2State, s.FlashMessage, s.FlashErrorMessage, s.Language, s.Theme) +} + +// Value converts the session data to JSON. +func (s SessionData) Value() (driver.Value, error) { + j, err := json.Marshal(s) + return j, err +} + +// Scan converts raw JSON data. +func (s *SessionData) Scan(src interface{}) error { + source, ok := src.([]byte) + if !ok { + return errors.New("session: unable to assert type of src") + } + + err := json.Unmarshal(source, s) + if err != nil { + return fmt.Errorf("session: %v", err) + } + + return err +} + +// Session represents a session in the system. +type Session struct { + ID string + Data *SessionData +} + +func (s *Session) String() string { + return fmt.Sprintf(`ID="%s", Data={%v}`, s.ID, s.Data) +} diff --git a/model/session.go b/model/session.go deleted file mode 100644 index 763f709..0000000 --- a/model/session.go +++ /dev/null @@ -1,58 +0,0 @@ -// Copyright 2017 Frédéric Guillot. All rights reserved. -// Use of this source code is governed by the Apache 2.0 -// license that can be found in the LICENSE file. - -package model - -import ( - "database/sql/driver" - "encoding/json" - "errors" - "fmt" -) - -// SessionData represents the data attached to the session. -type SessionData struct { - CSRF string `json:"csrf"` - OAuth2State string `json:"oauth2_state"` - FlashMessage string `json:"flash_message"` - FlashErrorMessage string `json:"flash_error_message"` - Language string `json:"language"` - PocketRequestToken string `json:"pocket_request_token"` -} - -func (s SessionData) String() string { - return fmt.Sprintf(`CSRF="%s", "OAuth2State="%s", FlashMessage="%s", FlashErrorMessage="%s", Lang="%s"`, - s.CSRF, s.OAuth2State, s.FlashMessage, s.FlashErrorMessage, s.Language) -} - -// Value converts the session data to JSON. -func (s SessionData) Value() (driver.Value, error) { - j, err := json.Marshal(s) - return j, err -} - -// Scan converts raw JSON data. -func (s *SessionData) Scan(src interface{}) error { - source, ok := src.([]byte) - if !ok { - return errors.New("session: unable to assert type of src") - } - - err := json.Unmarshal(source, s) - if err != nil { - return fmt.Errorf("session: %v", err) - } - - return err -} - -// Session represents a session in the system. -type Session struct { - ID string - Data *SessionData -} - -func (s *Session) String() string { - return fmt.Sprintf(`ID="%s", Data={%v}`, s.ID, s.Data) -} diff --git a/model/theme.go b/model/theme.go index 5d32df4..f58f91c 100644 --- a/model/theme.go +++ b/model/theme.go @@ -15,6 +15,18 @@ func Themes() map[string]string { } } +// ThemeColor returns the color for the address bar or/and the browser color. +// https://developer.mozilla.org/en-US/docs/Web/Manifest#theme_color +// https://developers.google.com/web/tools/lighthouse/audits/address-bar +func ThemeColor(theme string) string { + switch theme { + case "black": + return "#222" + default: + return "#fff" + } +} + // ValidateTheme validates theme value. func ValidateTheme(theme string) error { for key := range Themes() { -- cgit v1.2.3