Slide 1

Slide 1 text

@GerardPaligot DISCOVERING JETPACK COMPOSE

Slide 2

Slide 2 text

WHAT’S JETPACK COMPOSE? 100% Kotlin Compiler Open Source Declarative UI toolkit Android Team

Slide 3

Slide 3 text

THINK DIFFERENT.

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

MovieMetadata Rating TopAppBar Poster PosterNoted MovieItem

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

App MovieHome Home Section Home Section Home Section Data Events

Slide 12

Slide 12 text

App MovieHome Home Section Home Section Home Section Data Events

Slide 13

Slide 13 text

PLACING ELEMENTS

Slide 14

Slide 14 text

@Composable fun MovieMetadata( title: String, genres: List, releaseDate: String, runtime: Int ) { }

Slide 15

Slide 15 text

@Composable fun MovieMetadata( title: String, genres: List, releaseDate: String, runtime: Int ) { Text( text = title ) }

Slide 16

Slide 16 text

@Composable fun MovieMetadata( title: String, genres: List, releaseDate: String, runtime: Int ) { Text( text = title, style = MaterialTheme.typography.h6 ) }

Slide 17

Slide 17 text

@Composable fun MovieMetadata( title: String, genres: List, releaseDate: String, runtime: Int ) { Text( text = title, style = MaterialTheme.typography.h6, maxLines = 2, overflow = TextOverflow.Ellipsis ) }

Slide 18

Slide 18 text

@Composable fun MovieMetadata( title: String, genres: List, releaseDate: String, runtime: Int ) { Column { Text( text = title, style = MaterialTheme.typography.h6, maxLines = 2, overflow = TextOverflow.Ellipsis ) } }

Slide 19

Slide 19 text

@Composable fun MovieMetadata( title: String, genres: List, releaseDate: String, runtime: Int ) { Column { Text(...) } }

Slide 20

Slide 20 text

@Composable fun MovieMetadata( title: String, genres: List, releaseDate: String, runtime: Int ) { Column { Text(...) genres.forEach { Tag(text = it) } } }

Slide 21

Slide 21 text

@Composable fun MovieMetadata( title: String, genres: List, releaseDate: String, runtime: Int ) { Column { Text(...) Row { genres.forEach { Tag(text = it) } } } }

Slide 22

Slide 22 text

@Composable fun MovieMetadata( title: String, genres: List, releaseDate: String, runtime: Int ) { Column { Text(...) Row(modifier = Modifier.padding(top = 5.dp)) { genres.forEach { Tag(text = it) } } } }

Slide 23

Slide 23 text

@Composable fun MovieMetadata( title: String, genres: List, releaseDate: String, runtime: Int ) { Column { Text(...) Row(modifier = Modifier.padding(top = 5.dp)) { ... } } }

Slide 24

Slide 24 text

@Composable fun MovieMetadata( title: String, genres: List, releaseDate: String, runtime: Int ) { Column { Text(...) Row(modifier = Modifier.padding(top = 5.dp)) { ... } val year = releaseDate.getCalendar().get(Calendar.YEAR) val time = "${runtime / 60}h ${runtime % 60}min" Text( text ="$year - $time", modifier = Modifier.padding(top = 5.dp) ) } }

Slide 25

Slide 25 text

@Composable fun MovieMetadata( title: String, genres: List, releaseDate: String, runtime: Int ) { Column { Text(...) Row(modifier = Modifier.padding(top = 5.dp)) { ... } val year = releaseDate.getCalendar().get(Calendar.YEAR) val time = "${runtime / 60}h ${runtime % 60}min" Text( text = if (runtime != 0) "$year - $time" else "$year", modifier = Modifier.padding(top = 5.dp) ) } }

Slide 26

Slide 26 text

@Composable fun MovieMetadata( title: String, genres: List, releaseDate: String, runtime: Int ) { Column { Text(...) Row(modifier = Modifier.padding(top = 5.dp)) { ... } CompositionLocalProvider( LocalContentAlpha provides ContentAlpha.medium ) { val year = releaseDate.getCalendar().get(Calendar.YEAR) val time = "${runtime / 60}h ${runtime % 60}min" Text( text = if (runtime != 0) "$year - $time" else "$year", modifier = Modifier.padding(top = 5.dp) ) } } }

