This repository has been archived by the owner on Jun 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
todos.go
145 lines (127 loc) · 3.03 KB
/
todos.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package main
import (
"fmt"
"io/ioutil"
"os"
"path"
"github.com/fsnotify/fsnotify"
"github.com/wailsapp/wails"
)
type Todos struct {
filename string
runtime *wails.Runtime
logger *wails.CustomLogger
watcher *fsnotify.Watcher
}
// NewTodos attempts to create a new Todo list
func NewTodos() (*Todos, error) {
// Create new Todos instance
result := &Todos{}
// Return it
return result, nil
}
func (t *Todos) startWatcher() error {
t.logger.Info("Starting Watcher")
watcher, err := fsnotify.NewWatcher()
t.watcher = watcher
if err != nil {
return err
}
go func() {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
if event.Op&fsnotify.Write == fsnotify.Write {
t.logger.Infof("modified file: %s", event.Name)
t.runtime.Events.Emit("filemodified")
}
case err, ok := <-watcher.Errors:
if !ok {
return
}
t.logger.Error(err.Error())
}
}
}()
err = watcher.Add(t.filename)
if err != nil {
return err
}
return nil
}
func (t *Todos) LoadList() (string, error) {
t.logger.Infof("Loading list from: %s", t.filename)
bytes, err := ioutil.ReadFile(t.filename)
if err != nil {
err = fmt.Errorf("Unable to open list: %s", t.filename)
}
t.runtime.Window.SetTitle(t.filename)
return string(bytes), err
}
func (t *Todos) saveListByName(todos string, filename string) error {
return ioutil.WriteFile(filename, []byte(todos), 0600)
}
func (t *Todos) SaveList(todos string) error {
t.logger.Infof("Saving list: %s", todos)
return t.saveListByName(todos, t.filename)
}
func (t *Todos) setFilename(filename string) error {
var err error
// Stop watching the current file and return any error
err = t.watcher.Remove(t.filename)
if err != nil {
return err
}
// Set the filename
t.filename = filename
// Add the new file to the watcher and return any errors
err = t.watcher.Add(filename)
if err != nil {
return err
}
t.logger.Info("Now watching: " + filename)
t.runtime.Window.SetTitle(t.filename)
return nil
}
func (t *Todos) SaveAs(todos string) error {
filename := t.runtime.Dialog.SelectSaveFile()
t.logger.Info("Save As: " + filename)
err := t.saveListByName(todos, filename)
if err != nil {
return err
}
return t.setFilename(filename)
}
func (t *Todos) LoadNewList() {
filename := t.runtime.Dialog.SelectFile()
if len(filename) > 0 {
t.setFilename(filename)
t.runtime.Events.Emit("filemodified")
}
}
func (t *Todos) ensureFileExists() {
// Check status of file
_, err := os.Stat(t.filename)
// If it doesn't exist
if os.IsNotExist(err) {
// Create it with a blank list
ioutil.WriteFile(t.filename, []byte("[]"), 0600)
}
}
func (t *Todos) WailsInit(runtime *wails.Runtime) error {
t.runtime = runtime
t.logger = t.runtime.Log.New("Todos")
t.logger.Info("I'm here")
// Set the default filename to $HOMEDIR/mylist.json
homedir, err := runtime.FileSystem.HomeDir()
if err != nil {
return err
}
t.filename = path.Join(homedir, "mylist.json")
t.runtime.Window.SetTitle(t.filename)
t.ensureFileExists()
return t.startWatcher()
}