[Dagger] integration with workers (For Java People)
|
public class AndroidWorkerInjection { |
|
|
|
public static void inject(Worker worker) { |
|
checkNotNull(worker, "worker"); |
|
Object application = worker.getApplicationContext(); |
|
if (!(application instanceof HasWorkerInjector)) { |
|
throw new RuntimeException( |
|
String.format( |
|
"%s does not implement %s", |
|
application.getClass().getCanonicalName(), |
|
HasWorkerInjector.class.getCanonicalName())); |
|
} |
|
|
|
AndroidInjector<Worker> workerInjector = |
|
((HasWorkerInjector) application).workerInjector(); |
|
checkNotNull(workerInjector, "%s.workerInjector() returned null", application.getClass()); |
|
workerInjector.inject(worker); |
|
} |
|
} |
|
|
|
@Module |
|
public abstract class AndroidWorkerInjectionModule { |
|
|
|
@Multibinds |
|
abstract Map<Class<? extends Worker>, AndroidInjector.Factory<? extends Worker>> |
|
workerInjectorFactories(); |
|
} |
|
@Singleton |
|
@Component(modules = { |
|
AndroidSupportInjectionModule.class, |
|
AndroidWorkerInjectionModule.class, |
|
WorkerModule.class}) |
|
public interface AppComponent { |
|
@Component.Builder |
|
interface Builder { |
|
@BindsInstance |
|
Builder application(MyApplication application); |
|
AppComponent build(); |
|
} |
|
void inject(MyApplication app); |
|
} |
|
public interface HasWorkerInjector { |
|
AndroidInjector<Worker> workerInjector(); |
|
} |
|
public class MyApplication extends MultiDexApplication implements HasWorkerInjector { |
|
|
|
@Inject DispatchingAndroidInjector<Worker> workerDispatchingAndroidInjector; |
|
|
|
public AppComponent appComponent; |
|
|
|
@Override |
|
public void onCreate() { |
|
super.onCreate(); |
|
MultiDex.install(this); |
|
appComponent = DaggerAppComponent |
|
.builder() |
|
.application(this) |
|
.build(); |
|
appComponent.inject(this); |
|
|
|
//Call the Profile Worker |
|
OneTimeWorkRequest refreshProfile = |
|
new OneTimeWorkRequest.Builder(ProfileWorker.class) |
|
.addTag(ConstantsUtils.TAG_OUTPUT) |
|
.build(); |
|
WorkManager.getInstance().enqueue(refreshProfile); |
|
} |
|
|
|
@Override |
|
public AndroidInjector<Activity> activityInjector() { |
|
return dispatchingAndroidInjector; |
|
} |
|
|
|
|
|
@Override |
|
public AndroidInjector<Worker> workerInjector() { |
|
return workerDispatchingAndroidInjector; |
|
} |
|
} |
|
public class ProfileWorker extends Worker { |
|
|
|
@Inject |
|
SomeClass clazz; |
|
|
|
@NonNull |
|
@Override |
|
public WorkerResult doWork() { |
|
AndroidWorkerInjection.inject(this); |
|
// Do work here : |
|
return WorkerResult.SUCCESS; |
|
} |
|
} |
|
@Subcomponent |
|
public interface ProfileWorkerModule extends AndroidInjector<ProfileWorker> { |
|
|
|
@Subcomponent.Builder |
|
abstract class Builder extends AndroidInjector.Builder<ProfileWorker>{} |
|
} |
|
@Module(subcomponents = { |
|
ProfileWorkerModule.class, |
|
UploadWorkerModule.class |
|
}) |
|
public abstract class WorkerModule { |
|
|
|
@Binds |
|
@IntoMap |
|
@WorkerKey(ProfileWorker.class) |
|
abstract AndroidInjector.Factory<? extends Worker> bindProfileWorkerFactory(ProfileWorkerModule.Builder profileWorker); |
|
} |
This comment has been minimized.
usmanrana07 commentedAug 7, 2018
•
edited
Hi!
I'm getting exception: java.lang.IllegalArgumentException: No injector factory bound for Class. What's missing?