lib/ios: complete implementation of app::ui with support for ListLayout
[nit.git] / lib / ios / ui / ui.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 # Implementation of `app::ui` for iOS
16 module ui
17
18 import app::ui
19
20 import ios
21 import uikit
22
23 in "ObjC" `{
24 // Objective-C object receiving callbacks from UI events
25 @interface NitCallbackReference: NSObject
26
27 // Nit object target of the callbacks from UI events
28 @property (nonatomic) Button nit_button;
29
30 // Actual callback method
31 -(void) nitOnEvent: (UIButton*) sender;
32 @end
33
34 @implementation NitCallbackReference
35
36 -(void) nitOnEvent: (UIButton*) sender {
37 Button_on_click(self.nit_button);
38 }
39 @end
40
41 // Proxy for both delegates of UITableView relaying all callbacks to `nit_list_layout`
42 @interface UITableViewAndDataSource: NSObject <UITableViewDelegate, UITableViewDataSource>
43
44 // Nit object receiving the callbacks
45 @property ListLayout nit_list_layout;
46
47 // List of native views added to this list view from the Nit side
48 @property NSMutableArray *views;
49 @end
50
51 @implementation UITableViewAndDataSource
52
53 - (id)init
54 {
55 self = [super init];
56 self.views = [[NSMutableArray alloc] initWithCapacity:8];
57 return self;
58 }
59
60 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
61 return ListLayout_number_of_sections_in_table_view(self.nit_list_layout, tableView);
62 }
63
64 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
65 return ListLayout_number_of_rows_in_section(self.nit_list_layout, tableView, section);
66 }
67
68 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
69 return ListLayout_title_for_header_in_section(self.nit_list_layout, tableView, section);
70 }
71
72 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
73 return ListLayout_cell_for_row_at_index_path(self.nit_list_layout, tableView, indexPath);
74 }
75 @end
76 `}
77
78 redef class App
79 redef fun did_finish_launching_with_options
80 do
81 on_create
82 window.native.make_key_and_visible
83 return true
84 end
85
86 redef fun window=(window)
87 do
88 app_delegate.window = window.native
89 super
90 end
91 end
92
93 redef class AppDelegate
94
95 # The main application window, must be set by `App::on_create`
96 fun window: UIWindow in "ObjC" `{ return [self window]; `}
97
98 # The main application window, must be set by `App::on_create`
99 fun window=(window: UIWindow) in "ObjC" `{ self.window = window; `}
100 end
101
102 redef class Control
103
104 # Native implementation of this control
105 fun native: NATIVE is abstract
106
107 # Type of the `native` implementation of this control
108 type NATIVE: NSObject
109 end
110
111 redef class View
112 redef type NATIVE: UIView
113
114 redef var enabled = null is lazy
115 end
116
117 redef class CompositeControl
118
119 redef fun remove(view)
120 do
121 super
122
123 var native_view = view.native
124 assert native_view isa UIView
125 native_view.remove_from_superview
126 end
127 end
128
129 redef class Window
130
131 redef type NATIVE: UIWindow
132 redef var native = new UIWindow
133
134 init do native.background_color = new UIColor.white_color
135
136 redef fun add(view)
137 do
138 super
139
140 var native_view = view.native
141 assert native_view isa UIView
142 native.add_subview native_view
143
144 fill_whole_window_with(native_view, native)
145 end
146
147 private fun fill_whole_window_with(native: UIView, window: UIWindow)
148 in "ObjC" `{
149 // Hard coded borders including the top bar
150 // FIXME this may cause problems with retina devices
151 [window addConstraints:[NSLayoutConstraint
152 constraintsWithVisualFormat: @"V:|-24-[view]-8-|"
153 options: 0 metrics: nil views: @{@"view": native}]];
154 [window addConstraints:[NSLayoutConstraint
155 constraintsWithVisualFormat: @"H:|-8-[view]-8-|"
156 options: 0 metrics: nil views: @{@"view": native}]];
157
158 // Set the required root view controller
159 window.rootViewController = [[UIViewController alloc]initWithNibName:nil bundle:nil];
160 window.rootViewController.view = native;
161 `}
162 end
163
164 redef class Layout
165
166 redef type NATIVE: UIStackView
167 redef var native = new UIStackView
168
169 init
170 do
171 native.alignment = new UIStackViewAlignment.fill
172 native.distribution = new UIStackViewDistribution.fill_equally
173 native.translates_autoresizing_mask_into_constraits = false
174
175 # TODO make customizable
176 native.spacing = 4.0
177 end
178
179 redef fun add(view)
180 do
181 super
182
183 var native_view = view.native
184 assert native_view isa UIView
185 self.native.add_arranged_subview native_view
186 end
187 end
188
189 redef class HorizontalLayout
190 redef init do native.axis = new UILayoutConstraintAxis.horizontal
191 end
192
193 redef class VerticalLayout
194 redef init do native.axis = new UILayoutConstraintAxis.vertical
195 end
196
197 redef class Label
198
199 redef type NATIVE: UILabel
200 redef var native = new UILabel
201
202 redef fun text=(text) do native.text = (text or else "").to_nsstring
203 redef fun text do return native.text.to_s
204 end
205
206 redef class TextInput
207
208 redef type NATIVE: UITextField
209 redef var native = new UITextField
210
211 redef fun text=(text) do native.text = (text or else "").to_nsstring
212 redef fun text do return native.text.to_s
213 end
214
215 redef class Button
216
217 redef type NATIVE: UIButton
218 redef var native = new UIButton(new UIButtonType.system)
219
220 init do native.set_callback self
221
222 redef fun text=(text) do if text != null then native.title = text.to_nsstring
223 redef fun text do return native.current_title.to_s
224
225 private fun on_click do notify_observers new ButtonPressEvent(self)
226
227 redef fun enabled=(enabled) do native.enabled = enabled or else true
228 redef fun enabled do return native.enabled
229 end
230
231 redef class UIButton
232 # Register callbacks on this button to be relayed to `sender`
233 private fun set_callback(sender: Button)
234 import Button.on_click in "ObjC" `{
235
236 NitCallbackReference *ncr = [[NitCallbackReference alloc] init];
237 ncr.nit_button = sender;
238
239 // Pin the objects in both Objective-C and Nit GC
240 Button_incr_ref(sender);
241 ncr = (__bridge NitCallbackReference*)CFBridgingRetain(ncr);
242
243 [self addTarget:ncr action:@selector(nitOnEvent:)
244 forControlEvents:UIControlEventTouchUpInside];
245 `}
246 end
247
248 redef class ListLayout
249
250 redef type NATIVE: UITableView
251 redef var native = new UITableView(new UITableViewStyle.plain)
252
253 init
254 do
255 native.autoresizing_mask
256 native.assign_delegate_and_data_source self
257 end
258
259 redef fun add(item)
260 do
261 # Adding a view to a UITableView is a bit tricky.
262 #
263 # Items are added to the Objective-C view only by callbacks.
264 # We must store the sub views in local lists while waiting
265 # for the callbacks.
266 #
267 # As usual, we keep the Nity object in `items`.
268 # But we also keep their native counterparts in a list of
269 # the `UITableViewAndDataSource` set as `native.delegate`.
270 # Otherwise the native views could be freed by the Objective-C GC.
271
272 # TODO use an adapter for the app.nit ListLayout closer to what exists
273 # on both iOS and Android, to support large data sets.
274
275 if item isa View then
276 add_view_to_native_list(native, item.native)
277 end
278
279 super
280
281 # Force redraw and trigger callbacks
282 native.reload_data
283 end
284
285 private fun add_view_to_native_list(native: UITableView, item: UIView) in "ObjC" `{
286 [((UITableViewAndDataSource*)native.delegate).views addObject:item];
287 `}
288
289 private fun get_view_from_native_list(native: UITableView, index: Int): UIView in "ObjC" `{
290 return [((UITableViewAndDataSource*)native.delegate).views objectAtIndex:index];
291 `}
292
293 # Number of sections in this view
294 #
295 # By default, we assume that all `items` are in a single section,
296 # so there is only one section.
297 #
298 # iOS callback: `numberOfSectionsInTableView`
299 protected fun number_of_sections_in_table_view(view: UITableView): Int
300 do return 1
301
302 # Number of entries in `section`
303 #
304 # By default, we assume that all `items` are in a single section,
305 # so no matter the section, this returns `items.length`.
306 #
307 # iOS callback: `numberOfRowsInSection`
308 protected fun number_of_rows_in_section(view: UITableView, section: Int): Int
309 do return items.length
310
311 # Title for `section`, return `new NSString.nil` for no title
312 #
313 # By default, this returns no title.
314 #
315 # iOS callback: `titleForHeaderInSection`
316 protected fun title_for_header_in_section(view: UITableView, section: Int): NSString
317 do return new NSString.nil
318
319 # Return a `UITableViewCell` for the item at `index_path`
320 #
321 # By default, we assume that all `items` are in a single section.
322 # So no matter the depth of the `index_path`, this returns a cell with
323 # the view at index part of `index_path`.
324 #
325 # iOS callback: `cellForRowAtIndexPath`
326 protected fun cell_for_row_at_index_path(table_view: UITableView, index_path: NSIndexPath): UITableViewCell
327 do
328 var reuse_id = "NitCell".to_nsstring
329 var cell = new UITableViewCell(reuse_id)
330
331 # TODO if there is performance issues, reuse cells with
332 # the following code, but clear the cell before use.
333
334 #var cell = table_view.dequeue_reusable_cell_with_identifier(reuse_id)
335 #if cell.address_is_null then cell = new UITableViewCell(reuse_id)
336
337 var index = index_path.index_at_position(1)
338 var view_native = get_view_from_native_list(table_view, index)
339 var cv = cell.content_view
340 cv.add_subview view_native
341
342 return cell
343 end
344 end
345
346 redef class UITableView
347
348 # Assign `list_view` as `delegate` and `dataSource`, and pin all references in both GCs
349 private fun assign_delegate_and_data_source(list_view: ListLayout)
350 import ListLayout.number_of_sections_in_table_view,
351 ListLayout.number_of_rows_in_section,
352 ListLayout.title_for_header_in_section,
353 ListLayout.cell_for_row_at_index_path in "ObjC" `{
354
355 UITableViewAndDataSource *objc_delegate = [[UITableViewAndDataSource alloc] init];
356 objc_delegate = (__bridge UITableViewAndDataSource*)CFBridgingRetain(objc_delegate);
357
358 objc_delegate.nit_list_layout = list_view;
359 ListLayout_incr_ref(list_view);
360
361 // Set our
362 self.delegate = objc_delegate;
363 self.dataSource = objc_delegate;
364 `}
365 end