402c1731a9072f9f6a3a10cef7cef44537d6574e
[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 SectionHeader(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 SectionHeader(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 SectionHeader(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 var news_header = new SectionHeader(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_cask = new EventView(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 SectionTitle(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 private class EventView
136 super VerticalLayout
137 super ItemView
138
139 var text: Text
140
141 var lbl = new Label(parent=self, text=text) is lazy
142 init do lbl
143 end
144
145 # Async request to update the beer list on the home screen
146 class ListDiffAction
147 super WindowHttpRequest
148
149 redef type W: HomeWindow
150
151 redef fun on_load(beers, status)
152 do
153 window.layout_beers.remove window.beer_list
154 window.beer_list = new VerticalLayout(parent=window.layout_beers)
155
156 if intercept_error(beers) then return
157
158 if not beers isa Array[BeerAndRatings] then
159 app.feedback "Communication Error".t
160 return
161 end
162
163 # Sort beers per preference
164 var comparator = new BeerComparator
165 comparator.sort beers
166
167 var max_beers = 6
168 while beers.length > max_beers do beers.pop
169
170 for bar in beers do
171 var view = new BeerView(bar, parent=window.beer_list)
172 end
173 end
174 end
175
176 # Async request to list users
177 class HomeListPeopleAction
178 super WindowHttpRequest
179
180 redef type W: HomeWindow
181
182 redef fun on_load(users, status)
183 do
184 window.layout_social.remove window.social_list
185 window.social_list = new VerticalLayout(parent=window.layout_social)
186
187 if intercept_error(users) then return
188
189 if users isa Array[UserAndFollowing] then for uaf in users do
190 var view = new PeopleView(uaf, true, parent=window.social_list)
191 end
192 end
193 end
194
195 # Async request to check if `app.token` is still valid
196 class CheckTokenAction
197 super WindowHttpRequest
198
199 redef type W: HomeWindow
200
201 redef fun on_load(res, status) do intercept_error(res)
202 end
203
204 redef class BenitluxHttpRequest
205 redef fun intercept_error(res)
206 do
207 var r = super
208 if res isa BenitluxTokenError then
209 var window = app.window
210 if window isa HomeWindow then window.refresh
211 end
212 return r
213 end
214 end
215
216 # Today's date as a `String`
217 fun today: String
218 do
219 var tm = new Tm.localtime
220 return "{tm.year+1900}-{tm.mon+1}-{tm.mday}"
221 end