Skip to content

Instantly share code, notes, and snippets.

@DHosseiny
Created May 26, 2023 13:13
Show Gist options
  • Save DHosseiny/fa996246ecaca612030975bd3375edf3 to your computer and use it in GitHub Desktop.
Save DHosseiny/fa996246ecaca612030975bd3375edf3 to your computer and use it in GitHub Desktop.
DaggerWorkerFactory
class DaggerWorkerFactory(private val workerFactories: WorkerProviders) : WorkerFactory() {
override fun createWorker(
context: Context,
workerClassName: String,
workerParameters: WorkerParameters
): ListenableWorker? {
return try {
val factoryEntry = workerFactories.entries
.find { Class.forName(workerClassName).isAssignableFrom(it.key) }
// If it returns null, createWorkerWithDefaultFallback will be called inside WorkerWrapper
factoryEntry?.value?.get()?.create(context, workerParameters)
} catch (classNotFoundException: ClassNotFoundException) {
Logger.e(Throwable("Worker class not found: $workerClassName", classNotFoundException))
DummyWorker(context, workerParameters)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment