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 # Tnitter minimal portable app listing the latest Tnits
17 # This app use push notification to be updated in the second a new Tnit is posted.
18 # So it begins by requesting a full list and the successive push request for updates.
19 # If an error occurs, the full list is requested again after a short delay.
21 # This approach may still miss a few updates it they happen too close to one another.
22 # To solve this, we could send an id for the latest known Tnit to the server.
23 # Which could recognize if a client is not up to date.
26 app_version
(0, 1, git_revision
)
27 app_namespace
"net.xymus.tnitter"
31 import app
::http_request
32 import app
::data_store
38 # Delay in seconds before the next request after an error
39 fun request_delay_on_error
: Float do return 60.0
43 private var layout
= new VerticalLayout(parent
=self)
44 private var list_posts
= new ListLayout(parent
=layout
)
45 private var lbl_init
= new Label(parent
=list_posts
, text
="Awaiting connection to server")
47 # Request an initial full update
48 init do (new ListPostRequest(self, "rest/list?count=16", false)).start
50 # Request a full update after a delay
51 fun request_full_list_on_error
53 (new ListPostRequest(self, "rest/list?count=16", true)).start
56 # Open a push notification connection and thread
57 fun request_push_notification
59 (new ListPostRequest(self, "push/", false)).start
62 # Update the screen to show the new `posts`
63 fun apply_update
(posts
: Array[Post])
67 var line
= new VerticalLayout(parent
=list_posts
)
68 var author
= new LabelAuthor(parent
=line
, text
="@"+post
.user
)
69 var text
= new Label(parent
=line
, text
=post
.text
)
74 # Label to display the author's name
76 # By default, this view is identical a `Label`,
77 # but if can be refined per platforms.
78 class LabelAuthor super Label end
81 # Async RESTful actions
83 # URI of the remote RESTful server
84 fun tnitter_server_uri
: String do return "http://localhost:8080"
86 # `AsyncHttpRequest` with services to act on the windows of the app
87 abstract class AsyncTnitterRequest
88 super AsyncHttpRequest
90 private var window
: Window
92 redef fun uri_root
do return tnitter_server_uri
96 # Should this request be delayed by `request_delay_on_error` seconds?
97 fun after_error
(value
: Bool) is autoinit
do if value
then delay
= request_delay_on_error
100 # Async request to list latest posts, either immediately or by push notification
102 # Implementation note:
103 # This class could as well be merged with `AsyncTnitterRequest` or have two versions,
104 # one for the immediate update and one for the push notification.
105 # We chose this structure for simplicity of the example,
106 # and as more services may be added in the future.
107 # If these future services expect data of a different format,
108 # they will need a different `on_load` but could still use `AsyncTnitterRequest`.
109 class ListPostRequest
110 super AsyncTnitterRequest
112 redef fun on_load
(posts
, status
)
114 # Deal with server-side errors
115 if posts
isa Error then
116 print_error
"Server Error: '{posts.message}' from '{uri}'"
121 if not posts
isa Array[Post] then
122 print_error
"Error: Got '{posts or else "null"}'"
126 # Update UI and prepare for the next update
127 window
.apply_update posts
128 window
.request_push_notification
131 redef fun on_fail
(error
)
133 print
"Warning: Request {uri} failed with {error}"
134 window
.request_full_list_on_error
141 redef class Deserializer
142 redef fun deserialize_class
(name
)
144 # This is usually generated using `nitserial`,
145 # but for a single generic class it is easier to implement manually
147 if name
== "Array[Post]" then return new Array[Post].from_deserializer
(self)