Skip to content

Instantly share code, notes, and snippets.

@sdetilly
Created July 31, 2023 19:28
Show Gist options
  • Select an option

  • Save sdetilly/7c0783d3d57f00b745c2b7ccb56abc2b to your computer and use it in GitHub Desktop.

Select an option

Save sdetilly/7c0783d3d57f00b745c2b7ccb56abc2b to your computer and use it in GitHub Desktop.
Koin Service Setup
// 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