Move dagger classes to injection directory

This commit is contained in:
inorichi
2015-10-21 21:04:10 +02:00
parent 57290a5395
commit 38c6fac385
7 changed files with 12 additions and 15 deletions

View File

@@ -0,0 +1,97 @@
package eu.kanade.mangafeed.injection;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.concurrent.ConcurrentHashMap;
/**
* This class allows to inject into objects through a base class,
* so we don't have to repeat injection code everywhere.
*
* The performance drawback is about 0.013 ms per injection on a very slow device,
* which is negligible in most cases.
*
* Example:
* <pre>{@code
* Component {
* void inject(B b);
* }
*
* class A {
* void onCreate() {
* componentReflectionInjector.inject(this);
* }
* }
*
* class B extends A {
* @Inject MyDependency dependency;
* }
*
* new B().onCreate() // dependency will be injected at this point
*
* class C extends B {
*
* }
*
* new C().onCreate() // dependency will be injected at this point as well
* }</pre>
*
* @param <T> a type of dagger 2 component.
*/
public final class ComponentReflectionInjector<T> {
private final Class<T> componentClass;
private final T component;
private final HashMap<Class<?>, Method> methods;
public ComponentReflectionInjector(Class<T> componentClass, T component) {
this.componentClass = componentClass;
this.component = component;
this.methods = getMethods(componentClass);
}
public T getComponent() {
return component;
}
public void inject(Object target) {
Class targetClass = target.getClass();
Method method = methods.get(targetClass);
while (method == null && targetClass != null) {
targetClass = targetClass.getSuperclass();
method = methods.get(targetClass);
}
if (method == null)
throw new RuntimeException(String.format("No %s injecting method exists in %s component", target.getClass(), componentClass));
try {
method.invoke(component, target);
}
catch (Exception e) {
throw new RuntimeException(e);
}
}
private static final ConcurrentHashMap<Class<?>, HashMap<Class<?>, Method>> cache = new ConcurrentHashMap<>();
private static HashMap<Class<?>, Method> getMethods(Class componentClass) {
HashMap<Class<?>, Method> methods = cache.get(componentClass);
if (methods == null) {
synchronized (cache) {
methods = cache.get(componentClass);
if (methods == null) {
methods = new HashMap<>();
for (Method method : componentClass.getMethods()) {
Class<?>[] params = method.getParameterTypes();
if (params.length == 1)
methods.put(params[0], method);
}
cache.put(componentClass, methods);
}
}
}
return methods;
}
}

View File

@@ -0,0 +1,37 @@
package eu.kanade.mangafeed.injection.component;
import android.app.Application;
import javax.inject.Singleton;
import dagger.Component;
import eu.kanade.mangafeed.injection.module.AppModule;
import eu.kanade.mangafeed.injection.module.DataModule;
import eu.kanade.mangafeed.presenter.CataloguePresenter;
import eu.kanade.mangafeed.presenter.LibraryPresenter;
import eu.kanade.mangafeed.presenter.MangaChaptersPresenter;
import eu.kanade.mangafeed.presenter.MangaDetailPresenter;
import eu.kanade.mangafeed.presenter.MangaInfoPresenter;
import eu.kanade.mangafeed.presenter.ReaderPresenter;
import eu.kanade.mangafeed.presenter.SourcePresenter;
@Singleton
@Component(
modules = {
AppModule.class,
DataModule.class
}
)
public interface AppComponent {
void inject(LibraryPresenter libraryPresenter);
void inject(MangaDetailPresenter mangaDetailPresenter);
void inject(SourcePresenter sourcePresenter);
void inject(CataloguePresenter cataloguePresenter);
void inject(MangaInfoPresenter mangaInfoPresenter);
void inject(MangaChaptersPresenter mangaChaptersPresenter);
void inject(ReaderPresenter readerPresenter);
Application application();
}

View File

@@ -0,0 +1,28 @@
package eu.kanade.mangafeed.injection.module;
import android.app.Application;
import javax.inject.Singleton;
import dagger.Module;
import dagger.Provides;
/**
* Provide application-level dependencies. Mainly singleton object that can be injected from
* anywhere in the app.
*/
@Module
public class AppModule {
protected final Application mApplication;
public AppModule(Application application) {
mApplication = application;
}
@Provides
@Singleton
Application provideApplication() {
return mApplication;
}
}

View File

@@ -0,0 +1,58 @@
package eu.kanade.mangafeed.injection.module;
import android.app.Application;
import javax.inject.Singleton;
import dagger.Module;
import dagger.Provides;
import eu.kanade.mangafeed.data.caches.CacheManager;
import eu.kanade.mangafeed.data.helpers.DatabaseHelper;
import eu.kanade.mangafeed.data.helpers.NetworkHelper;
import eu.kanade.mangafeed.data.helpers.PreferencesHelper;
import eu.kanade.mangafeed.data.helpers.SourceManager;
import rx.Scheduler;
import rx.schedulers.Schedulers;
/**
* Provide dependencies to the DataManager, mainly Helper classes and Retrofit services.
*/
@Module
public class DataModule {
@Provides
@Singleton
PreferencesHelper providePreferencesHelper(Application app) {
return new PreferencesHelper(app);
}
@Provides
@Singleton
DatabaseHelper provideDatabaseHelper(Application app) {
return new DatabaseHelper(app);
}
@Provides
@Singleton
Scheduler provideSubscribeScheduler() {
return Schedulers.io();
}
@Provides
@Singleton
CacheManager provideCacheManager(Application app) {
return new CacheManager(app);
}
@Provides
@Singleton
NetworkHelper provideNetworkHelper() {
return new NetworkHelper();
}
@Provides
@Singleton
SourceManager provideSourceManager(NetworkHelper networkHelper, CacheManager cacheManager) {
return new SourceManager(networkHelper, cacheManager);
}
}