calculator & benitlux: remove obsolete explicit observer declaration
[nit.git] / contrib / benitlux / src / client / views / home_views.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # Main home window
16 module home_views
17
18 import beer_views
19 import social_views
20 import user_views
21
22 redef class App
23 redef fun on_create
24 do
25 if debug then print "App::on_create"
26
27 # Create the main window
28 show_home
29 super
30 end
31
32 # Show the home/main windows
33 fun show_home
34 do
35 var window = new HomeWindow
36 window.refresh
37 push_window window
38 end
39
40 redef fun on_log_in
41 do
42 super
43
44 # Send back to the home window when logging in
45 if not window isa HomeWindow then pop_window
46 end
47 end
48
49 # Social pane with networking features
50 class HomeWindow
51 super Window
52
53 private var layout = new ListLayout(parent=self)
54
55 # Cut-point for the iOS adaptation
56 var layout_user = new VerticalLayout(parent=layout)
57 private var layout_login = new HorizontalLayout(parent=layout_user)
58 private var lbl_login_status = new Label(parent=layout_login, text="Welcome".t, size=1.5)
59 private var but_login = new Button(parent=layout_login, text="Login or signup".t)
60 private var but_preferences = new Button(parent=layout_login, text="Preferences".t)
61
62 private var layout_beers = new VerticalLayout(parent=layout)
63 var layout_beers_title = new HorizontalLayout(parent=layout_beers)
64 var title_beers = new SectionTitle(parent=layout_beers_title, text="Beer Menu".t, size=1.5)
65 private var beer_button = new Button(parent=layout_beers_title, text="View all".t)
66 private var beer_list = new VerticalLayout(parent=layout_beers)
67 private var beer_temp_lbl = new Label(parent=beer_list, text="Loading...".t)
68
69 private var layout_social = new VerticalLayout(parent=layout)
70 private var social_header = new HorizontalLayout(parent=layout_social)
71 private var social_title = new SectionTitle(parent=social_header, text="Friends".t, size=1.5)
72 private var social_button = new Button(parent=social_header, text="Manage".t)
73 private var social_list = new VerticalLayout(parent=layout_social)
74 private var social_temp_lbl = new Label(parent=social_list, text="Loading...".t)
75
76 private var layout_news = new VerticalLayout(parent=layout)
77 private var news_header = new HorizontalLayout(parent=layout_news)
78 private var news_title = new SectionTitle(parent=news_header, text="Events".t, size=1.5)
79 #private var news_button = new Button(parent=news_header, text="Open website") # TODO
80 private var news_label = new Label(parent=layout_news, text="Bière en cask le jeudi!")
81
82 redef fun on_resume do refresh
83
84 # Refresh content of this page
85 fun refresh
86 do
87 if not app.restored then return
88
89 layout_login.clear
90 if app.user != null then
91 # Logged in
92 lbl_login_status.parent = layout_login
93 but_preferences.parent = layout_login
94 lbl_login_status.set_welcome
95 else
96 but_login.parent = layout_login
97 but_preferences.parent = layout_login
98 end
99
100 # Fill beers
101 (new ListDiffAction(self, "rest/since?token={app.token}")).start
102
103 # Fill people
104 (new HomeListPeopleAction(self, "rest/friends?token={app.token}")).start
105
106 # Check if token is still valid
107 (new CheckTokenAction(self, "rest/check_token?token={app.token}")).start
108 end
109
110 redef fun on_event(event)
111 do
112 if debug then print "BenitluxWindow::on_event {event}"
113
114 if event isa ButtonPressEvent then
115 var sender = event.sender
116 if sender == but_preferences then
117 app.push_window new UserWindow
118 return
119 else if sender == but_login then
120 app.push_window new SignupWindow
121 return
122 else if sender == beer_button then
123 app.push_window new BeersWindow
124 return
125 else if sender == social_button then
126 app.push_window new SocialWindow
127 return
128 #else if sender == news_button then
129 # TODO open browser?
130 end
131 end
132
133 super
134 end
135 end
136
137 # `Label` used in section headers
138 class SectionTitle super Label end
139
140 # Async request to update the beer list on the home screen
141 class ListDiffAction
142 super WindowHttpRequest
143
144 redef type W: HomeWindow
145
146 redef fun on_load(beers, status)
147 do
148 window.layout_beers.remove window.beer_list
149 window.beer_list = new VerticalLayout(parent=window.layout_beers)
150
151 if intercept_error(beers) then return
152
153 if not beers isa Array[BeerAndRatings] then
154 app.feedback "Communication Error".t
155 return
156 end
157
158 # Sort beers per preference
159 var comparator = new BeerComparator
160 comparator.sort beers
161
162 var max_beers = 6
163 while beers.length > max_beers do beers.pop
164
165 for bar in beers do
166 var view = new BeerView(bar, parent=window.beer_list)
167 end
168 end
169 end
170
171 # Async request to list users
172 class HomeListPeopleAction
173 super WindowHttpRequest
174
175 redef type W: HomeWindow
176
177 redef fun on_load(users, status)
178 do
179 window.layout_social.remove window.social_list
180 window.social_list = new VerticalLayout(parent=window.layout_social)
181
182 if intercept_error(users) then return
183
184 if users isa Array[UserAndFollowing] then for uaf in users do
185 var view = new PeopleView(uaf, true, parent=window.social_list)
186 end
187 end
188 end
189
190 # Async request to check if `app.token` is still valid
191 class CheckTokenAction
192 super WindowHttpRequest
193
194 redef type W: HomeWindow
195
196 redef fun on_load(res, status) do intercept_error(res)
197 end
198
199 # Today's date as a `String`
200 fun today: String
201 do
202 var tm = new Tm.localtime
203 return "{tm.year+1900}-{tm.mon+1}-{tm.mday}"
204 end