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 # Common services for the Benitlux app
19 import app
::data_store
20 import app
::http_request
28 fun debug
: Bool do return true
30 # Root URI of the remote RESTfule server
31 fun benitlux_rest_server_uri
: String do return "http://localhost:8080/"
35 # Current connection token, or "none"
36 var token
: String is lazy
, writable do
37 var token
= app
.data_store
["token"]
38 if token
isa String then return token
42 # Name of the currently logged in user
43 var user
: nullable String is lazy
, writable do
44 var user
= app
.data_store
["user"]
45 if user
isa nullable String then return user
49 # Event when user logs in or out
50 fun on_log_in
do on_save_state
52 redef fun on_save_state
54 app
.data_store
["user"] = user
55 app
.data_store
["token"] = token
59 # Has this app state been restored yet?
62 redef fun on_restore_state
66 # TODO this may happen before the lazy loading above
69 if token
!= "none" then on_log_in
72 # Show simple feedback to the user on important errors
73 fun feedback
(text
: Text) do print_error text
76 # Show a notification to the user
77 fun notify
(title
, content
: Text, uniqueness_id
: Int)
78 do print
"Notification {uniqueness_id}: {title}; {content}"
80 # View for an item in a list, like a beer or a person
81 abstract class ItemView super View end
83 # Descriptive label providing extra information
84 class DescLabel super Label end
86 # `HorizontalLayout` header to a section
87 class SectionHeader super HorizontalLayout end
89 # `Label` used in section headers
90 class SectionTitle super Label end
92 # Basic async HTTP request for this app
94 # Note that connection errors are passed to `on_fail`, and
95 # server errors or authentification errors are received by `on_load`
96 # and should be passed to `intercept_error`.
97 class BenitluxHttpRequest
98 super AsyncHttpRequest
100 redef fun uri_root
do return benitlux_rest_server_uri
104 redef fun on_fail
(error
)
106 if error
isa IOError then
107 # This should be a normal network error like being offline.
108 # Print to log, but don't show to the user.
109 print_error error
.class_name
111 # This could be a deserialization error,
112 # it may be related to an outdated client.
114 print_error
"Request Error: {uri} with {error}"
115 app
.feedback
"Request Error: {error}"
119 # Intercept known server side errors
120 fun intercept_error
(res
: nullable Object): Bool
122 if res
isa BenitluxTokenError then
126 else if res
isa BenitluxError then
127 feedback
((res
.user_message
or else res
.message
).t
)
129 else if res
isa Error then
130 feedback res
.message
.t
136 # Show feedback pertinent to the user, defaults to a platform specific popup
137 fun feedback
(text
: String) do app
.feedback text
140 # Async request with services to act on the windows of the app
141 class WindowHttpRequest
142 super BenitluxHttpRequest
144 autoinit window
, uri_tail
146 # Type of the related `window`
149 # `Window` on which to apply the results of this request
152 # `Views` to disable while this request is in progress
153 var affected_views
= new Array[View]
155 redef fun before
do for view
in affected_views
do view
.enabled
= false
157 redef fun after
do for view
in affected_views
do view
.enabled
= true
161 # Ellipsize `self` so it fits within `max_length` characters
163 # FIXME Remove this when labels are correctly ellipsized on iOS.
164 fun ellipsize
: Text do return self