Slide 27

Slide 27 text

@Composable fun MovieMetadata( title: String, genres: List, releaseDate: String, runtime: Int ) { Column { Text(...) Row(modifier = Modifier.padding(top = 5.dp)) { ... } CompositionLocalProvider( LocalContentAlpha provides ContentAlpha.medium ) { ... } } }

Slide 28

Slide 28 text

@Composable fun MovieMetadata( title: String, genres: List, releaseDate: String, runtime: Int, modifier: Modifier = Modifier ) { Column(modifier = modifier) { Text(...) Row(modifier = Modifier.padding(top = 5.dp)) { ... } CompositionLocalProvider( LocalContentAlpha provides ContentAlpha.medium ) { ... } } }

Slide 29

Slide 29 text

LET’S CODE A LIST

Slide 30

Slide 30 text

@Composable fun PosterNoted( posterUrl: String, voteAverage: Int, ratingSize: Dp, ratingAlignment: Alignment, onClick: () -> Unit = {} )

Slide 31

Slide 31 text

@Composable fun MovieItem( movie: Movie, modifier: Modifier = Modifier, onClick: (movie: Movie) -> Unit ) { }

Slide 32

Slide 32 text

@Composable fun MovieItem( movie: Movie, modifier: Modifier = Modifier, onClick: (movie: Movie) -> Unit ) { PosterNoted( posterUrl = movie.pictureUrl, voteAverage = movie.percentage, ratingSize = 50.dp, ratingAlignment = Alignment.TopEnd ) }

Slide 33

Slide 33 text

@Composable fun MovieItem( movie: Movie, modifier: Modifier = Modifier, onClick: (movie: Movie) -> Unit ) { Box( modifier = Modifier .width(130.dp) .aspectRatio(0.7f) ) { PosterNoted( posterUrl = movie.pictureUrl, voteAverage = movie.percentage, ratingSize = 50.dp, ratingAlignment = Alignment.TopEnd ) } }

Slide 34

Slide 34 text

@Composable fun MovieItem( movie: Movie, modifier: Modifier = Modifier, onClick: (movie: Movie) -> Unit ) { Box( modifier = Modifier .width(130.dp) .aspectRatio(0.7f) ) { … } }

Slide 35

Slide 35 text

@Composable fun MovieItem( movie: Movie, modifier: Modifier = Modifier, onClick: (movie: Movie) -> Unit ) { Box(modifier = modifier) { Box( modifier = Modifier .width(130.dp) .aspectRatio(0.7f) ) { ... } } }

Slide 36

Slide 36 text

@Composable fun MovieItem( movie: Movie, modifier: Modifier = Modifier, onClick: (movie: Movie) -> Unit ) { val posterWidth = 130 val height = (posterWidth / 0.7f).roundToInt() Box(modifier = modifier) { Surface( modifier = Modifier .fillMaxWidth() .preferredHeight((height - 30).dp) ) { } Box( modifier = Modifier .width(posterWidth.dp) .aspectRatio(0.7f) ) { ... } } }

Slide 37

Slide 37 text

@Composable fun MovieItem( movie: Movie, modifier: Modifier = Modifier, onClick: (movie: Movie) -> Unit ) { val posterWidth = 130 val height = (posterWidth / 0.7f).roundToInt() Box(modifier = modifier) { Surface( modifier = Modifier .padding(top = 60.dp) .fillMaxWidth() .preferredHeight((height - 30).dp) ) { } Box( modifier = Modifier .width(posterWidth.dp) .aspectRatio(0.7f) ) { ... } } }

Slide 38

Slide 38 text

@Composable fun MovieItem( movie: Movie, modifier: Modifier = Modifier, onClick: (movie: Movie) -> Unit ) { val posterWidth = 130 val height = (posterWidth / 0.7f).roundToInt() Box(modifier = modifier) { Surface( modifier = Modifier .padding(top = 60.dp) .fillMaxWidth() .preferredHeight((height - 30).dp) .clickable(onClick = { onClick(movie) }) ) { } Box( modifier = Modifier .width(posterWidth.dp) .aspectRatio(0.7f) ) { ... } } }

