https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
y

Yauhen Pelkin

05/21/2020, 7:06 PM
Well I am trying to figure out how to do this with no di at least.
r

russhwolf

05/21/2020, 7:12 PM
You have a dependency. You're going to have to get it somehow. The strategy isn't going to be all that different from what you'd do in a non-mpp android app. Either make it a constructor parameter of a class that
isLocationAvailable()
is a member of, or you'll need to save a top-level reference to your application during its
onCreate()
☝️ 3
k

Kris Wong

05/21/2020, 7:14 PM
or
expect class Context
and pass a
Context
to the function
r

russhwolf

05/21/2020, 7:17 PM
yeah it can be a function parameter instead of a class thing. In practice though I usually see this as part of a
LocationManager
sort of class and it's nice if you can pass the context just once at initialization instead of needing to pass it for every method call
1
y

Yauhen Pelkin

05/21/2020, 7:21 PM
make it a constructor parameter of a class that
so, as i understand in this case instance should be created on android-platform (app) side and passed to commonMain code?
r

russhwolf

05/21/2020, 7:22 PM
yeah
y

Yauhen Pelkin

05/21/2020, 7:25 PM
Now i has something like this:
Copy code
class MyApp : Application() {

    override fun onCreate() {
        super.onCreate()
        Core.start()
    }
}
Core - is object in commonMain which (according to my idea) will start “business logic processing” But it seems it should look like Core.start(thousands of ContextWrapper, LocationManagerWrapper, whateverPlatformSpecieficWrapper) Am I right?
r

russhwolf

05/21/2020, 7:29 PM
Oh if you already have an injection point like that it helps. You could just define
fun Core.start(/* Android dependencies */)
in your Android sources and
fun Core.start(/* iOS dependencies*/)
on iOS, and wire whatever platform-specific stuff you need in there
Might just be
Core.start(applicationContext)
on Android and
Core.start()
on iOS if that's the only dependency you have
If it starts getting unwieldy, that's where having some sort of dependency manager is helpful
y

Yauhen Pelkin

05/21/2020, 7:37 PM
Something seems to have cleared up 🙂 thank you!
👍 1