Slide 1

Slide 1 text

First Look Into Android Jetpack Compose

Slide 2

Slide 2 text

SU MYAT @sumyathtun9 Wave Money Wave Money has brought financial inclusion to millions of Burmese through the largest nationwide agent network of Wave Shops and WavePay app.

Slide 3

Slide 3 text

Let’s get into JetPack Compose

Slide 4

Slide 4 text

So what’s is JetPack Compose

Slide 5

Slide 5 text

‣ An unbundled Android UI toolkit designed to simplify UI development ‣ Borrowing the concepts from React, Litho, Vue, Flutter, and more

Slide 6

Slide 6 text

Android UI Toolkit ??

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

The existing UI System Vs. The new Compose Library

Slide 9

Slide 9 text

In 1) Unbundled from Android Platform

Slide 10

Slide 10 text

‣ The existing UI system is platform dependent ‣ Compose library itself is made into a Jetpack component, that means it's a platform independent library

Slide 11

Slide 11 text

In 2) All are Kotlin API

Slide 12

Slide 12 text

‣ Perviously, have to deal with various file (style.xml, animations.xml, and so on) ‣ XML describe the UI layout itself, then use Java/Kotlin for control logic ‣ Now Koltin has made it possible to write a declarative UI in a programming language instead of xml

Slide 13

Slide 13 text

In 3) Composable
 composition over inheritance

Slide 14

Slide 14 text

‣ It’s cumbersome write a custom view in the existing UI System ‣ It’s inheritance (for example, TextView.class has ~30k line of code because inherit View.class ‣ Composable makes everything composable

Slide 15

Slide 15 text

@Composable fun MyText() { Wrap { Padding(30.dp) { Text("Hello World") } } }

Slide 16

Slide 16 text

Changes ‣ TextView to Text() ‣ The android:padding property of TextView becomes Padding wraps Text Advantages ‣Text only responsible for text rendering ‣Padding aslo only handles padding

Slide 17

Slide 17 text

In 4) Destination of Date Flow

Slide 18

Slide 18 text

‣ Previously, a stateful CheckBox when is clicked, it’s state become checked = true, this class updates itself and expose a listener to listen to this state change ‣ Compose Library is one of the design principle 1.the data model can now be fed into the Compose component 2. the compose doesn't change it's own state by itself, but instead, it only exposes the listener, and it's the application's responsibility to update the state

Slide 19

Slide 19 text

In 4) Get More Debugging

Slide 20

Slide 20 text

‣ Built using all of kotlin code ‣ Debugger and breakpoint will work

Slide 21

Slide 21 text

Let’s make it our hands on Dirty :)

Slide 22

Slide 22 text

1) TextView vs Text()

Slide 23

Slide 23 text

@Composable fun Greeting(name: String) { Padding(10.dp) { Text(text = "Hello $name") } } override fun onCreate(savedInstanceState: Bundle?) { setContent { CraneWrapper { MaterialTheme { Greeting() } } } }

Slide 24

Slide 24 text

2) ListView vs Column()

Slide 25

Slide 25 text

@Composable fun SimpleComposable() { Padding(30.dp) { Column(crossAxisAlignment = CrossAxisAlignment.Baseline) { for (i in 1..10) { Text("Count : $i") } } } }

Slide 26

Slide 26 text

@Composable fun Greeting(names: List) { Padding(10.dp) { Column{ if(names.isEmpty()) { Text("No people") }else{ for(name in names){ Text("Hello * $name *") } } } } } Greeting( arrayListOf("Honey","Smith", "John","Brad","Chilly","Kerry","Andrew"))

Slide 27

Slide 27 text

@Model class CounterModel { var counter = +state { 0 } var header = "Counter App" fun add() { counter.value++ } fun subtract() { counter.value-- } } @Composable fun CounterAppComposable(counterModel: CounterModel) { Column { androidx.ui.layout.HeightSpacer(10.dp) CounterHeader(counterModel) androidx.ui.layout.HeightSpacer(10.dp) AddSubtractButtons(counterModel) androidx.ui.layout.HeightSpacer(10.dp) CounterLabel(counterModel) } } @Composable fun CounterHeader(counterModel: CounterModel) { Text(text = counterModel.header,style = +androidx.ui.material.themeTextStyle { h3 }) } @Composable fun AddSubtractButtons(counterModel: CounterModel) { Button( text = "Add", onClick = { counterModel.add() }) androidx.ui.layout.HeightSpacer(10.dp) Button( text = "Subtract", onClick = { counterModel.subtract() }) } @Composable fun CounterLabel(counterModel: CounterModel) { Text(text = "Clicks: ${counterModel.counter.value}",style = +androidx.ui.material.themeTextStyle { h5 }) }

Slide 28

Slide 28 text

3) Recycler View
 how am I feel

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

@Composable fun MyComposeApp() { val dividerColor = Color(0xFFC6C6C6.toInt()) VerticalScroller { Column{ mainPagesEntries.forEachIndexed { index, page -> HeightSpacer(height = 10.dp) Text(page.title) HeightSpacer(height = 10.dp) Button("Click", onClick = { TODO()}) HeightSpacer(height = 10.dp) Divider(color = dividerColor, height = 0.5.dp) } } } } val mainPagesEntries = listOf( Page("AppBarDemo"), Page("TextDemo"), Page(“ButtonDemo”) } class MyAdapter(private val myDataset: Array) : RecyclerView.Adapter() { class MyViewHolder(val textView: TextView) : RecyclerView.ViewHolder(textView) override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyAdapter.MyViewHolder { val textView = LayoutInflater.from(parent.context) .inflate(R.layout.my_tex t_view, parent, false) as TextView return MyViewHolder(textView) } override fun onBindViewHolder(holder: MyViewHolder, position: Int) { holder.textView.text = myDataset[position] } override fun getItemCount() = myDataset.size }

Slide 31

Slide 31 text

Keep Learning …. Declarative UI Patterns (Google I/O'19) Compose From First Principle Diving into Jetpack Compose

Slide 32

Slide 32 text

How to get into … ‣ Installation ‣ Jetpack Compose haven’t publish yet, all dependecies prebuilt. ‣ Download link ‣ Run ./studiow

Slide 33

Slide 33 text

No content