Slide 39

Slide 39 text

@Composable fun MovieItem( movie: Movie, modifier: Modifier = Modifier, onClick: (movie: Movie) -> Unit ) { val posterWidth = 130 val height = (posterWidth / 0.7f).roundToInt() Box(modifier = modifier) { Surface( modifier = Modifier .padding(top = 60.dp) .fillMaxWidth() .preferredHeight((height - 30).dp) .clickable(onClick = { onClick(movie) }), shape = RoundedCornerShape(8.dp) ) { } Box( modifier = Modifier .width(posterWidth.dp) .aspectRatio(0.7f) ) { ... } } }

Slide 40

Slide 40 text

@Composable fun MovieItem( movie: Movie, modifier: Modifier = Modifier, onClick: (movie: Movie) -> Unit ) { val posterWidth = 130 val height = (posterWidth / 0.7f).roundToInt() Box(modifier = modifier) { Surface( modifier = Modifier .padding(top = 60.dp) .fillMaxWidth() .preferredHeight((height - 30).dp) .clickable(onClick = { onClick(movie) }), shape = RoundedCornerShape(8.dp), elevation = 5.dp ) { } Box( modifier = Modifier .width(posterWidth.dp) .aspectRatio(0.7f) ) { ... } } }

Slide 41

Slide 41 text

val posterWidth = 130 val height = (posterWidth / 0.7f).roundToInt() Box(modifier = modifier) { Surface( modifier = Modifier .padding(top = 60.dp) .fillMaxWidth() .preferredHeight((height - 30).dp) .clickable(onClick = { onClick(movie) }), shape = RoundedCornerShape(8.dp), elevation = 5.dp ) { } Box( modifier = Modifier .width(posterWidth.dp) .aspectRatio(0.7f) ) { ... } } }

Slide 42

Slide 42 text

val posterWidth = 130 val height = (posterWidth / 0.7f).roundToInt() Box(modifier = modifier) { Surface( modifier = Modifier .padding(top = 60.dp) .fillMaxWidth() .preferredHeight((height - 30).dp) .clickable(onClick = { onClick(movie) }), shape = RoundedCornerShape(8.dp), elevation = 5.dp ) { } Box( modifier = Modifier .padding(start = 30.dp) .width(posterWidth.dp) .aspectRatio(0.7f) ) { ... } } }

Slide 43

Slide 43 text

val posterWidth = 130 val height = (posterWidth / 0.7f).roundToInt() Box(modifier = modifier) { Surface( modifier = Modifier .padding(top = 60.dp) .fillMaxWidth() .preferredHeight((height - 30).dp) .clickable(onClick = { onClick(movie) }), shape = RoundedCornerShape(8.dp), elevation = 5.dp ) { MovieMetadata( title = movie.title, genres = movie.genres, releaseDate = movie.releaseDate, runtime = movie.runtime, modifier = Modifier.padding(start = (posterWidth + 15).dp) .align(alignment = Alignment.CenterStart) ) } Box( modifier = Modifier .padding(start = 30.dp) .width(posterWidth.dp) .aspectRatio(0.7f) ) { ... } }

Slide 44

Slide 44 text

@Composable fun MovieList( title: String, movies: List, onClick: (movie: Movie) -> Unit ) { }

Slide 45

Slide 45 text

@Composable fun MovieList( title: String, movies: List, onClick: (movie: Movie) -> Unit ) { val state = rememberScrollState() Column(modifier = Modifier.verticalScroll(state = state)) { movies.forEach { MovieItem( movie = it, modifier = Modifier .padding(start = 10.dp, top = 10.dp, end = 10.dp), onClick = onClick ) } } }

Slide 46

Slide 46 text

@Composable fun MovieList( title: String, movies: List, onClick: (movie: Movie) -> Unit ) { LazyColumn { items(movies) { movie -> MovieItem( movie = movie, modifier = Modifier .padding(start = 10.dp, top = 10.dp, end = 10.dp), onClick = onClick ) } } }

Slide 47

Slide 47 text

