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

Jetpack Compose: What does it mean for U and I?

Jetpack Compose: What does it mean for U and I?

Jetpack compose is the first meaningful departure from the original Android UI toolkit. Declarative UI rendering is not something we've ever had from the Android framework. Come take a stroll through the history of UI development on Android and the journey towards Jetpack compose.

Zachary Smith

June 19, 2019
Tweet

More Decks by Zachary Smith

Other Decks in Technology

Transcript

  1. Fragments - The code for a given component is spread

    across several files: - MyFragment.kt - my_fragment.xml - styles.xml
  2. Custom Views/View Groups - They are better for finer grained

    situations - A lot more freedom - There are many things you need to get right
  3. Custom Views/View Groups - Override onMeasure() and onDraw() - XML

    attributes (attrs.xml) - Define style-ables and themes - Code for dealing with layout and touch events.
  4. Custom Views/View Groups - Inheritance Base API - Button extends

    TextView - Every button you create brings along all the behavior of TextView
  5. Deciding Between Implementations Can Be Hard - Fragments vs Custom

    Views - Both have pros and cons, no definitive rules for choosing either - Cross over between abstractions is difficult
  6. Managing State - What is the source of truth? -

    Who owns it? - Who can change it?
  7. Synchronizing Multiple States - Views have optional listeners - Spinner

    -> OnItemSelectedListener - Checkbox -> OnCheckedChangeListener - These listeners notify that user changed a value after the it was changed in the View - Can end up in a loop of updates
  8. Jetpack Compose - Declarative UI framework - Highly composable -

    Stateless - Departure from existing android UI toolkit - Implemented entirely in kotlin
  9. Jetpack Compose @Composable fun UserNameLabel(user: User?) { if (user !=

    null) { Greeting(user.name) } else { Text("User is not available") } }
  10. Jetpack Compose setContent { Padding(padding = 10.dp) { VerticalScroller {

    Column { classmates.forEach { student -> Greeting(name = student.name) } } } } }
  11. Jetpack Compose setContent { Padding(padding = 10.dp) { VerticalScroller {

    Column { classmates.forEach { student -> Greeting(name = student.name) } } } } }
  12. Jetpack Compose setContent { Padding(padding = 10.dp) { VerticalScroller {

    Column { classmates.forEach { student -> Greeting(name = student.name) } } } } }
  13. Jetpack Compose setContent { Padding(padding = 10.dp) { VerticalScroller {

    Column { classmates.forEach { student -> Greeting(name = student.name) } } } } }
  14. Jetpack Compose setContent { Padding(padding = 10.dp) { VerticalScroller {

    Column { classmates.forEach { student -> Greeting(name = student.name) } } } } }
  15. Jetpack Compose setContent { Padding(padding = 10.dp) { VerticalScroller {

    Column { classmates.forEach { student -> Greeting(name = student.name) } } } } }
  16. Jetpack Compose setContent { Padding(padding = 10.dp) { VerticalScroller {

    Column { classmates.forEach { student -> Greeting(name = student.name) } } } } }
  17. Jetpack Compose Mental Models - UI is a pure transformation

    of application state to UI hierarchy - Execute (composable) functions that “emit” UI widgets into the hierarchy (not views) - UI hierarchy is a tree of widgets
  18. Jetpack Compose Mental Models - If state changes, you just

    generate a new tree - Compose handles updating what the user sees to reflect the new tree - No more reconciliation of data and logic via things like textView.setText()
  19. But wait, my performance! - Jetpack has many optimizations (memoization)

    - Regenerating a new tree actually only updates changed nodes, not the whole tree - Tree DSL doesn’t actually instantiate objects, it “emits” UI nodes into the UI tree
  20. How it’s made - Compose UI + Runtime - Compiler

    plugin generates IR code which is transformed to java byte code - Unbundled from the android framework
  21. How it’s made - Compiler plugin allows you to leverage

    the kotlin type system - @Composeable functions can only be called from other @Composeable functions - Compiler plugins allows more flexibility than annotation processors
  22. Java/View Interop - Geared towards kotlin (probably no way to

    write @Composeable function in java) - There will be a way to kick off composition from java
  23. Java/View Interop - Can generate a classic view to be

    used with existing android toolkit - Can use compose tree within existing views - Can put existing views inside compose tree
  24. Migration Story - Bottom up, small, encapsulated, stateful, dynamic subsections

    are best candidates to migrate first - Can host compose widgets from any view - Activities and fragments aren’t going anywhere
  25. Work To be Done - Resources - Scrolling performance improvements

    - Testing - Accessibility - Animations - No Recyclerview - No motion layout - No complex transitions
  26. Benefits of compose - Simpler programming model - Single source

    of truth - Declarative syntax with heavy lifting done by runtime - Composition is a first class principle - Simple to extract reusable components from existing compose functions
  27. How to use it today - Forked version of the

    kotlin compiler - Special version android studio
  28. • Declarative UI Patterns (Google I/O'19) ◦ https://www.youtube.com/watch?v=VsStyq4Lzxo • Android

    Developers Backstage Episode 115: Jetpack Compose ◦ http://androidbackstage.blogspot.com/2019/06/episode-115-jetpack-compose.html • Compose From First Principles ◦ http://intelligiblebabble.com/compose-from-first-principles/ • How to get the source code ◦ https://android.googlesource.com/platform/frameworks/support/+/refs/heads/androidx- master-dev/ui/README.md Resources
  29. Thank you! Zach Smith Twitter: zw33 GitHub: zws33 Yousuf Haque

    Twitter: yousufhaque GitHub: yousuf-haque