Hello, how can I implement common callback in KMM ...
# multiplatform
c
Hello, how can I implement common callback in KMM for Android and iOS?
p
Just implement it. What is the problem you don't have visibility in swift side or kotlin-ios side? Post the code of what you have tried so far
o
I played around with the Kotlin callback flow for the location callbacks (iOS and Android), probably it worth looking into. The callback flow declaration goes to the "commonMain" shared class (could be the level of the repository or maybe level down, if you follow the repository pattern). Then you'll need "expect"/"actual" for the callback implementation for each platform, and a custom interface that features a function to be called in these "actual" classes when the callbacks on each platform get triggered. This interface will be instantiated as an anonymous object in a callback flow declaration and passed down to your actual classes, and it will connect the platform-level callbacks in your actual Android and iOS classes with the callback flow in the "commonMain" class. Here is the sample from my repo - https://github.com/OlgaDery/health_connect_multiplatform/blob/main/shared/src/comm[…]th_multiplstform/locationDataSource/LocationCallbacksManager.kt. You can ignore the "ILocationDataSource" interface, it's basically an abstraction and added for testability. I think there could be directly expect/actual dependencies.
👆 2