calculator & benitlux: remove obsolete explicit observer declaration
[nit.git] / contrib / benitlux / src / client / features / push.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 # Push notification support
16 module push
17
18 import app::http_request
19
20 import client
21
22 redef class App
23 redef fun on_log_in
24 do
25 super
26 #(new PushHttpRequest("push/check_token?token={app.token}")).start
27 end
28
29 # Names of the known users currently on location
30 var users_on_location = new Set[String]
31
32 # Should we show a daily notification when new beers are available?
33 fun notify_on_new_beers: Bool
34 do return app.data_store["notify_on_new_beers"].as(nullable Bool) or else true
35
36 # Should we show a daily notification when new beers are available?
37 fun notify_on_new_beers=(value: Bool) do app.data_store["notify_on_new_beers"] = value
38
39 # Should we show a daily notification of the menu?
40 fun notify_menu_daily: Bool
41 do return app.data_store["notify_menu_daily"].as(nullable Bool) or else false
42
43 # Should we show a daily notification of the menu?
44 fun notify_menu_daily=(value: Bool) do app.data_store["notify_menu_daily"] = value
45
46 # Should we show a notification when friends check in at the location?
47 fun notify_on_checkins: Bool
48 do return app.data_store["notify_on_checkins"].as(nullable Bool) or else true
49
50 # Should we show a notification when friends check in at the location?
51 fun notify_on_checkins=(value: Bool) do app.data_store["notify_on_checkins"] = value
52 end
53
54 # Open push notification request
55 class PushHttpRequest
56 super BenitluxHttpRequest
57
58 redef fun on_fail(error)
59 do
60 if app.user == null then return
61
62 super
63
64 print_error "{class_name}: on_failĀ {error}"
65
66 var t = new PushHttpRequest("push/?token={app.token}")
67 t.delay = 10.0
68 t.start
69 end
70
71 redef fun on_load(data, status)
72 do
73 if app.user == null then return
74
75 var delay = 0.0
76 if data isa Pushable then
77 data.apply_push_if_desired
78 else if data isa BenitluxError then
79 # TODO if forbidden ask for a new token
80 delay = 5.0*60.0
81 else
82 print_error "{class_name}: Received {data or else "null"}"
83 end
84
85 var t = new PushHttpRequest("push/?token={app.token}")
86 t.delay = delay
87 t.start
88 end
89 end
90
91 # ---
92 # Objects sent from the server to the client
93
94 # Objects sent as push notifications by the server
95 interface Pushable
96 # Act on this push notification
97 fun apply_push do print_error "Unimplemented `apply_push` on {class_name}"
98
99 # Consider to `apply_push` if the user preferences wants to
100 fun apply_push_if_desired do apply_push
101 end
102
103 redef class CheckinReport
104 super Pushable
105
106 # Flattened array of the name of users
107 var user_names: Array[String] = [for u in users do u.name] is lazy
108
109 redef fun apply_push_if_desired
110 do
111 if not app.notify_on_checkins then return
112
113 var there_is_a_new_user = false
114 for new_users in user_names do
115 if not app.users_on_location.has(new_users) then
116 there_is_a_new_user = true
117 break
118 end
119 end
120
121 app.users_on_location.clear
122 app.users_on_location.add_all user_names
123
124 # Apply only if there is someone new on location
125 if there_is_a_new_user then super
126 end
127
128 redef fun apply_push
129 do
130 if users.is_empty then
131 #app.notif_push.cancel
132 #self.cancel(tag, (int)id);
133 return
134 end
135
136 var title = "TTB!".t
137 var names = [for user in users do user.name]
138 var content = "From %0".t.format(names.join(", "))
139
140 notify(title, content, 1)
141 end
142 end
143
144 redef class DailyNotification
145 super Pushable
146
147 redef fun apply_push_if_desired
148 do
149 if app.notify_menu_daily then
150 super
151 return
152 end
153
154 if app.notify_on_new_beers then
155 for beer in beers do
156 if beer.is_new then
157 super
158 return
159 end
160 end
161 end
162 end
163
164 redef fun apply_push
165 do
166 var title = if beers.has_new_beers then
167 "New beers are on the menu".t
168 else "Beer Menu".t
169
170 var content = beers.beers_to_notification
171 notify(title, content, 3)
172 end
173 end
174
175 # ---
176 # UI
177
178 redef class UserWindow
179
180 private var layout_push_options = new VerticalLayout(parent=layout)
181
182 private var lbl_push_options_title = new Label(parent=layout_push_options,
183 text="Notifications options".t, size=1.5)
184
185 private var chk_notify_on_new_beers = new CheckBox(parent=layout_push_options,
186 text="Notify when there are new beers".t)
187
188 private var chk_notify_menu_daily = new CheckBox(parent=layout_push_options,
189 #text="Show the menu every work day?".t)
190 text="Show the menu every work day".t)
191
192 private var chk_notify_on_checkins = new CheckBox(parent=layout_push_options,
193 text="Notify when a friend checks in".t)
194
195 init
196 do
197 chk_notify_on_new_beers.is_checked = app.notify_on_new_beers
198 chk_notify_menu_daily.is_checked = app.notify_menu_daily
199 chk_notify_on_checkins.is_checked = app.notify_on_checkins
200 end
201
202 redef fun on_event(event)
203 do
204 super
205
206 if event isa ToggleEvent then
207 var sender = event.sender
208 if sender == chk_notify_on_new_beers then
209 app.notify_on_new_beers = sender.is_checked
210 else if sender == chk_notify_menu_daily then
211 app.notify_menu_daily = sender.is_checked
212 else if sender == chk_notify_on_checkins then
213 app.notify_on_checkins = sender.is_checked
214 end
215 end
216 end
217 end