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

alex.hart

07/16/2019, 4:58 PM
Come up with a ‘PreferenceStore’ interface, and then have an Android impl (Using SharedPreferences) and an iOS impl (using UserDefaults). Then wherever you need it, just accept it as an argument, and let dependency injection do the rest.
k

kpgalligan

07/16/2019, 5:06 PM
Unless I’m missing a part of the request, that’s what this is: https://github.com/russhwolf/multiplatform-settings
r

russhwolf

07/16/2019, 5:16 PM
To the more general question of platform-specific dependencies, you can use `expect`/`actual` or constructor injection to pass in platform-specific dependencies. eg you could define a
expect class MyDependency
in common and in Android it might be
actual class MyDependency(context: Context)
and in iOS
actual class MyDependency()
. Then your common code can take a
MyDependency
parameter and your Android code can read the context out of it where necessary
i

ian.shaun.thomas

07/16/2019, 5:36 PM
definitely lean on constructor DI over expect/actual. expect/actual is fantastic but it's easy to go down a rabbit hole you never needed to go down.
l

louiscad

07/16/2019, 11:42 PM
On Android, you can just use
appCtx
from my little library Splitties App Context, as you only need application context for this use case.
s

Sergioedcigreja

07/17/2019, 8:22 AM
I can't use the expect actual on the "main" class because it shares a lot of code between both platforms so I will use DI with an expect class. Thank you for your time guys
c

ciriti

07/17/2019, 7:45 PM
Talking about https://github.com/russhwolf/multiplatform-settings, do you know if it works with kotlin 1.3.4?
k

kpgalligan

07/17/2019, 7:46 PM
c

ciriti

07/17/2019, 7:47 PM
amazing! Thanks!