6131d983b59bd9d2b14797916904a92f0ebb9f62
[nit.git] / contrib / benitlux / src / client / features / checkins.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 # On location checkin services
16 module checkins
17
18 import client
19
20 redef class App
21
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
25
26 # Should we share our checkins with the server and friends?
27 fun share_checkins=(value: Bool)
28 do
29 # Notify server
30 if currently_on_location then
31 if value then
32 server_check_in
33 else server_check_out
34 end
35
36 app.data_store["share_checkins"] = value
37 end
38
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
42
43 # Are we currently at the location?
44 fun currently_on_location=(value: Bool) do app.data_store["currently_on_location"] = value
45
46 # Request beer menu from the server
47 #
48 # It includes a diff if `checkins` remembers a previous visit.
49 fun request_menu
50 do
51 var checkins = checkins
52 var since = checkins.latest
53 if since != null then
54 var today = today
55 if since == today then
56 since = checkins.previous
57 end
58 end
59
60 (new MenuHttpRequest("rest/since?token={token}&date={since or else ""}")).start
61 end
62
63 # User checks in
64 fun on_check_in
65 do
66 if currently_on_location then return
67
68 if share_checkins then server_check_in
69
70 currently_on_location = true
71 request_menu
72 checkins.update today
73 end
74
75 # User checks out
76 fun on_check_out
77 do
78 if not currently_on_location then return
79
80 if share_checkins then server_check_out
81 currently_on_location = false
82 end
83
84 # Notify server of checkin
85 private fun server_check_in do (new BenitluxHttpRequest("rest/checkin?token={app.token}&is_in=true")).start
86
87 # Notify server of checkout
88 private fun server_check_out do (new BenitluxHttpRequest("rest/checkin?token={app.token}&is_in=false")).start
89
90 # History of the last 1 or 2 checkins
91 var checkins = new SimpleMemory
92
93 redef fun on_save_state
94 do
95 super
96 app.data_store["checkins"] = checkins
97 end
98
99 redef fun on_restore_state
100 do
101 var checkins = app.data_store["checkins"]
102 if checkins isa SimpleMemory then self.checkins = checkins
103
104 super
105 end
106 end
107
108 # Request the menu from the server for a notification
109 class MenuHttpRequest
110 super BenitluxHttpRequest
111
112 redef fun on_load(data, status)
113 do
114 if not data isa Array[BeerAndRatings] then
115 on_fail new Error("Server sent unexpected data {data or else "null"}")
116 return
117 end
118
119 var content = data.beers_to_notification
120
121 notify("Passing by the Benelux?".t, content, 2)
122 end
123 end
124
125 # ---
126 # Support services
127
128 # Memory of an element and the previous one, avoiding duplication
129 #
130 # Used to remember the last day at the location,
131 # ignoring multiple reports on the same day.
132 class SimpleMemory
133 serialize
134
135 # Before latest remembered entry
136 var previous: nullable String = null
137
138 # Last remembered entry
139 var latest: nullable String = null
140
141 # Update `latest` if `value` is different
142 fun update(value: String)
143 do
144 if value == latest then return
145
146 previous = latest
147 latest = value
148 end
149 end
150
151 # ---
152 # UI
153
154 redef class UserWindow
155
156 private var lbl_checkins_options_title = new Label(parent=layout,
157 text="Share options".t)
158
159 private var chk_share_checkins = new CheckBox(parent=layout,
160 text="Share checkins with your friends".t)
161
162 init
163 do
164 chk_share_checkins.is_checked = app.share_checkins
165 lbl_checkins_options_title.size = 1.5
166 end
167
168 redef fun on_event(event)
169 do
170 super
171
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
176 end
177 end
178 end
179 end