402c1731a9072f9f6a3a10cef7cef44537d6574e
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 SectionHeader(parent
=layout_user
)
58 private var but_preferences
: nullable Button = null
59 private var but_login
: nullable Button = null
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
)
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
)
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!")
81 redef fun on_resume
do refresh
83 # Refresh content of this page
86 if not app
.restored
then return
89 if app
.user
!= null then
91 var lbl_login_status
= new SectionTitle(parent
=layout_login
, text
="Welcome".t
, size
=1.5)
92 lbl_login_status
.set_welcome
94 self.but_login
= new Button(parent
=layout_login
, text
="Login or signup".t
)
96 self.but_preferences
= new Button(parent
=layout_login
, text
="Preferences".t
)
99 (new ListDiffAction(self, "rest/since?token={app.token}")).start
102 (new HomeListPeopleAction(self, "rest/friends?token={app.token}")).start
104 # Check if token is still valid
105 if app
.token
!= "none" then (new CheckTokenAction(self, "rest/check_token?token={app.token}")).start
108 redef fun on_event
(event
)
110 if debug
then print
"BenitluxWindow::on_event {event}"
112 if event
isa ButtonPressEvent then
113 var sender
= event
.sender
114 if sender
== but_preferences
then
115 app
.push_window
new UserWindow
117 else if sender
== but_login
then
118 app
.push_window
new SignupWindow
120 else if sender
== beer_button
then
121 app
.push_window
new BeersWindow
123 else if sender
== social_button
then
124 app
.push_window
new SocialWindow
126 #else if sender == news_button then
135 private class EventView
141 var lbl
= new Label(parent
=self, text
=text
) is lazy
145 # Async request to update the beer list on the home screen
147 super WindowHttpRequest
149 redef type W
: HomeWindow
151 redef fun on_load
(beers
, status
)
153 window
.layout_beers
.remove window
.beer_list
154 window
.beer_list
= new VerticalLayout(parent
=window
.layout_beers
)
156 if intercept_error
(beers
) then return
158 if not beers
isa Array[BeerAndRatings] then
159 app
.feedback
"Communication Error".t
163 # Sort beers per preference
164 var comparator
= new BeerComparator
165 comparator
.sort beers
168 while beers
.length
> max_beers
do beers
.pop
171 var view
= new BeerView(bar
, parent
=window
.beer_list
)
176 # Async request to list users
177 class HomeListPeopleAction
178 super WindowHttpRequest
180 redef type W
: HomeWindow
182 redef fun on_load
(users
, status
)
184 window
.layout_social
.remove window
.social_list
185 window
.social_list
= new VerticalLayout(parent
=window
.layout_social
)
187 if intercept_error
(users
) then return
189 if users
isa Array[UserAndFollowing] then for uaf
in users
do
190 var view
= new PeopleView(uaf
, true, parent
=window
.social_list
)
195 # Async request to check if `app.token` is still valid
196 class CheckTokenAction
197 super WindowHttpRequest
199 redef type W
: HomeWindow
201 redef fun on_load
(res
, status
) do intercept_error
(res
)
204 redef class BenitluxHttpRequest
205 redef fun intercept_error
(res
)
208 if res
isa BenitluxTokenError then
209 var window
= app
.window
210 if window
isa HomeWindow then window
.refresh
216 # Today's date as a `String`
219 var tm
= new Tm.localtime
220 return "{tm.year+1900}-{tm.mon+1}-{tm.mday}"