-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
288 lines (240 loc) · 11.7 KB
/
main.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
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.scrollview import ScrollView
from kivy.clock import Clock
from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.properties import StringProperty, NumericProperty, ListProperty, BooleanProperty
from kivymd.uix.list import TwoLineAvatarListItem, TwoLineListItem, OneLineAvatarListItem
from kivymd.uix.tab import MDTabsBase
from kivy.uix.screenmanager import ScreenManager, Screen, NoTransition
from kivy.storage.jsonstore import JsonStore
import os.path
from lib.bus.data import AllBusData
from StopInfo import StopInfo
from lib import taskdb
from newtask import NewTaskPage
from settings import SettingsPage
from detailedview import DetailedViewPage
class ContentScroll(ScrollView):
#define the ContentScroll widget as a ScrollView
pass
class ContentHomeworkScroll(BoxLayout):
#define the ContentHomeworkScroll widget as a BoxLayout
pass
class ContentHwView(BoxLayout):
#define the ContentHwView page as a BoxLayout
pass
class HomeworkListItem(TwoLineListItem):
#define HomeworkListItem as a TwoLineListItem but with the extra attribute 'task_id'
task_id = NumericProperty()
class HomeworkGridItem(BoxLayout):
#define HomeworkListItem as a BoxLayout with the extra attributes name, subject, and due date
name = StringProperty()
subject = StringProperty()
due = StringProperty()
class DetailedBusItem(TwoLineAvatarListItem):
#define HomeworkListItem as a TwoLineListItem with an icon at the extra attribute 'icon'
icon = StringProperty()
class ShortBusItem(OneLineAvatarListItem):
#define HomeworkListItem as a OneLineListItem with an icon at the extra attribute 'icon'
icon = StringProperty()
class HomeworkTab(BoxLayout, MDTabsBase):
#define the tabs on the homework page as BoxLayouts
pass
class SchoolApp(MDApp):
#instantiate basic settings and global variables
dbPath = StringProperty('homework.db')
sub_list = ListProperty()
home_stop = StringProperty('036006209209')
school_stop = StringProperty('03700075')
currentStop = StringProperty('school')
bus_title = StringProperty('Bus times')
current_hwid = NumericProperty()
archive = BooleanProperty(False)
def build(self):
#the build function is the first thing that is run, telling the program to load settings from file and then the main GUI file
self.load_settings()
return Builder.load_file('main.kv')
def load_settings(self):
#if the settings file exists, load the school and home stops, and the automatic archiving setting
app = self.get_running_app()
store = JsonStore('settings.json')
if store.exists('settings'):
app.home_stop = store['settings']['home_stop']
app.school_stop = store['settings']['school_stop']
app.archive = store['settings']['archive']
def on_start(self):
#once the GUI is created, the following code is run
#setting the current stop to school, if it is the afternoon
import datetime
dt = datetime.datetime.now()
if dt.time() < datetime.time(12, 30):
self.currentStop = 'home'
if not self.previously_open_check():
#if the app has not been set-up previously
#open the settings screen
self.changeScreen('settings_screen')
#refresh all loads all the data
self.refresh_all()
#schedules the bus times to be refreshed in 30 seconds
Clock.schedule_once(self.bus_auto_refresh, 30)
def previously_open_check(self):
#checks for the existence of settings file
if os.path.isfile('settings.json'):
#if settings exist, return true
print('settings detected')
return True
else:
#otherwise, return false
print('no settings detected')
return False
def refresh_all(self):
self.refreshBusTimes() #load bus times
self.refeshTaskList() #load current homework tasks
self.setSubjectList() #load list of subjects from the database
self.refreshArchiveList() #load list of all tasks for selected subject
def bus_auto_refresh(self, now = True):
#function to automatically refresh bus times every 30 seconds
if now:
if self.root.ids.panel.ids.tab_manager.current in ['bus','home']:
#if the app currently has a tab containing bus times open, refresh the bus times, and schedule another refresh in 30 seconds
self.refreshBusTimes()
Clock.schedule_once(self.bus_auto_refresh, 30)
else:
#if any other part of the app is open, just schedule it to check again in 30 seconds
Clock.schedule_once(self.bus_auto_refresh, 30)
else:
#provides a method to schedule another refresh without actually refresshing on the call
Clock.schedule_once(self.bus_auto_refresh, 30)
def setBusList(self):
#function to set the lists of bus times
#sets the current bus stop code to request data for to match the current stop variable
if self.currentStop == 'school':
current_atco = self.school_stop
elif self.currentStop == 'home':
current_atco = self.home_stop
#sets the title of the bus times tab to reflect the times shown
self.bus_title = 'Bus times for ' + self.currentStop
#gets all the bus times from the bus time module
results = AllBusData(current_atco)
for bus in results:
#iterating though each bus in order to add the time to the lists
#calculating the time until the bus
min, sec = divmod((bus['departure']-bus['requestTime']).total_seconds(),60)
text = str(int(min)) + " mins " + str(int(sec)) + " seconds"
#creating text for the departure list, based on whether the time is live or not
if bus['live']:
secondary_text = bus['destination'] + ', Live: ' + bus['departure'].astimezone(tz=None).strftime("%H:%M:%S") + ', Operator: ' + bus['operator']
else:
secondary_text = bus['destination'] + ', Scheduled: ' + bus['departure'].astimezone(tz=None).strftime("%H:%M:%S") + ', Operator: ' + bus['operator']
icon = 'img/' + str(bus['line']) + '.png'
#adding full bus details to the bus times page
self.root.ids.bus_scroll.ids.box_item.add_widget(
DetailedBusItem(
text=text,
icon=icon,
secondary_text= secondary_text,
)
)
#adding short bus details to the home page
self.root.ids.short_bus.ids.box_item.add_widget(
ShortBusItem(
text=text, #the text is the minutes and seconds left
icon=icon, #the icon is the bus number in its colour
)
)
#adding extra data information to the bottom of the bus times
refreshText = 'Last refresh: ' + str(results[0]['requestTime'].astimezone(tz=None).strftime("%H:%M:%S"))
self.root.ids.bus_scroll.ids.box_item.add_widget(
DetailedBusItem(
text= refreshText,
secondary_text='Data provided by: TfL, Traveline, Reading Buses',
icon= 'img/refresh.png',
)
)
def refreshBusTimes(self):
#function used when refreshing bus times - clears both bus tile lists, then reloads them
self.root.ids.bus_scroll.ids.box_item.clear_widgets()
self.root.ids.short_bus.ids.box_item.clear_widgets()
self.setBusList()
def setTaskList(self):
#function to load the list of current homework tasks
taskdb.checkDB(self.dbPath) #check if the homework database exists, or create a new one
tasks = taskdb.CurrentTasks(self.dbPath) #load all the current tasks from the database
tasks.sort(key=lambda x:x['dueDate']) #sort tasks, earliest due date first
for task in tasks:
if self.archive:
#if auto-archival is on in settings, archive task if was due before today
import datetime
today = datetime.date.today()
if str(today) > task['dueDate']:
taskdb.SetArchiveHW(self.dbPath, task['taskID'], True)
continue
#add the task to the main homework list
text = task['taskName'] + ', ' + task['subject']
secondarytext = task['dueDate'] + ' ' + str(task['taskID'])
self.root.ids.homework_screen.ids.current_task_scroll.ids.box_item.add_widget(
HomeworkListItem(
text=text,
secondary_text= secondarytext,
task_id = task['taskID']
)
)
#add the task to the home page list
self.root.ids.short_hw.ids.box_item.add_widget(
HomeworkGridItem(
name = task['taskName'],
subject = task['subject'],
due = task['dueDate']
)
)
def setSubjectList(self):
#function to load every subject to a list
self.sub_list = taskdb.SubjectList(self.dbPath)
def setArchiveList(self):
#function to load list of all homework for a specific subject
self.sub_list = taskdb.SubjectList(self.dbPath) #refresh list of subjects
subject = self.root.ids.homework_screen.ids.subject_dropdown.current_item #get the currently selected subject name
tasks = taskdb.ArchiveTasks(self.dbPath,subject) #retreive tasks for selected subject
for task in tasks:
#add each task to the archive list
text = task['taskName'] + ', Set on ' + task['issueDate']
secondarytext = 'Due: ' + task['dueDate'] + ' ' + str(task['taskID'])
self.root.ids.homework_screen.ids.subject_task_scroll.ids.box_item.add_widget(
HomeworkListItem(
text=text,
secondary_text= secondarytext,
task_id = task['taskID']
)
)
def refreshArchiveList(self):
#function to clear out the archive list of tasks, then reload it
self.root.ids.homework_screen.ids.subject_task_scroll.ids.box_item.clear_widgets()
self.setArchiveList()
def refeshTaskList(self):
#function to clear out the current list of tasks, then reload it
self.root.ids.homework_screen.ids.current_task_scroll.ids.box_item.clear_widgets()
self.root.ids.short_hw.ids.box_item.clear_widgets()
self.setTaskList()
def changeScreen(self, screen):
#function to change the app screen to the desired one instantly
print(screen)
sm = self.root
sm.transition = NoTransition()
sm.current = screen
def store_settings(self):
#function to store the current settings to an external JSON file
app = self.get_running_app()
store = JsonStore('settings.json')
store['settings'] = {
'home_stop': app.home_stop,
'school_stop': app.school_stop,
'archive': app.archive
}
def homework_detailed_view(self, id):
#function to open the detailed view page for a specific task
self.current_hwid = id
self.changeScreen('detailed_view_screen')
self.root.ids.detailed_view.load_data()
SchoolApp().run()