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.
25 if debug
then print
"App::on_create"
27 # Create the main window
32 # Show the home/main windows
35 var window
= new HomeWindow
44 # Send back to the home window when logging in
45 if not window
isa HomeWindow then pop_window
49 # Social pane with networking features
53 private var layout
= new ListLayout(parent
=self)
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
)
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
)
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
)
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!")
84 for c
in [but_login
, but_preferences
, beer_button
, social_button
] do
89 redef fun on_resume
do refresh
91 # Refresh content of this page
94 if not app
.restored
then return
97 if app
.user
!= null then
99 lbl_login_status
.parent
= layout_login
100 but_preferences
.parent
= layout_login
101 lbl_login_status
.set_welcome
103 but_login
.parent
= layout_login
104 but_preferences
.parent
= layout_login
108 (new ListDiffAction(self, "rest/since?token={app.token}")).start
111 (new HomeListPeopleAction(self, "rest/friends?token={app.token}")).start
113 # Check if token is still valid
114 (new CheckTokenAction(self, "rest/check_token?token={app.token}")).start
117 redef fun on_event
(event
)
119 if debug
then print
"BenitluxWindow::on_event {event}"
121 if event
isa ButtonPressEvent then
122 var sender
= event
.sender
123 if sender
== but_preferences
then
124 app
.push_window
new UserWindow
126 else if sender
== but_login
then
127 app
.push_window
new SignupWindow
129 else if sender
== beer_button
then
130 app
.push_window
new BeersWindow
132 else if sender
== social_button
then
133 app
.push_window
new SocialWindow
135 #else if sender == news_button then
144 # `Label` used in section headers
145 class SectionTitle super Label end
147 # Async request to update the beer list on the home screen
149 super WindowHttpRequest
151 redef type W
: HomeWindow
153 redef fun on_load
(beers
)
155 window
.layout_beers
.remove window
.beer_list
156 window
.beer_list
= new VerticalLayout(parent
=window
.layout_beers
)
158 if intercept_error
(beers
) then return
160 if not beers
isa Array[BeerAndRatings] then
161 app
.feedback
"Communication Error".t
165 # Sort beers per preference
166 var comparator
= new BeerComparator
167 comparator
.sort beers
170 while beers
.length
> max_beers
do beers
.pop
173 var view
= new BeerView(bar
, parent
=window
.beer_list
)
178 # Async request to list users
179 class HomeListPeopleAction
180 super WindowHttpRequest
182 redef type W
: HomeWindow
184 redef fun on_load
(users
)
186 window
.layout_social
.remove window
.social_list
187 window
.social_list
= new VerticalLayout(parent
=window
.layout_social
)
189 if intercept_error
(users
) then return
191 if users
isa Array[UserAndFollowing] then for uaf
in users
do
192 var view
= new PeopleView(uaf
, true, parent
=window
.social_list
)
197 # Async request to check if `app.token` is still valid
198 class CheckTokenAction
199 super WindowHttpRequest
201 redef type W
: HomeWindow
203 redef fun on_load
(res
) do intercept_error
(res
)
206 # Today's date as a `String`
209 var tm
= new Tm.localtime
210 return "{tm.year+1900}-{tm.mon+1}-{tm.mday}"