Slide 1

Slide 1 text

LITHO: BEST PRACTICES FOR BUILDING EFFICIENT UI Sergey Ryabov Facebook

Slide 2

Slide 2 text

AGENDA ✦ What is Litho ✦ How does it work ✦ State management ✦ Animations ✦ Interop ✦ Tools

Slide 3

Slide 3 text

WHAT IS LITHO

Slide 4

Slide 4 text

WHAT IS LITHO

Slide 5

Slide 5 text

WHAT IS LITHO ✦ UI framework

Slide 6

Slide 6 text

WHAT IS LITHO ✦ UI framework ✦ 4.5 years old

Slide 7

Slide 7 text

WHAT IS LITHO ✦ UI framework ✦ 4.5 years old ✦ Used in Facebook apps

Slide 8

Slide 8 text

WHAT IS LITHO ✦ UI framework ✦ 4.5 years old ✦ Used in Facebook apps ✦ But not only...

Slide 9

Slide 9 text

WHAT IS LITHO ✦ UI framework ✦ 4.5 years old ✦ Used in Facebook apps ✦ But not only... appbrain.com/stats/libraries/details/litho/litho

Slide 10

Slide 10 text

WHAT IS LITHO ABOUT

Slide 11

Slide 11 text

Inflate Measure Layout Draw WHAT IS LITHO ABOUT

Slide 12

Slide 12 text

In Me La Dr WHAT IS LITHO ABOUT

Slide 13

Slide 13 text

Infl Mea Lay Dra WHAT IS LITHO ABOUT

Slide 14

Slide 14 text

UI BG Infl Mea Lay Dra WHAT IS LITHO ABOUT

Slide 15

Slide 15 text

UI BG Infl WHAT IS LITHO ABOUT Mea Lay Dra

Slide 16

Slide 16 text

UI BG Infl WHAT IS LITHO ABOUT Meas Layo Draw

Slide 17

Slide 17 text

UI BG Infl Draw WHAT IS LITHO ABOUT Meas Layo

Slide 18

Slide 18 text

UI BG Infl Draw WHAT IS LITHO ABOUT Meas Layo

Slide 19

Slide 19 text

UI BG Infl Draw WHAT IS LITHO ABOUT Meas Layo

Slide 20

Slide 20 text

WHAT IS LITHO ABOUT

Slide 21

Slide 21 text

✦ Asynchronous layout WHAT IS LITHO ABOUT

Slide 22

Slide 22 text

UI BG Infl Draw WHAT IS LITHO ABOUT Meas Layo

Slide 23

Slide 23 text

UI BG Infl Draw WHAT IS LITHO ABOUT Meas Layo

Slide 24

Slide 24 text

UI BG Infl Draw WHAT IS LITHO ABOUT Meas Layo yogalayout.com

Slide 25

Slide 25 text

✦ Asynchronous layout WHAT IS LITHO ABOUT

Slide 26

Slide 26 text

WHAT IS LITHO ABOUT ✦ Asynchronous layout ✦ Declarative API

Slide 27

Slide 27 text

UI = f(properties)

Slide 28

Slide 28 text

UI = f(props)

Slide 29

Slide 29 text

fun f( title: String, subtitle: String ): UI { return Column.create() .child( Text.create() .text(title)) .child( Text.create() .text(subtitle)) .build() }

Slide 30

Slide 30 text

@OnCreateLayout fun onCreateLayout( c: ComponentContext, @Prop title: String, @Prop subtitle: String ): Component { return Column.create(c) .child( Text.create(c) .text(title)) .child( Text.create(c) .text(subtitle)) .build() }

Slide 31

Slide 31 text

@OnCreateLayout fun onCreateLayout( c: ComponentContext, @Prop title: String, @Prop subtitle: String ): Component { return Column.create(c) .child( Text.create(c) .text(title)) .child( Text.create(c) .text(subtitle)) .build() }

Slide 32

Slide 32 text

@LayoutSpec object ListItemSpec { @OnCreateLayout fun onCreateLayout( c: ComponentContext, @Prop title: String, @Prop subtitle: String ): Component { return Column.create(c) .child( Text.create(c) .text(title)) .child( Text.create(c) .text(subtitle)) .build() } }

Slide 33

Slide 33 text

@LayoutSpec object ListItemSpec { @OnCreateLayout fun onCreateLayout( c: ComponentContext, @Prop title: String, @Prop subtitle: String ): Component { return Column.create(c) .child( Text.create(c) .text(title)) .child( Text.create(c) .text(subtitle)) .build() } }

Slide 34

Slide 34 text

@LayoutSpec object ListItemSpec { @OnCreateLayout fun onCreateLayout( c: ComponentContext, @Prop title: String, @Prop subtitle: String ): Component { return Column.create(c) .child( Text.create(c) .text(title)) .child( Text.create(c) .text(subtitle)) .build() } }

Slide 35

Slide 35 text

@LayoutSpec object ListItemSpec { @OnCreateLayout fun onCreateLayout( c: ComponentContext, @Prop title: String, @Prop subtitle: String ): Component { return Column.create(c) .paddingDip(ALL, 16f) .child( Text.create(c) .text(title) .textSizeSp(20f)) .child( Text.create(c) .text(subtitle)) .build() } }

