forked from m4rreParre/Simple-TaskManagerCLITool
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
171 lines (153 loc) · 4.61 KB
/
Program.cs
File metadata and controls
171 lines (153 loc) · 4.61 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
169
170
171
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text.RegularExpressions;
using Newtonsoft.Json;
namespace TaskManagerCLI;
public class Program
{
private static string TaskFilePath
{
get
{
string appDataPath = Environment.GetFolderPath(
Environment.SpecialFolder.ApplicationData
);
string appFolder = Path.Combine(appDataPath, "Simple-TaskManagerCLITool");
Directory.CreateDirectory(appFolder);
return Path.Combine(appFolder, "tasks.json");
}
}
public static List<Task> tasks = new List<Task>();
public class Task
{
public static void IdChange(int newId)
{
nextID = newId;
}
private static int nextID = 1;
public int TaskID { get; private set; }
public string WhatToDo { get; set; }
public bool Finished { get; set; }
public Task(string whatToDo)
{
TaskID = nextID;
nextID++;
WhatToDo = whatToDo;
}
public void ToggleTaskState()
{
Finished = !Finished;
}
}
public static void EditTask(int ID, string whatToDo)
{
foreach (Task task in tasks)
{
if (task.TaskID == ID)
{
task.WhatToDo = whatToDo;
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Task was changed!");
Console.ResetColor();
}
}
}
public static void ToggleTaskState(int ID)
{
foreach (Task task in tasks)
{
if (task.TaskID == ID)
{
task.ToggleTaskState();
}
}
}
public static void AddTask(string whatToDo)
{
tasks.Add(new Task(whatToDo));
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Task was added!");
Console.ResetColor();
}
public static void RemoveTask(int ID)
{
for (int i = 0; i < tasks.Count; i++)
{
if (tasks[i].TaskID == ID)
{
tasks.RemoveAt(i);
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Task was Removed!");
Console.ResetColor();
return;
}
}
}
public static void SaveTasks()
{
string json = JsonConvert.SerializeObject(tasks, Formatting.Indented);
File.WriteAllText(TaskFilePath, json);
}
public static void LoadTasks()
{
string filePath = TaskFilePath;
if (!File.Exists(filePath))
{
// Create empty tasks list and save it
tasks = new List<Task>();
SaveTasks();
return;
}
string json = File.ReadAllText(filePath);
tasks = JsonConvert.DeserializeObject<List<Task>>(json)!;
int currentID = LoadID();
Task.IdChange(currentID);
}
static int LoadID()
{
int id = 0;
foreach (Task task in tasks)
{
if (task.TaskID > id)
{
id = task.TaskID;
}
}
return id + 1;
}
public static void WriteTasks(List<Task> tasks, int selectedIndex)
{
string tasksString = ""; //Add later to write everything in one Console.WriteLine() to remove the flickering
//Needs to do the same for the clearing of the screen
for (int i = 0; i < tasks.Count; i++)
{
if (i == selectedIndex)
{
Console.BackgroundColor = ConsoleColor.White;
Console.ForegroundColor = ConsoleColor.Black;
if (tasks[i].Finished)
{
Console.WriteLine($"[x] {tasks[i].TaskID}: {tasks[i].WhatToDo}\n\r");
}
else
{
Console.WriteLine($"[ ] {tasks[i].TaskID}: {tasks[i].WhatToDo}\n\r");
}
Console.ResetColor();
}
else
{
if (tasks[i].Finished)
{
Console.WriteLine($"[x] {tasks[i].TaskID}: {tasks[i].WhatToDo}");
}
else
{
Console.WriteLine($"[ ] {tasks[i].TaskID}: {tasks[i].WhatToDo}");
}
}
}
}
}