-
Notifications
You must be signed in to change notification settings - Fork 0
/
ops.go
232 lines (203 loc) · 6.25 KB
/
ops.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package main
import (
"fmt"
"math"
tea "github.com/charmbracelet/bubbletea"
todoist "github.com/psethwick/todoist/lib"
"github.com/psethwick/tuidoist/task"
)
// todo this is _not_ a good place/idea
// I think []{actionTaken, Task} ? and treat it like a stack
// long game is complete sync workflow where offline actions can be synced later
// which means serializing this to disk
// var lastCompletedTask task.Task
//
// func (m *mainModel) undoCompleteTask() tea.Cmd {
// m.taskList.AddItem(lastCompletedTask)
// m.statusBarModel.SetMessage("undo complete", lastCompletedTask.Title)
// args := map[string]interface{}{"id": lastCompletedTask.Item.ID}
// // todo undoop
// return m.sync(todoist.NewCommand("item_uncomplete", args))
// }
// todo confirm
func (m *mainModel) deleteTasks() tea.Cmd {
return m.bulkOps("deleted", true, func(t task.Task) todoist.Command {
return todoist.NewCommand("item_delete", map[string]interface{}{"id": t.Item.ID})
})
}
func (m *mainModel) completeTasks() tea.Cmd {
return m.bulkOps("completed", true, func(t task.Task) todoist.Command {
return todoist.NewCommand("item_close", map[string]interface{}{"id": t.Item.ID})
})
}
func (m *mainModel) setPriority(p int) tea.Cmd {
return m.bulkOps(fmt.Sprint("priority", p), true, func(t task.Task) todoist.Command {
return todoist.NewCommand("item_update", map[string]interface{}{"id": t.Item.ID, "priority": p})
})
}
func (m *mainModel) ReorderTasks(items []map[string]interface{}) tea.Cmd {
return m.sync(todoist.NewCommand("item_reorder", map[string]interface{}{"items": items}))
}
func (m *mainModel) rescheduleTasks(newDate string) tea.Cmd {
return m.bulkOps("rescheduled", false, func(t task.Task) todoist.Command {
t.Item.Due = &todoist.Due{
String: newDate,
}
return todoist.NewCommand("item_update", t.Item.UpdateParam())
})
}
func (m *mainModel) MoveItemsToNewParent(newParentID string) tea.Cmd {
return m.bulkOps("moved", false, func(t task.Task) todoist.Command {
args := map[string]interface{}{"id": t.Item.ID}
args["parent_id"] = newParentID
return todoist.NewCommand("item_move", args)
})
}
func (m *mainModel) MoveItemsToProject(p project) tea.Cmd {
return m.bulkOps("moved", true, func(t task.Task) todoist.Command {
args := map[string]interface{}{"id": t.Item.ID}
if p.section.ID != "" {
args["section_id"] = p.section.ID
} else {
args["project_id"] = p.project.ID
}
return todoist.NewCommand("item_move", args)
})
}
func (m *mainModel) bulkOps(name string, clear bool, builder func(task.Task) todoist.Command) tea.Cmd {
var cmds []todoist.Command
selector := m.taskList.SelectedItems
if clear {
selector = m.taskList.SelectedItemsClear
}
for _, t := range selector() {
m.statusBarModel.SetMessage(name, t.Title)
cmds = append(cmds, builder(t))
}
cmdLen := len(cmds)
if cmdLen > 1 {
m.statusBarModel.SetMessage(name, fmt.Sprint(cmdLen, "tasks"))
}
return m.sync(cmds...)
}
func (m *mainModel) addTaskBottom(content string) tea.Cmd {
return m.addTask(content, false)
}
func (m *mainModel) addTaskTop(content string) tea.Cmd {
return m.addTask(content, true)
}
func (m *mainModel) addTask(content string, top bool) tea.Cmd {
if content == "" {
return func() tea.Msg { return nil }
}
item := todoist.Item{}
item.Content = content
item.Priority = 1
item.ProjectID = m.projectId
item.SectionID = m.sectionId
m.statusBarModel.SetMessage("added", item.Content)
args := map[string]interface{}{}
if item.Content != "" {
args["content"] = item.Content
}
if item.SectionID != "" {
args["section_id"] = item.SectionID
}
if item.Description != "" {
args["description"] = item.Description
}
if item.DateString != "" {
args["date_string"] = item.DateString
}
if len(item.LabelNames) != 0 {
args["labels"] = item.LabelNames
}
if item.Priority != 0 {
args["priority"] = item.Priority
}
if item.ProjectID != "" {
args["project_id"] = item.ProjectID
}
if item.Due != nil {
args["due"] = item.Due
}
if top {
minChildOrder := math.MaxInt
// todo this really should be by project/section, etc
// much easier if I make my local store more useful (buckets of tasks by project)
for _, t := range m.local.Items {
minChildOrder = min(minChildOrder, t.ChildOrder)
}
args["child_order"] = minChildOrder
}
args["auto_reminder"] = item.AutoReminder
return m.sync(todoist.NewCommand("item_add", args))
}
func (m *mainModel) AddProject(name string) tea.Cmd {
param := map[string]interface{}{
"name": name,
}
return m.sync(todoist.NewCommand("project_add", param))
}
func (m *mainModel) RenameProject(projectId string, newName string) tea.Cmd {
args := map[string]interface{}{
"id": projectId,
"name": newName,
}
return m.sync(todoist.NewCommand("project_update", args))
}
func (m *mainModel) UpdateItem(i todoist.Item) tea.Cmd {
return m.sync(todoist.NewCommand("item_update", i.UpdateParam()))
}
// todo reorder sections
func (m *mainModel) MoveSection(ID string, projectId string) tea.Cmd {
args := map[string]interface{}{
"id": ID,
"project_id": projectId,
}
return m.sync(todoist.NewCommand("section_move", args))
}
func (m *mainModel) RenameSection(section todoist.Section, newName string) tea.Cmd {
args := map[string]interface{}{
"id": section.ID,
"name": newName,
"project_id": section.ProjectID,
}
return m.sync(todoist.NewCommand("section_update", args))
}
func (m *mainModel) AddSection(name string, projectId string) tea.Cmd {
args := map[string]interface{}{
"name": name,
"project_id": projectId,
}
return m.sync(todoist.NewCommand("section_add", args))
}
func (m *mainModel) DeleteSection(ID string) tea.Cmd {
args := map[string]interface{}{
"id": ID,
}
return m.sync(todoist.NewCommand("section_delete", args))
}
func (m *mainModel) ArchiveSection(ID string) tea.Cmd {
args := map[string]interface{}{
"id": ID,
}
return m.sync(todoist.NewCommand("section_archive", args))
}
func (m *mainModel) DeleteProject(ID string) tea.Cmd {
args := map[string]interface{}{
"id": ID,
}
m.openInbox()
return m.sync(todoist.NewCommand("project_delete", args))
}
func (m *mainModel) ArchiveProject() tea.Cmd {
if m.projectId == "" {
return nil
}
args := map[string]interface{}{
"id": m.projectId,
}
m.openInbox()
return m.sync(todoist.NewCommand("project_archive", args))
}