Created
January 13, 2020 11:06
-
-
Save ubarua123/900bba0d8fcbcb27ce6609a67d458523 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
public class WorkManagerWorkFactory extends WorkerFactory { | |
private final WorkManagerFactorySubComponent.Builder subComponentBuilder; | |
@Inject | |
public WorkManagerWorkFactory(WorkManagerFactorySubComponent.Builder workerFactorySubComponent) { | |
this.subComponentBuilder = workerFactorySubComponent; | |
} | |
@Nullable | |
@Override | |
public RxWorker createWorker(@NonNull Context appContext, @NonNull String workerClassName, @NonNull WorkerParameters workerParameters) { | |
WorkManagerFactorySubComponent workManagerFactorySubComponent = subComponentBuilder.parameters(workerParameters).build(); | |
return createWorker(workerClassName, workManagerFactorySubComponent.getWorkerMap()); | |
} | |
@SneakyThrows | |
private RxWorker createWorker(String workerClassName, Map<Class<? extends RxWorker>, Provider<RxWorker>> workerMap) { | |
// Get the worker class definition | |
Class<? extends RxWorker> workerClass = Class.forName(workerClassName).asSubclass(RxWorker.class); | |
// Get the worker class instance | |
Provider<RxWorker> provider = workerMap.get(workerClass); | |
if (provider == null) { | |
// Fetch the binding | |
for (Map.Entry<Class<? extends RxWorker>, Provider<RxWorker>> entry : workerMap.entrySet()) { | |
if (workerClass.isAssignableFrom(entry.getKey())) { | |
provider = entry.getValue(); | |
break; | |
} | |
} | |
} | |
if (provider == null) | |
throw new IllegalArgumentException("Missing binding for " + workerClassName + ". Consider adding an entry in " + WorkerBindingModule.class.getSimpleName()); | |
return provider.get(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment