calculator & benitlux: remove obsolete explicit observer declaration
[nit.git] / contrib / benitlux / src / client / views / social_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 # Window to list beers and other beer-related views
16 module social_views
17
18 import base
19
20 # Social pane with networking features
21 class SocialWindow
22 super Window
23
24 private var layout = new VerticalLayout(parent=self)
25
26 private var list_search = new ListLayout(parent=layout)
27
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)
32
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)
36
37 init
38 do
39 # Load friends and suggestions
40 (new ListUsersAction(self, "rest/friends?token={app.token}&n=16")).start
41 end
42
43 redef fun on_event(event)
44 do
45 if debug then print "BenitluxWindow::on_event {event}"
46
47 if event isa ButtonPressEvent then
48 var sender = event.sender
49 if sender == but_search then
50 search
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
57 end
58 end
59
60 super
61 end
62
63 # Execute search with `txt_query.text`
64 fun search
65 do
66 var query = txt_query.text
67 if query == null or query.is_empty then return
68
69 var res = "rest/search?token={app.token}&query={query}&offset=0"
70 (new ListUsersAction(self, res)).start
71 end
72
73 # Fill `list_search` with views for each of `users`
74 fun list_users(users: Array[UserAndFollowing])
75 do
76 for uaf in users do
77 var view = new PeopleView(uaf, false, parent=list_search)
78 end
79 end
80 end
81
82 # View to describe, and follow a person
83 class PeopleView
84 super VerticalLayout
85 super ItemView
86
87 autoinit user_and_following, home_window_mode, parent
88
89 # Description of the user
90 var user_and_following: UserAndFollowing
91
92 # Toggle tweaks for the home window where the is no "unfollow" buttons
93 var home_window_mode: Bool
94
95 init
96 do
97 var user = user_and_following.user
98
99 var layout_top_line = new HorizontalLayout(parent=self)
100 var lbl_name = new Label(parent=layout_top_line, text=user.name)
101
102 if app.user != null then
103
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)
107 end
108 end
109
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)
114 end
115 end
116
117 # Button to follow or unfollow a user
118 class FollowButton
119 super Button
120
121 autoinit followed_id, following, followed_by, parent, enabled, text
122
123 # Id of the user to be followd/unfollow
124 var followed_id: Int
125
126 # Does the local user already follows `followed_id`
127 var following: Bool
128
129 # Does `followed_id` already follows the local user
130 var followed_by: Bool
131
132 # Update the visible text according to `following`
133 fun update_text do text = if following then "Unfollow".t else "Follow".t
134
135 init do update_text
136
137 redef fun on_event(event)
138 do
139 assert event isa ButtonPressEvent
140 var cmd = "rest/follow?token={app.token}&user_to={followed_id}&follow={not following}"
141 enabled = false
142 text = "Updating...".t
143 (new FollowAction(app.window, cmd, self)).start
144 end
145 end
146
147 # Async request to receive and display a list of users
148 #
149 # This is used by many features of the social window:
150 # search, list followed and list followers.
151 class ListUsersAction
152 super WindowHttpRequest
153
154 redef type W: SocialWindow
155
156 init do affected_views.add_all([window.but_search, window.but_followed, window.but_followers])
157
158 redef fun on_load(users, status)
159 do
160 window.layout.remove window.list_search
161 window.list_search = new ListLayout(parent=window.layout)
162 window.layout_header.parent = window.list_search
163
164 if intercept_error(users) then return
165
166 if users isa Array[UserAndFollowing] then window.list_users users
167 end
168 end
169
170 # Async request to follow or unfollow a user
171 class FollowAction
172 super WindowHttpRequest
173
174 private var button: FollowButton
175 init do affected_views.add(button)
176
177 redef fun on_load(res, status)
178 do
179 if intercept_error(res) then return
180 end
181
182 redef fun after
183 do
184 button.following = not button.following
185 button.update_text
186 button.enabled = true
187
188 super
189 end
190
191 redef fun before
192 do
193 button.enabled = false
194 super
195 end
196 end