@Composable fun MovieList( title: String, movies: List, onClick: (movie: Movie) -> Unit ) { MovieScaffold(title = title) { LazyColumn { items(movies) { movie -> MovieItem( movie = it, modifier = Modifier .padding(start = 10.dp, top = 10.dp, end = 10.dp), onClick = onClick ) } } } }

Slide 48

Slide 48 text

@Composable fun Scaffold( modifier: Modifier = Modifier, scaffoldState: ScaffoldState = rememberScaffoldState(), topBar: @Composable () -> Unit = emptyContent(), bottomBar: @Composable () -> Unit = emptyContent(), snackbarHost: @Composable (SnackbarHostState) -> Unit = { SnackbarHost(it) }, floatingActionButton: @Composable () -> Unit = emptyContent(), floatingActionButtonPosition: FabPosition = FabPosition.End, isFloatingActionButtonDocked: Boolean = false, drawerContent: @Composable (ColumnScope.() -> Unit)? = null, drawerGesturesEnabled: Boolean = true, drawerShape: Shape = MaterialTheme.shapes.large, drawerElevation: Dp = DrawerConstants.DefaultElevation, drawerBackgroundColor: Color = MaterialTheme.colors.surface, drawerContentColor: Color = contentColorFor(drawerBackgroundColor), drawerScrimColor: Color = DrawerConstants.defaultScrimColor, backgroundColor: Color = MaterialTheme.colors.background, contentColor: Color = contentColorFor(backgroundColor), bodyContent: @Composable (PaddingValues) -> Unit )

Slide 49

Slide 49 text

LET’S CODE A LIST WITH ACTUAL FRAMEWORK UI

Slide 50

Slide 50 text

Slide 51

Slide 51 text

Slide 52

Slide 52 text

Slide 53

Slide 53 text

Slide 54

Slide 54 text

data class Activity( @DrawableRes val picture: Int, val displayName: String, val type: String, val price: Int ) class ActivityListAdapter(val items: MutableList) : RecyclerView.Adapter() { val onClick: LiveData get() = this._onClick private val _onClick: MutableLiveData by lazy { return@lazy MutableLiveData() }

Slide 55

Slide 55 text

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ActivityViewHolder = ActivityViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_activity, parent, false)) override fun getItemCount(): Int = items.size override fun onBindViewHolder(holder: ActivityViewHolder, position: Int) { val activity = items[position] holder.bind(activity, View.OnClickListener { _onClick.value = activity }) } fun update(items: List) { this.items.clear() this.items.addAll(items) notifyDataSetChanged() }

Slide 56

Slide 56 text

class ActivityViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { fun bind(activity: Activity, onClickListener: View.OnClickListener) { itemView.setOnClickListener(onClickListener) itemView.picture.setImageDrawable(ContextCompat.getDrawable(itemView.context, activity.picture)) itemView.name.text = activity.displayName itemView.type.text = activity.type val color = if (activity.price > 0) ContextCompat.getColor(itemView.context, R.color.green) else ContextCompat.getColor(itemView.context, R.color.red) itemView.price.setTextColor(color) } } }

Slide 57

Slide 57 text

LIGHT DARK

Slide 58

Slide 58 text

val orange800 = Color(255, 150, 29) val orange900 = Color(255, 118, 26) val blue400 = Color(26, 163, 255) private val LightColorPalette = lightColors( primary = orange900, primaryVariant = orange800, secondary = blue400, background = Color.White, surface = Color.White, onPrimary = Color.White, onSecondary = Color.Black, onBackground = Color.Black, onSurface = Color.Black, )

Slide 59

Slide 59 text

val orange400 = Color(255, 205, 56) val blue800 = Color(0, 98, 203) val blue900 = Color(0, 68, 171) val grayDark = Color(45, 47, 49) private val DarkColorPalette = darkColors( primary = blue900, primaryVariant = blue800, secondary = orange400, background = grayDark, surface = grayDark, onPrimary = Color.White, onSecondary = Color.Black, onBackground = Color.White, onSurface = Color.White )

Slide 60

Slide 60 text

