Fix more TypeReference issues and cleanup

This commit is contained in:
AntsyLich 2024-03-24 05:16:31 +06:00
parent 05071b4205
commit e020ae5ed5
No known key found for this signature in database
11 changed files with 38 additions and 49 deletions

View file

@ -31,6 +31,7 @@ import mihon.domain.extensionrepo.interactor.GetExtensionRepoCount
import mihon.domain.extensionrepo.interactor.ReplaceExtensionRepo
import mihon.domain.extensionrepo.interactor.UpdateExtensionRepo
import mihon.domain.extensionrepo.repository.ExtensionRepoRepository
import mihon.domain.extensionrepo.service.ExtensionRepoService
import tachiyomi.data.category.CategoryRepositoryImpl
import tachiyomi.data.chapter.ChapterRepositoryImpl
import tachiyomi.data.history.HistoryRepositoryImpl
@ -179,6 +180,7 @@ class DomainModule : InjektModule {
addFactory { TrustExtension(get()) }
addSingletonFactory<ExtensionRepoRepository> { ExtensionRepoRepositoryImpl(get()) }
addFactory { ExtensionRepoService(get(), get()) }
addFactory { GetExtensionRepo(get()) }
addFactory { GetExtensionRepoCount(get()) }
addFactory { CreateExtensionRepo(get(), get()) }

View file

@ -48,7 +48,7 @@ object Migrations {
coroutineScope.launchIO {
for ((index, source) in sourcePreferences.extensionRepos().get().withIndex()) {
try {
extensionRepoRepository.upsertRepository(
extensionRepoRepository.upsertRepo(
source,
"Repo #${index + 1}",
null,

View file

@ -19,11 +19,11 @@ class ExtensionRepoRepositoryImpl(
return handler.awaitList { extension_reposQueries.findAll(::mapExtensionRepo) }
}
override suspend fun getRepository(baseUrl: String): ExtensionRepo? {
override suspend fun getRepo(baseUrl: String): ExtensionRepo? {
return handler.awaitOneOrNull { extension_reposQueries.findOne(baseUrl, ::mapExtensionRepo) }
}
override suspend fun getRepositoryBySigningKeyFingerprint(fingerprint: String): ExtensionRepo? {
override suspend fun getRepoBySigningKeyFingerprint(fingerprint: String): ExtensionRepo? {
return handler.awaitOneOrNull {
extension_reposQueries.findOneBySigningKeyFingerprint(fingerprint, ::mapExtensionRepo)
}
@ -33,7 +33,7 @@ class ExtensionRepoRepositoryImpl(
return handler.subscribeToOne { extension_reposQueries.count() }.map { it.toInt() }
}
override suspend fun insertRepository(
override suspend fun insertRepo(
baseUrl: String,
name: String,
shortName: String?,
@ -47,7 +47,7 @@ class ExtensionRepoRepositoryImpl(
}
}
override suspend fun upsertRepository(
override suspend fun upsertRepo(
baseUrl: String,
name: String,
shortName: String?,
@ -61,7 +61,7 @@ class ExtensionRepoRepositoryImpl(
}
}
override suspend fun replaceRepository(newRepo: ExtensionRepo) {
override suspend fun replaceRepo(newRepo: ExtensionRepo) {
handler.await {
extension_reposQueries.replace(
newRepo.baseUrl,
@ -73,7 +73,7 @@ class ExtensionRepoRepositoryImpl(
}
}
override suspend fun deleteRepository(baseUrl: String) {
override suspend fun deleteRepo(baseUrl: String) {
return handler.await { extension_reposQueries.delete(baseUrl) }
}

View file

@ -1,37 +1,30 @@
package mihon.domain.extensionrepo.interactor
import eu.kanade.tachiyomi.network.NetworkHelper
import logcat.LogPriority
import mihon.domain.extensionrepo.exception.SaveExtensionRepoException
import mihon.domain.extensionrepo.model.ExtensionRepo
import mihon.domain.extensionrepo.repository.ExtensionRepoRepository
import mihon.domain.extensionrepo.service.ExtensionRepoService
import okhttp3.OkHttpClient
import tachiyomi.core.common.util.system.logcat
class CreateExtensionRepo(
private val extensionRepoRepository: ExtensionRepoRepository,
private val networkHelper: NetworkHelper,
private val repository: ExtensionRepoRepository,
private val service: ExtensionRepoService,
) {
private val repoRegex = """^https://.*/index\.min\.json$""".toRegex()
private val client: OkHttpClient
get() = networkHelper.client
private val extensionRepoService = ExtensionRepoService(client)
suspend fun await(repoUrl: String): Result {
if (!repoUrl.matches(repoRegex)) {
return Result.InvalidUrl
}
val baseUrl = repoUrl.removeSuffix("/index.min.json")
return extensionRepoService.fetchRepoDetails(baseUrl)?.let { insert(it) } ?: Result.InvalidUrl
return service.fetchRepoDetails(baseUrl)?.let { insert(it) } ?: Result.InvalidUrl
}
private suspend fun insert(repo: ExtensionRepo): Result {
return try {
extensionRepoRepository.insertRepository(
repository.insertRepo(
repo.baseUrl,
repo.name,
repo.shortName,
@ -57,12 +50,11 @@ class CreateExtensionRepo(
*/
@Suppress("ReturnCount")
private suspend fun handleInsertionError(repo: ExtensionRepo): Result {
val repoExists = extensionRepoRepository.getRepository(repo.baseUrl)
val repoExists = repository.getRepo(repo.baseUrl)
if (repoExists != null) {
return Result.RepoAlreadyExists
}
val matchingFingerprintRepo =
extensionRepoRepository.getRepositoryBySigningKeyFingerprint(repo.signingKeyFingerprint)
val matchingFingerprintRepo = repository.getRepoBySigningKeyFingerprint(repo.signingKeyFingerprint)
if (matchingFingerprintRepo != null) {
return Result.DuplicateFingerprint(matchingFingerprintRepo, repo)
}

View file

@ -3,9 +3,9 @@ package mihon.domain.extensionrepo.interactor
import mihon.domain.extensionrepo.repository.ExtensionRepoRepository
class DeleteExtensionRepo(
private val extensionRepoRepository: ExtensionRepoRepository,
private val repository: ExtensionRepoRepository,
) {
suspend fun await(baseUrl: String) {
extensionRepoRepository.deleteRepository(baseUrl)
repository.deleteRepo(baseUrl)
}
}

View file

@ -5,9 +5,9 @@ import mihon.domain.extensionrepo.model.ExtensionRepo
import mihon.domain.extensionrepo.repository.ExtensionRepoRepository
class GetExtensionRepo(
private val extensionRepoRepository: ExtensionRepoRepository,
private val repository: ExtensionRepoRepository,
) {
fun subscribeAll(): Flow<List<ExtensionRepo>> = extensionRepoRepository.subscribeAll()
fun subscribeAll(): Flow<List<ExtensionRepo>> = repository.subscribeAll()
suspend fun getAll(): List<ExtensionRepo> = extensionRepoRepository.getAll()
suspend fun getAll(): List<ExtensionRepo> = repository.getAll()
}

View file

@ -3,7 +3,7 @@ package mihon.domain.extensionrepo.interactor
import mihon.domain.extensionrepo.repository.ExtensionRepoRepository
class GetExtensionRepoCount(
private val extensionRepoRepository: ExtensionRepoRepository,
private val repository: ExtensionRepoRepository,
) {
fun subscribe() = extensionRepoRepository.getCount()
fun subscribe() = repository.getCount()
}

View file

@ -4,9 +4,9 @@ import mihon.domain.extensionrepo.model.ExtensionRepo
import mihon.domain.extensionrepo.repository.ExtensionRepoRepository
class ReplaceExtensionRepo(
private val extensionRepoRepository: ExtensionRepoRepository,
private val repository: ExtensionRepoRepository,
) {
suspend fun await(repo: ExtensionRepo) {
extensionRepoRepository.replaceRepository(repo)
repository.replaceRepo(repo)
}
}

View file

@ -1,6 +1,5 @@
package mihon.domain.extensionrepo.interactor
import eu.kanade.tachiyomi.network.NetworkHelper
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.coroutineScope
@ -9,25 +8,23 @@ import mihon.domain.extensionrepo.repository.ExtensionRepoRepository
import mihon.domain.extensionrepo.service.ExtensionRepoService
class UpdateExtensionRepo(
private val extensionRepoRepository: ExtensionRepoRepository,
networkService: NetworkHelper,
private val repository: ExtensionRepoRepository,
private val service: ExtensionRepoService,
) {
private val extensionRepoService = ExtensionRepoService(networkService.client)
suspend fun awaitAll() = coroutineScope {
extensionRepoRepository.getAll()
repository.getAll()
.map { async { await(it) } }
.awaitAll()
}
suspend fun await(repo: ExtensionRepo) {
val newRepo = extensionRepoService.fetchRepoDetails(repo.baseUrl) ?: return
val newRepo = service.fetchRepoDetails(repo.baseUrl) ?: return
if (
repo.signingKeyFingerprint.startsWith("NOFINGERPRINT") ||
repo.signingKeyFingerprint == newRepo.signingKeyFingerprint
) {
extensionRepoRepository.upsertRepository(newRepo)
repository.upsertRepo(newRepo)
}
}
}

View file

@ -9,13 +9,13 @@ interface ExtensionRepoRepository {
suspend fun getAll(): List<ExtensionRepo>
suspend fun getRepository(baseUrl: String): ExtensionRepo?
suspend fun getRepo(baseUrl: String): ExtensionRepo?
suspend fun getRepositoryBySigningKeyFingerprint(fingerprint: String): ExtensionRepo?
suspend fun getRepoBySigningKeyFingerprint(fingerprint: String): ExtensionRepo?
fun getCount(): Flow<Int>
suspend fun insertRepository(
suspend fun insertRepo(
baseUrl: String,
name: String,
shortName: String?,
@ -23,7 +23,7 @@ interface ExtensionRepoRepository {
signingKeyFingerprint: String,
)
suspend fun upsertRepository(
suspend fun upsertRepo(
baseUrl: String,
name: String,
shortName: String?,
@ -31,8 +31,8 @@ interface ExtensionRepoRepository {
signingKeyFingerprint: String,
)
suspend fun upsertRepository(repo: ExtensionRepo) {
upsertRepository(
suspend fun upsertRepo(repo: ExtensionRepo) {
upsertRepo(
baseUrl = repo.baseUrl,
name = repo.name,
shortName = repo.shortName,
@ -41,7 +41,7 @@ interface ExtensionRepoRepository {
)
}
suspend fun replaceRepository(newRepo: ExtensionRepo)
suspend fun replaceRepo(newRepo: ExtensionRepo)
suspend fun deleteRepository(baseUrl: String)
suspend fun deleteRepo(baseUrl: String)
}

View file

@ -10,14 +10,12 @@ import mihon.domain.extensionrepo.model.ExtensionRepo
import okhttp3.OkHttpClient
import tachiyomi.core.common.util.lang.withIOContext
import tachiyomi.core.common.util.system.logcat
import uy.kohesive.injekt.injectLazy
class ExtensionRepoService(
private val client: OkHttpClient,
private val json: Json,
) {
private val json: Json by injectLazy()
@Suppress("TooGenericExceptionCaught")
suspend fun fetchRepoDetails(
repo: String,