Merge: doc: fixed some typos and other misc. corrections
[nit.git] / contrib / benitlux / src / client / base.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 # Common services for the Benitlux app
16 module base
17
18 import app::ui
19 import app::data_store
20 import app::http_request
21 import android::aware
22 import json
23
24 import benitlux_model
25 import translations
26
27 # Show debug output?
28 fun debug: Bool do return true
29
30 # Root URI of the remote RESTfule server
31 fun benitlux_rest_server_uri: String do return "http://localhost:8080/"
32
33 redef class App
34
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
39 return "none"
40 end
41
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
46 return null
47 end
48
49 # Event when user logs in or out
50 fun on_log_in do on_save_state
51
52 redef fun on_save_state
53 do
54 app.data_store["user"] = user
55 app.data_store["token"] = token
56 super
57 end
58
59 # Has this app state been restored yet?
60 var restored = false
61
62 redef fun on_restore_state
63 do
64 super
65
66 # TODO this may happen before the lazy loading above
67 restored = true
68
69 if token != "none" then on_log_in
70 end
71
72 # Show simple feedback to the user on important errors
73 fun feedback(text: Text) do print_error text
74 end
75
76 # Show a notification to the user
77 fun notify(title, content: Text, uniqueness_id: Int)
78 do print "Notification {uniqueness_id}: {title}; {content}"
79
80 # View for an item in a list, like a beer or a person
81 abstract class ItemView super View end
82
83 # Descriptive label providing extra information
84 class DescLabel super Label end
85
86 # `HorizontalLayout` header to a section
87 class SectionHeader super HorizontalLayout end
88
89 # `Label` used in section headers
90 class SectionTitle super Label end
91
92 # Basic async HTTP request for this app
93 #
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
99
100 redef fun uri_root do return benitlux_rest_server_uri
101
102 redef var uri_tail
103
104 redef fun on_fail(error)
105 do
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
110 else
111 # This could be a deserialization error,
112 # it may be related to an outdated client.
113 # Report to user.
114 print_error "Request Error: {uri} with {error}"
115 app.feedback "Request Error: {error}"
116 end
117 end
118
119 # Intercept known server side errors
120 fun intercept_error(res: nullable Object): Bool
121 do
122 if res isa BenitluxTokenError then
123 app.token = "none"
124 app.user = null
125 return true
126 else if res isa BenitluxError then
127 feedback((res.user_message or else res.message).t)
128 return true
129 else if res isa Error then
130 feedback res.message.t
131 return true
132 end
133 return false
134 end
135
136 # Show feedback pertinent to the user, defaults to a platform specific popup
137 fun feedback(text: String) do app.feedback text
138 end
139
140 # Async request with services to act on the windows of the app
141 class WindowHttpRequest
142 super BenitluxHttpRequest
143
144 autoinit window, uri_tail
145
146 # Type of the related `window`
147 type W: Window
148
149 # `Window` on which to apply the results of this request
150 var window: W
151
152 # `Views` to disable while this request is in progress
153 var affected_views = new Array[View]
154
155 redef fun before do for view in affected_views do view.enabled = false
156
157 redef fun after do for view in affected_views do view.enabled = true
158 end
159
160 redef class Text
161 # Ellipsize `self` so it fits within `max_length` characters
162 #
163 # FIXME Remove this when labels are correctly ellipsized on iOS.
164 fun ellipsize: Text do return self
165 end