-
Notifications
You must be signed in to change notification settings - Fork 0
/
Menu.cs
271 lines (209 loc) · 10.3 KB
/
Menu.cs
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using IniParser;
using IniParser.Model;
using Todoist.Net;
using Todoist.Net.Models;
namespace Todoist_Automation
{
public partial class Menu : Form
{
IniData settings;
ITodoistClient client;
public Menu()
{
InitializeComponent();
var parser = new FileIniDataParser();
//Loads the settings
if (!File.Exists("settings.ini"))
CreateSettings();
settings = parser.ReadFile("settings.ini");
}
private static void CreateSettings()
{
StreamWriter writer = new StreamWriter("settings.ini");
writer.WriteLine("[Todoist]");
writer.WriteLine("token=\"\"");
writer.Close();
}
//Acces the Todoist servers using the users private API-Token
//No usernames or passwords needed
private async void LoginToTodoist()
{
//Hides the buttons until connection with server
SetButtonVisability(false);
label_loading.Text = "Loading Todoist Data";
try
{
//Connects to server
client = new TodoistClient(settings["Todoist"]["token"]);
//Test the connection by fetching the username and displaying it
var resources = await client.GetResourcesAsync();
label_loading.Text = "Loged in as " + resources.UserInfo.FullName;
SetButtonVisability(true);
}
catch
{
//Failed connection
MessageBox.Show("Failed to load Todoist data, check API-Token");
label_loading.Text = "Load failed";
}
}
private void Menu_Load(object sender, EventArgs e)
{
LoginToTodoist();
}
//Hides every button except for the settingsbutton
private void SetButtonVisability(bool visable)
{
foreach (Control control in Controls)
{
if (control is Button && !control.Name.Contains("Setting"))
control.Visible = visable;
}
label_ask.Visible = visable;
}
private void button_Notes_Click(object sender, EventArgs e)
{
AddNoteTranscribing addNotes = new AddNoteTranscribing(client);
addNotes.ShowDialog();
if (addNotes.DialogResult == DialogResult.OK)
{
AddNotesTask(addNotes.ProjectName, addNotes.LectureDate);
}
}
private void button_Settings_Click(object sender, EventArgs e)
{
SettingsWindow settingsWindow = new SettingsWindow(settings);
settingsWindow.ShowDialog();
//Resets the connection to Todoist server if the settings are changed
if (settingsWindow.DialogResult == DialogResult.OK)
{
LoginToTodoist();
}
}
private void button_Book_Click(object sender, EventArgs e)
{
AddReading addReading = new AddReading(client);
addReading.ShowDialog();
if (addReading.DialogResult == DialogResult.OK)
{
AddReadingTask(addReading.ProjectName, addReading.BookName, addReading.StartDate, addReading.DueDate, addReading.LastChapter, addReading.FirstChapter);
}
}
public async void AddNotesTask(string projectName, DateTime lectureDate)
{
//Loads all projects and searches for the selected project
var projects = await client.Projects.GetAsync();
foreach (var proj in projects)
{
if (proj.Name == projectName)
{
//Create a transaction to lower the times we request and send data to the server
var transaction = client.CreateTransaction();
//Adding the main task
var quickAddItem = new QuickAddItem("Renskrivning föreläsning " + projectName + " " + lectureDate.Day + " / " + lectureDate.Month
+ " @Renskrivning #" + projectName);
var task = await client.Items.QuickAddAsync(quickAddItem);
//Adds subsaks and moves them under the main task
var sub1ID = await transaction.Items.AddAsync(new Item("Scanna orginal", proj.Id));
await transaction.Items.MoveAsync(ItemMoveArgument.CreateMoveToParent(sub1ID, task.Id));
var sub2ID = await transaction.Items.AddAsync(new Item("Renskriv", proj.Id));
await transaction.Items.MoveAsync(ItemMoveArgument.CreateMoveToParent(sub2ID, task.Id));
//var sub3ID = await transaction.Items.AddAsync(new Item("Samanfatta", proj.Id));
//await transaction.Items.MoveAsync(ItemMoveArgument.CreateMoveToParent(sub3ID, task.Id));
//Sends the rest of the data to server
await transaction.CommitAsync();
MessageBox.Show("Added \"Renskrivning föreläsning " + projectName + " " + lectureDate.Day + "/" + lectureDate.Month + "\"");
}
}
}
public async void AddReadingTask(string projectName, string bookName, DateTime startDate, DateTime dueDate, int lastChapter, int firstChapter)
{
//Gets all projects and finds the choosen
var projects = await client.Projects.GetAsync();
foreach (var proj in projects)
{
if (proj.Name == projectName)
{
//Creates a transaction to cut down on times we contact server
var transaction = client.CreateTransaction();
//Adds main task
var quickAddItem = new QuickAddItem("Läs \"" + bookName + "\" @Läsning #" + projectName);
var task = await client.Items.QuickAddAsync(quickAddItem);
task.DueDate = new DueDate(dueDate.Day + "/" + dueDate.Month);
await client.Items.UpdateAsync(task);
//Calculates how many days we have per chapter
int chaptersToRead = lastChapter - (firstChapter-1);
int daysToDeadline = BusinessDaysUntil(startDate.Date, dueDate.Date);
int daysPerChapter = daysToDeadline / chaptersToRead;
var nextDueDate = startDate;
for (int i = firstChapter; i <= lastChapter; i++)
{
//Moves the duedate forward
nextDueDate = nextDueDate.AddDays(daysPerChapter + (i % 2) * (daysToDeadline % lastChapter));
//Ignores saturdays and sundays
if (nextDueDate.DayOfWeek == System.DayOfWeek.Saturday)
nextDueDate = nextDueDate.AddDays(2);
else if (nextDueDate.DayOfWeek == System.DayOfWeek.Sunday)
nextDueDate = nextDueDate.AddDays(1);
//Adds the chapter as a subtask with suptasks of its own
var quickAddSub = new QuickAddItem("Kapitel " + i);
var subtask = await client.Items.QuickAddAsync(quickAddSub);
subtask.DueDate = new DueDate(nextDueDate.Day + "/" + nextDueDate.Month);
await client.Items.UpdateAsync(subtask);
await transaction.Items.MoveAsync(ItemMoveArgument.CreateMoveToParent(subtask.Id, task.Id));
var readTaskID = await transaction.Items.AddAsync(new Item("Läsa", proj.Id));
await transaction.Items.MoveAsync(ItemMoveArgument.CreateMoveToParent(readTaskID, subtask.Id));
var summaryTaskID = await transaction.Items.AddAsync(new Item("Sammanfatta", proj.Id));
await transaction.Items.MoveAsync(ItemMoveArgument.CreateMoveToParent(summaryTaskID, subtask.Id));
}
//Sends the unsynced changes to the server
await transaction.CommitAsync();
MessageBox.Show("Added \"Läs " + bookName + "\"");
}
}
}
private int BusinessDaysUntil(DateTime firstDay, DateTime lastDay)
{
//https://stackoverflow.com/questions/1617049/calculate-the-number-of-business-days-between-two-dates
firstDay = firstDay.Date;
lastDay = lastDay.Date;
if (firstDay > lastDay)
throw new ArgumentException("Incorrect last day " + lastDay);
TimeSpan span = lastDay - firstDay;
int businessDays = span.Days + 1;
int fullWeekCount = businessDays / 7;
// find out if there are weekends during the time exceedng the full weeks
if (businessDays > fullWeekCount * 7)
{
// we are here to find out if there is a 1-day or 2-days weekend
// in the time interval remaining after subtracting the complete weeks
int firstDayOfWeek = (int)firstDay.DayOfWeek;
int lastDayOfWeek = (int)lastDay.DayOfWeek;
if (lastDayOfWeek < firstDayOfWeek)
lastDayOfWeek += 7;
if (firstDayOfWeek <= 6)
{
if (lastDayOfWeek >= 7)// Both Saturday and Sunday are in the remaining time interval
businessDays -= 2;
else if (lastDayOfWeek >= 6)// Only Saturday is in the remaining time interval
businessDays -= 1;
}
else if (firstDayOfWeek <= 7 && lastDayOfWeek >= 7)// Only Sunday is in the remaining time interval
businessDays -= 1;
}
// subtract the weekends during the full weeks in the interval
businessDays -= fullWeekCount + fullWeekCount;
return businessDays;
}
}
}