Slide 36

Slide 36 text

@LayoutSpec object ListItemSpec { @OnCreateLayout fun onCreateLayout( c: ComponentContext, @Prop title: String, @Prop subtitle: String ): Component { return Column.create(c) .child( Text.create(c) .text(title)) .child( Text.create(c) .text(subtitle)) .build() } }

Slide 37

Slide 37 text

@LayoutSpec object ListItemSpec { @OnCreateLayout fun onCreateLayout( c: ComponentContext, @Prop title: String, @Prop subtitle: String ): Component { return Column.create(c) .child( Text.create(c) .text(title)) .child( Text.create(c) .text(subtitle)) .build() } } @LayoutSpec object ListItemWithImageSpec { @OnCreateLayout fun onCreateLayout( c: ComponentContext, @Prop image: Drawable, @Prop title: String, @Prop subtitle: String): Component { return Row.create(c) .child( Image.create(c) .drawable(image)) .child( ListItem.create(c) .title(title) .subtitle(subtitle)) .build() } }

Slide 38

Slide 38 text

@LayoutSpec object ListItemSpec { @OnCreateLayout fun onCreateLayout( c: ComponentContext, @Prop title: String, @Prop subtitle: String ): Component { return Column.create(c) .child( Text.create(c) .text(title)) .child( Text.create(c) .text(subtitle)) .build() } } @LayoutSpec object ListItemWithImageSpec { @OnCreateLayout fun onCreateLayout( c: ComponentContext, @Prop image: Drawable, @Prop title: String, @Prop subtitle: String): Component { return Row.create(c) .child( Image.create(c) .drawable(image)) .child( ListItem.create(c) .title(title) .subtitle(subtitle)) .build() } }

Slide 39

Slide 39 text

@LayoutSpec object ListItemSpec { @OnCreateLayout fun onCreateLayout( c: ComponentContext, @Prop title: String, @Prop subtitle: String ): Component { return Column.create(c) .child( Text.create(c) .text(title)) .child( Text.create(c) .text(subtitle)) .build() } } @LayoutSpec object ListItemWithImageSpec { @OnCreateLayout fun onCreateLayout( c: ComponentContext, @Prop image: Drawable, @Prop title: String, @Prop subtitle: String): Component { return Row.create(c) .child( Image.create(c) .drawable(image)) .child( ListItem.create(c) .title(title) .subtitle(subtitle)) .build() } }

Slide 40

Slide 40 text

@LayoutSpec object ListItemSpec { @OnCreateLayout fun onCreateLayout( c: ComponentContext, @Prop title: String, @Prop subtitle: String ): Component { return Column.create(c) .child( Text.create(c) .text(title)) .child( Text.create(c) .text(subtitle)) .build() } } @LayoutSpec object ListItemWithImageSpec { @OnCreateLayout fun onCreateLayout( c: ComponentContext, @Prop image: Drawable, @Prop title: String, @Prop subtitle: String): Component { return Row.create(c) .child( Image.create(c) .drawable(image)) .child( ListItem.create(c) .title(title) .subtitle(subtitle)) .build() } }

Slide 41

Slide 41 text

@LayoutSpec object ListItemSpec { @OnCreateLayout fun onCreateLayout( c: ComponentContext, @Prop title: String, @Prop subtitle: String ): Component { return Column.create(c) .child( Text.create(c) .text(title)) .child( Text.create(c) .text(subtitle)) .build() } } @LayoutSpec object ListItemWithImageSpec { @OnCreateLayout fun onCreateLayout( c: ComponentContext, @Prop image: Drawable, @Prop title: String, @Prop subtitle: String): Component { return Row.create(c) .child( Image.create(c) .drawable(image)) .child( ListItem.create(c) .title(title) .subtitle(subtitle)) .build() } }

Slide 42

Slide 42 text

@LayoutSpec object ListItemSpec { @OnCreateLayout fun onCreateLayout( c: ComponentContext, @Prop title: String, @Prop subtitle: String ): Component { return Column.create(c) .child( Text.create(c) .text(title)) .child( Text.create(c) .text(subtitle)) .build() } }

Slide 43

Slide 43 text

@LayoutSpec object ListItemSpec { @OnCreateLayout fun onCreateLayout( c: ComponentContext, @Prop title: String, @Prop subtitle: String ): Component { return Column.create(c) .child( Text.create(c) .text(title)) .child( Text.create(c) .text(subtitle)) .build() } } class MainActivity : AppCompatActivity() { override fun onCreate(state: Bundle?) { super.onCreate(savedInstanceState) val c = ComponentContext(this) setContentView(LithoView.create(c, ListItem.create(c) .title("Title") .subtitle("Subtitle") .build())) } }

Slide 44

Slide 44 text

