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

Mental Modles of Jetpack Compose

Louis Tsai
September 25, 2019

Mental Modles of Jetpack Compose

With the raise of declarative UI, Jetpack Compose just might be the future of creating UI for Android. While the API is far from stable, there are already a lot of things that we can learn from other UI framework over the years, like React, Vue, and more recently, Flutter and SwiftUI.

Join me to learn some fundamental concepts (mental models) of this new way of building UI, and hopefully, when the time comes, you will be able to write idiomatic declarative UI with Jetpack Compose!

Louis Tsai

September 25, 2019
Tweet

More Decks by Louis Tsai

Other Decks in Technology

Transcript

  1. 1. Mental Model 2. Programming Paradigm 3. Inheritance 4. Composition

    5. Reactive Declarative UI 6. Component 7. Functional component 8. Hindsight @louis993546
  2. A mental model is an explanation of someone's thought process

    about how something works in the real world. -- Wikipedia @louis993546
  3. val linearLayout = LinearLayout(context).apply { orientation = LinearLayout.VERTICAL } linearLayout.addView(

    TextView(context).apply { text = "Hello, World" } ) linearLayout.addView( ImageView(context).apply { imageResource = R.drawable.star } ) @louis993546
  4. val linearLayout = LinearLayout(context).apply { orientation = LinearLayout.VERTICAL } //

    a statement (instruction) linearLayout.addView( TextView(context).apply { text = "Hello, World" } ) // another statement (instruction) linearLayout.addView( ImageView(context).apply { imageResource = R.drawable.star } ) // yet another statement (instruction) @louis993546
  5. Imperative programming val linearLayout = LinearLayout(context).apply { orientation = LinearLayout.VERTICAL

    } // a statement (instruction) linearLayout.addView( TextView(context).apply { text = "Hello, World" } ) // another statement (instruction) linearLayout.addView( ImageView(context).apply { imageResource = R.drawable.star } ) // yet another statement (instruction) @louis993546
  6. <!-- beginning of definition --> <LinearLayout orientation="vertical"> <TextView android:text="Hello, World"/>

    <ImageView android:src="@drawable/star"/> </LinearLayout> <!-- end of definition --> @louis993546
  7. Declarative programming <!-- beginning of definition --> <LinearLayout orientation="vertical"> <TextView

    android:text="Hello, World"/> <ImageView android:src="@drawable/star"/> </LinearLayout> <!-- end of definition --> @louis993546
  8. Commonalities Breath Walk Work Take over the world Blep Blop

    ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ @louis993546
  9. Commonalities Breath Walk Work Take over the world Blep Blop

    ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ Animal @louis993546
  10. Commonalities Breath Walk Work Take over the world Blep Blop

    Blup ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ @louis993546
  11. Commonalities Breath Walk Work Take over the world Blep Blop

    Blup ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ @louis993546
  12. Commonalities Breath Walk Work Take over the world Blep Blop

    Blup ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ @louis993546
  13. class : Breathable, Walkable, Workable, Blopable class : Breathable, Walkable,

    TakeOverTheWorldable, Blepable class : Walkable, Workable, TakeOverTheWorldable, Blupable @louis993546
  14. class : Breathable, Walkable, Workable, Blopable class : Breathable, Walkable,

    TakeOverTheWorldable, Blepable class : Walkable, Workable, TakeOverTheWorldable, Blupable class : Breathable, Blupable @louis993546
  15. <layout> <data> <variable name="user" type="com.example.User"/> </data> <LinearLayout android:orientation="vertical"> <TextView android:text="@{user.firstName}"/>

    <View android:visibility="@{user.age > 18 ? View.GONE : View.VISIBLE}" /> </LinearLayout> </layout> @louis993546
  16. S A B C D F ? XML Data Binding

    ? ? ? @louis993546
  17. // a JavaScript file class HelloWorld extends React.Component { render()

    { // ↓ HTML ↓ JS ↓ Back to HTML return <h1>Hello, {this.props.target}!</h1> } } @louis993546
  18. sealed class ViewState { data class Success(val data: List<User>) :

    ViewState() data class Error(val error: Throwable) : ViewState() } viewStateLiveData.observe(this) { viewState -> when (viewState) { is Success -> { ... } is Error -> { ... } } } @louis993546
  19. class HelloWorld extends React.Component { render() { return ( <h1>Hello,

    {this.props.target}!</h1> ) } } class HelloWorld extends React.Component { constructor(props) { super(props); this.state = {target: "World"} } componentDidMount() { console.log('mounting......') } componentWillUnmount() { /* ... */ } render() { return ( <h1>Hello, {this.state.target}!</h1> ) } } @louis993546
  20. 1. Notify someone when click 2. Have a background 3.

    Display Text 1. Notify someone when click 2. Have a background 3. Display Image @louis993546
  21. 1. Notify someone when click 2. Have a background 3.

    Display Text 1. Notify someone when click 2. Have a background 3. Display Image @louis993546
  22. //... FloatingActionButton( onPressed: () { // Add your onPressed code

    here! }, child: Icon(Icons.navigation), backgroundColor: Colors.green, ), //... @louis993546
  23. //... FloatingActionButton( onPressed: () { // Add your onPressed code

    here! }, child: Text('¯\_(ツ)_/¯'), backgroundColor: Colors.green, ), //... @louis993546
  24. class HelloWorld extends React.Component { render() { if (this.props.isFormal) {

    return /* 2000 lines of very complex layout #1 */ } return /* 2000 lines very complex layout #2 */ } } @louis993546
  25. class FormalHelloWorld extends React.Component { render() { return /* 2000

    lines of very complex layout #1 */ } } class InformalHelloWorld extends React.Component { render() { return /* 2000 lines of very complex layout #2 */ } } @louis993546
  26. Why now? • Better IDE (Autocomplete, linter, etc.) • Better

    performance -> Better developer ergonomics • Better languages (type inference, lambda/closure, etc.) ◦ JavaScript ◦ TypeScript/Flow ◦ Dart ◦ Swift ◦ Kotlin ◦ (Java 10+) • etc. @louis993546
  27. @louis993546 S A B C D F Flutter XML Data

    Binding React Compose SwiftUI
  28. class HelloWorld extends React.Component { constructor(props) { super(props); this.state =

    {target: "World"} } componentDidMount() { console.log('mounting......') } componentWillUnmount() { /* ... */ } render() { return ( <h1>Hello, {this.state.target}!</h1> ) } } @louis993546
  29. class HelloWorld extends React.Component { constructor(props) { super(props); this.state =

    {target: "World"} } componentDidMount() { console.log('mounting......') } componentWillUnmount() { /* ... */ } render() { return ( <h1>Hello, {this.state.target}!</h1> ) } } @louis993546
  30. function HelloWorld(props) { const [target, setTarget] = useState("World") useEffect(() =>

    log()) return <h1>Hello, {target}!</h1> } function log() { console.log('mounting......') } @louis993546 class HelloWorld extends React.Component { constructor(props) { super(props); this.state = {target: "World"} } componentDidMount() { console.log('mounting......') } componentWillUnmount() { /* ... */ } render() { return ( <h1>Hello, {this.state.target}!</h1> ) } } →
  31. function HelloWorld(props) { const [target, setTarget] = useState("World") useEffect(() =>

    log()) return <h1>Hello, {target}!</h1> } function log() { console.log('mounting......') } function HelloWorld2(props) { // ... log() // ... } @louis993546 class HelloWorld extends React.Component { constructor(props) { super(props); this.state = {target: "World"} } componentDidMount() { console.log('mounting......') } componentWillUnmount() { /* ... */ } render() { return ( <h1>Hello, {this.state.target}!</h1> ) } } →
  32. perception of the nature of an event after it has

    happened -- Merriam-Webster @louis993546
  33. “Read later” • ${React.js stuff} ◦ React hooks • ${Flutter

    stuff} ◦ Material Components widgets • Architecture-stuff ◦ “Events & Data” ◦ MVI, BLoC, etc. ◦ Redux, MobX, etc. • Kotlin extension properties • Kotlin IR • Kotlin compiler plugin • How suspend function compiles • Virtual DOM @louis993546
  34. “Read later” • ${React.js stuff} ◦ React hooks • ${Flutter

    stuff} ◦ Material Components widgets • Architecture-stuff ◦ “Events & Data” ◦ MVI, BLoC, etc. ◦ Redux, MobX, etc. • Kotlin extension properties • Kotlin IR • Kotlin compiler plugin • How suspend function compiles • Virtual DOM @louis993546 https://bit.ly/2kVVO93