mirror of
				https://github.com/mihonapp/mihon.git
				synced 2025-10-30 22:07:57 +01:00 
			
		
		
		
	Rename project
This commit is contained in:
		| @@ -0,0 +1,97 @@ | ||||
| package eu.kanade.tachiyomi.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; | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,63 @@ | ||||
| package eu.kanade.tachiyomi.injection.component; | ||||
|  | ||||
| import android.app.Application; | ||||
|  | ||||
| import javax.inject.Singleton; | ||||
|  | ||||
| import dagger.Component; | ||||
| import eu.kanade.tachiyomi.data.download.DownloadService; | ||||
| import eu.kanade.tachiyomi.data.mangasync.services.MyAnimeList; | ||||
| import eu.kanade.tachiyomi.data.source.base.Source; | ||||
| import eu.kanade.tachiyomi.data.sync.LibraryUpdateService; | ||||
| import eu.kanade.tachiyomi.data.sync.UpdateMangaSyncService; | ||||
| import eu.kanade.tachiyomi.injection.module.AppModule; | ||||
| import eu.kanade.tachiyomi.injection.module.DataModule; | ||||
| import eu.kanade.tachiyomi.ui.catalogue.CataloguePresenter; | ||||
| import eu.kanade.tachiyomi.ui.download.DownloadPresenter; | ||||
| import eu.kanade.tachiyomi.ui.library.category.CategoryPresenter; | ||||
| import eu.kanade.tachiyomi.ui.library.LibraryPresenter; | ||||
| import eu.kanade.tachiyomi.ui.manga.MangaActivity; | ||||
| import eu.kanade.tachiyomi.ui.manga.MangaPresenter; | ||||
| import eu.kanade.tachiyomi.ui.manga.chapter.ChaptersPresenter; | ||||
| import eu.kanade.tachiyomi.ui.manga.info.MangaInfoPresenter; | ||||
| import eu.kanade.tachiyomi.ui.manga.myanimelist.MyAnimeListPresenter; | ||||
| import eu.kanade.tachiyomi.ui.reader.ReaderActivity; | ||||
| import eu.kanade.tachiyomi.ui.reader.ReaderPresenter; | ||||
| import eu.kanade.tachiyomi.ui.setting.SettingsAccountsFragment; | ||||
| import eu.kanade.tachiyomi.ui.setting.SettingsActivity; | ||||
|  | ||||
| @Singleton | ||||
| @Component( | ||||
|         modules = { | ||||
|                 AppModule.class, | ||||
|                 DataModule.class | ||||
|         } | ||||
| ) | ||||
| public interface AppComponent { | ||||
|  | ||||
|     void inject(LibraryPresenter libraryPresenter); | ||||
|     void inject(MangaPresenter mangaPresenter); | ||||
|     void inject(CataloguePresenter cataloguePresenter); | ||||
|     void inject(MangaInfoPresenter mangaInfoPresenter); | ||||
|     void inject(ChaptersPresenter chaptersPresenter); | ||||
|     void inject(ReaderPresenter readerPresenter); | ||||
|     void inject(DownloadPresenter downloadPresenter); | ||||
|     void inject(MyAnimeListPresenter myAnimeListPresenter); | ||||
|     void inject(CategoryPresenter categoryPresenter); | ||||
|  | ||||
|     void inject(ReaderActivity readerActivity); | ||||
|     void inject(MangaActivity mangaActivity); | ||||
|     void inject(SettingsAccountsFragment settingsAccountsFragment); | ||||
|     void inject(SettingsActivity settingsActivity); | ||||
|  | ||||
|     void inject(Source source); | ||||
|  | ||||
|     void inject(MyAnimeList myAnimeList); | ||||
|  | ||||
|     void inject(LibraryUpdateService libraryUpdateService); | ||||
|     void inject(DownloadService downloadService); | ||||
|     void inject(UpdateMangaSyncService updateMangaSyncService); | ||||
|  | ||||
|     Application application(); | ||||
|  | ||||
| } | ||||
| @@ -0,0 +1,28 @@ | ||||
| package eu.kanade.tachiyomi.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; | ||||
|     } | ||||
|  | ||||
| } | ||||
| @@ -0,0 +1,73 @@ | ||||
| package eu.kanade.tachiyomi.injection.module; | ||||
|  | ||||
| import android.app.Application; | ||||
|  | ||||
| import javax.inject.Singleton; | ||||
|  | ||||
| import dagger.Module; | ||||
| import dagger.Provides; | ||||
| import eu.kanade.tachiyomi.data.cache.ChapterCache; | ||||
| import eu.kanade.tachiyomi.data.cache.CoverCache; | ||||
| import eu.kanade.tachiyomi.data.mangasync.MangaSyncManager; | ||||
| import eu.kanade.tachiyomi.data.database.DatabaseHelper; | ||||
| import eu.kanade.tachiyomi.data.download.DownloadManager; | ||||
| import eu.kanade.tachiyomi.data.network.NetworkHelper; | ||||
| import eu.kanade.tachiyomi.data.preference.PreferencesHelper; | ||||
| import eu.kanade.tachiyomi.data.source.SourceManager; | ||||
|  | ||||
| /** | ||||
|  * 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 | ||||
|     ChapterCache provideChapterCache(Application app) { | ||||
|         return new ChapterCache(app); | ||||
|     } | ||||
|  | ||||
|     @Provides | ||||
|     @Singleton | ||||
|     CoverCache provideCoverCache(Application app) { | ||||
|         return new CoverCache(app); | ||||
|     } | ||||
|  | ||||
|     @Provides | ||||
|     @Singleton | ||||
|     NetworkHelper provideNetworkHelper() { | ||||
|         return new NetworkHelper(); | ||||
|     } | ||||
|  | ||||
|     @Provides | ||||
|     @Singleton | ||||
|     SourceManager provideSourceManager(Application app) { | ||||
|         return new SourceManager(app); | ||||
|     } | ||||
|  | ||||
|     @Provides | ||||
|     @Singleton | ||||
|     DownloadManager provideDownloadManager( | ||||
|             Application app, SourceManager sourceManager, PreferencesHelper preferences) { | ||||
|         return new DownloadManager(app, sourceManager, preferences); | ||||
|     } | ||||
|  | ||||
|     @Provides | ||||
|     @Singleton | ||||
|     MangaSyncManager provideMangaSyncManager(Application app) { | ||||
|         return new MangaSyncManager(app); | ||||
|     } | ||||
|  | ||||
| } | ||||
		Reference in New Issue
	
	Block a user