Merge: doc: fixed some typos and other misc. corrections
[nit.git] / contrib / tnitter / src / tnitter_app.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 # Tnitter minimal portable app listing the latest Tnits
16 #
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.
20 #
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.
24 module tnitter_app is
25 app_name "Tnitter"
26 app_version(0, 1, git_revision)
27 app_namespace "net.xymus.tnitter"
28 end
29
30 import app::ui
31 import app::http_request
32 import app::data_store
33 import android::aware
34 import json
35
36 import model
37
38 # Delay in seconds before the next request after an error
39 fun request_delay_on_error: Float do return 60.0
40
41 redef class Window
42
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")
46
47 # Request an initial full update
48 init do (new ListPostRequest(self, "rest/list?count=16", false)).start
49
50 # Request a full update after a delay
51 fun request_full_list_on_error
52 do
53 (new ListPostRequest(self, "rest/list?count=16", true)).start
54 end
55
56 # Open a push notification connection and thread
57 fun request_push_notification
58 do
59 (new ListPostRequest(self, "push/", false)).start
60 end
61
62 # Update the screen to show the new `posts`
63 fun apply_update(posts: Array[Post])
64 do
65 list_posts.clear
66 for post in posts do
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)
70 end
71 end
72 end
73
74 # Label to display the author's name
75 #
76 # By default, this view is identical a `Label`,
77 # but if can be refined per platforms.
78 class LabelAuthor super Label end
79
80 # ---
81 # Async RESTful actions
82
83 # URI of the remote RESTful server
84 fun tnitter_server_uri: String do return "http://localhost:8080"
85
86 # `AsyncHttpRequest` with services to act on the windows of the app
87 abstract class AsyncTnitterRequest
88 super AsyncHttpRequest
89
90 private var window: Window
91
92 redef fun uri_root do return tnitter_server_uri
93
94 redef var uri_tail
95
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
98 end
99
100 # Async request to list latest posts, either immediately or by push notification
101 #
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
111
112 redef fun on_load(posts, status)
113 do
114 # Deal with server-side errors
115 if posts isa Error then
116 print_error "Server Error: '{posts.message}' from '{uri}'"
117 return
118 end
119
120 # Type check
121 if not posts isa Array[Post] then
122 print_error "Error: Got '{posts or else "null"}'"
123 return
124 end
125
126 # Update UI and prepare for the next update
127 window.apply_update posts
128 window.request_push_notification
129 end
130
131 redef fun on_fail(error)
132 do
133 print "Warning: Request {uri} failed with {error}"
134 window.request_full_list_on_error
135 end
136 end
137
138 # ---
139 # Services
140
141 redef class Deserializer
142 redef fun deserialize_class(name)
143 do
144 # This is usually generated using `nitserial`,
145 # but for a single generic class it is easier to implement manually
146
147 if name == "Array[Post]" then return new Array[Post].from_deserializer(self)
148 return super
149 end
150 end