6834a747295467c58dfd95de6d49c03cb01dc6e6
[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 init
83 do
84 for c in [but_login, but_preferences, beer_button, social_button] do
85 c.observers.add self
86 end
87 end
88
89 redef fun on_resume do refresh
90
91 # Refresh content of this page
92 fun refresh
93 do
94 if not app.restored then return
95
96 layout_login.clear
97 if app.user != null then
98 # Logged in
99 lbl_login_status.parent = layout_login
100 but_preferences.parent = layout_login
101 lbl_login_status.set_welcome
102 else
103 but_login.parent = layout_login
104 but_preferences.parent = layout_login
105 end
106
107 # Fill beers
108 (new ListDiffAction(self, "rest/since?token={app.token}")).start
109
110 # Fill people
111 (new HomeListPeopleAction(self, "rest/friends?token={app.token}")).start
112
113 # Check if token is still valid
114 (new CheckTokenAction(self, "rest/check_token?token={app.token}")).start
115 end
116
117 redef fun on_event(event)
118 do
119 if debug then print "BenitluxWindow::on_event {event}"
120
121 if event isa ButtonPressEvent then
122 var sender = event.sender
123 if sender == but_preferences then
124 app.push_window new UserWindow
125 return
126 else if sender == but_login then
127 app.push_window new SignupWindow
128 return
129 else if sender == beer_button then
130 app.push_window new BeersWindow
131 return
132 else if sender == social_button then
133 app.push_window new SocialWindow
134 return
135 #else if sender == news_button then
136 # TODO open browser?
137 end
138 end
139
140 super
141 end
142 end
143
144 # `Label` used in section headers
145 class SectionTitle super Label end
146
147 # Async request to update the beer list on the home screen
148 class ListDiffAction
149 super WindowHttpRequest
150
151 redef type W: HomeWindow
152
153 redef fun on_load(beers, status)
154 do
155 window.layout_beers.remove window.beer_list
156 window.beer_list = new VerticalLayout(parent=window.layout_beers)
157
158 if intercept_error(beers) then return
159
160 if not beers isa Array[BeerAndRatings] then
161 app.feedback "Communication Error".t
162 return
163 end
164
165 # Sort beers per preference
166 var comparator = new BeerComparator
167 comparator.sort beers
168
169 var max_beers = 6
170 while beers.length > max_beers do beers.pop
171
172 for bar in beers do
173 var view = new BeerView(bar, parent=window.beer_list)
174 end
175 end
176 end
177
178 # Async request to list users
179 class HomeListPeopleAction
180 super WindowHttpRequest
181
182 redef type W: HomeWindow
183
184 redef fun on_load(users, status)
185 do
186 window.layout_social.remove window.social_list
187 window.social_list = new VerticalLayout(parent=window.layout_social)
188
189 if intercept_error(users) then return
190
191 if users isa Array[UserAndFollowing] then for uaf in users do
192 var view = new PeopleView(uaf, true, parent=window.social_list)
193 end
194 end
195 end
196
197 # Async request to check if `app.token` is still valid
198 class CheckTokenAction
199 super WindowHttpRequest
200
201 redef type W: HomeWindow
202
203 redef fun on_load(res, status) do intercept_error(res)
204 end
205
206 # Today's date as a `String`
207 fun today: String
208 do
209 var tm = new Tm.localtime
210 return "{tm.year+1900}-{tm.mon+1}-{tm.mday}"
211 end