Created
May 30, 2018 06:40
-
-
Save ferrerojosh/82bd92748f315155fa6a842f4ed64c82 to your computer and use it in GitHub Desktop.
androidx workmanager injector temporary impl
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 androidx.work.Worker | |
object AndroidWorkerInjection { | |
fun inject(worker: Worker) { | |
checkNotNull(worker, { "worker" }) | |
val application = worker.applicationContext | |
if (application !is HasWorkerInjector) { | |
throw RuntimeException("${application.javaClass.canonicalName} does not implement ${HasWorkerInjector::class.java.canonicalName}") | |
} | |
val workerInjector = (application as HasWorkerInjector).workerInjector() | |
checkNotNull(workerInjector, { "${application.javaClass}.workerInjector() return null" }) | |
workerInjector.inject(worker) | |
} | |
} |
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 androidx.work.Worker | |
import dagger.Module | |
import dagger.android.AndroidInjector | |
import dagger.multibindings.Multibinds | |
@Module | |
abstract class AndroidWorkerInjectionModule { | |
@Multibinds | |
abstract fun workerInjectorFactories(): Map<Class<out Worker>, AndroidInjector.Factory<out Worker>> | |
} |
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 dagger.Component | |
import dagger.android.AndroidInjectionModule | |
import dagger.android.AndroidInjector | |
import javax.inject.Singleton | |
@Singleton | |
@Component(modules = [ | |
// AppModule::class, | |
// AndroidInjectionModule::class | |
// And all other related modules | |
AndroidWorkerInjectionModule::class | |
]) | |
interface AppComponent : AndroidInjector<App> { | |
@Component.Builder | |
abstract class Builder : AndroidInjector.Builder<App>() | |
} |
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 androidx.work.Worker | |
import dagger.android.AndroidInjector | |
interface HasWorkerInjector { | |
fun workerInjector(): AndroidInjector<Worker> | |
} |
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 androidx.work.Worker | |
import javax.inject.Inject | |
class MyWorker : Worker() { | |
override fun doWork(): WorkerResult { | |
AndroidWorkerInjection.inject(this) | |
return WorkerResult.SUCCESS | |
} | |
} |
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 dagger.Subcomponent | |
import dagger.android.AndroidInjector | |
@Subcomponent | |
interface MyWorkerSubcomponent : AndroidInjector<MyWorker> { | |
@Subcomponent.Builder | |
abstract class Builder : AndroidInjector.Builder<MyWorker>() | |
} |
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 androidx.work.Worker | |
import dagger.MapKey | |
import kotlin.reflect.KClass | |
@MapKey | |
@Retention(AnnotationRetention.RUNTIME) | |
@Target(AnnotationTarget.FUNCTION) | |
annotation class WorkerKey(val value: KClass<out Worker>) |
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 dagger.Binds | |
import dagger.Module | |
import dagger.android.AndroidInjector | |
import dagger.multibindings.IntoMap | |
@Module(subcomponents = [ | |
MyWorkerSubcomponent::class | |
]) | |
abstract class WorkerModule { | |
@Binds | |
@IntoMap | |
@WorkerKey(MyWorker::class) | |
abstract fun bindMyWorkerFactory(builder: MyWorkerSubcomponent.Builder): AndroidInjector.Factory<out Worker> | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Since the update from Work Manager and Dagger broke some things, please use this gist @luanmm made instead: https://gist.github.com/luanmm/85dd8217ed3f7384e6bab075a8ab7a61