Resolve Injekt dependencies directly from the Metro graph (#3965)
The previous setup had a circular dependency between Injekt registration and extension loading. In regard to #3931, I've moved the culprit ExtensionManager out of Injekt altogether, alongside CoverCache, PreferenceStore and TrackPreferences, none of which a source needs.
This commit is contained in:
@@ -19,6 +19,7 @@ import eu.kanade.tachiyomi.core.security.SecurityPreferences
|
||||
import eu.kanade.tachiyomi.data.backup.create.BackupCreateJob
|
||||
import eu.kanade.tachiyomi.data.backup.restore.BackupRestoreJob
|
||||
import eu.kanade.tachiyomi.data.cache.ChapterCache
|
||||
import eu.kanade.tachiyomi.data.cache.CoverCache
|
||||
import eu.kanade.tachiyomi.data.download.DownloadCache
|
||||
import eu.kanade.tachiyomi.data.download.DownloadJob
|
||||
import eu.kanade.tachiyomi.data.download.DownloadManager
|
||||
@@ -29,6 +30,7 @@ import eu.kanade.tachiyomi.data.track.TrackerManager
|
||||
import eu.kanade.tachiyomi.data.updater.AppUpdateChecker
|
||||
import eu.kanade.tachiyomi.extension.ExtensionManager
|
||||
import eu.kanade.tachiyomi.extension.util.ExtensionInstallActivity
|
||||
import eu.kanade.tachiyomi.network.JavaScriptEngine
|
||||
import eu.kanade.tachiyomi.network.NetworkHelper
|
||||
import eu.kanade.tachiyomi.network.NetworkPreferences
|
||||
import eu.kanade.tachiyomi.ui.base.delegate.SecureActivityDelegateImpl
|
||||
@@ -39,8 +41,10 @@ import eu.kanade.tachiyomi.ui.setting.track.BaseOAuthLoginActivity
|
||||
import eu.kanade.tachiyomi.ui.webview.WebViewActivity
|
||||
import eu.kanade.tachiyomi.util.CrashLogUtil
|
||||
import kotlinx.serialization.json.Json
|
||||
import kotlinx.serialization.protobuf.ProtoBuf
|
||||
import mihon.core.metro.IsDebugBuild
|
||||
import mihon.domain.extension.interactor.GetExtensionStoreCountAsFlow
|
||||
import nl.adaptivity.xmlutil.serialization.XML
|
||||
import tachiyomi.domain.backup.service.BackupPreferences
|
||||
import tachiyomi.domain.category.interactor.GetCategories
|
||||
import tachiyomi.domain.category.interactor.ResetCategoryFlags
|
||||
@@ -101,10 +105,14 @@ interface AppGraph : ViewModelGraph {
|
||||
val trackerManager: TrackerManager
|
||||
val extensionManager: ExtensionManager
|
||||
val chapterCache: ChapterCache
|
||||
val coverCache: CoverCache
|
||||
val downloadCache: DownloadCache
|
||||
|
||||
val json: Json
|
||||
val protoBuf: ProtoBuf
|
||||
val xml: XML
|
||||
val networkHelper: NetworkHelper
|
||||
val javaScriptEngine: JavaScriptEngine
|
||||
|
||||
val getFavorites: GetFavorites
|
||||
val getCategories: GetCategories
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
package mihon.app.di.injekt
|
||||
|
||||
import android.app.Application
|
||||
import android.content.Context
|
||||
import eu.kanade.tachiyomi.network.JavaScriptEngine
|
||||
import eu.kanade.tachiyomi.network.NetworkHelper
|
||||
import kotlinx.serialization.json.Json
|
||||
import kotlinx.serialization.protobuf.ProtoBuf
|
||||
import mihon.app.di.AppGraph
|
||||
import mihon.core.metro.GraphProvider
|
||||
import nl.adaptivity.xmlutil.serialization.XML
|
||||
import uy.kohesive.injekt.api.InjektModule
|
||||
import uy.kohesive.injekt.api.InjektRegistrar
|
||||
import uy.kohesive.injekt.api.InjektionException
|
||||
import uy.kohesive.injekt.api.TypeReference
|
||||
import java.lang.reflect.Type
|
||||
|
||||
class MetroInjektRegistrar(
|
||||
private val application: Application,
|
||||
private val graphProvider: GraphProvider<AppGraph>,
|
||||
) : InjektRegistrar {
|
||||
|
||||
private val graph: AppGraph inline get() = graphProvider.graph
|
||||
|
||||
private val bindings = mapOf<Type, () -> Any>(
|
||||
Application::class.java to { application },
|
||||
Context::class.java to { application },
|
||||
|
||||
Json::class.java to { graph.json },
|
||||
ProtoBuf::class.java to { graph.protoBuf },
|
||||
XML::class.java to { graph.xml },
|
||||
|
||||
NetworkHelper::class.java to { graph.networkHelper },
|
||||
JavaScriptEngine::class.java to { graph.javaScriptEngine },
|
||||
)
|
||||
|
||||
override fun <R : Any> getInstance(forType: Type): R = getInstanceOrNull(forType)
|
||||
?: throw InjektionException("$forType is not exposed to Injekt, add it to ${this::class.simpleName}")
|
||||
|
||||
override fun <R : Any> getInstanceOrElse(forType: Type, default: R): R = getInstanceOrNull(forType) ?: default
|
||||
|
||||
override fun <R : Any> getInstanceOrElse(forType: Type, default: () -> R): R =
|
||||
getInstanceOrNull(forType) ?: default()
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
override fun <R : Any> getInstanceOrNull(forType: Type): R? = bindings[forType]?.invoke() as R?
|
||||
|
||||
override fun <R : Any, K : Any> getKeyedInstance(forType: Type, key: K): R = unsupported("Keyed injection")
|
||||
|
||||
override fun <R : Any, K : Any> getKeyedInstanceOrElse(forType: Type, key: K, default: R): R =
|
||||
unsupported("Keyed injection")
|
||||
|
||||
override fun <R : Any, K : Any> getKeyedInstanceOrElse(forType: Type, key: K, default: () -> R): R =
|
||||
unsupported("Keyed injection")
|
||||
|
||||
@Suppress("RedundantNullableReturnType")
|
||||
override fun <R : Any, K : Any> getKeyedInstanceOrNull(forType: Type, key: K): R? = unsupported("Keyed injection")
|
||||
|
||||
override fun <R : Any> getLogger(expectedLoggerType: Type, byName: String): R = unsupported("Logger injection")
|
||||
|
||||
override fun <R : Any, T : Any> getLogger(expectedLoggerType: Type, forClass: Class<T>): R =
|
||||
unsupported("Logger injection")
|
||||
|
||||
override fun <T : Any> addSingleton(forType: TypeReference<T>, singleInstance: T) = readOnly()
|
||||
|
||||
override fun <R : Any> addSingletonFactory(forType: TypeReference<R>, factoryCalledOnce: () -> R) = readOnly()
|
||||
|
||||
override fun <R : Any> addFactory(forType: TypeReference<R>, factoryCalledEveryTime: () -> R) = readOnly()
|
||||
|
||||
override fun <R : Any> addPerThreadFactory(forType: TypeReference<R>, factoryCalledOncePerThread: () -> R) =
|
||||
readOnly()
|
||||
|
||||
override fun <R : Any, K : Any> addPerKeyFactory(forType: TypeReference<R>, factoryCalledPerKey: (K) -> R) =
|
||||
readOnly()
|
||||
|
||||
override fun <R : Any, K : Any> addPerThreadPerKeyFactory(
|
||||
forType: TypeReference<R>,
|
||||
factoryCalledPerKeyPerThread: (K) -> R,
|
||||
) = readOnly()
|
||||
|
||||
override fun <R : Any> addLoggerFactory(
|
||||
forLoggerType: TypeReference<R>,
|
||||
factoryByName: (String) -> R,
|
||||
factoryByClass: (Class<Any>) -> R,
|
||||
) = readOnly()
|
||||
|
||||
override fun <O : Any, T : O> addAlias(
|
||||
existingRegisteredType: TypeReference<T>,
|
||||
otherAncestorOrInterface: TypeReference<O>,
|
||||
) = readOnly()
|
||||
|
||||
override fun <T : Any> hasFactory(forType: TypeReference<T>): Boolean = forType.type in bindings
|
||||
|
||||
override fun importModule(submodule: InjektModule) = readOnly()
|
||||
|
||||
private fun readOnly(): Nothing = throw UnsupportedOperationException("Mihon's Injekt instance is read-only")
|
||||
|
||||
private fun unsupported(feature: String): Nothing =
|
||||
throw UnsupportedOperationException("$feature is not supported by Mihon's Injekt instance")
|
||||
}
|
||||
@@ -1,49 +0,0 @@
|
||||
package mihon.app.di.injekt
|
||||
|
||||
import dev.zacsweers.metro.Inject
|
||||
import eu.kanade.domain.track.service.TrackPreferences
|
||||
import eu.kanade.tachiyomi.data.cache.CoverCache
|
||||
import eu.kanade.tachiyomi.extension.ExtensionManager
|
||||
import eu.kanade.tachiyomi.network.JavaScriptEngine
|
||||
import eu.kanade.tachiyomi.network.NetworkHelper
|
||||
import kotlinx.serialization.json.Json
|
||||
import kotlinx.serialization.protobuf.ProtoBuf
|
||||
import nl.adaptivity.xmlutil.serialization.XML
|
||||
import tachiyomi.core.common.preference.PreferenceStore
|
||||
import uy.kohesive.injekt.api.InjektModule
|
||||
import uy.kohesive.injekt.api.InjektRegistrar
|
||||
import uy.kohesive.injekt.api.addSingleton
|
||||
|
||||
@Inject
|
||||
class MetroInteropModule(
|
||||
private val json: Json,
|
||||
private val protoBuf: ProtoBuf,
|
||||
private val xml: XML,
|
||||
|
||||
private val networkHelper: NetworkHelper,
|
||||
private val javaScriptEngine: JavaScriptEngine,
|
||||
|
||||
private val preferenceStore: PreferenceStore,
|
||||
private val trackPreferences: TrackPreferences,
|
||||
|
||||
private val extensionManager: ExtensionManager,
|
||||
|
||||
private val coverCache: CoverCache,
|
||||
) : InjektModule {
|
||||
|
||||
override fun InjektRegistrar.registerInjectables() {
|
||||
addSingleton(json)
|
||||
addSingleton(protoBuf)
|
||||
addSingleton(xml)
|
||||
|
||||
addSingleton(networkHelper)
|
||||
addSingleton(javaScriptEngine)
|
||||
|
||||
addSingleton(preferenceStore)
|
||||
addSingleton(trackPreferences)
|
||||
|
||||
addSingleton(extensionManager)
|
||||
|
||||
addSingleton(coverCache)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user