Jetpack Compose

Jetpack Compose

A374f41eab3f73c50d8bab0652cb207a?s=128

TakuSemba

May 21, 2019
Tweet

Transcript

  1. 7.
  2. 8.
  3. 12.

    What’s Composable ɾA new set of Jetpack UI widgets ɾA

    kotlin compiler plugin ɾFully compatible with your existing app/code
  4. 13.

    What’s Composable ɾA new set of Jetpack UI widgets ɾA

    kotlin compiler plugin ɾFully compatible with your existing app/code ɾExperimental
  5. 17.

    @Composable fun Greeting(name: String) { Text("Hello $name!") } class ComposeSampleActivity

    : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) } }
  6. 18.

    @Composable fun Greeting(name: String) { Text("Hello $name!") } class ComposeSampleActivity

    : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { CraneWrapper { Greeting("takusemba") } } } }
  7. 19.

    @Composable fun Greeting(name: String) { Text("Hello $name!") } class ComposeSampleActivity

    : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { CraneWrapper { Greeting("takusemba") } } } }
  8. 20.

    @Composable fun Greeting(name: String) { Text("Hello $name!") } class ComposeSampleActivity

    : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { CraneWrapper { Greeting("takusemba") } } } }
  9. 21.

    @Composable fun Greeting(name: String) { Text("Hello $name!") } class ComposeSampleActivity

    : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { CraneWrapper { Greeting("takusemba") } } } }
  10. 22.

    class ComposeSampleActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) {

    super.onCreate(savedInstanceState) setContent { CraneWrapper { MaterialTheme { Greeting("takusemba") } } } } } @Composable fun Greeting(name: String) { Text("Hello $name!") }
  11. 23.

    @Composable fun Greeting(name: String) { Text(text = "Hello $name!", style

    = +themeTextStyle { h5 }) } class ComposeSampleActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { CraneWrapper { MaterialTheme { Greeting("takusemba") } } } } }
  12. 25.

    @Composable fun <T> ScrollingList( data: List<T>, body: @Composable()(T) -> Unit

    ) { // ... body(item) // ... } @Composable fun NewsFeed(stories: List<Story>) { ScrollingList(stories) { story -> StoryWidget(story) } }
  13. 26.

    @Composable fun <T> ScrollingList( data: List<T>, body: @Composable()(T) -> Unit

    ) { // ... body(item) // ... } @Composable fun MenuList(items: List<Item>) { ScrollingList(items) { item -> when (item) { is FoodItem -> FoodWidget(item) is DrinkItem -> DrinkWidget(item) } } }
  14. 27.
  15. 29.

    @Composable fun Greeting(name: String) { val count = +state {

    0 } Column { Button(text = "greet", onClick = { count.value++ }) Text(text = "$name greeted ${count.value} times") } }
  16. 30.

    @Composable fun Greeting(name: String) { val count = +state {

    0 } Column { Button(text = "greet", onClick = { count.value++ }) Text(text = "$name greeted ${count.value} times") } }
  17. 31.

    @Composable fun Greeting(name: String) { val count = +state {

    0 } Column { Button(text = "greet", onClick = { count.value++ }) Text(text = "$name greeted ${count.value} times") } }
  18. 32.

    @Model class GreetCounter(var count: Int) { fun add() { count++

    } fun substract() { count-- } } @Composable fun Greeting(name: String) { val count = +state { 0 } Column { Button(text = "greet", onClick = { count.value++ }) Text(text = "$name greeted ${count.value} times") } }
  19. 33.

    @Model class GreetCounter(var count: Int) { fun add() { count++

    } fun substract() { count-- } } @Composable fun Greeting(name: String) { val counter = +memo { GreetCounter(0) } Column { Button(text = "greet", onClick = { count.add() }) Text(text = "$name greeted ${count.count} times") } }
  20. 34.

    @Composable fun Greeting(name: String) { val counter = +memo {

    GreetCounter(0) } Column { Button(text = "greet", onClick = { count.add() }) Text(text = "$name greeted ${count.count} times") } } @Model class GreetCounter(var count: Int) { fun add() { count++ } fun substract() { count-- } }
  21. 35.

    @Model class LoginState(var username: String, var password: String) { val

    valid: Boolean get() = username.isNotEmpty() && password.isNotEmpty() }
  22. 36.

    @Model class LoginState(var username: String, var password: String) { val

    valid: Boolean get() = username.isNotEmpty() && password.isNotEmpty() } @Composable fun LoginScreen(onClick: () -> Unit) { val model = +memo { LoginState() } EditText( text=model.username, onTextChange={ text -> model.username = text } ) EditText( text=model.password, onTextChange={ text -> model.password = text } ) Button(text="Login", enabled=model.valid, onClick={ onClick() }) }
  23. 37.

    @Model class LoginState(var username: String, var password: String) { val

    valid: Boolean get() = username.isNotEmpty() && password.isNotEmpty() } @Composable fun LoginScreen(onClick: () -> Unit) { val model = +memo { LoginState() } EditText( text=model.username, onTextChange={ text -> model.username = text } ) EditText( text=model.password, onTextChange={ text -> model.password = text } ) Button(text="Login", enabled=model.valid, onClick={ onClick() }) }
  24. 41.

    @Composable
 @GenerateView fun Greeting(name: String) { Text ("Hello $name!") }

    // In your existing xml <GreetingView app:name="takusemba" />
  25. 42.

    @Composable
 @GenerateView fun Greeting(name: String) { Text ("Hello $name!") }

    // In your existing xml <GreetingView app:name="takusemba" /> // find view by Id val greeting = findViewById(R.id.greeting)
  26. 44.