Created
July 31, 2023 19:28
-
-
Save sdetilly/7c0783d3d57f00b745c2b7ccb56abc2b to your computer and use it in GitHub Desktop.
Koin Service Setup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Common Code | |
| interface Bootstrap { | |
| val gpsService: GPSService | |
| } | |
| class KoinBootstrap : KoinComponent { | |
| fun initDependencies(bootstrap: Bootstrap) = startKoin { | |
| configureKoin(bootstrap) | |
| } | |
| } | |
| fun KoinApplication.configureKoin(bootstrap: Bootstrap) { | |
| modules( | |
| generalModule(bootstrap), | |
| // Your other modules if needed | |
| ) | |
| } | |
| fun generalModule(bootstrap: Bootstrap): Module { | |
| return module { | |
| single { bootstrap.gpsService } | |
| } | |
| } | |
| // Android Code | |
| class AndroidBootstrap(private val context: Context) : Bootstrap { | |
| override val gpsService = AndroidGPSService(context) | |
| } | |
| class MyApplication : Application() { | |
| val koin = KoinBootstrap() | |
| override fun onCreate() { | |
| super.onCreate() | |
| koin.initDependencies(AndroidBootstrap(this)) | |
| } | |
| } | |
| // iOS Code | |
| class IOSBootstrap: BootStrap { | |
| let gpsService: GPSService = IOSGPSService() | |
| } | |
| class AppDelegate: NSObject, UIApplicationDelegate { | |
| let koin = KoinBootstrap() | |
| let bootstrap = IOSBootstrap() | |
| func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { | |
| koin.doInitDependencies(bootstrap: bootstrap) | |
| return true | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment