contrib/tnitter: intro simple app
[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::serialization
35
36 import model
37
38 # Delay in seconds before the next request after an error
39 fun request_delay_on_error: Int do return 60
40
41 redef class App
42 redef fun on_create
43 do
44 # Create the main window
45 window = new TnitterWindow
46 super
47 end
48 end
49
50 # Main window
51 class TnitterWindow
52 super Window
53
54 private var layout = new VerticalLayout(parent=self)
55 private var list_posts = new ListLayout(parent=layout)
56 private var lbl_init = new Label(parent=list_posts, text="Awaiting connection to server")
57
58 # Request an initial full update
59 init do (new ListPostRequest(self, "rest/list?count=16", false)).start
60
61 # Request a full update after a delay
62 fun request_full_list_on_error
63 do
64 (new ListPostRequest(self, "rest/list?count=16", true)).start
65 end
66
67 # Open a push notification connection and thread
68 fun request_push_notification
69 do
70 (new ListPostRequest(self, "push/", false)).start
71 end
72
73 # Update the screen to show the new `posts`
74 fun apply_update(posts: Array[Post])
75 do
76 layout.remove list_posts
77 list_posts = new ListLayout(parent=layout)
78 for post in posts do
79 var line = new VerticalLayout(parent=list_posts)
80 var author = new LabelAuthor(parent=line, text="@"+post.user)
81 var text = new Label(parent=line, text=post.text)
82 end
83 end
84 end
85
86 # Label to display the author's name
87 #
88 # By default, this view is identical a `Label`,
89 # but if can be refined per platforms.
90 class LabelAuthor super Label end
91
92 # ---
93 # Async RESTful actions
94
95 # URI of the remote RESTful server
96 fun tnitter_server_uri: String do return "http://localhost:8080"
97
98 # `AsyncHttpRequest` with services to act on the windows of the app
99 abstract class AsyncTnitterRequest
100 super AsyncHttpRequest
101
102 private var window: TnitterWindow
103
104 redef fun rest_server_uri do return tnitter_server_uri
105
106 redef var rest_action
107
108 # Should this request be delayed by `request_delay_on_error` seconds?
109 var delay: Bool
110
111 redef fun main
112 do
113 if delay then nanosleep(request_delay_on_error, 0)
114 return super
115 end
116 end
117
118 # Async request to list latest posts, either immediately or by push notification
119 #
120 # Implementation note:
121 # This class could as well be merged with `AsyncTnitterRequest` or have two versions,
122 # one for the immediate update and one for the push notification.
123 # We chose this structure for simplicity of the example,
124 # and as more services may be added in the future.
125 # If these future services expect data of a different format,
126 # they will need a different `on_load` but could still use `AsyncTnitterRequest`.
127 class ListPostRequest
128 super AsyncTnitterRequest
129
130 redef fun on_load(posts)
131 do
132 # Deal with server-side errors
133 if posts isa Error then
134 print_error "Server Error: '{posts.message}' from '{rest_server_uri / rest_action}'"
135 return
136 end
137
138 # Type check
139 if not posts isa Array[Post] then
140 print_error "Error: Got '{posts or else "null"}'"
141 return
142 end
143
144 # Update UI and prepare for the next update
145 window.apply_update posts
146 window.request_push_notification
147 end
148
149 redef fun on_fail(error)
150 do
151 print "Warning: Request {rest_server_uri/rest_action} failed with {error}"
152 window.request_full_list_on_error
153 end
154 end
155
156 # ---
157 # Services
158
159 redef class Deserializer
160 redef fun deserialize_class(name)
161 do
162 # This is usually generated using `nitserial`,
163 # but for a single generic class it is easier to implement manually
164
165 if name == "Array[Post]" then return new Array[Post].from_deserializer(self)
166 return super
167 end
168 end