Skip to content

Commit

Permalink
Fix more TypeReference issues and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
AntsyLich committed Mar 23, 2024
1 parent 05071b4 commit e020ae5
Show file tree
Hide file tree
Showing 11 changed files with 38 additions and 49 deletions.
2 changes: 2 additions & 0 deletions app/src/main/java/eu/kanade/domain/DomainModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()) }
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/eu/kanade/tachiyomi/Migrations.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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?,
Expand All @@ -47,7 +47,7 @@ class ExtensionRepoRepositoryImpl(
}
}

override suspend fun upsertRepository(
override suspend fun upsertRepo(
baseUrl: String,
name: String,
shortName: String?,
Expand All @@ -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,
Expand All @@ -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) }
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,30 @@ 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?,
website: String,
signingKeyFingerprint: String,
)

suspend fun upsertRepository(
suspend fun upsertRepo(
baseUrl: String,
name: String,
shortName: String?,
website: String,
signingKeyFingerprint: String,
)

suspend fun upsertRepository(repo: ExtensionRepo) {
upsertRepository(
suspend fun upsertRepo(repo: ExtensionRepo) {
upsertRepo(
baseUrl = repo.baseUrl,
name = repo.name,
shortName = repo.shortName,
Expand All @@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit e020ae5

Please sign in to comment.