Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Building Multiplatform Apps with Compose
Search
Mohit S
April 25, 2023
Programming
630
2
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Building Multiplatform Apps with Compose
Mohit S
April 25, 2023
More Decks by Mohit S
See All by Mohit S
Guide to Improving Compose Performance
heyitsmohit
0
340
Building Shared UIs across Platforms with Compose
heyitsmohit
1
730
Building StateFlows with Jetpack Compose
heyitsmohit
6
2k
Building Android Testing Infrastructure
heyitsmohit
1
640
Migrating to Kotlin State & Shared Flows
heyitsmohit
1
890
Using Square Workflow for Android & iOS
heyitsmohit
1
530
Building Android Infrastructure Teams at Scale
heyitsmohit
3
430
Strategies for Migrating to Jetpack Compose
heyitsmohit
2
670
Challenges of Building Kotlin Multiplatform Libraries
heyitsmohit
1
520
Other Decks in Programming
See All in Programming
OpenSpecのproposalにbrainstormingを持たせてみた
tigertora7571
1
150
React本体のコードリーディング
high_g_engineer
0
110
属人化した知識を、 AIが辿れる地図にする
pkshadeck
PRO
1
120
SREの積み重ねがAI駆動開発のガードレールになった ― 7つの実践/SRE Guardrails The 7
tomoyakitaura
9
5.8k
【やさしく解説 設計編 #0】DDDのコード、読めるのに分からない人へ
panda728
PRO
2
290
AI時代のPHPer生存戦略 ~「言語、もうなんでもよくない?」に本気で向き合う~
vivion
0
210
【やさしく解説 設計編・中級 #1】一つの車に、運転手は一人 ~ある倉庫システムの事例から~
panda728
PRO
0
200
仕様書を書く前にハーネスを作る - Agent Native開発は「探索を速く、判定を固く」
gotalab555
2
1.3k
What's New in Android 2026
veronikapj
0
240
TSX の <Hoge<Fuga>> という構文に驚いた話 / tsx-type-argument-syntax
kanaru0928
0
130
Apache Hive: Toward a Cloud Native Lakehouse
okumin
0
170
Embedded SREと共に達成した会員管理システムのAWS移行 - SRE NEXT 2026 ランチスポンサーセッション
niftycorp
PRO
1
3.2k
Featured
See All Featured
Lessons Learnt from Crawling 1000+ Websites
charlesmeaden
PRO
1
1.5k
Exploring anti-patterns in Rails
aemeredith
3
450
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
360
30k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
254
22k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
21
1.6k
Typedesign – Prime Four
hannesfritz
42
3.1k
Collaborative Software Design: How to facilitate domain modelling decisions
baasie
1
270
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
31
3.3k
Why You Should Never Use an ORM
jnunemaker
PRO
61
9.9k
Ethics towards AI in product and experience design
skipperchong
2
330
The SEO identity crisis: Don't let AI make you average
varn
0
520
Facilitating Awesome Meetings
lara
57
7k
Transcript
Mohit Sarveiya Building Multiplatform Apps with Compose @
[email protected]
Building Multiplatform Apps with Compose • Setup Project
Building Multiplatform Apps with Compose • Setup Project • Share
Compose UI
Building Multiplatform Apps with Compose • Setup Project • Share
Compose UI • SwiftUI & Compose Interop
Building Multiplatform Apps with Compose • Setup Project • Share
Compose UI • SwiftUI & Compose Interop • Architecture & Navigation
Android Kotlin/JVM iOS Swift/LLVM Web JS Desktop Kotlin/JVM
Share
API Share
API Share Cache
API Share Cache Business Logic
API Share Cache Business Logic UI Components
https: / / github.com/JetBrains/compose-multiplatform compose-multiplatform
Compose Multiplatform • Android (via Jetpack Compose) • Desktop (Windows,
Mac OS, Linux) • Web (Experimental)
Compose Multiplatform • Android (via Jetpack Compose) • Desktop (Windows,
Mac OS, Linux) • Web (Experimental) • iOS (Alpha)
Compose Multiplatform
Compose Multiplatform
UI Structure
androidApp iOSApp desktopApp shared Structure
shared src commonMain androidMain iOSMain Shared Module
shared src commonMain androidMain iOSMain build.gradle.kts Shared Module
plugins { kotlin("multiplatform") } val commonMain by getting { dependencies
{ implementation(compose.ui) implementation(compose.foundation) implementation(compose.material) implementation(compose.runtime) } } Shared Module
plugins { kotlin("multiplatform") } val commonMain by getting { dependencies
{ implementation(compose.ui) implementation(compose.foundation) implementation(compose.material) implementation(compose.runtime) } } Shared Module
shared src commonMain androidMain iOSMain Shared Module
UI Structure App Root View Android iOS
UI Structure App Root View Android iOS
shared src commonMain androidMain iOSMain UI Structure
shared src commonMain androidMain iOSMain ImagesApp.commmon.kt UI Structure
fun ImagesAppCommon() { Scaffold( topBar = { TopAppBar( ... )
}, content = { ... } ) } UI Structure
fun ImagesAppCommon() { Scaffold( topBar = { TopAppBar( ... )
}, content = { ... } ) } UI Structure
shared src commonMain androidMain iOSMain ImagesApp.commmon.kt ImagesAppTheme.kt Shared Module
App Theme val LightColorPalette = lightColors( ... ) val DarkColorPalette
= darkColors( ... ) @Composable fun ImagesAppTheme( darkTheme: Boolean = isSystemInDarkTheme(), content: @Composable () -> Unit ) { MaterialTheme( colors = if (darkTheme) DarkColorPalette else LightColorPalette, content = content ) }
App Theme val LightColorPalette = lightColors( ... ) val DarkColorPalette
= darkColors( ... ) @Composable fun ImagesAppTheme( darkTheme: Boolean = isSystemInDarkTheme(), content: @Composable () -> Unit ) { MaterialTheme( colors = if (darkTheme) DarkColorPalette else LightColorPalette, content = content ) }
shared src commonMain androidMain iOSMain ImagesApp.android.kt Shared Module
UI Structure @Composable fun MainAndroid() { ImagesAppTheme { ImagesAppCommon() }
}
androidApp iOSApp desktopApp shared UI Structure
UI Structure class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState:
Bundle?) { super.onCreate(savedInstanceState) setContent { MainAndroid() } } }
UI Structure class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState:
Bundle?) { super.onCreate(savedInstanceState) setContent { MainAndroid() } } }
shared src commonMain androidMain iOSMain ImagesApp.iOS.kt Shared Module
UI Structure fun MainiOS(): UIViewController ComposeUIViewController { ImagesAppCommon() }
UI Structure fun MainiOS(): UIViewController = ComposeUIViewController { ImagesAppCommon() }
androidApp iOSApp desktopApp shared UI Structure
UI Structure @main struct iOSApp: App { var body:
some Scene { WindowGroup { ContentView() } } }
UI Structure @main struct iOSApp: App { var body:
some Scene { WindowGroup { ContentView() } } }
UI Structure struct ComposeView: UIViewControllerRepresentable { func makeUIViewController(context: Context)
-> UIViewController { let controller = Main_iosKt.MainiOS() return controller } }
UI Structure struct ComposeView: UIViewControllerRepresentable { func makeUIViewController(context: Context)
-> UIViewController { let controller = Main_iosKt.MainiOS() return controller } }
UI Structure struct ComposeView: UIViewControllerRepresentable { func makeUIViewController(context: Context)
-> UIViewController { let controller = Main_iosKt.MainiOS() return controller } }
UI Structure struct ContentView: View { var body: some
View { ZStack { ComposeView() ... } } }
UI Structure struct ContentView: View { var body: some
View { ZStack { ComposeView() ... } } }
UI Structure struct ContentView: View { var body: some
View { ZStack { ComposeView() ... } } } UIWindowScene UIWindow ComposeWindow SkikkoUIView View Hierarchy
UI Structure struct ContentView: View { var body: some
View { ZStack { ComposeView() ... } } } UIWindowScene UIWindow ComposeWindow SkikkoUIView View Hierarchy
UI Structure struct ContentView: View { var body: some
View { ZStack { ComposeView() ... } } } UIWindowScene UIWindow ComposeWindow SkikkoUIView View Hierarchy
https: / / github.com/JetBrains/skiko Skiko
UI Structure App Root View Android iOS
UI Structure
Challenge: Image Loading
https: / / github.com/coil-kt/coil/issues/842 Coil-kt
shared src resource image1.jpeg image2.jpeg image3.jpeg Shared Module
val commonMain by getting { dependencies { implementation(compose.components.resources) } }
Shared Module
val commonMain by getting { dependencies { implementation(compose.components.resources) } }
Shared Module
class ImageProvider { suspend fun getImageBitmap(picture: ImageData): ImageBitmap = resource(picture.url).readBytes().toImageBitmap()
} Shared Module
class ImageProvider { suspend fun getImageBitmap(picture: ImageData): ImageBitmap = resource(picture.url).readBytes()
} Shared Module
class ImageProvider { suspend fun getImageBitmap(picture: ImageData): ImageBitmap = resource(picture.url).readBytes().toImageBitmap()
} Shared Module
Except/Actual
shared src commonMain androidMain Shared Module iOSMain
expect fun ByteArray.toImageBitmap(): ImageBitmap Shared Module
shared src commonMain androidMain Shared Module iOSMain
actual fun ByteArray.toImageBitmap(): ImageBitmap = toAndroidBitmap().asImageBitmap() fun ByteArray.toAndroidBitmap(): Bitmap {
return BitmapFactory.decodeByteArray(this, 0, size) } Shared Module
actual fun ByteArray.toImageBitmap(): ImageBitmap = toAndroidBitmap().asImageBitmap() fun ByteArray.toAndroidBitmap(): Bitmap {
return BitmapFactory.decodeByteArray(this, 0, size) } Shared Module
actual fun ByteArray.toImageBitmap(): ImageBitmap = toAndroidBitmap().asImageBitmap() fun ByteArray.toAndroidBitmap(): Bitmap {
return BitmapFactory.decodeByteArray(this, 0, size) } Shared Module
actual fun ByteArray.toImageBitmap(): ImageBitmap = toAndroidBitmap().asImageBitmap() fun ByteArray.toAndroidBitmap(): Bitmap {
return BitmapFactory.decodeByteArray(this, 0, size) } Shared Module
shared src commonMain androidMain Shared Module iOSMain
actual fun ByteArray.toImageBitmap(): ImageBitmap = Image.makeFromEncoded(this).toComposeImageBitmap() Shared Module
actual fun ByteArray.toImageBitmap(): ImageBitmap = Image.makeFromEncoded(this).toComposeImageBitmap() Shared Module
actual fun ByteArray.toImageBitmap(): ImageBitmap = Image.makeFromEncoded(this).toComposeImageBitmap() Shared Module
class ImageProvider { suspend fun getImageBitmap(picture: ImageData): ImageBitmap = resource(picture.url).readBytes().toImageBitmap()
} Shared Module
UI Structure
@Composable fun ImagesList(images: List<ImageData>) { Column { LazyVerticalGrid( columns =
GridCells.Fixed(3) ) { items(images) { SplashImage( imageData = it ) } } } } UI Structure
@Composable fun ImagesList(images: List<ImageData>) { Column { LazyVerticalGrid( columns =
GridCells.Fixed(3) ) { items(images) { SplashImage( imageData = it ) } } } } UI Structure
@Composable fun ImagesList(images: List<ImageData>) { Column { LazyVerticalGrid( columns =
GridCells.Fixed(3) ) { items(images) { SplashImage( imageData = it ) } } } } UI Structure
@Composable fun SplashImage(imageData: ImageData) { val imageProvider = LocalImageProvider.current var
imageBitmap by remember(imageData) { mutableStateOf<ImageBitmap?>(null) } LaunchedEffect(imageData) { imageBitmap = imageProvider.getImageBitmap(imageData) } imageBitmap ?. let { Image( bitmap = bitmap, ... ) } } UI Structure
@Composable fun SplashImage(imageData: ImageData) { val imageProvider = LocalImageProvider.current var
imageBitmap by remember(imageData) { mutableStateOf<ImageBitmap?>(null) } LaunchedEffect(imageData) { imageBitmap = imageProvider.getImageBitmap(imageData) } imageBitmap ?. let { Image( bitmap = bitmap, ... ) } } UI Structure
@Composable fun SplashImage(imageData: ImageData) { val imageProvider = LocalImageProvider.current var
imageBitmap by remember(imageData) { mutableStateOf<ImageBitmap?>(null) } LaunchedEffect(imageData) { imageBitmap = imageProvider.getImageBitmap(imageData) } imageBitmap ?. let { Image( bitmap = bitmap, ... ) } } UI Structure
@Composable fun SplashImage(imageData: ImageData) { val imageProvider = LocalImageProvider.current var
imageBitmap by remember(imageData) { mutableStateOf<ImageBitmap?>(null) } LaunchedEffect(imageData) { imageBitmap = imageProvider.getImageBitmap(imageData) } imageBitmap ?. let { Image( bitmap = bitmap, ... ) } } UI Structure
UI Structure
UI Structure App Root View Android iOS Images List Images
Details Shared
Challenges: App Architecture, Navigation
Compose UI View Model UI State Events
UI Structure App Root View Android iOS Images List Images
Details Shared
UI Structure App Root View Android iOS Images List Images
Details Shared View Model
Shared Module shared src commonMain androidMain iOSMain
Shared Module shared src commonMain androidMain iOSMain ImagesListViewModel.kt
App Architecture sealed class ImagesListUiState { object Loading: ImagesListUiState() data
class Success( val images: List<ImageData> ): ImagesListUiState() data class Error( val errorMessage: String ): ImagesListUiState() }
App Architecture sealed class ImagesListUiState { object Loading: ImagesListUiState() data
class Success( val images: List<ImageData> ): ImagesListUiState() data class Error( val errorMessage: String ): ImagesListUiState() }
App Architecture sealed class ImagesListUiState { object Loading: ImagesListUiState() data
class Success( val images: List<ImageData> ): ImagesListUiState() data class Error( val errorMessage: String ): ImagesListUiState() }
App Architecture sealed class ImagesListUiState { object Loading: ImagesListUiState() data
class Success( val images: List<ImageData> ): ImagesListUiState() data class Error( val errorMessage: String ): ImagesListUiState() }
App Architecture class ImagesListViewModel { init { ... } val
state = MutableStateFlow<ImagesListUiState>(ImagesListUiState.Loading) val viewModelScope = CoroutineScope(Dispatchers.Main) } init } {
App Architecture class ImagesListViewModel { init { ... } }
init } { viewModelScope.launch(Dispatchers.Main) { try { val imagesList = imagesRepository.getImages() state.emit(uiState.Success(images = imagesList)) } catch (e: Exception) { state.emit(uiState.Error("Something went wrong")) } }
App Architecture @Composable fun ImagesAppCommon() { Scaffold( topBar = {
... }, content = { } ) }
App Architecture @Composable fun ImagesAppCommon() { Scaffold( topBar = {
... }, content = { } ) } val uiState by viewModel.state.collectAsState() ImagesListScreen(uiState)
App Architecture @Composable fun ImagesListScreen(uiState: UIState) { } }
App Architecture @Composable fun ImagesListScreen(uiState: UIState) { when (uiState) {
ImagesListUiState.Loading -> is ImagesListUiState.Success -> is ImagesListUiState.Error -> } }
App Architecture @Composable fun ImagesListScreen(uiState: UIState) { when (uiState) {
ImagesListUiState.Loading -> CircularProgressIndicator() is ImagesListUiState.Success -> is ImagesListUiState.Error -> } }
App Architecture @Composable fun ImagesListScreen(uiState: UIState) { when (uiState) {
ImagesListUiState.Loading -> CircularProgressIndicator() is ImagesListUiState.Success -> ImagesList(uiState.images) is ImagesListUiState.Error -> } }
Navigation App Root View List Details
App Architecture sealed class Screen { }
App Architecture sealed class Screen { object List : Screen()
}
App Architecture sealed class Screen { object List : Screen()
data class Details(val imageId: String) : Screen() }
App Architecture @Composable fun ImagesAppCommon() { var screenState by remember
{ mutableStateOf<Screen>(Screen.List) } when (val screen = screenState) { is Screen.List -> List( onItemClick = { screenState = Screen.Details(imageId = it) } ) is Screen.Details -> Details( text = screen.text, onBack = { screenState = Screen.List } ) } }
App Architecture @Composable fun ImagesAppCommon() { var screenState by remember
{ mutableStateOf<Screen>(Screen.List) } when (val screen = screenState) { is Screen.List -> List( onItemClick = { screenState = Screen.Details(imageId = it) } ) is Screen.Details -> Details( text = screen.text, onBack = { screenState = Screen.List } ) } }
App Architecture @Composable fun ImagesAppCommon() { var screenState by remember
{ mutableStateOf<Screen>(Screen.List) } when (val screen = screenState) { is Screen.List -> List( onItemClick = { screenState = Screen.Details(imageId = it) } ) is Screen.Details -> Details( text = screen.text, onBack = { screenState = Screen.List } ) } }
App Architecture @Composable fun ImagesAppCommon() { var screenState by remember
{ mutableStateOf<Screen>(Screen.List) } when (val screen = screenState) { is Screen.List -> List( onItemClick = { screenState = Screen.Details(imageId = it) } ) is Screen.Details -> Details( text = screen.imageId, onBack = { screenState = Screen.List } ) } }
App Architecture • Lifecycle Aware • Navigation
https: / / github.com/arkivanov/Decompose Decompose
Decompose • Lifecycle Aware Components • Back stack management
Decompose interface ListComponent { val uiState: Value<Model> fun onImageClicked(imageId:
String) data class UiState( val images: List<Image>, ) }
Decompose interface ListComponent { val uiState: Value<UiState> fun onImageClicked(imageId:
String) data class UiState( val images: List<Image>, ) }
Decompose interface ListComponent { val uiState: Value<UiState> fun onImageClicked(imageId:
String) data class UiState( val images: List<Image>, ) }
Decompose class ImagesListComponent( componentContext: ComponentContext, val onImageSelected: (imageId: String) ->
Unit, ) : ListComponent { override val uiState: Value<ListComponent.UiState> = MutableValue(UiState.Loading) override fun onItemClicked(imageId: String) { onImageSelected(imageId) } }
Decompose class ImagesListComponent( componentContext: ComponentContext, val onImageSelected: (imageId: String) ->
Unit, ) : ListComponent { override val uiState: Value<ListComponent.UiState> = MutableValue(UiState.Loading) override fun onItemClicked(imageId: String) { onImageSelected(imageId) } }
Decompose class ImagesListComponent( componentContext: ComponentContext, val onImageSelected: (imageId: String) ->
Unit, ) : ListComponent { override val uiState: Value<ListComponent.UiState> = MutableValue(UiState.Loading) override fun onItemClicked(imageId: String) { onImageSelected(imageId) } }
@Composable fun ImagesList( component: ListComponent, images: List<ImageData>, onImageClicked: (Int) ->
Unit ) { val uiState by component.uiState.subscribeAsState() } Decompose
@Composable fun ImagesList( component: ListComponent, images: List<ImageData>, onImageClicked: (Int) ->
Unit ) { val uiState by component.uiState.subscribeAsState() } Decompose
Navigation App Root View List Details
interface RootComponent { val stack: Value<ChildStack <* , Child >>
sealed class Child { class ListChild(val component: ListComponent) : Child() class DetailsChild(val component: DetailsComponent) : Child() } } App Architecture
interface RootComponent { val stack: Value<ChildStack <* , Child >>
sealed class Child { class ListChild(val component: ListComponent) : Child() class DetailsChild(val component: DetailsComponent) : Child() } } App Architecture
interface RootComponent { val stack: Value<ChildStack <* , Child >>
sealed class Child { class ListChild(val component: ListComponent) : Child() class DetailsChild(val component: DetailsComponent) : Child() } } App Architecture
class DefaultRootComponent( ... ): RootComponent { @Parcelize sealed interface Config
: Parcelable { object List : Config data class Details(val item: String) : Config } } App Architecture
class DefaultRootComponent( ... ): RootComponent { val navigation =
StackNavigation<Config>() @Parcelize sealed interface Config : Parcelable { object List : Config data class Details(val item: String) : Config } } App Architecture
class DefaultRootComponent( ... ): RootComponent { val navigation =
StackNavigation<Config>() @Parcelize sealed interface Config : Parcelable { object List : Config data class Details(val item: String) : Config } } App Architecture val stack = childStack( source = navigation, initialConfiguration = Config.List, handleBackButton = true, childFactory = :: child, )
class DefaultRootComponent( ... ): RootComponent { val navigation =
StackNavigation<Config>() @Parcelize sealed interface Config : Parcelable { object List : Config data class Details(val item: String) : Config } } App Architecture
class DefaultRootComponent( ... ): RootComponent { } App Architecture fun
listComponent(): ListComponent = ImagesListComponent( onItemSelected = { imageId: String -> navigation.push(Config.Details(item = imageId)) }, )
class DefaultRootComponent( ... ): RootComponent { } App Architecture fun
listComponent(): ListComponent = ImagesListComponent( onItemSelected = { imageId: String -> navigation.push(Config.Details(item = imageId)) }, )
class DefaultRootComponent( ... ): RootComponent { } App Architecture fun
detailsComponent(): DetailsComponent = ImageDetailsComponent( image = config.image, onFinished = navigation :: pop, )
class DefaultRootComponent( ... ): RootComponent { } App Architecture fun
detailsComponent(): DetailsComponent = ImageDetailsComponent( image = config.image, onFinished = navigation :: pop, )
Navigation App Root View List Details
@Composable fun ImagesAppCommon(component: RootComponent) { Children( stack = component.stack )
{ when (val child = it.instance) { is ListChild -> ListContent(component = child.component) is DetailsChild -> DetailsContent(component = child.component) } } } App Architecture
@Composable fun ImagesAppCommon(component: RootComponent) { Children( stack = component.stack )
{ when (val child = it.instance) { is ListChild -> ListContent(component = child.component) is DetailsChild -> DetailsContent(component = child.component) } } } App Architecture
class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?)
{ super.onCreate(savedInstanceState) val root = DefaultRootComponent( componentContext = defaultComponentContext(), ) setContent { MaterialTheme { Surface { RootContent(component = root, modifier = Modifier.fillMaxSize()) } } } App Architecture
class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?)
{ super.onCreate(savedInstanceState) val root = DefaultRootComponent( componentContext = defaultComponentContext(), ) setContent { MaterialTheme { Surface { RootContent(component = root, modifier = Modifier.fillMaxSize()) } } } App Architecture
class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?)
{ super.onCreate(savedInstanceState) val root = DefaultRootComponent( componentContext = defaultComponentContext(), ) setContent { MaterialTheme { Surface { ImageAppCommon(component = root) } } } App Architecture
class AppDelegate: NSObject, UIApplicationDelegate { let rootHolder: RootHolder = RootHolder()
} App Architecture
@main struct app_iosApp: App { var rootHolder: RootHolder {
appDelegate.rootHolder } var body: some Scene { WindowGroup { ComposeView(rootHolder.root) } } } App Architecture
Decompose • Lifecycle Aware Components • Back stack management
Building Multiplatform Apps with Compose • Setup Project • Share
Compose UI • SwiftUI & Compose Interop • Architecture & Navigation
Thank You! www.codingwithmohit.com @
[email protected]