1 # This file is part of NIT ( http://www.nitlanguage.org ).
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
22 redef fun root_window
do return new HomeWindow
29 # Send back to the home window when logging in
30 if not window
isa HomeWindow then pop_window
34 # Social pane with networking features
38 private var layout
= new ListLayout(parent
=self)
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
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
)
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
)
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!")
66 redef fun on_resume
do refresh
68 # Refresh content of this page
71 if not app
.restored
then return
74 if app
.user
!= null then
76 var lbl_login_status
= new SectionTitle(parent
=layout_login
, text
="Welcome".t
, size
=1.5)
77 lbl_login_status
.set_welcome
79 self.but_login
= new Button(parent
=layout_login
, text
="Login or signup".t
)
81 self.but_preferences
= new Button(parent
=layout_login
, text
="Preferences".t
)
84 (new ListDiffAction(self, "rest/since?token={app.token}")).start
87 (new HomeListPeopleAction(self, "rest/friends?token={app.token}")).start
89 # Check if token is still valid
90 if app
.token
!= "none" then (new CheckTokenAction(self, "rest/check_token?token={app.token}")).start
93 redef fun on_event
(event
)
95 if debug
then print
"BenitluxWindow::on_event {event}"
97 if event
isa ButtonPressEvent then
98 var sender
= event
.sender
99 if sender
== but_preferences
then
100 app
.push_window
new UserWindow
102 else if sender
== but_login
then
103 app
.push_window
new SignupWindow
105 else if sender
== beer_button
then
106 app
.push_window
new BeersWindow
108 else if sender
== social_button
then
109 app
.push_window
new SocialWindow
111 #else if sender == news_button then
120 private class EventView
126 var lbl
= new Label(parent
=self, text
=text
) is lazy
130 # Async request to update the beer list on the home screen
132 super WindowHttpRequest
134 redef type W
: HomeWindow
136 redef fun on_load
(beers
, status
)
138 window
.layout_beers
.remove window
.beer_list
139 window
.beer_list
= new VerticalLayout(parent
=window
.layout_beers
)
141 if intercept_error
(beers
) then return
143 if not beers
isa Array[BeerAndRatings] then
144 app
.feedback
"Communication Error".t
148 # Sort beers per preference
149 var comparator
= new BeerComparator
150 comparator
.sort beers
153 while beers
.length
> max_beers
do beers
.pop
156 var view
= new BeerView(bar
, parent
=window
.beer_list
)
161 # Async request to list users
162 class HomeListPeopleAction
163 super WindowHttpRequest
165 redef type W
: HomeWindow
167 redef fun on_load
(users
, status
)
169 window
.layout_social
.remove window
.social_list
170 window
.social_list
= new VerticalLayout(parent
=window
.layout_social
)
172 if intercept_error
(users
) then return
174 if users
isa Array[UserAndFollowing] then for uaf
in users
do
175 var view
= new PeopleView(uaf
, true, parent
=window
.social_list
)
180 # Async request to check if `app.token` is still valid
181 class CheckTokenAction
182 super WindowHttpRequest
184 redef type W
: HomeWindow
186 redef fun on_load
(res
, status
) do intercept_error
(res
)
189 redef class BenitluxHttpRequest
190 redef fun intercept_error
(res
)
193 if res
isa BenitluxTokenError then
194 var window
= app
.window
195 if window
isa HomeWindow then window
.refresh
201 # Today's date as a `String`
204 var tm
= new Tm.localtime
205 return "{tm.year+1900}-{tm.mon+1}-{tm.mday}"