-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
261 lines (205 loc) · 8.83 KB
/
main.py
File metadata and controls
261 lines (205 loc) · 8.83 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
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
from tkinter import *
from buttonbars import *
from Event import *
import json_handler
from datetime import date
import util as utils
from dailyview import renderCalendar
import datetime
from googleclient import authenticateGoogleCalendar
from reviewpopup import ReviewPopup
service = None
buttonbar = None
dailyView = None
weeklyView = None
breakdownPopup = None
# TIME FORMAT: HH:MM AM/PM
# Accepted times: 12:00 AM - 11:59 PM
# Holds the raw events
eventData = []
# Get date string: utils.toDateString(currentDate)
currentDate = datetime.datetime.today()
# Holds the events with the rows they're going to be displayed on
scheduledData = [[]]
# eventData.append(Event("Science Test", "Biology",
# "9:00 AM", "3:00 PM", eventType="event", currentDate=currentDate))
# eventData.append(Event("Science Test 2", "Biology 2",
# "9:00 AM", "3:00 PM", eventType="event", currentDate=currentDate))
# eventData.append(Event(
# "SS Project", "Finish our project - still waiting on Joe to notify", "8:45 AM", "11:15 AM", eventType="task", currentDate=currentDate))
# eventData.append(Event("Programming Test Prep", "CS1",
# "12:45 PM", "3:00 PM", eventType="task", currentDate=currentDate))
# eventData.append(Event(
# "Math Test - VERY LONG NAME THAT WILL WRAP INTO LOTS OF LINES AND POTENTIALLY CAUSE LOTS OF PROBLEMS BUT HOPEFULLY WE CAN FIX THEM", "Calculus - Start of a really long description that we need to be able to wrap and limit. Can we make this even longer though and not break the text?", "8:45 AM", "9:45 AM", eventType="event", currentDate=currentDate))
# eventData.append(Event("Hang out with Shelly", "Still haven't decided where we're going",
# "7:00 PM", "11:00 PM", actualStart="8:00PM", actualEnd="11:00 PM", eventType="task", currentDate=currentDate))
# eventData.append(Event("Math Test - VERY LONG NAME THAT WILL WRAP INTO LOTS OF LINES AND POTENTIALLY CAUSE LOTS OF PROBLEMS BUT HOPEFULLY WE CAN FIX THEM",
# "Calculus - Start of a really long description that we need to be able to wrap and limit. Can we make this even longer though and not break the text?", "8:45 AM", "9:45 AM",
# eventType="event", currentDate=currentDate))
# eventData.append(Event("Hang out with Shelly", "Still haven't decided where we're going","7:00 PM", "11:59 PM",
# actualStart="8:00PM", actualEnd="11:59 PM", eventType="task", currentDate=currentDate))
canvWidth = utils.canvWidth
canvHeight = utils.canvHeight
cellWidth = utils.cellWidth
canvStartHour = 0
def incStart():
global canvStartHour, dailyView
if canvStartHour >= 24 - canvWidth/cellWidth:
return
canvStartHour += 1
rerenderCanvas()
def decStart():
global canvStartHour, dailyView
if canvStartHour == 0:
return
canvStartHour -= 1
rerenderCanvas()
def incDate():
global currentDate, eventData
currentDate += datetime.timedelta(days=1)
rerenderTopBar()
# Swap out eventData (list of event objects) to load whatever JSON events are at the new currentDate value
eventData = []
eventData = json_handler.loadDayEvents(currentDate)
scheduleEvents()
rerenderCanvas()
def decDate():
global currentDate, eventData
currentDate -= datetime.timedelta(days=1)
rerenderTopBar()
# Swap out eventData (list of event objects) to load whatever JSON events are at the new currentDate value
eventData = []
eventData = json_handler.loadDayEvents(currentDate)
scheduleEvents()
rerenderCanvas()
def updateEvent(newVersion):
# Replace previous version with new version
# Re-sort and re-render
for i in range(0, len(eventData)):
event = eventData[i]
if event.uuid == newVersion.uuid:
eventData[i] = newVersion
json_handler.updateJSON(newVersion, currentDate)
scheduleEvents()
rerenderCanvas()
return True
return False # Indicate it's safe to replace prevVersion with newVersion
def deleteEvent(previousVersion):
# resort and render the calendar again
for i in range(0, len(eventData)):
event = eventData[i]
if event.uuid == previousVersion.uuid:
eventData.remove(event)
json_handler.delete_event(previousVersion, currentDate)
scheduleEvents()
rerenderCanvas()
return True
return False
# Call every time an event gets added or removed
def scheduleEvents():
eventData.sort()
scheduledData.clear()
for singleEvent in eventData:
index = 0
while True:
if index >= len(scheduledData):
scheduledData.append([])
break
elif len(scheduledData[index]) == 0:
break
else:
previousEventEnd = scheduledData[index][len(
scheduledData[index]) - 1].end
if(previousEventEnd.__le__(singleEvent.start)):
break
index += 1
scheduledData[index].append(singleEvent)
def addNewEvent(event):
eventData.append(event)
json_handler.new_event(event, currentDate)
scheduleEvents()
rerenderCanvas()
def syncCalendar():
global service
if service is None:
service = authenticateGoogleCalendar()
startTime = datetime.datetime(
currentDate.year, currentDate.month, currentDate.day, 0, 0, 0, 0).astimezone(tz=datetime.timezone.utc).isoformat() # midnight today
endTime = datetime.datetime(
currentDate.year, currentDate.month, currentDate.day, 23, 59, 59, 0).astimezone(tz=datetime.timezone.utc).isoformat() # 11:59PM today
# Call the Calendar API
now = datetime.datetime.utcnow().isoformat() + 'Z' # 'Z' indicates UTC time
events_result = service.events().list(calendarId='primary', timeMin=startTime, timeMax=endTime, singleEvents=True,
orderBy='startTime').execute()
google_events_list = events_result.get('items', [])
if not google_events_list:
print('No upcoming events found.')
print('Found ' + str(len(google_events_list)) + ' events')
# Remove any google events that we're pulled previously so we don't introduce any duplicates
i = 0
while len(eventData) > i:
e = eventData[i]
if e.fromGoogle == True:
eventData.remove(e)
json_handler.delete_event(e, currentDate)
else:
i += 1
for e in google_events_list:
name = e['summary']
description = e['description'] if 'description' in e else e['htmlLink']
start = datetime.datetime.fromisoformat(
e['start'].get('dateTime', e['start'].get('date'))).replace(tzinfo=None)
end = datetime.datetime.fromisoformat(
e['end'].get('dateTime', e['end'].get('date'))).replace(tzinfo=None)
# Remove any events that start/end on a different day, which are not currently supported by this application
if start.date() != currentDate.date() or end.date() != currentDate.date():
continue
newEvent = Event(name, description, start, end,
fromGoogle=True, currentDate=currentDate)
eventData.append(newEvent)
json_handler.new_event(newEvent, currentDate)
scheduleEvents()
rerenderCanvas()
def generateDailyReview():
global breakdownPopup
if not(breakdownPopup is None) and not (breakdownPopup.win is None):
breakdownPopup.win.destroy()
breakdownPopup = ReviewPopup(currentDate, eventData,
scheduledData, frequency="daily")
def generateWeeklyReview():
global breakdownPopup
if not(breakdownPopup is None) and not (breakdownPopup.win is None):
breakdownPopup.win.destroy()
breakdownPopup = ReviewPopup(currentDate, eventData,
scheduledData, frequency="weekly")
def rerenderTopBar():
for child in topButtonBar.winfo_children():
child.destroy()
topButtonBar.pack(fill=X)
renderTopBar(topButtonBar, currentDate,
(addNewEvent, syncCalendar, incDate, decDate))
print("re-rendered top bar")
def rerenderCanvas():
for child in dailyView.winfo_children():
child.destroy()
dailyView.pack()
renderCalendar(dailyView, currentDate, scheduledData, canvStartHour,
(updateEvent, deleteEvent))
eventData = json_handler.loadDayEvents(currentDate)
scheduleEvents()
window = Tk()
window.geometry("1500x1000")
window.resizable(0, 0)
window.configure(bg="white")
window.winfo_toplevel().title("Visualize Your Day")
topButtonBar = Frame(window, bd=5, bg="white")
topButtonBar.pack(fill=X)
rerenderTopBar()
dailyView = Frame(window)
dailyView.pack()
rerenderCanvas()
bottomButtonBar = Frame(window, bg="white")
bottomButtonBar.pack(fill=X)
renderBottomBar(bottomButtonBar, (incStart,
decStart, generateDailyReview, generateWeeklyReview))
window.mainloop()