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

Workshop on Jetpack compose

Adit Lal
January 03, 2023

Workshop on Jetpack compose

MAD with Jetpack compose workshop slides

Adit Lal

January 03, 2023
Tweet

More Decks by Adit Lal

Other Decks in Programming

Transcript

  1. Android GDE 
 🛠 Individual Consultant 
 🔊 Speaker 


    🌎 Globe Tro tt er 
 🍻 Beer enthusiast 🎯https:/ /androiddev.social/@aditlal 🔗aditlal.dev
  2. Jetpack Compose / jet ·pak kuhm· powz / noun Jetpack

    Compose is a declarative & modern toolkit for building native Android UI. It simpli fi es and accelerates UI development on Android.
  3. Before Compose UI Toolkit is tied to the OS State

    Management is tricky Lots of context switching Simple things still require a lot of code
  4. With Compose UI Toolkit is independent to the OS 🪄

    State Management is built in🥳 Lots of context switching 🎉 So rt ed code logic 🚀
  5. TOPIC TIME(in hr:mins) Thinking in Compose 0:05 Modifiers 0:20 Layouts

    0:30 State management 0:45 SideEffects 1:00 Codelab - idea Today’s Schedule
  6. ✓ Declarative UI is cleaner, more readable, and more performant

    than Imperative UI. ✓ Compose allows you to do more with less code compared to XML. ✓ Compose is Intuitive. This means that you just need to tell Compose what you want to show the user. ✓ Compose is compatible with all your existing code: you can call Compose code from Views and Views from Compose. Also integrated with many Jetpack Libraries. ✓ Compose improves your build time and APK size. Declarative UI Compose
  7. Text ( modifier = Modifier . background (Color.Green), text =

    "Hello World" ) Hello World .padding(10.dp) Modifiers
  8. Text ( modifier = Modifier . border( width = 2.dp,

    color = Color.Green ), text = "Hello World" ) Hello World .padding(10.dp) Modifiers
  9. Text ( modifier = Modifier text = "Hello World" )

    Hello World Order ma tt ers . border( width = 2.dp, color = Color.Green ) .padding(10.dp) .padding(10.dp), Modifiers
  10. Hello World Text ( modifier = Modifier . border( width

    = 2.dp, color = Color.Green ), text = "Hello World" ) .padding(10.dp) Order ma tt ers Modifiers
  11. Hello World Text ( modifier = Modifier .border( width =

    2.dp, color = Color.Green ) .background(Color.Green), text = "Hello World" ) .padding(10.dp) Modifiers
  12. Column { Text ( "Hello Text ( "Hello Text (

    "Hello } World" ) World" ) World" ) Hello World Hello World Hello World Column Column { }
  13. Row

  14. Row { Text ( “Hello" ) Text ( "World" )

    Text ( “!" ) HelloWorld! } Row
  15. Row { Image(…) Column { Text ( “…” ) Text

    ( “…” ) } } Row & Column
  16. Box

  17. Hello World Slot Layouts Text(text = “Home") Scaffold(topBar = {

    { } }) Home Text(text = “Hello World”)
  18. Column { Hello World Hello World } Text( text =

    "Hello World”, modifier = Modifier.background(Color.Green) ) Text( text = "Hello World”, modifier = Modifier.background(Color.Green) ) Layout modifiers
  19. Column ( Modifier .background(Color.Green) .fillMaxWidth() ) { } Hello World

    Hello World Text( text = "Hello World”, modifier = Modifier.background(Color.Green) ) Text( text = "Hello World”, modifier = Modifier.background(Color.Green) ) Layout modifiers
  20. Hello World Hello World Column ( Modifier .background(Color.Green) .fillMaxSize() )

    { } Text( text = "Hello World”, modifier = Modifier.background(Color.Green) ) Text( text = "Hello World”, modifier = Modifier.background(Color.Green) ) Layout modifiers
  21. Hello World Hello World 200dp Column ( Modifier .background(Color.Green) .width(200.dp)

    ) { } Text( text = "Hello World”, modifier = Modifier.background(Color.Green) ) Text( text = "Hello World”, modifier = Modifier.background(Color.Green) ) Layout modifiers
  22. Button ( ) { onClick = { counter ++ }

    0 Text ( text = "+" ) } + - State management
  23. Button ( ) { onClick = { counter -- }

    0 Text ( text = “-" ) } + - State management
  24. • Work outside of composable function • Open new screen

    when tapping bu tt on • Show no network message SideEffects
  25. A side effect of composition that must be reversed or

    cleaned up if the DisposableEffect leaves the composition. It is an error to call DisposableEffect without at least one key parameter. Schedule effect to run when the current composition completes successfully and applies changes. SideEffect can be used to apply side effects to objects managed by the composition that are not backed by snapshots so as not to leave those objects in an inconsistent state if the current composition operation fails. When LaunchedEffect enters the composition it will launch the block into the composition's CoroutineContext. The coroutine will be canceled and re-launched when LaunchedEffect is recomposed with a different key1. The coroutine will be canceled when the LaunchedEffect leaves the composition. LaunchedEffect SideEffect DisposableEffect SideEffects
  26. A side effect of composition that must be reversed or

    cleaned up if the DisposableEffect leaves the composition. It is an error to call DisposableEffect without at least one key parameter. Schedule effect to run when the current composition completes successfully and applies changes. SideEffect can be used to apply side effects to objects managed by the composition that are not backed by snapshots so as not to leave those objects in an inconsistent state if the current composition operation fails. When LaunchedEffect enters the composition it will launch the block into the composition's CoroutineContext. The coroutine will be canceled and re-launched when LaunchedEffect is recomposed with a different key1. The coroutine will be canceled when the LaunchedEffect leaves the composition. LaunchedEffect SideEffect DisposableEffect SideEffects
  27. • Triggers on fi rst composition or key change @Composable

    fun HomeView () { var counter by remember { mutableStateOf ( 0 ) } } LaunchedEffect
  28. • Triggers on fi rst composition or key change @Composable

    fun HomeView () { var counter by remember { mutableStateOf ( 0 ) } } LaunchedEffect { while(true) { counter ++ } } LaunchedEffect
  29. • Triggers on fi rst composition or key change @Composable

    fun HomeView () { var counter by remember { mutableStateOf ( 0 ) } } LaunchedEffect { while(true) { counter ++ } } delay(2000) LaunchedEffect
  30. • Triggers on fi rst composition or key change @Composable

    fun HomeView () { var counter by remember { mutableStateOf ( 0 ) } } LaunchedEffect(key1 = Unit){ while(true) { counter ++ } } delay(2000) LaunchedEffect
  31. Schedule effect to run when the current composition completes successfully

    and applies changes. SideEffect can be used to apply side effects to objects managed by the composition that are not backed by snapshots so as not to leave those objects in an inconsistent state if the current composition operation fails. When LaunchedEffect enters the composition it will launch the block into the composition's CoroutineContext. The coroutine will be canceled and re-launched when LaunchedEffect is recomposed with a different key1. The coroutine will be canceled when the LaunchedEffect leaves the composition. LaunchedEffect SideEffect SideEffects A side effect of composition that must be reversed or cleaned up if the DisposableEffect leaves the composition. It is an error to call DisposableEffect without at least one key parameter. DisposableEffect
  32. • Calls SideE ff ect when we want conditional e

    ff ect @Composable fun SearchBar() { } val focusRequester = remember { FocusRequester() } SideEffect { if(focused && !isSearchDummy) focusRequester.requestFocus() } SideEffect
  33. A side effect of composition that must be reversed or

    cleaned up if the DisposableEffect leaves the composition. It is an error to call DisposableEffect without at least one key parameter. Schedule effect to run when the current composition completes successfully and applies changes. SideEffect can be used to apply side effects to objects managed by the composition that are not backed by snapshots so as not to leave those objects in an inconsistent state if the current composition operation fails. When LaunchedEffect enters the composition it will launch the block into the composition's CoroutineContext. The coroutine will be canceled and re-launched when LaunchedEffect is recomposed with a different key1. The coroutine will be canceled when the LaunchedEffect leaves the composition. LaunchedEffect SideEffect DisposableEffect SideEffects
  34. DisposableEffect • Calls onDispose on terminate @Composable fun HomeView() {

    DisposableEffect (...) { onDispose { callback.remove() } } }
  35. 101

  36. Code class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?)

    { super.onCreate(savedInstanceState) setContent { NotesApplicationTheme { / / A surface container using the 'background' color from the theme Surface( modif i er = Modif i er.f i llMaxSize(), color = MaterialTheme.colors.background ) { Greeting("Android") } } } }
  37. Code class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?)

    { super.onCreate(savedInstanceState) setContent { NotesApplicationTheme { / / A surface container using the 'background' color from the theme Surface( modif i er = Modif i er.f i llMaxSize(), color = MaterialTheme.colors.background ) { Greeting("Android") } } } }
  38. Code class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?)

    { super.onCreate(savedInstanceState) setContent { NotesApplicationTheme { / / A surface container using the 'background' color from the theme Surface( modif i er = Modif i er.f i llMaxSize(), color = MaterialTheme.colors.background ) { Greeting("Android") } } } }
  39. Code class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?)

    { super.onCreate(savedInstanceState) setContent { NotesApplicationTheme { / / A surface container using the 'background' color from the theme Surface( modif i er = Modif i er.f i llMaxSize(), color = MaterialTheme.colors.background ) { Greeting("Android") } } } }
  40. Code class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?)

    { super.onCreate(savedInstanceState) setContent { NotesApplicationTheme { / / A surface container using the 'background' color from the theme Surface( modif i er = Modif i er.f i llMaxSize(), color = MaterialTheme.colors.background ) { Greeting("Android") } } } }
  41. Composable @Composable fun Greeting(name: String) { Text(text = "Hello $name!")

    } @Preview(showBackground = true) @Composable fun DefaultPreview() { NotesApplicationTheme { Greeting("Android") } }
  42. https://github.com/aldefy/Andromeda 
 https://bit.ly/3Nic0JF - Sample catalog app 
 
 Andromeda

    is an open-source Jetpack Compose design system. A collection of guidelines and components can be used to create amazing compose app user experiences. Foundations introduce Andromeda tokens and principles while Components provide the bolts and nuts that make Andromeda Compose wrapped apps tick. Check out