@LayoutSpec object ListItemSpec { @OnCreateLayout fun onCreateLayout( c: ComponentContext, @Prop title: String, @Prop subtitle: String ): Component { return Column.create(c) .child( Text.create(c) .text(title)) .child( Text.create(c) .text(subtitle)) .build() } } class MainActivity : AppCompatActivity() { override fun onCreate(state: Bundle?) { super.onCreate(savedInstanceState) val c = ComponentContext(this) setContentView(LithoView.create(c, ListItem.create(c) .title("Title") .subtitle("Subtitle") .build())) } }

Slide 45

Slide 45 text

@LayoutSpec object ListItemSpec { @OnCreateLayout fun onCreateLayout( c: ComponentContext, @Prop title: String, @Prop subtitle: String ): Component { return Column.create(c) .child( Text.create(c) .text(title)) .child( Text.create(c) .text(subtitle)) .build() } } class MainActivity : AppCompatActivity() { override fun onCreate(state: Bundle?) { super.onCreate(savedInstanceState) val c = ComponentContext(this) setContentView(LithoView.create(c, ListItem.create(c) .title("Title") .subtitle("Subtitle") .build())) } }

Slide 46

Slide 46 text

@LayoutSpec object ListItemSpec { @OnCreateLayout fun onCreateLayout( c: ComponentContext, @Prop title: String, @Prop subtitle: String ): Component { return Column.create(c) .child( Text.create(c) .text(title)) .child( Text.create(c) .text(subtitle)) .build() } } class MainActivity : AppCompatActivity() { override fun onCreate(state: Bundle?) { super.onCreate(savedInstanceState) val c = ComponentContext(this) setContentView(LithoView.create(c, ListItem.create(c) .title("Title") .subtitle("Subtitle") .build())) } }

Slide 47

Slide 47 text

@LayoutSpec object ListItemSpec { @OnCreateLayout fun onCreateLayout( c: ComponentContext, @Prop title: String, @Prop subtitle: String ): Component { return Column.create(c) .child( Text.create(c) .text(title)) .child( Text.create(c) .text(subtitle)) .build() } } class MainActivity : AppCompatActivity() { override fun onCreate(state: Bundle?) { super.onCreate(savedInstanceState) val c = ComponentContext(this) setContentView(LithoView.create(c, ListItem.create(c) .title("Title") .subtitle("Subtitle") .build())) } }

Slide 48

Slide 48 text

HOW DOES IT ALL WORK?

Slide 49

Slide 49 text

HOW DOES IT ALL WORK

Slide 50

Slide 50 text

HOW DOES IT ALL WORK 1. Create an Internal Tree

Slide 51

Slide 51 text

HOW DOES IT ALL WORK 1. Create an Internal Tree 2. Create LayoutState

Slide 52

Slide 52 text

HOW DOES IT ALL WORK 1. Create an Internal Tree 2. Create LayoutState 3. Mount LayoutState

Slide 53

Slide 53 text

