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
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Mohit S
April 25, 2023
Programming
610
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
330
Building Shared UIs across Platforms with Compose
heyitsmohit
1
710
Building StateFlows with Jetpack Compose
heyitsmohit
6
2k
Building Android Testing Infrastructure
heyitsmohit
1
630
Migrating to Kotlin State & Shared Flows
heyitsmohit
1
880
Using Square Workflow for Android & iOS
heyitsmohit
1
520
Building Android Infrastructure Teams at Scale
heyitsmohit
3
420
Strategies for Migrating to Jetpack Compose
heyitsmohit
2
660
Challenges of Building Kotlin Multiplatform Libraries
heyitsmohit
1
520
Other Decks in Programming
See All in Programming
Hunting Vulnerabilities in Symfony with LLMs
vinceamstoutz
0
530
[2026年度第1回ORセミナー] 計画最適化ベンチャーと競技プログラミング人材
terryu16
0
250
PHPで使える日時の表現と、その知り方 #frontend_phpcon_do
o0h
PRO
0
230
ふつうのFeature Flag実践入門
irof
7
3.7k
Lemonade + Foundry Toolkit でお手軽アプリ開発
seosoft
1
320
並列実装の現場、2ヶ月間実務でAIを使い倒したAIもPCも私も限界が近い
ming_ayami
0
120
例外の正しい扱い方 そのエラー try-catchして大丈夫?
jinwatanabe
0
180
Java × distroless で 軽量なコンテナイメージを / Java on Distroless
contour_gara
0
520
TAKTでAI駆動開発の品質を設計する
j5ik2o
6
1.1k
AI時代のUIはどこへ行く?その2!
yusukebe
19
7k
フロントエンドとバックエンドで「1文字」を揃えよう
youkidearitai
PRO
0
240
AI駆動開発で崩れていくコードベースを立て直す
kyoko_nr_nr
1
450
Featured
See All Featured
Primal Persuasion: How to Engage the Brain for Learning That Lasts
tmiket
0
360
Amusing Abliteration
ianozsvald
1
200
Designing Dashboards & Data Visualisations in Web Apps
destraynor
231
55k
Thoughts on Productivity
jonyablonski
76
5.2k
Making the Leap to Tech Lead
cromwellryan
135
9.9k
The #1 spot is gone: here's how to win anyway
tamaranovitovic
2
1.1k
[SF Ruby Conf 2025] Rails X
palkan
2
1.1k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
35
2.5k
Docker and Python
trallard
47
3.9k
Producing Creativity
orderedlist
PRO
348
40k
Automating Front-end Workflow
addyosmani
1370
210k
Ruling the World: When Life Gets Gamed
codingconduct
0
250
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]