mirror of
				https://github.com/mihonapp/mihon.git
				synced 2025-10-31 06:17:57 +01:00 
			
		
		
		
	Replace Dagger2 with Injekt, reorganize dependencies
This commit is contained in:
		| @@ -3,12 +3,10 @@ package eu.kanade.tachiyomi | ||||
| import android.app.Application | ||||
| import android.content.Context | ||||
| import eu.kanade.tachiyomi.data.preference.PreferencesHelper | ||||
| import eu.kanade.tachiyomi.injection.AppComponentFactory | ||||
| import eu.kanade.tachiyomi.injection.ComponentReflectionInjector | ||||
| import eu.kanade.tachiyomi.injection.component.AppComponent | ||||
| import org.acra.ACRA | ||||
| import org.acra.annotation.ReportsCrashes | ||||
| import timber.log.Timber | ||||
| import uy.kohesive.injekt.Injekt | ||||
|  | ||||
| @ReportsCrashes( | ||||
|         formUri = "http://tachiyomi.kanade.eu/crash_report", | ||||
| @@ -19,22 +17,13 @@ import timber.log.Timber | ||||
| ) | ||||
| open class App : Application() { | ||||
|  | ||||
|     lateinit var component: AppComponent | ||||
|         private set | ||||
|  | ||||
|     lateinit var componentReflection: ComponentReflectionInjector<AppComponent> | ||||
|         private set | ||||
|  | ||||
|     var appTheme = 0 | ||||
|  | ||||
|     override fun onCreate() { | ||||
|         super.onCreate() | ||||
|         Injekt.importModule(AppModule(this)) | ||||
|         if (BuildConfig.DEBUG) Timber.plant(Timber.DebugTree()) | ||||
|  | ||||
|         component = createAppComponent() | ||||
|  | ||||
|         componentReflection = ComponentReflectionInjector(AppComponent::class.java, component) | ||||
|  | ||||
|         setupTheme() | ||||
|         setupAcra() | ||||
|     } | ||||
| @@ -43,10 +32,6 @@ open class App : Application() { | ||||
|         appTheme = PreferencesHelper.getTheme(this) | ||||
|     } | ||||
|  | ||||
|     protected open fun createAppComponent(): AppComponent { | ||||
|         return AppComponentFactory.create(this) | ||||
|     } | ||||
|  | ||||
|     protected open fun setupAcra() { | ||||
|         ACRA.init(this) | ||||
|     } | ||||
|   | ||||
							
								
								
									
										41
									
								
								app/src/main/java/eu/kanade/tachiyomi/AppModule.kt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										41
									
								
								app/src/main/java/eu/kanade/tachiyomi/AppModule.kt
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,41 @@ | ||||
| package eu.kanade.tachiyomi | ||||
|  | ||||
| import android.app.Application | ||||
| import com.google.gson.Gson | ||||
| import eu.kanade.tachiyomi.data.cache.ChapterCache | ||||
| import eu.kanade.tachiyomi.data.cache.CoverCache | ||||
| import eu.kanade.tachiyomi.data.database.DatabaseHelper | ||||
| import eu.kanade.tachiyomi.data.download.DownloadManager | ||||
| import eu.kanade.tachiyomi.data.mangasync.MangaSyncManager | ||||
| import eu.kanade.tachiyomi.data.network.NetworkHelper | ||||
| import eu.kanade.tachiyomi.data.preference.PreferencesHelper | ||||
| import eu.kanade.tachiyomi.data.source.SourceManager | ||||
| import uy.kohesive.injekt.api.InjektModule | ||||
| import uy.kohesive.injekt.api.InjektRegistrar | ||||
| import uy.kohesive.injekt.api.addSingletonFactory | ||||
|  | ||||
| class AppModule(val app: Application) : InjektModule { | ||||
|  | ||||
|     override fun InjektRegistrar.registerInjectables() { | ||||
|  | ||||
|             addSingletonFactory { PreferencesHelper(app) } | ||||
|  | ||||
|             addSingletonFactory { DatabaseHelper(app) } | ||||
|  | ||||
|             addSingletonFactory { ChapterCache(app) } | ||||
|  | ||||
|             addSingletonFactory { CoverCache(app) } | ||||
|  | ||||
|             addSingletonFactory { NetworkHelper(app) } | ||||
|  | ||||
|             addSingletonFactory { SourceManager(app) } | ||||
|  | ||||
|             addSingletonFactory { DownloadManager(app) } | ||||
|  | ||||
|             addSingletonFactory { MangaSyncManager(app) } | ||||
|  | ||||
|             addSingletonFactory { Gson() } | ||||
|  | ||||
|     } | ||||
|  | ||||
| } | ||||
| @@ -1,16 +0,0 @@ | ||||
| package eu.kanade.tachiyomi.injection; | ||||
|  | ||||
| import eu.kanade.tachiyomi.App; | ||||
| import eu.kanade.tachiyomi.injection.component.AppComponent; | ||||
| import eu.kanade.tachiyomi.injection.component.DaggerAppComponent; | ||||
| import eu.kanade.tachiyomi.injection.module.AppModule; | ||||
|  | ||||
|  | ||||
| public class AppComponentFactory { | ||||
|  | ||||
|     public static AppComponent create(App app) { | ||||
|         return DaggerAppComponent.builder().appModule(new AppModule(app)).build(); | ||||
|     } | ||||
| } | ||||
|  | ||||
|  | ||||
| @@ -1,97 +0,0 @@ | ||||
| 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 static final ConcurrentHashMap<Class<?>, HashMap<Class<?>, Method>> cache = new ConcurrentHashMap<>(); | ||||
|  | ||||
|     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 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; | ||||
|     } | ||||
| } | ||||
| @@ -1,67 +0,0 @@ | ||||
| package eu.kanade.tachiyomi.injection.component | ||||
|  | ||||
| import android.app.Application | ||||
| import dagger.Component | ||||
| import eu.kanade.tachiyomi.data.download.DownloadService | ||||
| import eu.kanade.tachiyomi.data.glide.AppGlideModule | ||||
| import eu.kanade.tachiyomi.data.glide.MangaModelLoader | ||||
| import eu.kanade.tachiyomi.data.library.LibraryUpdateService | ||||
| import eu.kanade.tachiyomi.data.mangasync.MangaSyncService | ||||
| import eu.kanade.tachiyomi.data.mangasync.UpdateMangaSyncService | ||||
| import eu.kanade.tachiyomi.data.source.Source | ||||
| import eu.kanade.tachiyomi.data.source.online.OnlineSource | ||||
| import eu.kanade.tachiyomi.data.updater.UpdateDownloader | ||||
| import eu.kanade.tachiyomi.injection.module.AppModule | ||||
| import eu.kanade.tachiyomi.injection.module.DataModule | ||||
| import eu.kanade.tachiyomi.ui.backup.BackupPresenter | ||||
| import eu.kanade.tachiyomi.ui.catalogue.CataloguePresenter | ||||
| import eu.kanade.tachiyomi.ui.category.CategoryPresenter | ||||
| import eu.kanade.tachiyomi.ui.download.DownloadPresenter | ||||
| import eu.kanade.tachiyomi.ui.library.LibraryPresenter | ||||
| import eu.kanade.tachiyomi.ui.main.MainActivity | ||||
| 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.ReaderPresenter | ||||
| import eu.kanade.tachiyomi.ui.recent_updates.RecentChaptersPresenter | ||||
| import eu.kanade.tachiyomi.ui.recently_read.RecentlyReadPresenter | ||||
| import eu.kanade.tachiyomi.ui.setting.SettingsActivity | ||||
| import javax.inject.Singleton | ||||
|  | ||||
| @Singleton | ||||
| @Component(modules = arrayOf(AppModule::class, DataModule::class)) | ||||
| interface AppComponent { | ||||
|  | ||||
|     fun inject(libraryPresenter: LibraryPresenter) | ||||
|     fun inject(mangaPresenter: MangaPresenter) | ||||
|     fun inject(cataloguePresenter: CataloguePresenter) | ||||
|     fun inject(mangaInfoPresenter: MangaInfoPresenter) | ||||
|     fun inject(chaptersPresenter: ChaptersPresenter) | ||||
|     fun inject(readerPresenter: ReaderPresenter) | ||||
|     fun inject(downloadPresenter: DownloadPresenter) | ||||
|     fun inject(myAnimeListPresenter: MyAnimeListPresenter) | ||||
|     fun inject(categoryPresenter: CategoryPresenter) | ||||
|     fun inject(recentChaptersPresenter: RecentChaptersPresenter) | ||||
|     fun inject(recentlyReadPresenter: RecentlyReadPresenter) | ||||
|     fun inject(backupPresenter: BackupPresenter) | ||||
|  | ||||
|     fun inject(mainActivity: MainActivity) | ||||
|     fun inject(settingsActivity: SettingsActivity) | ||||
|  | ||||
|     fun inject(source: Source) | ||||
|     fun inject(mangaSyncService: MangaSyncService) | ||||
|  | ||||
|     fun inject(onlineSource: OnlineSource) | ||||
|  | ||||
|     fun inject(libraryUpdateService: LibraryUpdateService) | ||||
|     fun inject(downloadService: DownloadService) | ||||
|     fun inject(updateMangaSyncService: UpdateMangaSyncService) | ||||
|  | ||||
|     fun inject(mangaModelLoader: MangaModelLoader) | ||||
|     fun inject(appGlideModule: AppGlideModule) | ||||
|  | ||||
|     fun inject(updateDownloader: UpdateDownloader) | ||||
|     fun application(): Application | ||||
|  | ||||
| } | ||||
| @@ -1,21 +0,0 @@ | ||||
| package eu.kanade.tachiyomi.injection.module | ||||
|  | ||||
| import android.app.Application | ||||
| import dagger.Module | ||||
| import dagger.Provides | ||||
| import javax.inject.Singleton | ||||
|  | ||||
| /** | ||||
|  * Provide application-level dependencies. Mainly singleton object that can be injected from | ||||
|  * anywhere in the app. | ||||
|  */ | ||||
| @Module | ||||
| class AppModule(private val application: Application) { | ||||
|  | ||||
|     @Provides | ||||
|     @Singleton | ||||
|     fun provideApplication(): Application { | ||||
|         return application | ||||
|     } | ||||
|  | ||||
| } | ||||
| @@ -1,70 +0,0 @@ | ||||
| package eu.kanade.tachiyomi.injection.module | ||||
|  | ||||
| import android.app.Application | ||||
| 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.database.DatabaseHelper | ||||
| import eu.kanade.tachiyomi.data.download.DownloadManager | ||||
| import eu.kanade.tachiyomi.data.mangasync.MangaSyncManager | ||||
| import eu.kanade.tachiyomi.data.network.NetworkHelper | ||||
| import eu.kanade.tachiyomi.data.preference.PreferencesHelper | ||||
| import eu.kanade.tachiyomi.data.source.SourceManager | ||||
| import javax.inject.Singleton | ||||
|  | ||||
| /** | ||||
|  * Provide dependencies to the DataManager, mainly Helper classes and Retrofit services. | ||||
|  */ | ||||
| @Module | ||||
| open class DataModule { | ||||
|  | ||||
|     @Provides | ||||
|     @Singleton | ||||
|     fun providePreferencesHelper(app: Application): PreferencesHelper { | ||||
|         return PreferencesHelper(app) | ||||
|     } | ||||
|  | ||||
|     @Provides | ||||
|     @Singleton | ||||
|     open fun provideDatabaseHelper(app: Application): DatabaseHelper { | ||||
|         return DatabaseHelper(app) | ||||
|     } | ||||
|  | ||||
|     @Provides | ||||
|     @Singleton | ||||
|     fun provideChapterCache(app: Application): ChapterCache { | ||||
|         return ChapterCache(app) | ||||
|     } | ||||
|  | ||||
|     @Provides | ||||
|     @Singleton | ||||
|     fun provideCoverCache(app: Application): CoverCache { | ||||
|         return CoverCache(app) | ||||
|     } | ||||
|  | ||||
|     @Provides | ||||
|     @Singleton | ||||
|     open fun provideNetworkHelper(app: Application): NetworkHelper { | ||||
|         return NetworkHelper(app) | ||||
|     } | ||||
|  | ||||
|     @Provides | ||||
|     @Singleton | ||||
|     open fun provideSourceManager(app: Application): SourceManager { | ||||
|         return SourceManager(app) | ||||
|     } | ||||
|  | ||||
|     @Provides | ||||
|     @Singleton | ||||
|     fun provideDownloadManager(app: Application, sourceManager: SourceManager, preferences: PreferencesHelper): DownloadManager { | ||||
|         return DownloadManager(app, sourceManager, preferences) | ||||
|     } | ||||
|  | ||||
|     @Provides | ||||
|     @Singleton | ||||
|     fun provideMangaSyncManager(app: Application): MangaSyncManager { | ||||
|         return MangaSyncManager(app) | ||||
|     } | ||||
|  | ||||
| } | ||||
		Reference in New Issue
	
	Block a user