-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathnote.go
102 lines (84 loc) · 1.82 KB
/
note.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
package main
import (
"errors"
"fmt"
"strings"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/data/binding"
)
const (
countKey = "notecount"
noteKey = "note%d"
noteDeletedKey = "note%ddeleted"
)
type note struct {
content binding.String
deleted binding.Bool
}
func (n *note) title() binding.String {
return newTitleString(n.content)
}
type notelist struct {
all []*note
pref fyne.Preferences
}
func (l *notelist) add() *note {
key := fmt.Sprintf(noteKey, len(l.all))
deleteKey := fmt.Sprintf(noteDeletedKey, len(l.all))
n := ¬e{
binding.BindPreferenceString(key, l.pref),
binding.BindPreferenceBool(deleteKey, l.pref),
}
l.all = append([]*note{n}, l.all...)
l.save()
return n
}
func (l *notelist) delete(n *note) {
n.deleted.Set(true)
}
func (l *notelist) load() {
l.all = nil
count := l.pref.Int(countKey)
if count == 0 {
return
}
for i := count - 1; i >= 0; i-- {
key := fmt.Sprintf(noteKey, i)
deleteKey := fmt.Sprintf(noteDeletedKey, i)
content := binding.BindPreferenceString(key, l.pref)
deleted := binding.BindPreferenceBool(deleteKey, l.pref)
l.all = append(l.all, ¬e{content, deleted})
}
}
func (l *notelist) notes() []*note {
var visible []*note
for _, n := range l.all {
if del, _ := n.deleted.Get(); del {
continue
}
visible = append(visible, n)
}
return visible
}
func (l *notelist) save() {
l.pref.SetInt(countKey, len(l.all))
}
type titleString struct {
binding.String
}
func (t *titleString) Get() (string, error) {
content, err := t.String.Get()
if err != nil {
return "Error", err
}
if content == "" {
return "Untitled", nil
}
return strings.SplitN(content, "\n", 2)[0], nil
}
func (t *titleString) Set(string) error {
return errors.New("cannot set content from title")
}
func newTitleString(in binding.String) binding.String {
return &titleString{in}
}