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 # Push notification support
18 import app
::http_request
26 #(new PushHttpRequest("push/check_token?token={app.token}")).start
29 # Names of the known users currently on location
30 var users_on_location
= new Set[String]
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
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
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
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
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
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
54 # Open push notification request
56 super BenitluxHttpRequest
58 redef fun on_fail
(error
)
60 if app
.user
== null then return
64 print_error
"{class_name}: on_failĀ {error}"
66 var t
= new PushHttpRequest("push/?token={app.token}")
71 redef fun on_load
(data
, status
)
73 if app
.user
== null then return
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
82 print_error
"{class_name}: Received {data or else "null"}"
85 var t
= new PushHttpRequest("push/?token={app.token}")
92 # Objects sent from the server to the client
94 # Objects sent as push notifications by the server
96 # Act on this push notification
97 fun apply_push
do print_error
"Unimplemented `apply_push` on {class_name}"
99 # Consider to `apply_push` if the user preferences wants to
100 fun apply_push_if_desired
do apply_push
103 redef class CheckinReport
106 # Flattened array of the name of users
107 var user_names
: Array[String] = [for u
in users
do u
.name
] is lazy
109 redef fun apply_push_if_desired
111 if not app
.notify_on_checkins
then return
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
121 app
.users_on_location
.clear
122 app
.users_on_location
.add_all user_names
124 # Apply only if there is someone new on location
125 if there_is_a_new_user
then super
130 if users
.is_empty
then
131 #app.notif_push.cancel
132 #self.cancel(tag, (int)id);
137 var names
= [for user
in users
do user
.name
]
138 var content
= "From %0".t
.format
(names
.join
(", "))
140 notify
(title
, content
, 1)
144 redef class DailyNotification
147 redef fun apply_push_if_desired
149 if app
.notify_menu_daily
then
154 if app
.notify_on_new_beers
then
166 var title
= if beers
.has_new_beers
then
167 "New beers are on the menu".t
170 var content
= beers
.beers_to_notification
171 notify
(title
, content
, 3)
178 redef class UserWindow
180 private var layout_push_options
= new VerticalLayout(parent
=layout
)
182 private var lbl_push_options_title
= new Label(parent
=layout_push_options
,
183 text
="Notifications options".t
, size
=1.5)
185 private var chk_notify_on_new_beers
= new CheckBox(parent
=layout_push_options
,
186 text
="Notify when there are new beers".t
)
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
)
192 private var chk_notify_on_checkins
= new CheckBox(parent
=layout_push_options
,
193 text
="Notify when a friend checks in".t
)
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
202 redef fun on_event
(event
)
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