I am interested in developing common type componen...
# multiplatform
m
I am interested in developing common type components that control their own UI, I want have the component implemented in common kotlin code but I obviously need to provide different UIs. I wonder if anyone did something similar because currently I hacked it together like this
Copy code
expect abstract class UiContent<S : State> {
    @Composable
    abstract fun android(
        storeContext: StoreContext,
        state: S,
    )

    abstract fun ios(
        storeContext: StoreContext,
        state: S,
    ): Any
}
android
is overriden from android side and
ios
returns SwiftUI, this is obviously a hack and I would like to hear an opinion on how to improve it but it does work (at least for now) and allows me to work the component itself
Copy code
val content =
    object : UiContent<RootState>() {
        @Composable
        override fun android(
            storeContext: StoreContext,
            state: RootState,
        ) {
            UiContent(storeContext, state)
        }

        override fun ios(
            storeContext: StoreContext,
            state: RootState,
        ) = Unit
    }

val component = RootComponent(content, container)


setContent {
    KoloTheme {
        Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
            val context = remember(store) { StoreContextDelegate(store) }

            component.content.android(context, store.states.collectAsState().value)
        }
    }
}
Copy code
...
	var body: some Scene {
		WindowGroup {
            let swiftUiView = component.content.ios(storeContext: storeContext, state: store.states.value) as! (any View)
            AnyView(swiftUiView)
		}
	}
}

class RootUiContentImpl: RootUiContent {
    override func ios(storeContext: any StoreContext, state: RootState) -> Any {
        RootComponentUi(storeContext: storeContext, state: state)
    }
}
Not even sure what my question is, I guess does anyone know if there are any libraries, talks or good articles that provide an example on how to share everything but UI?
b
Why do you need to have different UIs? I don't, I've written two apps so far and have use a shard UI in both. Try this wizard to generate a starter project for you. Choose the option that uses the same UI for all platforms. https://kmp.jetbrains.com/
m
Compose Multiplatform is in Beta, that’s a good enough reason