Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Litho на практике

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers. →
MOSDROID
November 10, 2018

Litho на практике

Михаил Розумянский, Joom – MOSDROID #13 Aluminium

Некоторое время назад компания Facebook презентовала библиотеку Litho. С тех пор о ней не так много слышно, примеров использования почти не найти, а разобраться в том, как эта библиотека работает, крайне затруднительно. В этом докладе мы попробуем понять, что побудило Facebook разработать эту библиотеку, какие задачи она решает и каким образом. Но это ещё не всё! Мы также рассмотрим реальные проблемы, с которыми вы, скорее всего, столкнётесь при использовании Litho в реальном проекте, и научимся их обходить.

MOSDROID

November 10, 2018

Transcript

94 slides
  1. Ускоряем Ускоряем TextView TextView Google way Google way Создаём PrecomputedText

    асинхронно Устанавливаем PrecomputedText в TextView 7
  2. Spec для элемента Spec для элемента @LayoutSpec(isPureRender = true) object

    Rule34ItemSpec { @OnCreateLayout fun onCreateLayout( context: ComponentContext, @Prop white: Boolean, @Prop(resType = ResType.STRING) title: CharSequence ): Component { return Text.create(context) .backgroundColor(if (white) Color.WHITE else Color.BLACK) .text(title) .textColor(if (white) Color.BLACK else Color.WHITE) .build() } } 25
  3. Spec для контейнера Spec для контейнера @LayoutSpec(isPureRender = true) object

    Rule34Spec { @OnCreateLayout fun onCreateLayout context: ComponentContext, @Prop showBlack: Boolean ): Component { val builder = Column.create(context) builder.child(createItem(context, "Rule", white = true)) if (showBlack) { builder.child(createItem(context, "¯\\_( ツ)_/¯", white = false)) } builder.child(createItem(context, "34", white = true)) return builder.build() } } 26
  4. createItem() createItem() fun createItem( context: ComponentContext, title: String, white: Boolean

    ): Component.Builder<*> { return Rule34Item.create(context) .title(title) .white(white) } 27
  5. Внутри Внутри Activity Activity private lateinit var lithoView: LithoView override

    fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) context = ComponentContext(this) lithoView = LithoView(context) setContentView(lithoView) updateComponent(showBlack = false) } 28
  6. createComponent() createComponent() fun updateComponent( context: ComponentContext, showBlack: Boolean ): Component

    { val component = Rule34.create(context) .title(title) .showBlack(showBlack) .build() lithoView.setComponentAsync(component) } 29
  7. Внутри Внутри Rule34Spec Rule34Spec @OnCreateTransition fun onCreateTransition( context: ComponentContext, @Prop

    showBlack: Boolean ): Transition { val whiteTransition = Transition.create("white-1", "white-2") .animate(AnimatedProperties.Y) val blackTransition = Transition.create("black") .animate(AnimatedProperties.X) .appearFrom(DimensionValue.widthPercentageOffset(-100f)) .disappearTo(DimensionValue.widthPercentageOffset(-100f)) return if (showBlack) { Transition.sequence(whiteTransition, blackTransition) } else { Transition.sequence(blackTransition, whiteTransition) } } 31
  8. Полезные ссылки Полезные ссылки View performance deep dive Litho: A

    Declarative Framework for Efficient UIs Litho: A Declarative UI Framework for Android Multi-threaded Rendering on Android (with Litho & Infer) Video In News Feed Using Litho https://fblitho.com https://youtu.be/ILtVu2EC6Ec https://developers.facebook.com/FacebookforDevelopers/videos/10154614569978553 https://youtu.be/uzCK4Vnme7o https://youtu.be/YLFusYyehxQ https://www.facebook.com/facebooknyc/videos/10155425215188651 47

More Decks by MOSDROID

Other Decks in Programming