Skip to content

Commit

Permalink
Switch Injekt to Koin-Injekt bridge and implement InjektRegistrar bridge
Browse files Browse the repository at this point in the history
  • Loading branch information
jobobby04 committed Oct 14, 2024
1 parent dea3891 commit 2f8efe0
Show file tree
Hide file tree
Showing 11 changed files with 115 additions and 54 deletions.
4 changes: 4 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,10 @@ dependencies {
// Google drive
implementation(sylibs.google.api.services.drive)
implementation(sylibs.google.api.client.oauth)

// Koin
implementation(sylibs.koin.core)
implementation(sylibs.koin.android)
}

androidComponents {
Expand Down
7 changes: 3 additions & 4 deletions app/src/main/java/eu/kanade/domain/DomainModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ import eu.kanade.domain.track.interactor.AddTracks
import eu.kanade.domain.track.interactor.RefreshTracks
import eu.kanade.domain.track.interactor.SyncChapterProgressWithTrack
import eu.kanade.domain.track.interactor.TrackChapter
import eu.kanade.tachiyomi.di.InjektModule
import eu.kanade.tachiyomi.di.addFactory
import eu.kanade.tachiyomi.di.addSingletonFactory
import mihon.data.repository.ExtensionRepoRepositoryImpl
import mihon.domain.chapter.interactor.FilterChaptersForDownload
import mihon.domain.extensionrepo.interactor.CreateExtensionRepo
Expand Down Expand Up @@ -91,11 +94,7 @@ import tachiyomi.domain.track.interactor.InsertTrack
import tachiyomi.domain.track.repository.TrackRepository
import tachiyomi.domain.updates.interactor.GetUpdates
import tachiyomi.domain.updates.repository.UpdatesRepository
import uy.kohesive.injekt.api.InjektModule
import uy.kohesive.injekt.api.InjektRegistrar
import uy.kohesive.injekt.api.addFactory
import uy.kohesive.injekt.api.addSingletonFactory
import uy.kohesive.injekt.api.get

class DomainModule : InjektModule {

Expand Down
7 changes: 3 additions & 4 deletions app/src/main/java/eu/kanade/domain/SYDomainModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import eu.kanade.domain.source.interactor.GetSourceCategories
import eu.kanade.domain.source.interactor.RenameSourceCategory
import eu.kanade.domain.source.interactor.SetSourceCategories
import eu.kanade.domain.source.interactor.ToggleExcludeFromDataSaver
import eu.kanade.tachiyomi.di.InjektModule
import eu.kanade.tachiyomi.di.addFactory
import eu.kanade.tachiyomi.di.addSingletonFactory
import eu.kanade.tachiyomi.source.online.MetadataSource
import exh.search.SearchEngine
import tachiyomi.data.manga.CustomMangaRepositoryImpl
Expand Down Expand Up @@ -71,11 +74,7 @@ import tachiyomi.domain.source.interactor.InsertSavedSearch
import tachiyomi.domain.source.repository.FeedSavedSearchRepository
import tachiyomi.domain.source.repository.SavedSearchRepository
import tachiyomi.domain.track.interactor.IsTrackUnfollowed
import uy.kohesive.injekt.api.InjektModule
import uy.kohesive.injekt.api.InjektRegistrar
import uy.kohesive.injekt.api.addFactory
import uy.kohesive.injekt.api.addSingletonFactory
import uy.kohesive.injekt.api.get
import xyz.nulldev.ts.api.http.serializer.FilterSerializer

class SYDomainModule : InjektModule {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,32 +29,24 @@ import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.draw.clip
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.PreviewLightDark
import androidx.compose.ui.unit.dp
import androidx.core.app.ActivityCompat
import eu.kanade.domain.ui.UiPreferences
import eu.kanade.domain.ui.model.AppTheme
import eu.kanade.presentation.manga.components.MangaCover
import eu.kanade.presentation.theme.TachiyomiTheme
import eu.kanade.tachiyomi.util.system.DeviceUtil
import eu.kanade.tachiyomi.util.system.isDynamicColorAvailable
import tachiyomi.core.common.preference.InMemoryPreferenceStore
import tachiyomi.i18n.MR
import tachiyomi.presentation.core.components.material.padding
import tachiyomi.presentation.core.i18n.stringResource
import tachiyomi.presentation.core.util.secondaryItemAlpha
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.fullType

@Composable
internal fun AppThemePreferenceWidget(
Expand Down Expand Up @@ -257,18 +249,18 @@ fun AppThemePreviewItem(
}
}

@PreviewLightDark
@Composable
private fun AppThemesListPreview() {
var appTheme by remember { mutableStateOf(AppTheme.DEFAULT) }
Injekt.addSingleton(fullType<UiPreferences>(), UiPreferences(InMemoryPreferenceStore()))
TachiyomiTheme(appTheme = appTheme) {
Surface {
AppThemesList(
currentTheme = appTheme,
amoled = false,
onItemClick = { appTheme = it },
)
}
}
}
// @PreviewLightDark
// @Composable
// private fun AppThemesListPreview() {
// var appTheme by remember { mutableStateOf(AppTheme.DEFAULT) }
// Injekt.addSingleton(fullType<UiPreferences>(), UiPreferences(InMemoryPreferenceStore()))
// TachiyomiTheme(appTheme = appTheme) {
// Surface {
// AppThemesList(
// currentTheme = appTheme,
// amoled = false,
// onItemClick = { appTheme = it },
// )
// }
// }
// }
5 changes: 5 additions & 0 deletions app/src/main/java/eu/kanade/tachiyomi/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,11 @@ import eu.kanade.tachiyomi.data.coil.TachiyomiImageDecoder
import eu.kanade.tachiyomi.data.notification.Notifications
import eu.kanade.tachiyomi.data.sync.SyncDataJob
import eu.kanade.tachiyomi.di.AppModule
import eu.kanade.tachiyomi.di.InjektKoinBridge
import eu.kanade.tachiyomi.di.PreferenceModule
import eu.kanade.tachiyomi.di.SYPreferenceModule
import eu.kanade.tachiyomi.di.importModule
import eu.kanade.tachiyomi.di.initExpensiveComponents
import eu.kanade.tachiyomi.network.NetworkHelper
import eu.kanade.tachiyomi.network.NetworkPreferences
import eu.kanade.tachiyomi.ui.base.delegate.SecureActivityDelegate
Expand Down Expand Up @@ -123,6 +126,8 @@ class App : Application(), DefaultLifecycleObserver, SingletonImageLoader.Factor
// SY -->
Injekt.importModule(SYPreferenceModule(this))
Injekt.importModule(SYDomainModule())
InjektKoinBridge.startKoin(this)
initExpensiveComponents(this)
// SY <--

setupExhLogging() // EXH logging
Expand Down
30 changes: 15 additions & 15 deletions app/src/main/java/eu/kanade/tachiyomi/di/AppModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,8 @@ import tachiyomi.domain.source.service.SourceManager
import tachiyomi.domain.storage.service.StorageManager
import tachiyomi.source.local.image.LocalCoverManager
import tachiyomi.source.local.io.LocalSourceFileSystem
import uy.kohesive.injekt.api.InjektModule
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.InjektRegistrar
import uy.kohesive.injekt.api.addSingleton
import uy.kohesive.injekt.api.addSingletonFactory
import uy.kohesive.injekt.api.get
import uy.kohesive.injekt.injectLazy

Expand Down Expand Up @@ -170,23 +168,25 @@ class AppModule(val app: Application) : InjektModule {
addSingletonFactory { EHentaiUpdateHelper(app) }

addSingletonFactory { PagePreviewCache(app) }

addSingletonFactory { GoogleDriveService(app) }
// SY <--
}
}

// Asynchronously init expensive components for a faster cold start
ContextCompat.getMainExecutor(app).execute {
get<NetworkHelper>()
fun initExpensiveComponents(app: Application) {
// Asynchronously init expensive components for a faster cold start
ContextCompat.getMainExecutor(app).execute {
Injekt.get<NetworkHelper>()

get<SourceManager>()
Injekt.get<SourceManager>()

get<Database>()
Injekt.get<Database>()

get<DownloadManager>()
Injekt.get<DownloadManager>()

// SY -->
get<GetCustomMangaInfo>()
// SY <--
}

addSingletonFactory { GoogleDriveService(app) }
// SY -->
Injekt.get<GetCustomMangaInfo>()
// SY <--
}
}
64 changes: 64 additions & 0 deletions app/src/main/java/eu/kanade/tachiyomi/di/InjektKoinBridge.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package eu.kanade.tachiyomi.di

import android.content.Context
import logcat.LogPriority
import logcat.logcat
import org.koin.android.ext.koin.androidContext
import org.koin.core.context.startKoin
import org.koin.core.logger.Level
import org.koin.core.logger.Logger
import org.koin.core.logger.MESSAGE
import org.koin.core.module.Module
import org.koin.core.scope.Scope
import uy.kohesive.injekt.api.InjektRegistrar
import uy.kohesive.injekt.api.InjektScope

object InjektKoinBridge {
private val modules = mutableMapOf<InjektModule, Module>()
fun getModule(injektModule: InjektModule) = modules.getOrPut(injektModule) { Module() }

fun startKoin(context: Context) {
startKoin {
logger(
object : Logger() {
override fun display(level: Level, msg: MESSAGE) {
logcat(
when (level) {
Level.DEBUG -> LogPriority.DEBUG
Level.INFO -> LogPriority.INFO
Level.WARNING -> LogPriority.WARN
Level.ERROR -> LogPriority.ERROR
Level.NONE -> LogPriority.VERBOSE
},
) { msg }
}
},
)
androidContext(context)
modules(modules.values.toList())
}
}
}

interface InjektModule {
fun InjektRegistrar.registerInjectables()
}

inline fun <reified T> InjektModule.addSingleton(instance: T) {
val module = InjektKoinBridge.getModule(this)
module.single<T> { instance }
}

inline fun <reified T> InjektModule.addSingletonFactory(crossinline instance: Scope.() -> T) {
val module = InjektKoinBridge.getModule(this)
module.single<T> { instance() }
}

inline fun <reified T> InjektModule.addFactory(crossinline instance: Scope.() -> T) {
val module = InjektKoinBridge.getModule(this)
module.factory<T> { instance() }
}

fun InjektScope.importModule(injektModule: InjektModule) {
with(injektModule) { registrar.registerInjectables() }
}
3 changes: 0 additions & 3 deletions app/src/main/java/eu/kanade/tachiyomi/di/PreferenceModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ import tachiyomi.domain.backup.service.BackupPreferences
import tachiyomi.domain.download.service.DownloadPreferences
import tachiyomi.domain.library.service.LibraryPreferences
import tachiyomi.domain.storage.service.StoragePreferences
import uy.kohesive.injekt.api.InjektModule
import uy.kohesive.injekt.api.InjektRegistrar
import uy.kohesive.injekt.api.addSingletonFactory
import uy.kohesive.injekt.api.get

class PreferenceModule(val app: Application) : InjektModule {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ package eu.kanade.tachiyomi.di
import android.app.Application
import exh.pref.DelegateSourcePreferences
import tachiyomi.domain.UnsortedPreferences
import uy.kohesive.injekt.api.InjektModule
import uy.kohesive.injekt.api.InjektRegistrar
import uy.kohesive.injekt.api.addSingletonFactory
import uy.kohesive.injekt.api.get

class SYPreferenceModule(val application: Application) : InjektModule {

Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ sqlite-android = "com.github.requery:sqlite-android:3.45.0"

preferencektx = "androidx.preference:preference-ktx:1.2.1"

injekt-core = "com.github.inorichi.injekt:injekt-core:65b0440"
injekt-core = "com.github.null2264:injekt-koin:ee267b2e27"

coil-bom = { module = "io.coil-kt.coil3:coil-bom", version = "3.0.0-alpha10" }
coil-core = { module = "io.coil-kt.coil3:coil" }
Expand Down
6 changes: 5 additions & 1 deletion gradle/sy.versions.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[versions]
koin = "4.0.0"

[libraries]
firebase-analytics = "com.google.firebase:firebase-analytics:22.0.2"
Expand All @@ -18,4 +19,7 @@ sqlcipher = "net.zetetic:sqlcipher-android:4.6.0"
exifinterface = "androidx.exifinterface:exifinterface:1.3.7"

google-api-services-drive = "com.google.apis:google-api-services-drive:v3-rev197-1.25.0"
google-api-client-oauth = "com.google.oauth-client:google-oauth-client:1.36.0"
google-api-client-oauth = "com.google.oauth-client:google-oauth-client:1.36.0"

koin-core = { module = "io.insert-koin:koin-core", version.ref = "koin" }
koin-android = { module = "io.insert-koin:koin-android", version.ref = "koin" }

0 comments on commit 2f8efe0

Please sign in to comment.