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

Jetpack Compose

Jetpack Compose

TakuSemba

May 21, 2019
Tweet

More Decks by TakuSemba

Other Decks in Technology

Transcript

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

    kotlin compiler plugin ɾFully compatible with your existing app/code
  2. What’s Composable ɾA new set of Jetpack UI widgets ɾA

    kotlin compiler plugin ɾFully compatible with your existing app/code ɾExperimental
  3. @Composable fun Greeting(name: String) { Text("Hello $name!") } class ComposeSampleActivity

    : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) } }
  4. @Composable fun Greeting(name: String) { Text("Hello $name!") } class ComposeSampleActivity

    : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { CraneWrapper { Greeting("takusemba") } } } }
  5. @Composable fun Greeting(name: String) { Text("Hello $name!") } class ComposeSampleActivity

    : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { CraneWrapper { Greeting("takusemba") } } } }
  6. @Composable fun Greeting(name: String) { Text("Hello $name!") } class ComposeSampleActivity

    : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { CraneWrapper { Greeting("takusemba") } } } }
  7. @Composable fun Greeting(name: String) { Text("Hello $name!") } class ComposeSampleActivity

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

    super.onCreate(savedInstanceState) setContent { CraneWrapper { MaterialTheme { Greeting("takusemba") } } } } } @Composable fun Greeting(name: String) { Text("Hello $name!") }
  9. @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") } } } } }
  10. @Composable fun <T> ScrollingList( data: List<T>, body: @Composable()(T) -> Unit

    ) { // ... body(item) // ... } @Composable fun NewsFeed(stories: List<Story>) { ScrollingList(stories) { story -> StoryWidget(story) } }
  11. @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) } } }
  12. @Composable fun Greeting(name: String) { val count = +state {

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

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

    0 } Column { Button(text = "greet", onClick = { count.value++ }) Text(text = "$name greeted ${count.value} times") } }
  15. @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") } }
  16. @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") } }
  17. @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-- } }
  18. @Model class LoginState(var username: String, var password: String) { val

    valid: Boolean get() = username.isNotEmpty() && password.isNotEmpty() }
  19. @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() }) }
  20. @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() }) }
  21. @Composable
 @GenerateView fun Greeting(name: String) { Text ("Hello $name!") }

    // In your existing xml <GreetingView app:name="takusemba" />
  22. @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)