@LayoutSpec object ListItemWithImageSpec { // ... Row.create(c) .child( Image.create(c) .drawable(image)) .child( ListItem.create(c) .title(title) .subtitle(subtitle)) .build() } }

Slide 54

Slide 54 text

@LayoutSpec object ListItemWithImageSpec { // ... Row.create(c) .child( Image.create(c) .drawable(image)) .child( ListItem.create(c) .title(title) .subtitle(subtitle)) .build() } }

Slide 55

Slide 55 text

@LayoutSpec object ListItemWithImageSpec { // ... Row.create(c) .child( Image.create(c) .drawable(image)) .child( ListItem.create(c) .title(title) .subtitle(subtitle)) .build() } } ListItemWithImage

Slide 56

Slide 56 text

@LayoutSpec object ListItemWithImageSpec { // ... Row.create(c) .child( Image.create(c) .drawable(image)) .child( ListItem.create(c) .title(title) .subtitle(subtitle)) .build() } } Row ListItemWithImage

Slide 57

Slide 57 text

@LayoutSpec object ListItemWithImageSpec { // ... Row.create(c) .child( Image.create(c) .drawable(image)) .child( ListItem.create(c) .title(title) .subtitle(subtitle)) .build() } } Row ListItemWithImage

Slide 58

Slide 58 text

@LayoutSpec object ListItemWithImageSpec { // ... Row.create(c) .child( Image.create(c) .drawable(image)) .child( ListItem.create(c) .title(title) .subtitle(subtitle)) .build() } } Row ListItemWithImage Image

Slide 59

Slide 59 text

@LayoutSpec object ListItemWithImageSpec { // ... Row.create(c) .child( Image.create(c) .drawable(image)) .child( ListItem.create(c) .title(title) .subtitle(subtitle)) .build(). } } Row ListItemWithImage Image

Slide 60

Slide 60 text

@LayoutSpec object ListItemWithImageSpec { // ... Row.create(c) .child( Image.create(c) .drawable(image)) .child( ListItem.create(c) .title(title) .subtitle(subtitle)) .build(). } } Row ListItemWithImage Image ListItem

Slide 61

Slide 61 text

Row ListItemWithImage Image ListItem ListItem.create(c) .title(title) .subtitle(subtitle)) .build(). } } @LayoutSpec object ListItemSpec { // ... Column.create(c) .child( Text.create(c) .text(title)) .child( Text.create(c) .text(subtitle)) .build() } }

Slide 62

Slide 62 text

Row ListItemWithImage Image Column ListItem @LayoutSpec object ListItemSpec { // ... Column.create(c) .child( Text.create(c) .text(title)) .child( Text.create(c) .text(subtitle)) .build() } }

Slide 63

Slide 63 text

Row ListItemWithImage Image Column ListItem @LayoutSpec object ListItemSpec { // ... Column.create(c) .child( Text.create(c) .text(title)) .child( Text.create(c) .text(subtitle)) .build() } }

Slide 64

Slide 64 text

Row ListItemWithImage Image Column ListItem Text @LayoutSpec object ListItemSpec { // ... Column.create(c) .child( Text.create(c) .text(title)) .child( Text.create(c) .text(subtitle)) .build() } }

Slide 65

Slide 65 text

Row ListItemWithImage Image Column ListItem Text @LayoutSpec object ListItemSpec { // ... Column.create(c) .child( Text.create(c) .text(title)) .child( Text.create(c) .text(subtitle)) .build() } }

Slide 66

Slide 66 text

Row ListItemWithImage Image Column ListItem Text Text @LayoutSpec object ListItemSpec { // ... Column.create(c) .child( Text.create(c) .text(title)) .child( Text.create(c) .text(subtitle)) .build() } }

Slide 67

Slide 67 text

Row ListItemWithImage Image Column ListItem Text Text @LayoutSpec object ListItemSpec { // ... Column.create(c) .child( Text.create(c) .text(title)) .child( Text.create(c) .text(subtitle)) .build() } }

Slide 68

Slide 68 text

CREATE LAYOUTSTATE Row Image Column Text Text

Slide 69

Slide 69 text

CREATE LAYOUTSTATE Measure with Yoga Row Image Column Text Text

Slide 70

Slide 70 text

CREATE LAYOUTSTATE Measure with Yoga Row Image Column Text Text (0, 0 - 640, 96) (88, 0 - 640, 96) (16, 16 - 80, 80) (88, 16 - 624, 48) (88, 48 - 624, 80)

Slide 71

Slide 71 text

CREATE LAYOUTSTATE Measure with Yoga Collect a list of items to be drawn and their positions Row Image Column Text Text (0, 0 - 640, 96) (88, 0 - 640, 96) (16, 16 - 80, 80) (88, 16 - 624, 48) (88, 48 - 624, 80)

Slide 72

Slide 72 text

Row Image Column Text Text (0, 0 - 640, 96) (88, 0 - 640, 96) (16, 16 - 80, 80) (88, 16 - 624, 48) (88, 48 - 624, 80)

Slide 73

Slide 73 text

Row Image Column Text Text (0, 0 - 640, 96) (88, 0 - 640, 96) (16, 16 - 80, 80) (88, 16 - 624, 48) (88, 48 - 624, 80) Root

Slide 74

Slide 74 text

Root Row Image Column Text Text (0, 0 - 640, 96) (88, 0 - 640, 96) (16, 16 - 80, 80) (88, 16 - 624, 48) (88, 48 - 624, 80)

Slide 75

Slide 75 text

Root Image Row Image Column Text Text (0, 0 - 640, 96) (88, 0 - 640, 96) (16, 16 - 80, 80) (88, 16 - 624, 48) (88, 48 - 624, 80)

Slide 76

Slide 76 text

Root Image Row Image Column Text Text (0, 0 - 640, 96) (88, 0 - 640, 96) (16, 16 - 80, 80) (88, 16 - 624, 48) (88, 48 - 624, 80)

Slide 77

Slide 77 text

Root Text Image Row Image Column Text Text (0, 0 - 640, 96) (88, 0 - 640, 96) (16, 16 - 80, 80) (88, 16 - 624, 48) (88, 48 - 624, 80)

Slide 78

Slide 78 text

Root Text Image Row Image Column Text Text (0, 0 - 640, 96) (88, 0 - 640, 96) (16, 16 - 80, 80) (88, 16 - 624, 48) (88, 48 - 624, 80) Text

Slide 79

Slide 79 text

Root Text Image Row Image Column Text Text (0, 0 - 640, 96) (88, 0 - 640, 96) (16, 16 - 80, 80) (88, 16 - 624, 48) (88, 48 - 624, 80) Text LayoutState

Slide 80

Slide 80 text

Root Text Image Row Image Column Text Text (0, 0 - 640, 96) (88, 0 - 640, 96) (16, 16 - 80, 80) (88, 16 - 624, 48) (88, 48 - 624, 80) Text Im ageDraw able TextDraw able TextDraw able LayoutState

Slide 81

Slide 81 text

MOUNT LAYOUTSTATE Root Text Image Text Im ageDraw able TextDraw able TextDraw able LayoutState

Slide 82

Slide 82 text

MOUNT LAYOUTSTATE The only step that always happens on UI thread Root Text Image Text Im ageDraw able TextDraw able TextDraw able LayoutState

Slide 83

Slide 83 text

MOUNT LAYOUTSTATE The only step that always happens on UI thread Text Image Text Root Text Image Text Im ageDraw able TextDraw able TextDraw able LayoutState

Slide 84

Slide 84 text

MOUNT LAYOUTSTATE The only step that always happens on UI thread Text Image Text Root Text Image Text Im ageDraw able TextDraw able TextDraw able LayoutState

Slide 85

Slide 85 text

BUILDING BLOCKS

Slide 86

Slide 86 text

BUILDING BLOCKS ✦ @LayoutSpec

Slide 87

Slide 87 text

BUILDING BLOCKS ✦ @LayoutSpec ListItemWithImage ListItem

Slide 88

Slide 88 text

BUILDING BLOCKS ✦ @LayoutSpec ✦ Row / Column

Slide 89

Slide 89 text

BUILDING BLOCKS ✦ @LayoutSpec ✦ Row / Column Row Column

Slide 90

Slide 90 text

BUILDING BLOCKS Image Text Text ✦ @LayoutSpec ✦ Row / Column

Slide 91

Slide 91 text

BUILDING BLOCKS ✦ @LayoutSpec ✦ Row / Column ✦ Image Text Text ...? ✦ @LayoutSpec ✦ Row / Column

Slide 92

Slide 92 text

BUILDING BLOCKS ✦ @LayoutSpec ✦ Row / Column ✦ Image Text Text ✦ @MountSpec ✦ @LayoutSpec ✦ Row / Column

Slide 93

Slide 93 text

@MountSpec object GradientSpec { @OnCreateMountContent fun onCreateMountContent(context: Context): GradientDrawable { return GradientDrawable() } @OnMount fun onMount( c: ComponentContext, drawable: GradientDrawable, @Prop @ColorInt colors: IntArray) { drawable.colors = colors } }

Slide 94

Slide 94 text

@MountSpec object GradientSpec { @OnCreateMountContent fun onCreateMountContent(context: Context): GradientDrawable { return GradientDrawable() } @OnMount fun onMount( c: ComponentContext, drawable: GradientDrawable, @Prop @ColorInt colors: IntArray) { drawable.colors = colors } }

Slide 95

Slide 95 text

@MountSpec object GradientSpec { @OnCreateMountContent fun onCreateMountContent(context: Context): GradientDrawable { return GradientDrawable() } @OnMount fun.onMount( c: ComponentContext, drawable: GradientDrawable, @Prop @ColorInt colors: IntArray) { drawable.colors = colors } }

Slide 96

Slide 96 text

@MountSpec object GradientSpec { @OnCreateMountContent fun onCreateMountContent(context: Context): GradientDrawable { return GradientDrawable() } @OnMount fun.onMount( c: ComponentContext, drawable: GradientDrawable, @Prop @ColorInt colors: IntArray) { drawable.colors = colors } }

Slide 97

Slide 97 text

public @interface MountSpec { int poolSize() default.3; boolean isPureRender() default false; }

Slide 98

Slide 98 text

public @interface MountSpec { int poolSize() default.3; boolean isPureRender() default false; }

Slide 99

Slide 99 text

public @interface MountSpec { int poolSize() default.3; boolean isPureRender() default false; }

Slide 100

Slide 100 text

public @interface MountSpec { int poolSize() default 3; boolean isPureRender() default false; } @MountSpec(poolSize = 30, isPureRender = true) class ImageSpec { @ShouldUpdate static boolean shouldUpdate(...) {} }

Slide 101

Slide 101 text

public @interface MountSpec { int poolSize() default 3; boolean isPureRender() default false; } @MountSpec(poolSize = 30, isPureRender = true) class ImageSpec { @ShouldUpdate static boolean shouldUpdate(...) {} }

Slide 102

Slide 102 text

OPTIMIZATIONS

Slide 103

Slide 103 text

OPTIMIZATIONS ✦ Layout/Mount Diffing

Slide 104

Slide 104 text

OPTIMIZATIONS ✦ Layout/Mount Diffing • Reuse Measurements

Slide 105

Slide 105 text

OPTIMIZATIONS ✦ Layout/Mount Diffing • Reuse Measurements • Reuse LayoutOutputs

Slide 106

Slide 106 text

Text Img Text (16, 16 - 80, 80) (88, 16 - 624, 48) (88, 48 - 624, 80) (16, 16 - 80, 80) (88, 16 - 624, 48) (88, 48 - 624, 80) Internal Tree Diff Tree

Slide 107

Slide 107 text

Text Img Text (16, 16 - 80, 80) (88, 16 - 624, 48) (88, 48 - 624, 80) (16, 16 - 80, 80) (88, 16 - 624, 48) (88, 48 - 624, 80) Internal Tree Diff Tree

Slide 108

Slide 108 text

Text Img Text (16, 16 - 80, 80) (88, 16 - 624, 48) (88, 48 - 624, 80) (16, 16 - 80, 80) (88, 16 - 624, 48) (88, 48 - 624, 80) Internal Tree Diff Tree

Slide 109

Slide 109 text

Text Img Text (16, 16 - 80, 80) (88, 16 - 624, 48) (88, 48 - 624, 80) (16, 16 - 80, 80) (88, 16 - 624, 48) (88, 48 - 624, 80) Internal Tree Diff Tree

Slide 110

Slide 110 text

(16, 16 - 80, 80) (88, 16 - 624, 48) (88, 48 - 624, 80) Text Text Img (16, 16 - 80, 80) (88, 16 - 624, 48) (88, 48 - 624, 80) Root LayoutState Internal Tree Diff Tree

Slide 111

Slide 111 text

(16, 16 - 80, 80) (88, 16 - 624, 48) (88, 48 - 624, 80) (16, 16 - 80, 80) (88, 16 - 624, 48) (88, 48 - 624, 80) Root Text Text Image LayoutState Internal Tree Diff Tree

Slide 112

Slide 112 text

OPTIMIZATIONS ✦ Layout/Mount Diffing • Reuse Measurements • Reuse LayoutOutputs

Slide 113

Slide 113 text

OPTIMIZATIONS ✦ Layout/Mount Diffing • Reuse Measurements • Reuse LayoutOutputs ✦ IncrementalMount

Slide 114

Slide 114 text

INCREMENTAL MOUNT

Slide 115

Slide 115 text

INCREMENTAL MOUNT

Slide 116

Slide 116 text

INCREMENTAL MOUNT

Slide 117

Slide 117 text

INCREMENTAL MOUNT

Slide 118

Slide 118 text

INCREMENTAL MOUNT

Slide 119

Slide 119 text

INCREMENTAL MOUNT

Slide 120

Slide 120 text

GOTCHAS

Slide 121

Slide 121 text

GOTCHAS ✦ Use isPureRender / @ShouldUpdate to reuse previous layout results for @MountSpecs

Slide 122

Slide 122 text

GOTCHAS ✦ Use isPureRender / @ShouldUpdate to reuse previous layout results for @MountSpecs ✦ Tweak MountPools according to you requirements

Slide 123

Slide 123 text

MANAGING STATE

Slide 124

Slide 124 text

@OnCreateLayout fun onCreateLayout( c: ComponentContext, @State count: Int): Component { return Column.create(c) .child( Text.create(c) .text("$count") ) .child( Text.create(c) .text("+") ) .build() }

Slide 125

Slide 125 text

@OnCreateLayout fun onCreateLayout( c: ComponentContext, @State count: Int): Component { return Column.create(c) .child( Text.create(c) .text("$count") ) .child( Text.create(c) .text("+") ) .build() }

Slide 126

Slide 126 text

@OnCreateLayout fun onCreateLayout( c: ComponentContext, @State count: Int): Component { return Column.create(c) .child( Text.create(c) .text("$count") ) .child( Text.create(c) .text("+") ) .build() }

Slide 127

Slide 127 text


 
 @OnUpdateState fun increment(count: StateValue) { count.set(count.get() + 1) }

Slide 128

Slide 128 text


 @OnUpdateState fun increment(count: StateValue) { count.set(count.get() + 1) }
 
 @OnEvent(ClickEvent::class) fun.onIncrement(c: ComponentContext) { RootComponent.increment(c) }

Slide 129

Slide 129 text


 @OnUpdateState fun increment(count: StateValue) { count.set(count.get() + 1) }
 
 @OnEvent(ClickEvent::class) fun.onIncrement(c: ComponentContext) { RootComponent.increment(c) } // ... .child( Text.create(c) .text("+") .clickHandler(RootComponent.onIncrement(c)) )

Slide 130

Slide 130 text

@OnUpdateState fun increment(count: StateValue) { count.set(count.get() + 1) }
 
 @OnEvent(ClickEvent::class) fun.onIncrement(c: ComponentContext) { RootComponent.increment(c) }

Slide 131

Slide 131 text

@OnUpdateState fun increment(count: StateValue) { count.set(count.get() + 1) }
 
 RootComponent.increment(c)

Slide 132

Slide 132 text

State updates? I'll take two!

Slide 133

Slide 133 text

@OnUpdateState fun changeColor(color: StateValue, @Param newColor: Color) { color.set(newColor) } @OnUpdateState fun changeName(name: StateValue, @Param newName: String) { name.set(newName) }

Slide 134

Slide 134 text

@OnUpdateState fun changeColor(color: StateValue, @Param newColor: Color) { color.set(newColor) } @OnUpdateState fun changeName(name: StateValue, @Param newName: String) { name.set(newName) } ... ... RootComponent.changeColor(c, Color.RED) RootComponent.changeName(c, "IronMan")

Slide 135

Slide 135 text

No content

Slide 136

Slide 136 text

No content

Slide 137

Slide 137 text

No content

Slide 138

Slide 138 text

No content

Slide 139

Slide 139 text

@OnUpdateState fun changeColor(color: StateValue, @Param newColor: Color) { color.set(newColor) } @OnUpdateState fun changeName(name: StateValue, @Param newName: String) { name.set(newName) } ... ... RootComponent.changeColor(c, Color.RED) RootComponent.changeName(c, "IronMan")

Slide 140

Slide 140 text

@OnUpdateState fun changeHero( color: StateValue, name: StateValue, @Param newColor: Color, @Param newName: String) { color.set(newColor) name.set(newName) } ... ... RootComponent.changeHero(c, Color.RED, "IronMan")

Slide 141

Slide 141 text

No content

Slide 142

Slide 142 text

No content

Slide 143

Slide 143 text

What if State is not tied to rendering?

Slide 144

Slide 144 text

@OnCreateLayout fun onCreateLayout( c: ComponentContext, @State count: Int, ): Component { return Column.create(c) // ... .build() }

Slide 145

Slide 145 text

@OnCreateLayout fun onCreateLayout( c: ComponentContext, @State count: Int, @State(canUpdateLazily = true) step: Int ): Component { return Column.create(c) // ... .child( TextInput.create(c).initialText("$step")) .build() }

Slide 146

Slide 146 text

@State(canUpdateLazily = true) step: Int RootComponent.lazyUpdateStep(c, value)

Slide 147

Slide 147 text

@State(canUpdateLazily = true) step: Int RootComponent.lazyUpdateStep(c, value)

Slide 148

Slide 148 text

GOTCHAS

Slide 149

Slide 149 text

GOTCHAS ✦ Batch your State updates

Slide 150

Slide 150 text

GOTCHAS ✦ Batch your State updates ✦ Use lazy State updates where possible

Slide 151

Slide 151 text

ANIMATE ALL THE THINGS

Slide 152

Slide 152 text

@OnCreateLayout fun onCreateLayout( c: ComponentContext, @State alignToEnd: Boolean): Component { val align = if (alignToEnd) FLEX_END else FLEX_START return Column.create(c) .justifyContent(CENTER) .child( Button.create(c) .text("Catch me!") .alignSelf(align) .clickHandler(RootComponent.onClick(c)) ) .build() }

Slide 153

Slide 153 text

@OnCreateLayout fun onCreateLayout( c: ComponentContext, @State alignToEnd: Boolean): Component { val align = if (alignToEnd) FLEX_END else FLEX_START return Column.create(c) .justifyContent(CENTER) .child( Button.create(c) .text("Catch me!") .alignSelf(align) .clickHandler(RootComponent.onClick(c)) ) .build() }

Slide 154

Slide 154 text

@OnCreateLayout fun onCreateLayout( c: ComponentContext, @State alignToEnd: Boolean): Component { val align = if (alignToEnd) FLEX_END else FLEX_START return Column.create(c) .justifyContent(CENTER) .child( Button.create(c) .text("Catch me!") .transitionKey("button") .alignSelf(align) .clickHandler(RootComponent.onClick(c)) ) .build() }

Slide 155

Slide 155 text

@OnCreateLayout fun onCreateLayout( c: ComponentContext, @State alignToEnd: Boolean): Component { val align = if (alignToEnd) FLEX_END else FLEX_START return Column.create(c) .justifyContent(CENTER) .child( Button.create(c) .text("Catch me!") .transitionKey("button") .alignSelf(align) .clickHandler(RootComponent.onClick(c)) ) .build() } @OnCreateTransition fun.onCreateTransition(c: ComponentContext): Transition { return.Transition.create("button") .animate(AnimatedProperties.X) }

Slide 156

Slide 156 text

@OnCreateLayout fun onCreateLayout( c: ComponentContext, @State alignToEnd: Boolean): Component { val align = if (alignToEnd) FLEX_END else FLEX_START return Column.create(c) .justifyContent(CENTER) .child( Button.create(c) .text("Catch me!") .transitionKey("button") .alignSelf(align) .clickHandler(RootComponent.onClick(c)) ) .build() } @OnCreateTransition fun.onCreateTransition(c: ComponentContext): Transition { return.Transition.create("button") .animate(AnimatedProperties.X) }

Slide 157

Slide 157 text

@OnCreateTransition fun.onCreateTransition(c: ComponentContext): Transition { return.Transition.create("button") .animate(AnimatedProperties.X) }

Slide 158

Slide 158 text

@OnCreateTransition fun.onCreateTransition(c: ComponentContext): Transition { return.Transition.allLayout() }

Slide 159

Slide 159 text

ANIMATIONS

Slide 160

Slide 160 text

ANIMATIONS onCreateTransition is called on every onCreateLayout

Slide 161

Slide 161 text

ANIMATIONS onCreateTransition is called on every onCreateLayout Even if you don't need to animate that specific UI update

Slide 162

Slide 162 text

ANIMATIONS onCreateTransition is called on every onCreateLayout Even if you don't need to animate that specific UI update More granular control?

Slide 163

Slide 163 text

@OnCreateTransition fun onCreateTransition(c: ComponentContext): Transition { return Transition.allLayout() }

Slide 164

Slide 164 text

@OnCreateTransition fun onCreateTransition(c: ComponentContext, @Prop prop: Diff, @State state: Diff): Transition? { return if (canAnimate(prop, state)) Transition.allLayout() else null }

Slide 165

Slide 165 text

@OnCreateTransition fun onCreateTransition(c: ComponentContext, @Prop prop: Diff, @State state: Diff): Transition? { return if (canAnimate(prop, state)) Transition.allLayout() else null }

Slide 166

Slide 166 text

@OnCreateTransition fun onCreateTransition(c: ComponentContext, @Prop prop: Diff, @State state: Diff): Transition? { return if (canAnimate(prop, state)) Transition.allLayout() else null } public final class Diff { public T getPrevious() public T getNext() }

Slide 167

Slide 167 text

GOTCHAS

Slide 168

Slide 168 text

GOTCHAS Use Diff for more granular control over transitions

Slide 169

Slide 169 text

GRADUAL ADOPTION

Slide 170

Slide 170 text

GRADUAL ADOPTION

Slide 171

Slide 171 text

GRADUAL ADOPTION Can't migrate the whole screen at once? Do step by step:

Slide 172

Slide 172 text

GRADUAL ADOPTION Can't migrate the whole screen at once? Do step by step: • Complex UI parts can be replaced with a LithoView and underlying Component structure

Slide 173

Slide 173 text

GRADUAL ADOPTION Can't migrate the whole screen at once? Do step by step: • Complex UI parts can be replaced with a LithoView and underlying Component structure • Custom Views can be wrapped in MountSpecs

Slide 174

Slide 174 text

SOMETHING DOESN'T WORK!

Slide 175

Slide 175 text

TOOLS

Slide 176

Slide 176 text

TOOLS Yoga Playground

Slide 177

Slide 177 text

yogalayout.com/playground

Slide 178

Slide 178 text

TOOLS Yoga Playground

Slide 179

Slide 179 text

TOOLS Yoga Playground Flipper + Layout plugin

Slide 180

Slide 180 text

fbflipper.com

Slide 181

Slide 181 text

TOOLS Yoga Playground Flipper + Layout plugin

Slide 182

Slide 182 text

TOOLS Yoga Playground Flipper + Layout plugin Litho IntelliJ Plugin (Beta)

Slide 183

Slide 183 text

No content

Slide 184

Slide 184 text

github.com/facebook/litho/tree/master/litho-intellij-plugin

Slide 185

Slide 185 text

TOOLS Yoga Playground Flipper + Layout plugin Litho IntelliJ Plugin (Beta)

Slide 186

Slide 186 text

TOOLS Yoga Playground Flipper + Layout plugin Litho IntelliJ Plugin (Beta) Lithography Sample app

Slide 187

Slide 187 text

No content

Slide 188

Slide 188 text

WHAT IS LITHO ✦ Asynchronous layout ✦ Declarative API ✦ Flatter view hierarchies ✦ Fine-grained recycling

Slide 189

Slide 189 text

RESOURCES ✦ Official Docs: fblitho.com ✦ Lithography Sample app: github.com/facebook/litho/tree/master/sample ✦ Litho Codelabs (preview): github.com/facebook/litho/tree/master/codelabs ✦ Dive into Litho Threading Model: youtu.be/78BUH1LZaZ4

Slide 190

Slide 190 text

THAT'S ALL FOLKS

Slide 191

Slide 191 text

THAT'S NOT ALL FOLKS

Slide 192

Slide 192 text

THAT'S NOT ALL FOLKS

Slide 193

Slide 193 text

Text(text = "Hello, Kotlin World!", textSize = 20.sp)

Slide 194

Slide 194 text

Column { +Text(text = "Hello, Kotlin World!", textSize = 20.sp) +Text( text = "with ❤ from London", textStyle = Typeface.ITALIC) }

Slide 195

Slide 195 text

Padding(all = 16.dp) { Column { +Text(text = "Hello, Kotlin World!", textSize = 20.sp) +Text( text = "with ❤ from London", textStyle = Typeface.ITALIC) } }

Slide 196

Slide 196 text

Clickable(onClick = { }) { Padding(all = 16.dp) { Column { +Text(text = "Hello, Kotlin World!", textSize = 20.sp) +Text( text = "with ❤ from London", textStyle = Typeface.ITALIC) } } }

Slide 197

Slide 197 text

val counter by useState { 1 } Clickable(onClick = { updateState { counter.value = counter.value + 1 } }) { Padding(all = 16.dp) { Column { +Text(text = "Hello, Kotlin World!", textSize = 20.sp) +Text( text = "with ${"❤".repeat(counter.value)} from London", textStyle = Typeface.ITALIC) } } }

Slide 198

Slide 198 text

setContent { val counter by useState { 1 } Clickable(onClick = { updateState { counter.value = counter.value + 1 } }) { Padding(all = 16.dp) { Column { +Text(text = "Hello, Kotlin World!", textSize = 20.sp) +Text( text = "with ${"❤".repeat(counter.value)} from London", textStyle = Typeface.ITALIC) } } } }

Slide 199

Slide 199 text

class PlaygroundActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { val counter by useState { 1 } Clickable(onClick = { updateState { counter.value = counter.value + 1 } }) { Padding(all = 16.dp) { Column { +Text(text = "Hello, Kotlin World!", textSize = 20.sp) +Text( text = "with ${"❤".repeat(counter.value)} from London", textStyle = Typeface.ITALIC) } } } } } }

Slide 200

Slide 200 text

THANK YOU Sergey Ryabov @colriot

Slide 201

Slide 201 text

Text.create( c, android.R.attr.buttonStyle, 0 ) Text.create( c, 0, R.style.Widget_AppCompat_Button_Colored )