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 # On location checkin services
22 # Should we share our checkins with the server and friends?
23 fun share_checkins
: Bool
24 do return app
.data_store
["share_checkins"].as(nullable Bool) or else true
26 # Should we share our checkins with the server and friends?
27 fun share_checkins
=(value
: Bool)
30 if currently_on_location
then
36 app
.data_store
["share_checkins"] = value
39 # Are we currently at the location?
40 fun currently_on_location
: Bool
41 do return app
.data_store
["currently_on_location"].as(nullable Bool) or else false
43 # Are we currently at the location?
44 fun currently_on_location
=(value
: Bool) do app
.data_store
["currently_on_location"] = value
46 # Request beer menu from the server
48 # It includes a diff if `checkins` remembers a previous visit.
51 var checkins
= checkins
52 var since
= checkins
.latest
55 if since
== today
then
56 since
= checkins
.previous
60 (new MenuHttpRequest("rest/since?token={token}&date={since or else ""}")).start
66 if currently_on_location
then return
68 if share_checkins
then server_check_in
70 currently_on_location
= true
78 if not currently_on_location
then return
80 if share_checkins
then server_check_out
81 currently_on_location
= false
84 # Notify server of checkin
85 private fun server_check_in
do (new BenitluxHttpRequest("rest/checkin?token={app.token}&is_in=true")).start
87 # Notify server of checkout
88 private fun server_check_out
do (new BenitluxHttpRequest("rest/checkin?token={app.token}&is_in=false")).start
90 # History of the last 1 or 2 checkins
91 var checkins
= new SimpleMemory
93 redef fun on_save_state
96 app
.data_store
["checkins"] = checkins
99 redef fun on_restore_state
101 var checkins
= app
.data_store
["checkins"]
102 if checkins
isa SimpleMemory then self.checkins
= checkins
108 # Request the menu from the server for a notification
109 class MenuHttpRequest
110 super BenitluxHttpRequest
112 redef fun on_load
(data
, status
)
114 if not data
isa Array[BeerAndRatings] then
115 on_fail
new Error("Server sent unexpected data {data or else "null"}")
119 var content
= data
.beers_to_notification
121 notify
("Passing by the Benelux?".t
, content
, 2)
128 # Memory of an element and the previous one, avoiding duplication
130 # Used to remember the last day at the location,
131 # ignoring multiple reports on the same day.
135 # Before latest remembered entry
136 var previous
: nullable String = null
138 # Last remembered entry
139 var latest
: nullable String = null
141 # Update `latest` if `value` is different
142 fun update
(value
: String)
144 if value
== latest
then return
154 redef class UserWindow
156 private var lbl_checkins_options_title
= new Label(parent
=layout
,
157 text
="Share options".t
)
159 private var chk_share_checkins
= new CheckBox(parent
=layout
,
160 text
="Share checkins with your friends".t
)
164 chk_share_checkins
.is_checked
= app
.share_checkins
165 lbl_checkins_options_title
.size
= 1.5
168 redef fun on_event
(event
)
172 if event
isa ToggleEvent then
173 var sender
= event
.sender
174 if sender
== chk_share_checkins
then
175 app
.share_checkins
= sender
.is_checked