-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimerManager.py
362 lines (285 loc) · 15.3 KB
/
TimerManager.py
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
from SubTask import SubTask
import SubTaskManager
from Timer import Timer
import FileManager
from InputThread import *
from SoundManager import SoundManager
from CalendarTrack import *
import datetime
from Meeting import Meeting
class TimerManager:
# Initialise an empty array to contain saved configurations
def __init__(self):
self.__configurations = []
self.soundManager = SoundManager()
self.__meetings = []
self.__calTrack = CalendarTrack()
def isValidDateString(self, dateStr):
# giving the date format
date_format = '%Y-%m-%d'
try:
# formatting the date using strptime() function
_dateObject = datetime.datetime.strptime(dateStr, date_format)
return True
# If the date validation goes wrong
except ValueError:
return False
# Manage the timer logic
def run(self):
isRunning = True
self.__configurations = FileManager.loadConfigs("ConfigInfo.csv")
self.__calTrack.loadCalendar()
self.__calTrack.sendNotification()
# Program keeps looping the main menu until the loop is exited by entering the exit code.
while isRunning:
# Print the main menu and get user selection
userSelection = self.mainMenu()
# If 1 is selected create a new Configuration object with input name and append it to the saved configs list
if userSelection == "1":
self.createNewConfiguration()
#Export a configuration
elif userSelection == "2":
self.exportConfiguration()
# Import a configuration
elif userSelection == "3":
self.importConfiguration()
# Run timer from config
elif userSelection == "4":
self.runTimer()
# Allow the user to modify the configuration
# First prompt the user for which configuration they want to modify
# After we know which configuration, we then allow user to modify, add, remove the subtask inside that configuration
elif userSelection == "5":
self.modifyConfiguration()
# develop one feature to enhance countdown timer, which can be used to support online meetings
elif userSelection == "6":
self.showMeetingMenu()
# Feature 5 - Sound
elif userSelection == "7":
self.setSoundSettings()
# Exit application
elif userSelection == "8":
isRunning = False
FileManager.saveConfigs("ConfigInfo.csv", self.__configurations)
self.__calTrack.saveCalendar()
# Incorrect input
else:
print("Please enter a valid selection as an integer 1-8")
def createNewConfiguration(self):
configNameIn = getInput("Enter new configuration name: ")
newConfig = SubTaskManager.createConfiguration(configNameIn)
# Ask user for subtask names & durations repeatedly until user chooses not to.
isContinue = True
while isContinue:
subTaskName = getInput("Enter the new subtask name (leave blank to end): ")
if subTaskName == "":
isContinue = False
else:
subTaskDuration = ""
while (not subTaskDuration.isdigit()):
subTaskDuration = getInput("Enter the time in minutes for subtask: ")
newSubTask = SubTask(subTaskName, int(subTaskDuration) * 60)
newConfig.addSubTask(newSubTask)
self.__configurations.append(newConfig)
print("Configuration created.")
# Once a config has been created, the user is prompted to add a reminder for the future.
calendarInput = getInput("Would you like to add a reminder for the config? Y/N")
if calendarInput.lower() == "y":
dateInput = getInput("What date will the reminder send a notification?")
while not self.isValidDateString(dateInput):
print("Incorrect data format, should be YYYY-MM-DD")
dateInput = getInput("What date will the reminder send a notification?")
self.__calTrack.addToCalendar(configNameIn, dateInput)
def exportConfiguration(self):
inputFailure = True
while inputFailure:
inputFailure = False
configIndexIn = getInput("Enter the index of the configuration you would like to export: ")
try:
FileManager.outputConfig(self.__configurations[int(configIndexIn)])
print("Configuration exported.")
except:
print("Invalid selection, please enter an integer corresponding to the configuration index (starting from 0).")
inputFailure = True
def importConfiguration(self):
filenameIn = getInput(
"Enter the name of the configuration you would like to import: ")
importedConfig = FileManager.inputConfig(filenameIn)
self.__configurations.append(importedConfig)
print("Configuration imported successfully.")
def runTimer(self):
configIndexIn = int(getInput("Enter the index of the configuration you would like to run (input from 0): "))
# prompt the user again if they enter incorrectly or enter the word
while configIndexIn < 0 or configIndexIn >= len(self.__configurations):
print("Invalid index")
configIndexIn = int(getInput("Enter the index of the configuration you would like to run: "))
config = self.__configurations[configIndexIn]
print("Type 'p' to pause the time")
print("Type 'r' to resume the time")
print("Type 'a' to advance the time")
print("Type 'e' to extend the time")
# Runs each subtask in the chosen config sequentially.
subTaskList = config.subTasks
for subTask in subTaskList:
print("Start", subTask.name)
timer = Timer(subTask.length, self.soundManager)
timer.countDown()
def modifyConfiguration(self):
configIndexIn = int(getInput("Enter the index of the configuration you would like to modify (Index must start from 0): "))
# prompt the user for input again if they enter incorrectly
while configIndexIn < 0 or configIndexIn >= len(self.__configurations):
print("Invalid index")
configIndexIn = int(getInput("Enter the index of the configuration you would like to modify (Index must start from 0): "))
config = self.__configurations[configIndexIn]
subTaskList = config.subTasks
isContinue = True
while isContinue:
print("Please make a selection:")
print("1. Modify a subtask")
print("2. Add a subtask")
print("3. Remove a subtask")
print("4. Exit configuration menu")
userSelection = getInput("Enter selection: ")
if userSelection == "1":
subTaskIndexIn = int(getInput("Enter the index of the subtask you would like to modify (Index must start from 0): "))
# prompt the user for input again if they enter incorrectly
while subTaskIndexIn < 0 or subTaskIndexIn >= len(subTaskList):
print("Invalid index")
subTaskIndexIn = int(getInput("Enter the index of the subtask you would like to modify (Index must start from 0): "))
subTask = subTaskList[subTaskIndexIn]
isContinue = True
while isContinue:
print("Please make a selection:")
print("1. Modify subtask name")
print("2. Modify subtask duration")
print("3. Exit subtask menu")
userSelection = getInput("Enter selection: ")
# verify the user input and prompt them for input again if entered incorrectly
while userSelection != "1" and userSelection != "2" and userSelection != "3":
print("Please enter a valid selection as an integer 1-3")
userSelection = getInput("Enter selection: ")
if userSelection == "1":
subTaskNameIn = getInput("Enter new subtask name: ")
subTask.name = subTaskNameIn
elif userSelection == "2":
subTaskDurationIn = int(getInput("Enter new subtask duration in minutes: "))
# run function verifySubTaskDuration() to verify user input for subtask duration
self.verifySubTaskDuration(subTaskDurationIn)
subTask.length = subTaskDurationIn * 60
elif userSelection == "3":
# allow user to exit the configuration menu and go back to the Main Menu
isContinue = False
else:
print("Please enter a valid selection as an integer 1-3")
elif userSelection == "2":
subTaskNameIn = getInput("Enter the new subtask name: ")
subTaskDurationIn = int(getInput("Enter the new subtask duration in minutes: "))
# run function verifySubTaskDuration() to verify user input for subtask duration
self.verifySubTaskDuration(subTaskDurationIn)
newSubTask = SubTask(subTaskNameIn, subTaskDurationIn * 60)
subTaskList.append(newSubTask)
elif userSelection == "3":
subTaskIndexIn = int(getInput("Enter the index of the subtask you would like to remove (Index must start from 0): "))
subTaskList.pop(subTaskIndexIn)
elif userSelection == "4":
isContinue = False
else:
print("Please enter a valid selection as an integer 1-4")
def showMeetingMenu(self):
isContinue = True
while isContinue:
print("Please make a selection:")
print("1. Create a new meeting")
print("2. Join a meeting")
print("3. Exit meeting menu")
userSelection = getInput("Enter selection: ")
# verify the user input and prompt them for input again if entered incorrectly
while userSelection != "1" and userSelection != "2" and userSelection != "3":
print("Please enter a valid selection as an integer 1-3")
userSelection = getInput("Enter selection: ")
if userSelection == "1":
meetingName = getInput("Enter the name of the meeting: ")
meetingPassword = getInput("Enter the password of the meeting: ")
meetingDuration = int(getInput("Enter the duration of the meeting in minutes: "))
# run function verifySubTaskDuration() to verify user input for meeting duration
self.verifySubTaskDuration(meetingDuration)
newMeeting = Meeting(meetingName, meetingPassword, meetingDuration * 60)
self.__meetings.append(newMeeting)
print("Meeting created successfully.")
elif userSelection == "2":
# allow user to join the meeting they created successfully by inputting the correct name and password of the meeting they set before
meetingName = getInput("Enter the name of the meeting: ")
meetingPassword = getInput("Enter the password of the meeting: ")
# display error and prompt user for input again if the meeting information is not found in the storage
while not any(meeting.name == meetingName and meeting.password == meetingPassword for meeting in self.__meetings):
print("Invalid meeting name or password")
meetingName = getInput("Enter the name of the meeting: ")
meetingPassword = getInput("Enter the password of the meeting: ")
# display the meeting information and allow user to join the meeting
for meeting in self.__meetings:
if meeting.name == meetingName and meeting.password == meetingPassword:
print("Meeting name: ", meeting.name)
print("Meeting password: ", meeting.password)
print("Meeting duration: ", meeting.duration/60, "minutes")
print("Your meeting starts now!")
timer = Timer(meeting.duration, self.soundManager)
timer.countDown()
elif userSelection == "3":
isContinue = False
else:
print("Please enter a valid selection as an integer 1-3")
def setSoundSettings(self):
print("Sound Settings")
print("Please make a selection:")
print("1. Toggle sounds on/off (currently ",end="")
# Show current sound toggle status
if(self.soundManager.enabled):
print("on)")
else:
print("off)")
# Manage user selection
soundMenuSelection = getInput("")
soundMenuInputFailure = True
while soundMenuInputFailure:
soundMenuInputFailure = False
if soundMenuSelection == "1":
self.soundManager.enabled = not self.soundManager.enabled
else:
soundMenuInputFailure = True
print("Please enter a valid selection as an integer")
# Print the main menu information and take in user selection
def mainMenu(self):
print("Main Menu")
print("Please make a selection:")
print("1. Create new configuration")
print("2. Export a configuration")
print("3. Import a configuration")
print("4. Run timer from a configuration")
print("5. Modify a configuration")
print("6. Meeting menu")
print("7. Set sound settings")
print("8. Exit application")
if len(self.__configurations) != 0:
print("Current configurations:")
self.showConfigurations()
# Kill the input thread
userSelection = getInput("Enter selection: ", "8")
return userSelection
# Print all currently saved configurations
def showConfigurations(self):
for i, configuration in enumerate(self.__configurations):
print(str(i) + ":", configuration)
# Print all saved subtasks
def showSubTasks(self):
for configuration in self.__configurations:
for subTask in configuration.subTasks:
print(subTask)
# function to verify if user enters the subtask duration correctly and if not, prompt them again
def verifySubTaskDuration(self, subTaskDurationIn):
while subTaskDurationIn < 0:
print("Please enter a valid duration for the subtask")
subTaskDurationIn = int(getInput("Enter the new subtask duration in minutes: "))
return subTaskDurationIn
startInputThread()
test = TimerManager()
test.run()