-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
615 lines (562 loc) · 21.8 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>Android Data Binding: from (null) to (data)</title>
<link rel="stylesheet" href="reveal.js/css/reveal.css">
<link rel="stylesheet" href="reveal.js/css/theme/black.css">
<!-- Theme used for syntax highlighting of code -->
<link rel="stylesheet" href="reveal.js/lib/css/zenburn.css">
<link rel="stylesheet" href="reveal.js/lib/css/zenburn-nohighlight.css">
<!-- FontAwesome http://fontawesome.io/examples/ and http://fontawesome.io/icons/ -->
<link rel="stylesheet" href="font-awesome-4.7.0/css/font-awesome.min.css">
<style>
.reveal h1,
.reveal h2 {
text-transform: none !important;
font-variant-caps: small-caps !important;
}
.reveal .small-column-left {
float: left;
width: 30%;
}
.reveal .big-column-right {
float: left;
width: 58%;
padding-left: 5% !important;
}
.reveal .big-column-left {
float: left;
width: 68%;
}
.reveal .small-column-right {
float: left;
width: 20%;
padding-left: 5% !important;
text-align: left;
}
.reveal .left {
text-align: left;
width: 90%;
}
.reveal .join-with-previous {
margin-top: -0.8em;
}
</style>
<!-- Printing and PDF exports -->
<script>
var link = document.createElement( 'link' );
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = window.location.search.match( /print-pdf/gi ) ? 'reveal.js/css/print/pdf.css' : 'reveal.js/css/print/paper.css';
document.getElementsByTagName( 'head' )[0].appendChild( link );
</script>
</head>
<body>
<div class="reveal">
<div class="slides">
<section style="font-size: 0.8em">
<h1>Android Data Binding</h1>
<h2>from (null) to (data)</h2>
<p>Xavier Rubio Jansana</p>
<p style="font-size: 0.8em">
<a href="https://twitter.com/teknik_tdr"><i class="fa fa-twitter" aria-hidden="true"></i> @teknik_tdr</a><br>
<a href="https://xrubio.com"><i class="fa fa-globe" aria-hidden="true"></i> https://xrubio.com</a><br>
<a href="https://github.com/xrubioj/"><i class="fa fa-github" aria-hidden="true"></i> https://github.com/xrubioj/</a>
</p>
</section>
<section>
<h1>What?</h1>
<ul>
<li class="fragment">Standalone library (no additional dependencies)</li>
<li class="fragment">Compatible with API 7+ (Android 2.1) 🙃</li>
<li class="fragment">Announced by Google at I/O 2015</li>
<li class="fragment">Integrated into Android Studio & Gradle</li>
<li class="fragment">Requires changes to the layouts... <span class="fragment">simple ones 😬</span></li>
<li class="fragment">...but can coexist with old methods <span class="fragment">→ progressive 🎉</span></li>
</ul>
</section>
<section>
<h1>Why?</h1>
<ul>
<li class="fragment">Declarative layouts</li>
<li class="fragment">Minimize code to bind data to layouts</li>
<li class="fragment">Cleaner separation</li>
<li class="fragment">Allows observables (reactive)</li>
</ul>
</section>
<section>
<h1>How?</h1>
<small class="left">In app Gradle file:</small>
<pre class="join-with-previous"><code class="gradle" data-trim data-noescape>
android {
...
dataBinding {
enabled = true
}
}
</code></pre>
<div class="fragment">
<small class="left">...if you use Kotlin also add:</small>
<pre class="join-with-previous"><code class="gradle" data-trim data-noescape>
apply plugin: 'kotlin-kapt'
...
dependencies {
...
kapt "com.android.databinding:compiler:2.3.0"
}
</code></pre>
</div>
</section>
<section>
<h1>Layout</h1>
<pre><code class="xml" data-trim data-noescape style="font-size: 0.82em">
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="model"
type="com.xrubio.databindingexample.model.PojoActivityModel"/>
<variable ... />
</data>
<!-- Your regular layout here -->
</layout>
</code></pre>
</section>
<section>
<h1>Layout syntax</h1>
<pre><code class="xml" data-trim data-noescape>
<TextView
<mark class="nohighlight strikethrough fragment" data-fragment-index="1" style="position: absolute"> android:id="@+id/message" </mark><mark class="nohighlight fragment" data-fragment-index="0"> android:id="@+id/message" </mark>
android:text="@{model.message}"<mark class="nohighlight grayout fragment" data-fragment-index="2">
tools:text="message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"</mark>/>
</code></pre>
</section>
<section>
<h1>Activity</h1>
<pre><code class="kotlin" data-trim data-noescape style="font-size: 0.82em">
class PojoModelActivity : AppCompatActivity() {
lateinit var binding: <mark class="fragment" data-fragment-index="1" style="position: absolute">ActivityPojoModel</mark>ActivityPojoModelBinding <mark class="nohighlight fragment" data-fragment-index="0">// activity_pojo_model</mark>
lateinit var model: PojoModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = DataBindingUtil.setContentView(
this, R.layout.activity_pojo_model)
model = PojoModel(message = "Hello World!")
binding.model = model
}
}
</code></pre>
</section>
<section>
<h1>Models</h1>
</section>
<section>
<h1>Models</h1>
<ul>
<li class="fragment">
Can be as simple as a POJO or data class
<pre><code class="kotlin" data-trim data-noescape style="font-size: 0.85em">
data class PojoModel(var message: String)
</code></pre>
</li>
<li class="fragment">
If you try to update this model UI will not update:
<pre><code class="kotlin" data-trim data-noescape>
binding.model.message = "You will never see this"
</code></pre>
</li>
<li class="fragment">
To update we need to replace the whole model:
<pre><code class="kotlin" data-trim data-noescape>
binding.model = PojoModel("But you will see this")
</code></pre>
</li>
</ul>
</section>
<section>
<h1>Models</h1>
<ul>
<li class="fragment">
More complex like an Observable
<pre style="width: inherit"><code class="kotlin" data-trim data-noescape>
class ObservableModel() : BaseObservable() {
<mark class="fragment" style="position: absolute">@get:Bindable</mark>@get:Bindable
var message: String? = null
set(message) {
field = message
<mark class="fragment" style="position: absolute">notifyPropertyChanged</mark>notifyPropertyChanged(BR.message)
}
}
</code></pre>
</li>
<li class="fragment">
You can update individual fields:
<pre style="width: inherit"><code class="kotlin" data-trim data-noescape>
binding.model.message = "You should see this"
</code></pre>
</li>
</ul>
</section>
<section>
<h1>Models</h1>
<ul>
<li class="fragment">
Middle ground... <code>ObservableField</code>s
<pre style="width: inherit"><code class="kotlin" data-trim data-noescape>
class ObservableFieldModel() {
val message: ObservableField<String> =
ObservableField()
val visible: ObservableBoolean = ObservableBoolean()
constructor(message: String) : this() {
this.message.set(message)
}
}
</code></pre>
</li>
<li class="fragment">
You can update individual fields:
<pre style="width: inherit"><code class="kotlin" data-trim data-noescape>
binding.model.message.<mark class="fragment" style="position: absolute">set</mark>set("You should see this")
</code></pre>
</li>
</ul>
</section>
<section>
<h1>Bindings</h1>
</section>
<section>
<h1>Bindings</h1>
<ul>
<li class="fragment">Bind variables: in/out</li>
<li class="fragment">Bind views: access views directly</li>
</ul>
<div class="fragment">
<p>Examples</p>
<pre><code class="kotlin" data-trim data-noescape style="font-size: 0.85em">
var binding: ActivityPojoModelBinding
binding.model = PojoModel("Hi!") // <variable ...>
binding.text.alpha = 0.5f // <TextView android:id="@+id/text" ...>
</code></pre>
</div>
</section>
<section>
<h1>Bindings</h1>
<ul style="font-size: 0.75em">
<li class="fragment">
Bind activity layout
<pre style="width: inherit"><code class="kotlin" data-trim data-noescape>
val binding: ActivityPojoModelBinding =
DataBindingUtil.setContentView(
this, R.layout.activity_pojo_model)
// or...
val binding: ActivityPojoModelBinding =
ActivityPojoModelBinding.inflate(layoutInflater)
setContentView(binding.root)
</code></pre>
</li>
<li class="fragment">
Bind general layouts
<pre style="width: inherit"><code class="kotlin" data-trim data-noescape>
var binding: ListItemBinding =
DataBindingUtil.inflate(layoutInflater, R.layout.list_item,
viewGroup, false)
// or...
var binding: ListItemBinding = ListItemBinding.inflate(layoutInflater,
viewGroup, false)
// ...and finally retrive layout root View
binding.root
</code></pre>
</li>
</ul>
</section>
<section>
<h1>Variables</h1>
<ul>
<li class="fragment">Expressions are not simple references</li>
<li class="fragment">
For example, we can set visibility like this:
<pre style="width: inherit"><code data-trim data-noescape style="font-size: 0.80em">
android:visibility="@{model.visible ? View.VISIBLE : View.GONE}"
android:text='@{"This is the message: " + model.message}'
</code></pre>
</li>
<li class="fragment">
Null coalescing operator:
<pre style="width: inherit"><code data-trim data-noescape style="font-size: 0.80em">
android:text='@{model.message ?? "(no message)"}' // equivalent to Elvis operator ?:
</code></pre>
</li>
<li class="fragment">
Null pointer handling: if <code>model</code> is null
<pre style="width: inherit"><code data-trim data-noescape style="font-size: 0.80em">
android:text='@{model.message}' // no crash, evaluates to "(null)"
android:visibility="@{model.visible ? View.VISIBLE : View.GONE}" // false => GONE
</code></pre>
</li>
</ul>
</section>
<section>
<h1>Operators</h1>
<p style="font-size: 0.85em">Java-like expressions:</p>
<ul style="font-size: 0.55em">
<li>Mathematical <code>+ - / * %</code></li>
<li>String concatenation <code>+</code></li>
<li>Logical <code>&& ||</code></li>
<li>Binary <code>& | ^</code></li>
<li>Unary <code>+ - ! ~</code></li>
<li>Shift <code>>> >>> <<</code></li>
<li>Comparison <code>== > < >= <=</code></li>
<li>instanceof</li>
<li>Grouping <code>()</code></li>
<li>Literals - <code>character</code>, <code>String</code>, <code>numeric</code>, <code>null</code></li>
<li>Cast</li>
<li><mark class="fragment" style="position: absolute">Method calls</mark>Method calls</li>
<li>Field access</li>
<li>
Array access <code>[]</code>
<span class="fragment"> → Collections
<code class="xml" style="border: 1px white solid">android:text="@{list[index]}"</code></span>
</li>
<li>Ternary operator <code>?:</code></li>
</ul>
</section>
<section>
<h1>String literals</h1>
<pre><code class="xml" data-trim data-noescape>
android:text='@{map["firstName"]}'
android:text="@{map[`firstName`}"
android:text="@{map['firstName']}"
</code></pre>
</section>
<section>
<h1>Two way bindings<br>and conversions</h1>
</section>
<section>
<h1>Two way bindings</h1>
<p class="fragment">Allows the binding to update the model</p>
<ul>
<li class="fragment">
For example:
<pre style="width: inherit"><code class="xml" data-trim data-noescape>
<EditText android:text="@<mark class="fragment" style="position: absolute">=</mark>={model.value}"/>
</code></pre>
</li>
<li class="fragment">Needs a setter on the POJO/data class or Observable Model</li>
<li class="fragment">⚠️ ObservableField doesn't work here <br>(weird compilation error 🤷 )</li>
</ul>
</section>
<section>
<h1>Conversions</h1>
<p class="left">Allows the binding to convert and format data</p>
<pre class="fragment" style="width: inherit"><code class="xml" data-trim data-noescape>
<data>
<variable name="model"
type="com.xrubio.databindingexample.model.TwoWayDataBindingConversionModel"/>
</data>
<mark class="nohighlight grayout">
<!-- Layout ... -->
</mark>
<EditText
android:text="@={<mark class="fragment" style="position: absolute">``</mark>`` + model.value}"/>
</code></pre>
<p class="left fragment">Simplified conversion ("hackish") 😬</p>
</section>
<section>
<h1>Conversions</h1>
<pre class="fragment"><code class="xml" data-trim data-noescape style="font-size: 0.6em; line-height: 1.2em;">
<data>
<import type="com.xrubio.databindingexample.converters.IntConverter"/>
<variable name="model"
type="com.xrubio.databindingexample.model.TwoWayDataBindingConversionModel"/>
</data>
<mark class="nohighlight grayout">
<!-- Layout ... -->
</mark>
<EditText
android:text="@={<mark class="fragment" style="position: absolute">IntConverter.INSTANCE.toString</mark>IntConverter.INSTANCE.toString(model.value)}"/>
</code></pre>
<div class="fragment">
<small class="left">Using converters functions</small>
<pre class="join-with-previous"><code class="kotlin" data-trim data-noescape
style="font-size: 0.6em; line-height: 1.2em;">
object IntConverter {
<mark class="fragment" style="position: absolute">@InverseMethod("toInt")</mark>@InverseMethod("toInt")
fun toString(value: Int): String {
return if (value >= 0)
value.toString()
else
""
}
fun toInt(value: String): Int {
return try {
Integer.parseInt(value)
} catch (e: NumberFormatException) {
-1
}
}
}
</code></pre>
</div>
</section>
<section>
<h1>Event handling</h1>
</section>
<section>
<h1>Event handling</h1>
<p class="left">Method References <span class="fragment">→ Evaluated at compile time</span></p>
<pre style="width: inherit"><code class="xml" data-trim data-noescape>
<data>
<variable name="view"
type="com.xrubio.databindingexample.ui.MainActivity"/>
</data>
<mark class="nohighlight grayout">
<!-- Layout ... -->
</mark>
<Button
android:text="Button"
android:onClick="@{view::onClickButton}"/>
</code></pre>
<pre style="width: inherit"><code class="kotlin" data-trim data-noescape>
fun onClickButton(view: View) {
// ...
}
</code></pre>
<aside class="notes" data-markdown>
- Method marked as **used** by the IDE
</aside>
</section>
<section>
<h1>Event handling</h1>
<p class="left">Listener Bindings <span class="fragment">→ Evaluated at run time</span></p>
<pre style="width: inherit"><code class="xml" data-trim data-noescape>
<data>
<variable name="view"
type="com.xrubio.databindingexample.ui.MainActivity"/>
<variable name="data"
type="com.xrubio.databindingexample.ui.ItemData"/>
</data>
<mark class="nohighlight grayout">
<!-- Layout ... -->
</mark>
<Button
android:text="Button"
android:onClick="@{(btn) -> view.onClickButton(btn, data)}"/>
</code></pre>
<pre style="width: inherit"><code class="kotlin" data-trim data-noescape>
fun <mark class="nohighlight grayout">onClickButton</mark>(btn: View, data: ItemData) {
// ...
}
</code></pre>
<aside class="notes" data-markdown>
- Method marked as **unused** by the IDE
- `btn` parameter is a `View`
</aside>
</section>
<section>
<h1>Caveats</h1>
</section>
<section>
<h1>Caveats</h1>
<p>Creation of the layout</p>
<img src="img/caveats-new-binding.png" alt="New binding not found">
<p class="fragment">
Doesn't find new binding on first usage 🕵️<br>
<span class="fragment">→ Build→Clean Project</span>
</p>
</section>
<section>
<h1>Caveats</h1>
<p>Annotation processor errors and kapt</p>
<img src="img/caveats-kapt-messages.png" alt="Messages don't give information">
<p class="fragment">Not very useful 🤷 <span class="fragment">→ Check Gradle Console</span></p>
</section>
<section>
<h1>Caveats</h1>
<p>Error messages in Gradle Console lines are off-by-1</p>
<pre class="fragment"><small class="left" style="color: red">e: java.lang.IllegalStateException: failed to analyze: java.lang.RuntimeException: Found data binding errors.
****/ data binding error ****msg:Cannot resolve type for IntConverter file:/Users/teknik/Documents/source/
_talks/android-data-binding-talk/databinding-example/app/src/main/res/layout/
activity_two_way_binding_conversion_converter.xml loc:<mark class="fragment" style="position: absolute">46:29</mark>46:29 - 46:62 ****\ data binding error ****
</small></pre>
<small class="fragment">It is really line 47, column 30 🤔</small>
</section>
<section>
<h1>References</h1>
<ul>
<li>
Google's Data Binding Library
<a href="https://developer.android.com/topic/libraries/data-binding/index.html">
https://developer.android.com/topic/libraries/data-binding/index.html</a>
</li>
<li>
George Mount Medium articles
<a href="https://medium.com/@georgemount007">https://medium.com/@georgemount007</a>
</li>
<li>
Android Data Binding In Practice
<a href="https://truveris.github.io/articles/android-advanced-data-binding/">
https://truveris.github.io/articles/android-advanced-data-binding/</a>
</li>
</ul>
</section>
<section>
<h1>Questions? 🤔</h1>
</section>
<section>
<h1>Thanks! 🎉</h1>
<p>Xavier Rubio Jansana</p>
<p style="font-size: 0.8em">
<a href="https://twitter.com/teknik_tdr"><i class="fa fa-twitter" aria-hidden="true"></i> @teknik_tdr</a><br>
<a href="https://xrubio.com"><i class="fa fa-globe" aria-hidden="true"></i> https://xrubio.com</a><br>
<a href="https://github.com/xrubioj/"><i class="fa fa-github" aria-hidden="true"></i> https://github.com/xrubioj/</a>
</p>
<p style="line-height: 0.6em"><small>This talk is available at:</small><br>
<a href="https://xrubio.com/talks/talk-android-data-binding-from-null-to-data/" style="font-size: 0.6em">
https://xrubio.com/talks/talk-android-data-binding-from-null-to-data/</a>
</p>
</section>
<!--
Dynamic Variables
Attribute Setters
Performance of updating an object:
- does update all the fields unconditionally?
- just the fields that have changed?
- strategies
Check performance:
- plain old findViewById()
- ButterKnife
- Data Binding
-->
</div>
</div>
<script src="reveal.js/lib/js/head.min.js"></script>
<script src="reveal.js/js/reveal.js"></script>
<script>
// More info https://github.com/hakimel/reveal.js#configuration
Reveal.initialize({
history: true,
// More info https://github.com/hakimel/reveal.js#dependencies
dependencies: [
{ src: 'reveal.js/plugin/markdown/marked.js' },
{ src: 'reveal.js/plugin/markdown/markdown.js' },
{ src: 'reveal.js/plugin/notes/notes.js', async: true },
{ src: 'reveal.js/plugin/highlight/highlight.js', async: true, callback: function() { hljs.initHighlightingOnLoad(); } }
]
});
</script>
</body>
</html>