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