Come up with a ‘PreferenceStore’ interface, and th...
# multiplatform
a
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
Unless I’m missing a part of the request, that’s what this is: https://github.com/russhwolf/multiplatform-settings
r
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
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
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
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
Talking about https://github.com/russhwolf/multiplatform-settings, do you know if it works with kotlin 1.3.4?
k
c
amazing! Thanks!