Created
March 6, 2019 08:28
-
-
Save lucassales2/673f95a82722238e80b09696e148679c to your computer and use it in GitHub Desktop.
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
import android.content.Context | |
import androidx.work.ListenableWorker | |
import androidx.work.WorkerFactory | |
import androidx.work.WorkerParameters | |
import javax.inject.Inject | |
import javax.inject.Provider | |
class DaggerWorkerFactory @Inject constructor( | |
private val creators: Map<Class<out ListenableWorker>, @JvmSuppressWildcards Provider<ChildWorkerFactory>> | |
) : WorkerFactory() { | |
override fun createWorker( | |
context: Context, | |
workerClassName: String, | |
workerParameters: WorkerParameters | |
): ListenableWorker? { | |
val workerClass = Class.forName(workerClassName) | |
val foundEntry = creators.entries.find { workerClass.isAssignableFrom(it.key) } | |
val factory = foundEntry?.value | |
?: throw IllegalArgumentException("unknown worker class name: $workerClassName") | |
return factory.get().create(context, workerParameters) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment