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

The Compose Champ : Understanding Koltin

The Compose Champ : Understanding Koltin

How beneficial is it to learn and understand Kotlin before proceeding to compose? This session tackles some key Kotlin features that compose UI framework uses to achieve a non-bloated API

Brian Odhiambo

June 15, 2023
Tweet

More Decks by Brian Odhiambo

Other Decks in Programming

Transcript

  1. 02 Design Centric Engineer Brian Odhiambo Co-organizer @ KotlinKenya Maintaining

    KotlinBits Mobile Engineer Yeah am all about that UI/UX & Kotlin
  2. How? How can we use Koltin knowledge in compose? Why?

    Why do we need to understand kotlin? Today's Agenda 03 What? What is Kotlin and compose?
  3. A modern pragmatic programming language which is safe, clear and

    concise. A fast, reactive & intuitive UI framework for Kotlin Kotlin Compose
  4. 07 If you're struggling using a programming language, then you're

    not using it right! 🤷🏽‍♂️ 🤷🏽‍♂️ 🤷🏽‍♂️ 🤩 Tip!
  5. 07 If you're struggling using a programming language, then you're

    not using it right! 🤷🏽‍♂️ 🤷🏽‍♂️ 🤷🏽‍♂️ 🤩 Tip! But Why 🤔 ?
  6. If you want to chop down a tree, sharpen the

    axe Then chop down the tree Simple enough right? Sharpen The Axe
  7. If you want to chop down a tree, sharpen the

    axe Then chop down the tree Simple enough right? Sharpen The Axe How does this relate to Kotlin? 🤔 🤔
  8. If you want to chop down a tree, sharpen the

    axe Then chop down the tree Simple enough right? Sharpen The Axe If you want to use compose fully, understand Kotlin Then use compose You shall know Bliss! Understand Kotlin
  9. Functions Composable are just Kotlin functions fun helloWorld() { println("Hello

    World") } @Composable fun HelloWorld() { Text(text ="Hello World") } @Preview @Composable fun HelloWorldPreview() { HelloWorld() }
  10. Default Arguments Default Arguments help in fun sayHello( greeting: String,

    name: String = "World" ) { println("$greeting $name!") } fun main() { sayHello("Jambo") } // Jambo World!
  11. Default Arguments Default Arguments help in @Composable fun Text( text:

    String, modifier: Modifier = Modifier, color: Color = Color.Unspecified, ... ){ ... } @Composable fun HelloWorld() { Text("Jambo World") }
  12. Named Arguments Specify the parameter name for an argument fun

    sayHello( greeting: String, name: String = "World", country: String = "Kenya" ) { println("$greeting $name,") println("Welcome to $country!") } fun main() { sayHello("Jambo",country="South Africa") } // Jambo World, // Welcome to South Africa!
  13. Named Arguments Specify the parameter name for an argument @Composable

    fun Text( text: String, modifier: Modifier = Modifier, color: Color = Color.Unspecified, ... ){ ... } @Composable fun HelloWorld() { Text( "Jambo World", color = Color.White ) }
  14. High Order Functions Pass functions as arguments to other functions

    fun sayHello( greeting: String, name: String = "World", country: String = "Kenya", celebrate: () -> Unit ) { println("$greeting $name,") println("Welcome to $country!") celebrate() } fun main() { sayHello("Jambo",country="South Africa"){ println("🎉🎊🥳") } } // Jambo World, // Welcome to South Africa! // 🎉🎊🥳
  15. High Order Functions Pass functions as arguments to other functions

    @Composable @Composable fun Button( onClick: () -> Unit, modifier: Modifier = Modifier, enabled: Boolean = true, ... ){ ... } @Composable fun HelloWorld() { Button(onClick = { println("Wohoo Soweto!") }) { Text("celebrate") } }
  16. Function literals with receiver Pass functions that have declared types

    data class Market(val name: String){ val list = mutableListOf<String>() fun buy()... fun sell()... fun previewMarket(block: List<String>.() -> Unit){ list.block() } } fun main() { val market = Market(name= "Nairobi") ... market.previewMarket { this: List<String> filter { it.length > 4 } println(this) } }
  17. @Composable inline fun Column( modifier: Modifier = Modifier, ..., content:

    @Composable ColumnScope.() -> Unit ) { ... } @Composable fun HelloWorld() { Column { this: ColumnScope AnimatedVisibility(visible = true ) //... } } Function literals with receiver Pass functions that have declared types @Composable fun ColumnScope.AnimatedVisibility(...) //...
  18. 07 @Composable fun HelloWorld(isTrue: Boolean) { if(isTrue) Text("this") else Text("that")

    } if-else if-else is an expression and returns a value @Composable fun HelloWorld(isTrue: Boolean) { val value = if(isTrue) "this" else "that" Text(value) }
  19. 07 @Composable fun HelloWorld(isTrue: Boolean) { val icon = if(isTrue)

    Icons.Rounded.KeyboardArrowUp else Icons.Rounded.ArrowDropDown val description = if(isTrue) "this" else "that" Icon(imageVector = icon, contentDescription = title) } desctructuring dismantle a value into various variables
  20. 07 @Composable fun HelloWorld(isTrue: Boolean) { val (icon, title) =

    if(isTrue) Pair(Icons.Rounded.KeyboardArrowUp, "this") else Pair(Icons.Rounded.ArrowDropDown, "that") Icon(imageVector = icon, contentDescription = title) } desctructuring dismantle a value into various variables
  21. What are the basics? 🤔 Literals(int, long, float, double, string,

    char), Functions(normal, local) Collections(list, set, array, dictionaries) Flow(conditional, loops, ranges) Classes(normal, object, data, enum) Safety(try-catch, not-null)