bd3f8514468bb45fee1ad938ceec53d83fd16d1b
[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 but_preferences: nullable Button = null
59 private var but_login: nullable Button = null
60
61 private var layout_beers = new VerticalLayout(parent=layout)
62 var layout_beers_title = new HorizontalLayout(parent=layout_beers)
63 var title_beers = new SectionTitle(parent=layout_beers_title, text="Beer Menu".t, size=1.5)
64 private var beer_button = new Button(parent=layout_beers_title, text="View all".t)
65 private var beer_list = new VerticalLayout(parent=layout_beers)
66 private var beer_temp_lbl = new Label(parent=beer_list, text="Loading...".t)
67
68 private var layout_social = new VerticalLayout(parent=layout)
69 private var social_header = new HorizontalLayout(parent=layout_social)
70 private var social_title = new SectionTitle(parent=social_header, text="Friends".t, size=1.5)
71 private var social_button = new Button(parent=social_header, text="Manage".t)
72 private var social_list = new VerticalLayout(parent=layout_social)
73 private var social_temp_lbl = new Label(parent=social_list, text="Loading...".t)
74
75 private var layout_news = new VerticalLayout(parent=layout)
76 private var news_header = new HorizontalLayout(parent=layout_news)
77 private var news_title = new SectionTitle(parent=news_header, text="Events".t, size=1.5)
78 #private var news_button = new Button(parent=news_header, text="Open website") # TODO
79 private var news_label = new Label(parent=layout_news, text="Bière en cask le jeudi!")
80
81 redef fun on_resume do refresh
82
83 # Refresh content of this page
84 fun refresh
85 do
86 if not app.restored then return
87
88 layout_login.clear
89 if app.user != null then
90 # Logged in
91 var lbl_login_status = new Label(parent=layout_login, text="Welcome".t, size=1.5)
92 lbl_login_status.set_welcome
93 else
94 self.but_login = new Button(parent=layout_login, text="Login or signup".t)
95 end
96 self.but_preferences = new Button(parent=layout_login, text="Preferences".t)
97
98 # Fill beers
99 (new ListDiffAction(self, "rest/since?token={app.token}")).start
100
101 # Fill people
102 (new HomeListPeopleAction(self, "rest/friends?token={app.token}")).start
103
104 # Check if token is still valid
105 if app.token != "none" then (new CheckTokenAction(self, "rest/check_token?token={app.token}")).start
106 end
107
108 redef fun on_event(event)
109 do
110 if debug then print "BenitluxWindow::on_event {event}"
111
112 if event isa ButtonPressEvent then
113 var sender = event.sender
114 if sender == but_preferences then
115 app.push_window new UserWindow
116 return
117 else if sender == but_login then
118 app.push_window new SignupWindow
119 return
120 else if sender == beer_button then
121 app.push_window new BeersWindow
122 return
123 else if sender == social_button then
124 app.push_window new SocialWindow
125 return
126 #else if sender == news_button then
127 # TODO open browser?
128 end
129 end
130
131 super
132 end
133 end
134
135 # `Label` used in section headers
136 class SectionTitle super Label end
137
138 # Async request to update the beer list on the home screen
139 class ListDiffAction
140 super WindowHttpRequest
141
142 redef type W: HomeWindow
143
144 redef fun on_load(beers, status)
145 do
146 window.layout_beers.remove window.beer_list
147 window.beer_list = new VerticalLayout(parent=window.layout_beers)
148
149 if intercept_error(beers) then return
150
151 if not beers isa Array[BeerAndRatings] then
152 app.feedback "Communication Error".t
153 return
154 end
155
156 # Sort beers per preference
157 var comparator = new BeerComparator
158 comparator.sort beers
159
160 var max_beers = 6
161 while beers.length > max_beers do beers.pop
162
163 for bar in beers do
164 var view = new BeerView(bar, parent=window.beer_list)
165 end
166 end
167 end
168
169 # Async request to list users
170 class HomeListPeopleAction
171 super WindowHttpRequest
172
173 redef type W: HomeWindow
174
175 redef fun on_load(users, status)
176 do
177 window.layout_social.remove window.social_list
178 window.social_list = new VerticalLayout(parent=window.layout_social)
179
180 if intercept_error(users) then return
181
182 if users isa Array[UserAndFollowing] then for uaf in users do
183 var view = new PeopleView(uaf, true, parent=window.social_list)
184 end
185 end
186 end
187
188 # Async request to check if `app.token` is still valid
189 class CheckTokenAction
190 super WindowHttpRequest
191
192 redef type W: HomeWindow
193
194 redef fun on_load(res, status) do intercept_error(res)
195 end
196
197 redef class BenitluxHttpRequest
198 redef fun intercept_error(res)
199 do
200 var r = super
201 if res isa BenitluxTokenError then
202 var window = app.window
203 if window isa HomeWindow then window.refresh
204 end
205 return r
206 end
207 end
208
209 # Today's date as a `String`
210 fun today: String
211 do
212 var tm = new Tm.localtime
213 return "{tm.year+1900}-{tm.mon+1}-{tm.mday}"
214 end