https://kotlinlang.org logo
Title
a

Alberto

08/10/2020, 4:28 PM
I'd like to create some interfaces in the common shared code that will be implemented in platform specifics code (Android/iOS either via expect/actual, but more likely interfaces implemented in separate packages). What would be the best way to pass in Platform specific objects, for example Context / Activity in Android, and equivalents in iOS? I'd imagine this could be a quite common scenario, didn't find an example on the Kamp Kit app (unless I've missed it).
r

russhwolf

08/10/2020, 5:01 PM
It's much easier to do this with interfaces than expect/actual. KaMPKit has a couple examples of this. Some are in libraries (eg
SqlDriver
and
Settings
), and there's a dummy
AppInfo
class to demonstrate more explicitly. That one doesn't pass a context on the Android side but it easily could
KaMPKit is passing all of that into Koin but the same patterns should translate to any other dependency management API you might use.
a

Alberto

08/10/2020, 7:22 PM
Thanks a lot for the pointer, will take a look!
Took a look at those examples, it seems they are mostly dealing with primitive types, but was wondering what would be a good design for platform specific classes. For example adding a "getPlatformContext" to the AppInfo that returns an Android.Context on Android and something else for iOS. I guess it could be achieved using generic objects or Any with casts?
r

russhwolf

08/10/2020, 7:57 PM
The details are going to depend a lot on your use-case. But in general you're more often going the other way, where an Android thing needs a
Context
input that iOS doesn't. And in that case having separate per-platform implementations (or even just separate constructors or factory methods) is usually enough
It's rare that I find myself looking for a
Context
from common code, because I've usually already given it to the things that need it at construction time
a

Alberto

08/10/2020, 8:02 PM
that makes sense, passing it in construction time. I guess an example would be having a method to show/hide a dialog in the common code, which would be implemented in Android and iOS platform code. Usually to show UX stuff we have to pass in Context objects on Android, so was considering putting it in the method itself, but perhaps could try to move it at creation time as you pointed out.
r

russhwolf

08/10/2020, 8:08 PM
Yeah. It might get dicier there if you need an activity context instead of application. But it should still be possible to get working. As long as the platforms are responsible for initializing platform-specific code, you can pass platform-specific dependencies.
a

Alberto

08/10/2020, 8:21 PM
on a related note, have you put any thoughts on how to use some observables specific platform implementation from the common code? Like for example having a method in a common interface that returns an observable. On Android the observable could be LiveData or using RX, while on iOS something else.
r

russhwolf

08/10/2020, 8:44 PM
That again is going to depend a lot on use-case. I wrote about coroutine and RxSwift interop here for one example, but there's lots of other ways you could do things instead. https://dev.to/touchlab/working-with-kotlin-coroutines-and-rxswift-24fa
a

Alberto

08/10/2020, 8:45 PM
will go through that, appreciate sharing that (and writing it)