2023-06-27 15:53:29 +08:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"gopkg.in/yaml.v3"
|
2023-09-29 23:02:24 +08:00
|
|
|
"log"
|
2023-10-04 22:21:35 +08:00
|
|
|
"reflect"
|
2023-06-27 15:53:29 +08:00
|
|
|
"strings"
|
|
|
|
"sync"
|
2023-10-08 23:37:07 +08:00
|
|
|
"tinyrdm/backend/consts"
|
2023-10-04 22:21:35 +08:00
|
|
|
"tinyrdm/backend/types"
|
2023-06-27 15:53:29 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type PreferencesStorage struct {
|
|
|
|
storage *localStorage
|
|
|
|
mutex sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewPreferences() *PreferencesStorage {
|
|
|
|
return &PreferencesStorage{
|
|
|
|
storage: NewLocalStore("preferences.yaml"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-04 22:21:35 +08:00
|
|
|
func (p *PreferencesStorage) DefaultPreferences() types.Preferences {
|
|
|
|
return types.NewPreferences()
|
2023-06-27 15:53:29 +08:00
|
|
|
}
|
|
|
|
|
2023-10-04 22:21:35 +08:00
|
|
|
func (p *PreferencesStorage) getPreferences() (ret types.Preferences) {
|
2023-06-27 15:53:29 +08:00
|
|
|
b, err := p.storage.Load()
|
|
|
|
if err != nil {
|
|
|
|
ret = p.DefaultPreferences()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-07-11 18:06:44 +08:00
|
|
|
if err = yaml.Unmarshal(b, &ret); err != nil {
|
2023-06-27 15:53:29 +08:00
|
|
|
ret = p.DefaultPreferences()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetPreferences Get preferences from local
|
2023-10-04 22:21:35 +08:00
|
|
|
func (p *PreferencesStorage) GetPreferences() (ret types.Preferences) {
|
2023-06-27 15:53:29 +08:00
|
|
|
p.mutex.Lock()
|
|
|
|
defer p.mutex.Unlock()
|
|
|
|
|
2023-10-04 22:21:35 +08:00
|
|
|
ret = p.getPreferences()
|
2023-10-08 23:37:07 +08:00
|
|
|
ret.Behavior.AsideWidth = max(ret.Behavior.AsideWidth, consts.DEFAULT_ASIDE_WIDTH)
|
2023-10-16 10:26:25 +08:00
|
|
|
ret.Behavior.WindowWidth = max(ret.Behavior.WindowWidth, consts.MIN_WINDOW_WIDTH)
|
|
|
|
ret.Behavior.WindowHeight = max(ret.Behavior.WindowHeight, consts.MIN_WINDOW_HEIGHT)
|
2023-07-11 18:06:44 +08:00
|
|
|
return
|
2023-06-27 15:53:29 +08:00
|
|
|
}
|
|
|
|
|
2023-10-04 22:21:35 +08:00
|
|
|
func (p *PreferencesStorage) setPreferences(pf *types.Preferences, key string, value any) error {
|
|
|
|
parts := strings.Split(key, ".")
|
|
|
|
if len(parts) > 0 {
|
|
|
|
var reflectValue reflect.Value
|
|
|
|
if reflect.TypeOf(pf).Kind() == reflect.Ptr {
|
|
|
|
reflectValue = reflect.ValueOf(pf).Elem()
|
2023-06-27 15:53:29 +08:00
|
|
|
} else {
|
2023-10-04 22:21:35 +08:00
|
|
|
reflectValue = reflect.ValueOf(pf)
|
|
|
|
}
|
|
|
|
for i, part := range parts {
|
|
|
|
part = strings.ToUpper(part[:1]) + part[1:]
|
|
|
|
reflectValue = reflectValue.FieldByName(part)
|
|
|
|
if reflectValue.IsValid() {
|
|
|
|
if i == len(parts)-1 {
|
|
|
|
reflectValue.Set(reflect.ValueOf(value))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
2023-06-27 15:53:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-04 22:21:35 +08:00
|
|
|
return fmt.Errorf("invalid key path(%s)", key)
|
2023-06-27 15:53:29 +08:00
|
|
|
}
|
|
|
|
|
2023-10-04 22:21:35 +08:00
|
|
|
func (p *PreferencesStorage) savePreferences(pf *types.Preferences) error {
|
|
|
|
b, err := yaml.Marshal(pf)
|
2023-06-27 15:53:29 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = p.storage.Store(b); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-04 22:21:35 +08:00
|
|
|
// SetPreferences replace preferences
|
|
|
|
func (p *PreferencesStorage) SetPreferences(pf *types.Preferences) error {
|
2023-06-27 15:53:29 +08:00
|
|
|
p.mutex.Lock()
|
|
|
|
defer p.mutex.Unlock()
|
|
|
|
|
|
|
|
return p.savePreferences(pf)
|
|
|
|
}
|
|
|
|
|
2023-10-04 22:21:35 +08:00
|
|
|
// UpdatePreferences update values by key paths, the key path use "." to indicate multiple level
|
|
|
|
func (p *PreferencesStorage) UpdatePreferences(values map[string]any) error {
|
2023-06-27 15:53:29 +08:00
|
|
|
p.mutex.Lock()
|
|
|
|
defer p.mutex.Unlock()
|
|
|
|
|
|
|
|
pf := p.getPreferences()
|
|
|
|
for path, v := range values {
|
2023-10-04 22:21:35 +08:00
|
|
|
if err := p.setPreferences(&pf, path, v); err != nil {
|
2023-06-27 15:53:29 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2023-09-29 23:02:24 +08:00
|
|
|
log.Println("after save", pf)
|
2023-06-27 15:53:29 +08:00
|
|
|
|
2023-10-04 22:21:35 +08:00
|
|
|
return p.savePreferences(&pf)
|
2023-06-27 15:53:29 +08:00
|
|
|
}
|
|
|
|
|
2023-10-04 22:21:35 +08:00
|
|
|
func (p *PreferencesStorage) RestoreDefault() types.Preferences {
|
|
|
|
p.mutex.Lock()
|
|
|
|
defer p.mutex.Unlock()
|
|
|
|
|
2023-06-27 15:53:29 +08:00
|
|
|
pf := p.DefaultPreferences()
|
2023-10-04 22:21:35 +08:00
|
|
|
p.savePreferences(&pf)
|
|
|
|
return pf
|
2023-06-27 15:53:29 +08:00
|
|
|
}
|