Please let me know if I'm integrating compose in m...
# multiplatform
g
Please let me know if I'm integrating compose in my existing Android Application or IOS, with some module. moving forward let's say it's integrated well in project now problem statement is , with compose UI just suppose it's a list view when click on any list it will open any Viewcontroller in iOS project and Activity in existing android project , need 2 way communication Compose -> existing project controller navigation and existing project controller -> Compose , how to achieve same or anyone did same ?
p
Can you share/point some code? - it will help to visualize what is your problem.
g
@Pablichjenkov see my problem statement is quite simple, let suppose you have an existing project completely in swift , now our aim to move some modules into KMP, lets forget web for now . moving forward suppose you integrated well in your project now , with compose UI lets say it's a list view when click on any list it will open any View controller in your IOS project and Activity in existing android project , need 2 way communication Compose UI -> existing project View controller / Activity and existing project controller/Activity -> Compose
p
Ah ok, I think you answer your own question. I would pass an interface from my swift code into the ComposeUiViewController. That interface will provide 2 way communication. A callback to receive events from compose and an event dispatcher so compose can subscribe to swift side events. I got some similar code in a project where I do:
Copy code
fun IosComponentRender( 
     rootComponent: Component, 
     iosBridge: IosBridge, 
     onBackPress: () -> Unit = {} 
 ): UIViewController = ComposeUIViewController { ... }
That interface IosBridge offers 2 way communication. I send willAppear/didDisapear events to compose and receive other events/results through that interface too.
Perhaps there is a more sophisticated way to achieve that
g
how we can achieve in Android then ?
p
In Android is the same, sort of:
Copy code
@Composable 
 fun AndroidComponentRender( 
     rootComponent: Component, 
     androidBridge: AndroidBridge, 
     onBackPress: () -> Unit = {} 
 ) { ... }
Try to check chrisbanes tivi App. I think that App manages to mix native navigation with compose.
g
If you don't mind will you be able to share bridge code and how it implements i think u get my point , i have an existing project completely in swift lets say a module 1 , now design a KMP screen which is common for both Android and IOS which can be termed as module 2 , i integrate in my existing code base through pod, now I want to communicate between 2 modules as KMP don't know about existing code base in Swift , so lets say in KMP any tuple click and need to open existing code base View controller .. Hope you understood my query well in detail module 1 knows about module 2 through shared folder in pods but module 2 is unaware about module 1 thats why i need help on this
p
No problem at all, is an open project. I understand, in short, you want 2 way communication between a UIViewController in swift and a ComposeUiViewController in kotlin. Check the links bellow. Declaration: https://github.com/pablichjenkov/component-toolkit/tree/master/component-toolkit/src/iosMain/kotlin/com/macaosoftware/platform Exposing them in :shared module kotlin Bindings file. (no cocoapods integration but regular framework) https://github.com/pablichjenkov/component-toolkit/blob/e6f0ee62c2f9974dd78e9de959[…]src/iosMain/kotlin/com/macaosoftware/component/demo/Bindings.kt Swift side usage: https://github.com/pablichjenkov/component-toolkit/blob/e6f0ee62c2f9974dd78e9de95951ae1a0fa68cb3/iosApp/iosApp/ContentView.swift#L18C3-L18C3 When I wrote my previous message I was on the phone and did not realized that IosBridge or IosBridge2(temporal testing that I am doing) are classes. Previously it was an Interface but I move to a class because basically what I want is this class to containing many interfaces implemented in Swift Side.
/shared/iosMain/Bindings.kt
check this file
Copy code
fun IosComponentRender(
    rootComponent: Component,
    iosBridge: IosBridge,
    onBackPress: () -> Unit = {}
): UIViewController = ComposeUIViewController {...}
You can call swift from kotlin either directly invoking a callback, see
onBackPress: () -> Unit
for instance or having an EventDispatcher inside iOSBridge where listeners subscribe in the swift side and you request to dispatch the events on the kotlin side.
g
this is throwing error when implementing same : Could not resolve all dependencies for configuration 'sharediosX64CompileKlibraries'.
Could not resolve io.github.pablichjenkovcomponent toolkit0.5.1.
Required by: project :shared @Pablichjenkov
p
Are you trying to use the component-toolkit library? In such a case it does not support the x64 target. It could be added but it increases compilation times. And tbh I don't think there is any Intel chip iPhone out there. If you are not using the X64 target, comment it out and see if it works.
g
so what do you recommending me, without using component-toolkit library how i can make bridge between swift code and Compose Ui
p
See what the component library does, I showed it as an example. In the code above IosComponentRenderer, replace it with your own iOSComposeUiViewController. And pass your own bridge class and your own lambda for the callback.
I already add the target x64 to the library in version
0.5.2
, if you plan to use it, let me know what needs to be added in the bridge for your use case. Or open a PR for whatever you want.
g
In your bridge , I'm checking Many Dispatcher event is placed, so without importing same component-toolkit library, am unable to use it , do you want me to write my own dispatcher event and bridge class , for simple dependency lets say navigateToController() a fun in need to pass in swift code , from compose ui , how can make it possible. for now
this is my bridging file
Copy code
@OptIn(ExperimentalObjCName::class)
@ObjCName("IosComponentRender")
fun IosComponentRender(
    iosBridge: IosBridge,
    onBackPress: () -> Unit = {}
): UIViewController = ComposeUIViewController {
  


}
Copy code
@OptIn(ExperimentalObjCName::class)
@ObjCName(name = "IosBridge", exact = true)
class IosBridge(
    var lifecycleDispatcher: Dispatchers
)
only just give me hint how will you expose Ios bridge to your swift code and also IosComponentRender
@Pablichjenkov
Check the code in iOSApp demo.