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
620
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
640
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
TAKTでAI駆動開発の品質を設計する
j5ik2o
7
1.5k
LLMによるContent Moderationの本番運用の裏側と品質担保への挑戦
suikabar
3
740
A2UI という光を覗いてみる
satohjohn
1
150
さぁV100、メモリをお食べ・・・
nilpe
0
150
Language Server 使ってる? 〜VSCode と Zed の場合〜 / Are you using a Language Server? ~For VS Code and Zed~
handlename
0
800
The ROI of Quarkus for Spring Boot Applications
hollycummins
0
140
Spec Driven Development | AI Summit Lisbon
danielsogl
PRO
0
210
依存関係から依存物へ―Dependencyという言葉の歴史をひも解く
j_lee
0
130
決定論的オーケストレーションの設計と実装 / Design and Implementation of Deterministic Orchestration
nrslib
4
1.5k
ECSアプリログをFireLensでコスト削減しようとしたけど諦めた話 in Fargate×Node.js
akihisaikeda
2
4.2k
Javaの型とAI時代に型が大事な理由 / java types and type in AI era
kishida
2
150
才能?センス?知らん、 続けたもん勝ちだ。-- 結婚・出産・癌を越えてなお、私がプロダクトを創り続ける理由
16bitidol
1
260
Featured
See All Featured
Statistics for Hackers
jakevdp
799
230k
[SF Ruby Conf 2025] Rails X
palkan
2
1.1k
A better future with KSS
kneath
240
18k
Documentation Writing (for coders)
carmenintech
77
5.4k
The Straight Up "How To Draw Better" Workshop
denniskardys
239
140k
Taking LLMs out of the black box: A practical guide to human-in-the-loop distillation
inesmontani
PRO
3
2.3k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
PRO
201
75k
Side Projects
sachag
455
43k
A Soul's Torment
seathinner
6
3k
The browser strikes back
jonoalderson
0
1.3k
Optimizing for Happiness
mojombo
378
71k
Git: the NoSQL Database
bkeepers
PRO
432
67k
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]