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.
15 # Window to list beers and other beer-related views
20 # Social pane with networking features
24 private var layout
= new VerticalLayout(parent
=self)
26 private var list_search
= new ListLayout(parent
=layout
)
28 private var layout_header
= new VerticalLayout(parent
=list_search
)
29 private var layout_search
= new HorizontalLayout(parent
=layout_header
)
30 private var txt_query
= new TextInput(parent
=layout_search
)
31 private var but_search
= new Button(parent
=layout_search
, text
="Search".t
)
33 private var layout_list
= new HorizontalLayout(parent
=layout_header
)
34 private var but_followed
= new Button(parent
=layout_list
, text
="List followed".t
)
35 private var but_followers
= new Button(parent
=layout_list
, text
="List followers".t
)
39 # Load friends and suggestions
40 (new ListUsersAction(self, "rest/friends?token={app.token}&n=16")).start
43 redef fun on_event
(event
)
45 if debug
then print
"BenitluxWindow::on_event {event}"
47 if event
isa ButtonPressEvent then
48 var sender
= event
.sender
49 if sender
== but_search
then
51 else if sender
== but_followed
then
52 var cmd
= "rest/followed?token={app.token}"
53 (new ListUsersAction(self, cmd
)).start
54 else if sender
== but_followers
then
55 var cmd
= "rest/followers?token={app.token}"
56 (new ListUsersAction(self, cmd
)).start
63 # Execute search with `txt_query.text`
66 var query
= txt_query
.text
67 if query
== null or query
.is_empty
then return
69 var res
= "rest/search?token={app.token}&query={query}&offset=0"
70 (new ListUsersAction(self, res
)).start
73 # Fill `list_search` with views for each of `users`
74 fun list_users
(users
: Array[UserAndFollowing])
77 var view
= new PeopleView(uaf
, false, parent
=list_search
)
82 # View to describe, and follow a person
87 autoinit user_and_following
, home_window_mode
, parent
89 # Description of the user
90 var user_and_following
: UserAndFollowing
92 # Toggle tweaks for the home window where the is no "unfollow" buttons
93 var home_window_mode
: Bool
97 var user
= user_and_following
.user
99 var layout_top_line
= new HorizontalLayout(parent
=self)
100 var lbl_name
= new Label(parent
=layout_top_line
, text
=user
.name
)
102 if app
.user
!= null then
104 # Show unfollow button if not on the home screen
105 if not home_window_mode
or not user_and_following
.following
then
106 var but
= new FollowButton(user
.id
, user_and_following
.following
, user_and_following
.followed
, parent
=layout_top_line
)
110 var favs
= if not user_and_following
.favs
.is_empty
then
111 "Favorites: %0".t
.format
(user_and_following
.favs
)
112 else "No favorites yet".t
113 var lbl_desc
= new Label(parent
=self, text
=favs
, size
=0.5)
117 # Button to follow or unfollow a user
121 autoinit followed_id
, following
, followed_by
, parent
, enabled
, text
123 # Id of the user to be followd/unfollow
126 # Does the local user already follows `followed_id`
129 # Does `followed_id` already follows the local user
130 var followed_by
: Bool
132 # Update the visible text according to `following`
133 fun update_text
do text
= if following
then "Unfollow".t
else "Follow".t
137 redef fun on_event
(event
)
139 assert event
isa ButtonPressEvent
140 var cmd
= "rest/follow?token={app.token}&user_to={followed_id}&follow={not following}"
142 text
= "Updating...".t
143 (new FollowAction(app
.window
, cmd
, self)).start
147 # Async request to receive and display a list of users
149 # This is used by many features of the social window:
150 # search, list followed and list followers.
151 class ListUsersAction
152 super WindowHttpRequest
154 redef type W
: SocialWindow
156 init do affected_views
.add_all
([window
.but_search
, window
.but_followed
, window
.but_followers
])
158 redef fun on_load
(users
, status
)
160 window
.layout
.remove window
.list_search
161 window
.list_search
= new ListLayout(parent
=window
.layout
)
162 window
.layout_header
.parent
= window
.list_search
164 if intercept_error
(users
) then return
166 if users
isa Array[UserAndFollowing] then window
.list_users users
170 # Async request to follow or unfollow a user
172 super WindowHttpRequest
174 private var button
: FollowButton
175 init do affected_views
.add
(button
)
177 redef fun on_load
(res
, status
)
179 if intercept_error
(res
) then return
184 button
.following
= not button
.following
186 button
.enabled
= true
193 button
.enabled
= false