Slide 1

Slide 1 text

Invitation for modern Android UI development - Basic of Jetpack Compose - @mochico 2022 April 28 WTM International Women's Day #ProgressNotPerfection

Slide 2

Slide 2 text

Hello! mochico (Miho Mochizuki) @_mochicon_ Joined Mercari in January 2018, Mercari is the 4th company

Slide 3

Slide 3 text

About my career Majored Sociology at University, wanted to be an editor of books Couldn’t find a job of editor, and jumped into the engineering Got a job at a small system development company with no experience, and learn Java Learn Android development as training during times of recession and no work Addicted to app development and looked for the job, finally joined to Mercari Listen more: https://anchor.fm/background-radio/episodes/Background-radio-ep-05-eqm2np

Slide 4

Slide 4 text

Try anything fun! I haven’t expected to be an engineer. But I really enjoying my career. The carrier style called “Planned happened stance”

Slide 5

Slide 5 text

Grow with the platform Addiction of mobile development changed my world. It's been over 10 years since Android was born, and the technology is evolving, and keeping up with it has led to my career.

Slide 6

Slide 6 text

Let’s follow up the trend of the mobile app development!

Slide 7

Slide 7 text

Jetpack Compose

Slide 8

Slide 8 text

Jetpack Compose • Declarative UI toolkit • Defined by Function • Write everything in Kotlin • Update all UI every time the state changes • Redraw only the changes Leave the drawing control to the recompose and declare only what kind of data is displayed

Slide 9

Slide 9 text

(Used to) Imperative UI findViewById(R.id.text_view) .setText(data.hello)

Slide 10

Slide 10 text

Declarative UI Text(text = model.hello) =

Slide 11

Slide 11 text

Recommended structure

Slide 12

Slide 12 text

Jetpack Compose × MVVM

Slide 13

Slide 13 text

MVVM

Slide 14

Slide 14 text

Up side for using Jetpack Compose + MVVM • Android Architecture Component (AAC) Lifecycle- aware components provides ViewModel • AAC ViewModel manages UI-related data in lifecycle conscious • Redraw handling of Jetpack Compose simplify the code for changes of data ※ Proper architecture depends on the project

Slide 15

Slide 15 text

Step by step into Jetpack Compose × MVVM

Slide 16

Slide 16 text

DL Android studio And see the sample code in: https://github.com/mochico/ComposeBasic

Slide 17

Slide 17 text

Select Compose Template

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

Data class data class Counter( val count:Int )

Slide 20

Slide 20 text

Screen State data class MainScreenState( val counter: Counter = Counter(0) )

Slide 21

Slide 21 text

ViewModel open class MainViewModel: ViewModel() { val state = MutableStateFlow(MainScreenState()) fun countUp() { state.update { it.copy( counter = Counter(it.counter.count.inc()) ) } } }

Slide 22

Slide 22 text

Composable @Composable fun MainScreenContent(viewModel: MainViewModel) { val state by viewModel.state.collectAsState() Column( modifier = Modifier .fillMaxSize() .padding(8.dp), horizontalAlignment = Alignment.CenterHorizontally, ) { Text(text = state.counter.count.toString()) TextButton( onClick = viewModel::countUp ) { Text(text = "count up") } } }

Slide 23

Slide 23 text

Activity class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val viewModel: MainViewModel by viewModels() setContent { MainScreenContent(viewModel) } } }

Slide 24

Slide 24 text

Project structure

Slide 25

Slide 25 text

Run app

Slide 26

Slide 26 text

MVVM in practice Activity Composable State Data operation can be added

Slide 27

Slide 27 text

Try easily, Develop fast By using a declarative UI and an architecture based on MVVM, you can create an app that makes each layer simple and easy to maintain

Slide 28

Slide 28 text

Enjoy tech! How is it? Did you find it complicated? Or did you find the responsibilities of each layer clear and easy to develop? Let's enjoy the evolution of technology and continue to grow together.

Slide 29

Slide 29 text

References: • Jetpack Compose | Android Developers https:// developer.android.com/jetpack/compose • Guide to app architecture | Android Developers https://developer.android.com/jetpack/guide • ViewModel Overview | Android Developers https://developer.android.com/topic/libraries/ architecture/viewmodel

Slide 30

Slide 30 text

Appendix: Preview @Preview(showBackground = true) @Composable fun MainScreenPreview() { MainScreenContent( viewModel = MainViewModel() ) } You can run a composable with interaction on Preview See DroidKaigi 2021 - Title : Let's Preview! Jetpack Compose / mochico [JA] - YouTube https://www.youtube.com/watch? v=aSdrXAlMKew&lc=UgxIaaw9PfTQSxOzapt4AaABAg too