This repository has been archived by the owner on Nov 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmenuscreen.py
309 lines (236 loc) · 10.2 KB
/
menuscreen.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
from kivy.uix.behaviors.togglebutton import ToggleButtonBehavior
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout
from kivy.uix.relativelayout import RelativeLayout
from kivy.uix.screenmanager import CardTransition, Screen
from kivy.properties import BooleanProperty
from mainwidgets import HoverWidget, MainPopup, SelectionPopup, MessagePopup
from guest import *
class ActivityToggleBehavior(ToggleButtonBehavior):
last = []
def on_state(self, instance, value):
if value == "down":
self.last.append(self)
if len(self.last) > 2:
self.last.pop(0)
# allow undo when booking challenge activity
@classmethod
def undo(cls):
cls.last[0].state = "down"
# note the index is 0 next line because cls.last is updated last line
cls.last[0].state = "normal"
MessagePopup(message="Booking abandoned!").open()
# block with detailed information of one activity and booking ability
class ActivityBlock(HoverWidget, ActivityToggleBehavior, BoxLayout, Activity):
def __init__(self, **kwargs):
super().__init__(**kwargs)
if Guest.get_booked("activities", self.day) == self.name:
self.state = "down"
self.bind(state=self.update)
def on_hover_enter(self):
if self.state == "normal":
self.border_color = .45, .45, .45, 1 # show grey border
def on_hover_leave(self):
if self.state == "normal":
self.border_color = 0, 0, 0, 0 # hide grey border
def on_state(self, instance, value):
super().on_state(instance, value)
if self.state == "down": # show orange border for booked activity
self.border_color = 1, .48, .15, 1
else: # remove border for unbooked activity
self.border_color = 0, 0, 0, 0
def _update(self, *args):
# update activity booking database
Guest.book_activity("activities", self.day, self.name)
MessagePopup(message="Booking saved!").open()
def update(self, instance, value):
if value == "down":
# confirmation required when booking challenging activity
if self.rating == "challenging":
popup = SelectionPopup(title="Warning",
message="Be aware that this activity is challenging.\n"
"Please confirm that you have no disability or disease "
"that could potentially cause harm to your body.")
popup.add_choice(text="Book anyway",
on_release=self._update)
popup.add_choice(text="Cancel",
on_release=lambda _: instance.undo())
popup.open()
else:
self._update()
class ActivityLayout(BoxLayout):
def __init__(self, **kwargs):
day = kwargs.pop("day")
super().__init__(**kwargs)
# instantiate all activity available on the day
for rating, activity in Activity.on_day(day=day).items():
self.add_widget(ActivityBlock(name=activity.name,
rating=activity.rating,
desc=activity.desc,
price=activity.price,
day=day))
class ActivityPicker(MainPopup):
def __init__(self, **kwargs):
day = kwargs.pop("day")
kwargs["title"] = "Activities available for day " + str(day)
super().__init__(**kwargs)
self.content.add_widget(ActivityLayout(day=day))
def dismiss(self, *largs, **kwargs):
super().dismiss(*largs, **kwargs)
# refresh calendar to show booked activity
self.refresh_ref.refresh()
"""
Below are meal booking widgets.
"""
class MealBlock(ToggleButtonBehavior, HoverWidget, BoxLayout, Meal):
def __init__(self, **kwargs):
super().__init__(**kwargs)
if Guest.get_booked("meals", self.day) == self.name:
self.state = "down"
self.bind(state=self.update)
def on_hover_enter(self):
if self.state == "normal":
self.border_color = .45, .45, .45, 1 # show grey border
def on_hover_leave(self):
if self.state == "normal":
self.border_color = 0, 0, 0, 0 # hide grey border
def on_state(self, instance, value):
if self.state == "down": # show orange border for booked activity
self.border_color = 1, .48, .15, 1
else: # remove border for unbooked activity
self.border_color = 0, 0, 0, 0
def update(self, instance, value):
if value == "down":
Guest.book_activity("meals", self.day, self.name)
MessagePopup(message="Meal booking saved!").open()
class MealLayout(BoxLayout):
def __init__(self, **kwargs):
day = kwargs.pop("day")
super().__init__(**kwargs)
for meal in Meal.load():
self.add_widget(MealBlock(name=meal.name,
desc=meal.desc,
day=day))
class MealPicker(MainPopup):
def __init__(self, **kwargs):
day = kwargs.pop("day")
kwargs["title"] = "Meals available for day " + str(day)
super().__init__(**kwargs)
self.content.add_widget(MealLayout(day=day))
def dismiss(self, *largs, **kwargs):
super().dismiss(*largs, **kwargs)
# update calendar to show booked meal
self.refresh_ref.refresh()
"""
Below are blocks that build up the calendar
"""
class BookButton(Button):
pass
class CalendarSubBlock(HoverWidget, RelativeLayout):
unavailable = BooleanProperty(False)
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.book_btn = BookButton()
self.book_btn.bind(on_release=self.book)
def on_hover_enter(self):
self.add_widget(self.book_btn)
def on_hover_leave(self):
self.remove_widget(self.book_btn)
def on_touch_up(self, touch):
pass
class CalendarActivityBlock(CalendarSubBlock):
def refresh(self):
if self.day <= Activity.BOOK_AHEAD:
self.name = "No activity available"
self.unavailable = True
if "image" in self.ids:
self.remove_widget(self.ids.image)
self.ids.pop("image") # remove weak reference
else:
activity = Guest.get_booked("activities", self.day)
if activity == Activity.no_activity().name:
self.name = Activity.no_activity().name
else:
self.name = activity
self.img = Activity.find(self.name).img
if self.day < get_day() + Activity.BOOK_AHEAD:
self.unavailable = True
else:
self.unavailable = False
def book(self, *args):
# handle unavailable situations
if self.day <= Activity.BOOK_AHEAD:
MessagePopup(message="There are no activities available for the first three days.").open()
elif self.day < get_day():
MessagePopup(message="You can not book activities for days before!").open()
elif self.day < get_day() + Activity.BOOK_AHEAD:
MessagePopup(message="You need to book activities at least three days ahead!").open()
else:
# initiate and open activity picker popup
activity_picker = ActivityPicker(day=self.day)
activity_picker.refresh_ref = self # save for future update
activity_picker.open()
class CalendarMealBlock(CalendarSubBlock):
def refresh(self):
self.name = Guest.get_booked("meals", self.day)
self.img = Meal.find(self.name).img
if self.day <= get_day():
self.unavailable = True
else:
self.unavailable = False
def book(self, *args):
if self.day <= get_day():
MessagePopup(message="You can not book meal for today or days before!").open()
else:
popup = MealPicker(day=self.day)
popup.refresh_ref = self
popup.open()
class CalendarBlock(BoxLayout):
def __init__(self, **kwargs):
day_count = kwargs.pop("day")
super().__init__(**kwargs)
self.day = day_count
self.doc = " (Today)"
def refresh(self):
self.ids.activity_btn.refresh()
self.ids.meal_btn.refresh()
if self.day == get_day():
self.doc = " (Today)"
else:
self.doc = ''
class CalendarLayout(GridLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
for day in range(1, 15):
self.add_widget(CalendarBlock(day=day))
def refresh(self):
for widget in self.children:
widget.refresh()
class MenuScreen(Screen):
def on_pre_enter(self, *args):
# refresh the booking calendar every time screen appears
self.ids.calendar.refresh()
self.username = Guest.logged_in
def logout(self):
def _logout(*args):
# change to login screen and logout
self.manager.transition = CardTransition(direction="down", mode="push")
self.manager.current = "login"
Guest.logout()
# warn about logging out first
popup = SelectionPopup(title="Warning",
message="After logout you have to type in your credentials again.\n"
"Do you really want to logout?")
popup.add_choice(text="Logout",
on_release=_logout)
popup.add_choice(text="Cancel")
popup.open()
def to_profile(self): # go to profile screen with set animation
self.manager.transition = CardTransition(direction="down", mode="push")
self.manager.current = "profile"
self.manager.transition = CardTransition(direction="up", mode="pop")
def to_cost(self): # go to cost screen with set animation
self.manager.transition = CardTransition(direction="down", mode="push")
self.manager.current = "cost"
self.manager.transition = CardTransition(direction="up", mode="pop")