-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtasks.go
More file actions
168 lines (141 loc) · 3.44 KB
/
Copy pathtasks.go
File metadata and controls
168 lines (141 loc) · 3.44 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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package main
import (
"encoding/json"
"errors"
"io"
"log"
"os"
"slices"
"strconv"
"time"
)
type Task struct {
Title string `json:"title"`
Deadline time.Time `json:"deadline"`
CreatedAt time.Time `json:"createdAt"`
Info []rune `json:"info"`
isDone bool
}
func NewTask(TitleN string, DeadlineN time.Time, CreatedAtN time.Time, InfoN []rune) Task {
return Task{Title: TitleN, Deadline: DeadlineN, CreatedAt: CreatedAtN, Info: InfoN}
}
func (a *App) ReadSave() {
file, err := os.Open(a.saveFilePath)
if errors.Is(err, os.ErrNotExist) {
a.taskSlice = []Task{}
return
} else if err != nil {
log.Fatal(err)
}
defer file.Close()
decoder := json.NewDecoder(file)
if err := decoder.Decode(&a.taskSlice); err != nil {
if err == io.EOF {
a.taskSlice = []Task{}
return
}
log.Fatal(err)
}
}
func (a *App) WriteSave() {
newSlice := []Task{}
for _, current := range a.taskSlice {
if !current.isDone {
newSlice = append(newSlice, current)
}
}
file, err := os.Create(a.saveFilePath)
if err != nil {
log.Fatal(err)
}
defer file.Close()
encoder := json.NewEncoder(file)
encoder.SetIndent("", " ")
if err := encoder.Encode(newSlice); err != nil {
log.Fatal(err)
}
}
func (a *App) SortTasks() {
slices.SortFunc(a.taskSlice, func(a, b Task) int {
if a.Deadline.Before(b.Deadline) {
return -1
} else if a.Deadline.After(b.Deadline) {
return 1
}
return 0
})
}
func (a *App) CompleteTasks(taskIndex int) {
a.taskSlice[taskIndex].isDone = !a.taskSlice[taskIndex].isDone
}
func (a *App) InitDisplayedTasks() {
for range a.taskSlice {
a.disLast++
if a.disLast == 6 {
break
}
}
a.displayedTasks = a.taskSlice[a.disFirst:a.disLast]
}
func (a *App) IncrementDisplayedTasks() {
if len(a.taskSlice) > a.disLast {
a.disFirst++
a.disLast++
a.displayedTasks = a.taskSlice[a.disFirst:a.disLast]
}
}
func (a *App) LowerDisplayedTasks() {
if a.disFirst > 0 {
a.disFirst--
a.disLast--
a.displayedTasks = a.taskSlice[a.disFirst:a.disLast]
}
}
func (t *taskMenu) AddTask(a *App, edit bool) {
stringHours := string(t.inputHours)
stringDays := string(t.inputDays)
intHours, err := strconv.Atoi(stringHours)
if err != nil {
intHours = 0
}
intDays, err := strconv.Atoi(stringDays)
if err != nil {
intDays = 0
}
finalTime := intHours + 24*intDays
stringFinal := strconv.Itoa(finalTime) + "h"
duration, err := time.ParseDuration(stringFinal)
if err != nil {
return
}
addedTask := NewTask(string(t.inputTitle), time.Now().Add(duration), time.Now(), t.inputInfo)
if edit {
a.taskSlice[a.disFirst+a.cursorPos-1] = addedTask
a.SortTasks()
a.displayedTasks = a.taskSlice[a.disFirst:a.disLast]
} else {
a.taskSlice = append(a.taskSlice, addedTask)
a.SortTasks()
if len(a.taskSlice) <= 6 {
a.disLast++
}
a.displayedTasks = a.taskSlice[a.disFirst:a.disLast]
}
}
func (a *App) LoadEditMenu(taskIndex int) {
for _, r := range a.taskSlice[taskIndex].Title {
a.TaskMenu.inputTitle = append(a.TaskMenu.inputTitle, r)
}
duration := a.taskSlice[taskIndex].Deadline.Sub(a.taskSlice[taskIndex].CreatedAt)
days := int(duration.Hours()) / 24
hours := int(duration.Hours()) % 24
daysString := strconv.Itoa(days)
hoursString := strconv.Itoa(hours)
for _, r := range daysString {
a.TaskMenu.inputDays = append(a.TaskMenu.inputDays, r)
}
for _, r := range hoursString {
a.TaskMenu.inputHours = append(a.TaskMenu.inputHours, r)
}
a.TaskMenu.inputInfo = a.taskSlice[taskIndex].Info
}