-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathsavefile.go
More file actions
74 lines (61 loc) · 1.61 KB
/
savefile.go
File metadata and controls
74 lines (61 loc) · 1.61 KB
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
package main
import (
"encoding/json"
"log/slog"
"github.com/goxray/desktop/internal/connlist"
)
const (
itemsConfigKey = "connections_config"
)
// SaveFile is used to store and load connection items from memory.
type SaveFile struct {
source Source
}
type Source interface {
// SetString saves key value pair.
SetString(key string, value string)
// StringWithFallback gets string by key, if not found return fallback.
StringWithFallback(key, fallback string) string
}
type SavedState struct {
Link string `json:"link"`
Label string `json:"label"`
}
func serialize(item *connlist.Item) SavedState {
return SavedState{
Link: item.Link(),
Label: item.Label(),
}
}
func NewSaveFile(source Source) *SaveFile {
return &SaveFile{
source: source,
}
}
// Update saves list into config.
func (s *SaveFile) Update(list *connlist.Collection) {
toSave := make([]SavedState, 0, len(list.All()))
for _, item := range list.All() {
if item == nil {
continue
}
toSave = append(toSave, serialize(item))
}
b, err := json.MarshalIndent(toSave, "", " ")
if err != nil {
slog.Warn(err.Error())
}
s.source.SetString(itemsConfigKey, string(b))
}
// Load loads saved items into list.
func (s *SaveFile) Load(list *connlist.Collection) {
loadedItems := make([]SavedState, 0)
if err := json.Unmarshal([]byte(s.source.StringWithFallback(itemsConfigKey, "[]")), &loadedItems); err != nil {
slog.Error("failed to unmarshal tray items", "error", err)
}
for _, item := range loadedItems {
if err := list.AddItem(item.Label, item.Link); err != nil {
slog.Error("failed to load new item", "error", err)
}
}
}