forked from tiny-craft/tiny-rdm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreferences.go
127 lines (108 loc) · 2.84 KB
/
preferences.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package storage
import (
"fmt"
"gopkg.in/yaml.v3"
"log"
"reflect"
"strings"
"sync"
"tinyrdm/backend/consts"
"tinyrdm/backend/types"
)
type PreferencesStorage struct {
storage *localStorage
mutex sync.Mutex
}
func NewPreferences() *PreferencesStorage {
return &PreferencesStorage{
storage: NewLocalStore("preferences.yaml"),
}
}
func (p *PreferencesStorage) DefaultPreferences() types.Preferences {
return types.NewPreferences()
}
func (p *PreferencesStorage) getPreferences() (ret types.Preferences) {
b, err := p.storage.Load()
ret = p.DefaultPreferences()
if err != nil {
return
}
if err = yaml.Unmarshal(b, &ret); err != nil {
ret = p.DefaultPreferences()
return
}
return
}
// GetPreferences Get preferences from local
func (p *PreferencesStorage) GetPreferences() (ret types.Preferences) {
p.mutex.Lock()
defer p.mutex.Unlock()
ret = p.getPreferences()
if ret.General.ScanSize <= 0 {
ret.General.ScanSize = consts.DEFAULT_SCAN_SIZE
}
ret.Behavior.AsideWidth = max(ret.Behavior.AsideWidth, consts.DEFAULT_ASIDE_WIDTH)
ret.Behavior.WindowWidth = max(ret.Behavior.WindowWidth, consts.MIN_WINDOW_WIDTH)
ret.Behavior.WindowHeight = max(ret.Behavior.WindowHeight, consts.MIN_WINDOW_HEIGHT)
return
}
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()
} else {
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
}
}
}
return fmt.Errorf("invalid key path(%s)", key)
}
func (p *PreferencesStorage) savePreferences(pf *types.Preferences) error {
b, err := yaml.Marshal(pf)
if err != nil {
return err
}
if err = p.storage.Store(b); err != nil {
return err
}
return nil
}
// SetPreferences replace preferences
func (p *PreferencesStorage) SetPreferences(pf *types.Preferences) error {
p.mutex.Lock()
defer p.mutex.Unlock()
return p.savePreferences(pf)
}
// UpdatePreferences update values by key paths, the key path use "." to indicate multiple level
func (p *PreferencesStorage) UpdatePreferences(values map[string]any) error {
p.mutex.Lock()
defer p.mutex.Unlock()
pf := p.getPreferences()
for path, v := range values {
if err := p.setPreferences(&pf, path, v); err != nil {
return err
}
}
log.Println("after save", pf)
return p.savePreferences(&pf)
}
func (p *PreferencesStorage) RestoreDefault() types.Preferences {
p.mutex.Lock()
defer p.mutex.Unlock()
pf := p.DefaultPreferences()
p.savePreferences(&pf)
return pf
}