Created
May 26, 2023 13:13
-
-
Save DHosseiny/fa996246ecaca612030975bd3375edf3 to your computer and use it in GitHub Desktop.
DaggerWorkerFactory
This file contains 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
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