val typography = Typography( h5 = TextStyle( fontFamily = FontFamily.Default, fontWeight = FontWeight.W700, fontSize = 24.sp ), subtitle1 = TextStyle( fontFamily = FontFamily.Default, fontWeight = FontWeight.W700, fontSize = 16.sp ), body1 = TextStyle( fontFamily = FontFamily.Default, fontWeight = FontWeight.Normal, fontSize = 16.sp ), caption = TextStyle( fontFamily = FontFamily.Default, fontWeight = FontWeight.W400, fontSize = 10.sp ) )

Slide 61

Slide 61 text

val shapes = Shapes( small = RoundedCornerShape(2.dp), medium = RoundedCornerShape(4.dp), large = RoundedCornerShape(8.dp) )

Slide 62

Slide 62 text

@Composable fun MaterialTheme( colors: Colors = MaterialTheme.colors, typography: Typography = MaterialTheme.typography, shapes: Shapes = MaterialTheme.shapes, content: @Composable () -> Unit )

Slide 63

Slide 63 text

@Composable fun ExploringMoviesTheme( isDarkMode: Boolean = isSystemInDarkTheme(), content: @Composable() () -> Unit ) { }

Slide 64

Slide 64 text

@Composable fun ExploringMoviesTheme( isDarkMode: Boolean = isSystemInDarkTheme(), content: @Composable() () -> Unit ) { MaterialTheme( content = content ) }

Slide 65

Slide 65 text

@Composable fun ExploringMoviesTheme( isDarkMode: Boolean = isSystemInDarkTheme(), content: @Composable() () -> Unit ) { MaterialTheme( colors = if (isDarkMode) DarkColorPalette else LightColorPalette, content = content ) }

Slide 66

Slide 66 text

@Composable fun ExploringMoviesTheme( isDarkMode: Boolean = isSystemInDarkTheme(), content: @Composable() () -> Unit ) { MaterialTheme( colors = if (isDarkMode) DarkColorPalette else LightColorPalette, typography = typography, content = content ) }

Slide 67

Slide 67 text

@Composable fun ExploringMoviesTheme( isDarkMode: Boolean = isSystemInDarkTheme(), content: @Composable() () -> Unit ) { MaterialTheme( colors = if (isDarkMode) DarkColorPalette else LightColorPalette, typography = typography, shapes = shapes, content = content ) }

Slide 68

Slide 68 text

@Composable fun MovieList( title: String, movies: List, onClick: (movie: Movie) -> Unit ) { MovieScaffold( title = title ){ LazyColumnFor(items = movies) { MovieItem( movie = it, modifier = Modifier .padding(start = 10.dp, top = 10.dp, end = 10.dp), onClick = onClick ) } } }

Slide 69

Slide 69 text

@Composable fun MovieList( title: String, movies: List, isDarkModeActive: Boolean, switchDarkMode: () -> Unit = {}, onClick: (movie: Movie) -> Unit ) { MovieScaffold( title = title, isDarkModeActive = isDarkModeActive, switchDarkMode = switchDarkMode ){ LazyColumnFor(items = movies) { MovieItem( movie = it, modifier = Modifier .padding(start = 10.dp, top = 10.dp, end = 10.dp), onClick = onClick ) } } }

Slide 70

Slide 70 text

setContent { val isSystemDark = isSystemInDarkTheme() }

Slide 71

Slide 71 text

setContent { val isSystemDark = isSystemInDarkTheme() val isDarkModeState = remember { mutableStateOf(isSystemDark) } }

Slide 72

Slide 72 text

setContent { val isSystemDark = isSystemInDarkTheme() val isDarkModeState = remember { mutableStateOf(isSystemDark) } ExploringMoviesTheme(isDarkMode = isDarkModeState.value) { } }

Slide 73

Slide 73 text

setContent { val isSystemDark = isSystemInDarkTheme() val isDarkModeState = remember { mutableStateOf(isSystemDark) } ExploringMoviesTheme(isDarkMode = isDarkModeState.value) { App( isDarkModeActive = isDarkModeState.value ) } }

Slide 74

Slide 74 text

setContent { val isSystemDark = isSystemInDarkTheme() val isDarkModeState = remember { mutableStateOf(isSystemDark) } ExploringMoviesTheme(isDarkMode = isDarkModeState.value) { App( isDarkModeActive = isDarkModeState.value, switchDarkMode = { isDarkModeState.value = !isDarkModeState.value } ) } }

Slide 75

Slide 75 text

OBSERVING DATA WITH VIEWMODEL

Slide 76

Slide 76 text

class MovieViewModel : ViewModel() { fun getPopulars() = flow { ... } fun getDailyTrending() = flow { ... } fun getUpComing() = flow { ... } fun getMovieDetail(movieId: Int) = flow { ... } }

Slide 77

Slide 77 text

▸ LiveData.observeAsState() ▸ Flow.collectAsState() ▸ Observable.subscribeAsState()

Slide 78

Slide 78 text

@Composable fun MyExample() { // Returns the same instance as long as the activity is alive, // just as if you grabbed the instance from an Activity or // Fragment val viewModel: MovieViewModel = viewModel() } @Composable fun MyExample2() { // Same instance as in MyExample val viewModel: MovieViewModel = viewModel() }

Slide 79

Slide 79 text

@Composable fun MovieListViewModel( movieSection: MovieSection, isDarkModeActive: Boolean, switchDarkMode: () -> Unit = {}, onClick: (movie: Movie) -> Unit ) { MovieList( title = title, movies = movies.value, isDarkModeActive = isDarkModeActive, switchDarkMode = switchDarkMode, onClick = onClick ) }

Slide 80

Slide 80 text

@Composable fun MovieListViewModel( movieSection: MovieSection, isDarkModeActive: Boolean, switchDarkMode: () -> Unit = {}, onClick: (movie: Movie) -> Unit ) { val viewModel: MovieViewModel = viewModel() MovieList( title = title, movies = movies.value, isDarkModeActive = isDarkModeActive, switchDarkMode = switchDarkMode, onClick = onClick ) }

Slide 81

Slide 81 text

@Composable fun MovieListViewModel( movieSection: MovieSection, isDarkModeActive: Boolean, switchDarkMode: () -> Unit = {}, onClick: (movie: Movie) -> Unit ) { val viewModel: MovieViewModel = viewModel() val movies = when (movieSection) { MovieSection.UPCOMING -> viewModel.getUpComing() .collectAsState(initial = emptyList()) MovieSection.POPULAR -> viewModel.getPopulars() .collectAsState(initial = emptyList()) MovieSection.TRENDING -> viewModel.getDailyTrending() .collectAsState(initial = emptyList()) } MovieList( movies = movies.value, isDarkModeActive = isDarkModeActive, switchDarkMode = switchDarkMode, onClick = onClick ) }

Slide 82

Slide 82 text

@GerardPaligot DISCOVERING JETPACK COMPOSE

Slide 83

Slide 83 text

@GerardPaligot DISCOVERING COMPOSE MULTIPLATFORM

Slide 84

Slide 84 text

UI/UX UI/UX UI/UX BUSINESS LOGIC AND CORE

Slide 85

Slide 85 text

UI/UX UI/UX UI/UX BUSINESS LOGIC AND CORE ACTUAL ACTUAL ACTUAL

Slide 86

Slide 86 text

BUSINESS LOGIC AND CORE ACTUAL ACTUAL ACTUAL COMPOSE MULTIPLATFORM

Slide 87

Slide 87 text

No content

Slide 88

Slide 88 text

BUSINESS LOGIC AND CORE ACTUAL ACTUAL ACTUAL COMPOSE MULTIPLATFORM

Slide 89

Slide 89 text

UX UX UX BUSINESS LOGIC AND CORE ACTUAL ACTUAL ACTUAL COMPOSE MULTIPLATFORM

Slide 90

Slide 90 text

COMPILER RUNTIME UI FOUNDATION-LAYOUT ANIMATION FOUNDATION MATERIAL 3 Transf m @C posable functi s Pro amming model, state management and runtime f the c pose c pil plugin Fundamental c p ents Basics lay ts like B , R and C umn C p ents animati s Basics c p ents and int acti s Implementati of Mat ial Design specificati s Implementati of Mat ial Y

Slide 91

Slide 91 text

COMPILER RUNTIME UI FOUNDATION-LAYOUT ANIMATION FOUNDATION MATERIAL 3 Transf m @C posable functi s Pro amming model, state management and runtime f the c pose c pil plugin

Slide 92

Slide 92 text

COMPILER RUNTIME UI FOUNDATION-LAYOUT ANIMATION FOUNDATION MATERIAL 3 Transf m @C posable functi s Pro amming model, state management and runtime f the c pose c pil plugin WEB-CORE-RUNTIME WEB-CORE WIDGETS Runtime dedicated to web apps to gen ate DOM tags Basics native web c p ents Modifi s and lay ts

Slide 93

Slide 93 text

UX UX UX BUSINESS LOGIC AND CORE ACTUAL ACTUAL ACTUAL COMPOSE MULTIPLATFORM

Slide 94

Slide 94 text

UX UX UX BUSINESS LOGIC AND CORE ACTUAL ACTUAL ACTUAL COMPOSE MULTIPLATFORM COMPOSE WEB

Slide 95

Slide 95 text

WHY COMPOSE WEB? ▸ Compose Canvas rendering not compatible (yet) with web technologies ▸ Compose Web emit DOM UI for NPM librairies compatibility and SEO

Slide 96

Slide 96 text

:android-app :desktop-app :data

Slide 97

Slide 97 text

:android-app :desktop-app :data

Slide 98

Slide 98 text

:android-app :desktop-app :data :components :theming

Slide 99

Slide 99 text

:android-app :desktop-app :data :components :theming

Slide 100

Slide 100 text

plugins { id("com.android.library") } android { ... }

Slide 101

Slide 101 text

plugins { id("com.android.library") kotlin("multiplatform") id("org.jetbrains.compose") } android { ... }

Slide 102

Slide 102 text

plugins { id("com.android.library") kotlin("multiplatform") id("org.jetbrains.compose") } android { ... } kotlin { android() jvm("desktop") }

Slide 103

Slide 103 text

import org.jetbrains.compose.compose plugins { id("com.android.library") kotlin("multiplatform") id("org.jetbrains.compose") } android { ... } kotlin { android() jvm("desktop") sourceSets { named("commonMain") { dependencies { implementation(compose.runtime) implementation(compose.foundation) implementation(compose.material) } } } }

Slide 104

Slide 104 text

SOURCE SET desktopMain SOURCE SET androidMain SOURCE SET commonMain

Slide 105

Slide 105 text

@Composable expect fun Font( fontName: String, weight: FontWeight, style: FontStyle ): Font SOURCE SET commonMain

Slide 106

Slide 106 text

@Composable actual fun Font( fontName: String, weight: FontWeight, style: FontStyle ): Font { val context = LocalContext.current val id = context.resources .getIdentifier(fontName, "font", context.packageName) return Font(id, weight, style) } SOURCE SET androidMain

Slide 107

Slide 107 text

@Composable actual fun Font( fontName: String, weight: FontWeight, style: FontStyle ): Font { return androidx.compose.ui.text.platform.Font( "font/${fontName}.ttf", weight, style ) } SOURCE SET desktopMain

Slide 108

Slide 108 text

object Fonts { val roboto: FontFamily @Composable get() = FontFamily( Font( "roboto_bold", FontWeight.Bold, FontStyle.Normal ), Font( "roboto_italic", FontWeight.Normal, FontStyle.Italic ), Font( "roboto_regular", FontWeight.Normal, FontStyle.Normal ) ) } SOURCE SET commonMain

Slide 109

Slide 109 text

object Fonts { val roboto: FontFamily @Composable get() = ... } SOURCE SET commonMain

Slide 110

Slide 110 text

object Fonts { val roboto: FontFamily @Composable get() = ... } object Typographies { val roboto: Typography @Composable get() = Typography(defaultFontFamily = Fonts.roboto) } SOURCE SET commonMain

Slide 111

Slide 111 text

object Fonts { val roboto: FontFamily @Composable get() = ... } object Typographies { val roboto: Typography @Composable get() = Typography(defaultFontFamily = Fonts.roboto) } @Composable fun ExploringMoviesTheme( isDarkMode: Boolean = isSystemInDarkTheme(), content: @Composable () -> Unit ) { MaterialTheme( colors = if (isDarkMode) DarkColorPalette else LightColorPalette, typography = Typographies.roboto, shapes = shapes, content = content ) } SOURCE SET commonMain

Slide 112

Slide 112 text

:android-app :desktop-app :data :components :theming

Slide 113

Slide 113 text

@Composable expect fun RemoteImage( url: String, contentDescription: String?, modifier: Modifier = Modifier, contentScale: ContentScale = ContentScale.Fit ) SOURCE SET commonMain

Slide 114

Slide 114 text

import org.jetbrains.compose.compose plugins { ... } android { ... } kotlin { android() jvm("desktop") sourceSets { named("commonMain") { ... } named("androidMain") { dependencies { implementation(Dependencies.Accompanist.coil) } } } }

Slide 115

Slide 115 text

@Composable actual fun CrossRemoteImage( url: String, contentDescription: String?, modifier: Modifier, contentScale: ContentScale ) { Image( painter = rememberAsyncImagePainter(model = url), modifier = modifier, contentDescription = contentDescription, contentScale = ContentScale.Crop, ) } SOURCE SET androidMain

Slide 116

Slide 116 text

import org.jetbrains.compose.compose plugins { ... } android { ... } kotlin { android() jvm("desktop") sourceSets { named("commonMain") { ... } named("androidMain") { ... } named("desktopMain") { dependencies { implementation(Dependencies.okhttp) } } } }

Slide 117

Slide 117 text

@Composable actual fun CrossRemoteImage( url: String, contentDescription: String?, modifier: Modifier, contentScale: ContentScale ) { val image = remember { mutableStateOf(null) } if (image.value != null) { Image( bitmap = image.value!!, contentDescription = contentDescription, modifier = modifier, contentScale = contentScale ) } } SOURCE SET desktopMain

Slide 118

Slide 118 text

@Composable actual fun CrossRemoteImage( url: String, contentDescription: String?, modifier: Modifier, contentScale: ContentScale ) { val image = remember { mutableStateOf(null) } LaunchedEffect(url) { } if (image.value != null) { Image( bitmap = image.value!!, contentDescription = contentDescription, modifier = modifier, contentScale = contentScale ) } } SOURCE SET desktopMain

Slide 119

Slide 119 text

@Composable actual fun CrossRemoteImage( url: String, contentDescription: String?, modifier: Modifier, contentScale: ContentScale ) { val image = remember { mutableStateOf(null) } LaunchedEffect(url) { ImageLoader.load(url)?.let { } } if (image.value != null) { Image( bitmap = image.value!!, contentDescription = contentDescription, modifier = modifier, contentScale = contentScale ) } } SOURCE SET desktopMain

Slide 120

Slide 120 text

@Composable actual fun CrossRemoteImage( url: String, contentDescription: String?, modifier: Modifier, contentScale: ContentScale ) { val image = remember { mutableStateOf(null) } LaunchedEffect(url) { ImageLoader.load(url)?.let { image.value = org.jetbrains.skia.Image.makeFromEncoded(it) .toComposeImageBitmap() } } if (image.value != null) { Image( bitmap = image.value!!, contentDescription = contentDescription, modifier = modifier, contentScale = contentScale ) } } SOURCE SET desktopMain

Slide 121

Slide 121 text

WHY SHOULD CARE ABOUT COMPOSE?

Slide 122

Slide 122 text

WHY SHOULD CARE ABOUT COMPOSE? ▸ Android UI framework never changed since 2008 ▸ Need to wait very long time before bug fixes deployed in production ▸ Need to worry about keeping the View hierarchy as flat as possible ▸ Unbundled from OS ▸ Exit View and Fragment ▸ Clarify state ownership and event handling ▸ Writing less code

Slide 123

Slide 123 text

slack.kotlinlang.org #compose Open Source Project github.com/androidx/androidx

Slide 124

Slide 124 text

REFERENCES ▸ Discovering Movies, GitHub Repository of Gerard Paligot https://github.com/GerardPaligot/discovering-movies ▸ Official documentation about Jetpack Compose, Developer Android Website https://developer.android.com/jetpack/compose/documentation ▸ Pathways Compose https://developer.android.com/courses/pathways/compose ▸ Compose Academy https://compose.academy/ ▸ Samples Compose app by community https://jetpackcompose.app/

Slide 125

Slide 125 text

THANK YOU! @GerardPaligot