refactor: Migrate more stuff to use SQLDelight

This commit is contained in:
Ahmad Ansori Palembani 2024-08-23 16:58:14 +07:00
parent 4d2909340e
commit 4af54a906a
Signed by: null2264
GPG key ID: BA64F8B60AF3EFB6
3 changed files with 29 additions and 22 deletions

View file

@ -82,6 +82,7 @@ import kotlinx.coroutines.sync.Semaphore
import kotlinx.coroutines.sync.withPermit
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
import yokai.domain.chapter.interactor.GetChapter
import yokai.domain.manga.interactor.GetLibraryManga
import yokai.domain.manga.interactor.UpdateManga
import yokai.domain.manga.models.cover
@ -92,6 +93,9 @@ class LibraryUpdateJob(private val context: Context, workerParams: WorkerParamet
CoroutineWorker(context, workerParams) {
private val db: DatabaseHelper = Injekt.get()
private val getChapter: GetChapter = Injekt.get()
private val coverCache: CoverCache = Injekt.get()
private val sourceManager: SourceManager = Injekt.get()
private val preferences: PreferencesHelper = Injekt.get()
@ -319,7 +323,7 @@ class LibraryUpdateJob(private val context: Context, workerParams: WorkerParamet
db.insertTrack(newTrack).executeAsBlocking()
if (service is EnhancedTrackService) {
syncChaptersWithTrackServiceTwoWay(db, db.getChapters(manga).executeAsBlocking(), track, service)
syncChaptersWithTrackServiceTwoWay(db, getChapter.awaitAll(manga.id!!, false), track, service)
}
} catch (e: Exception) {
Logger.e(e)

View file

@ -40,11 +40,13 @@ import eu.kanade.tachiyomi.util.view.setTitle
import eu.kanade.tachiyomi.util.view.snack
import eu.kanade.tachiyomi.util.view.withFadeTransaction
import eu.kanade.tachiyomi.widget.TriStateCheckBox
import java.util.Date
import java.util.Locale
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
import yokai.domain.chapter.interactor.GetChapter
import yokai.i18n.MR
import yokai.util.lang.getString
import java.util.*
import android.R as AR
fun Manga.isLocal() = source == LocalSource.ID
@ -411,6 +413,7 @@ private fun showAddDuplicateDialog(
fun Manga.autoAddTrack(db: DatabaseHelper, onMangaMoved: () -> Unit) {
val loggedServices = Injekt.get<TrackManager>().services.filter { it.isLogged }
val source = Injekt.get<SourceManager>().getOrStub(this.source)
val getChapter = Injekt.get<GetChapter>()
loggedServices
.filterIsInstance<EnhancedTrackService>()
.filter { it.accept(source) }
@ -418,11 +421,12 @@ fun Manga.autoAddTrack(db: DatabaseHelper, onMangaMoved: () -> Unit) {
launchIO {
try {
service.match(this@autoAddTrack)?.let { track ->
track.manga_id = this@autoAddTrack.id!!
val mangaId = this@autoAddTrack.id!!
track.manga_id = mangaId
(service as TrackService).bind(track)
db.insertTrack(track).executeAsBlocking()
syncChaptersWithTrackServiceTwoWay(db, db.getChapters(this@autoAddTrack).executeAsBlocking(), track, service as TrackService)
syncChaptersWithTrackServiceTwoWay(db, getChapter.awaitAll(mangaId, false), track, service as TrackService)
withUIContext {
onMangaMoved()
}

View file

@ -12,6 +12,7 @@ import eu.kanade.tachiyomi.util.system.e
import eu.kanade.tachiyomi.util.system.isOnline
import eu.kanade.tachiyomi.util.system.launchIO
import eu.kanade.tachiyomi.util.system.w
import eu.kanade.tachiyomi.util.system.withIOContext
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import uy.kohesive.injekt.Injekt
@ -26,32 +27,30 @@ import yokai.domain.chapter.interactor.UpdateChapter
* @param remoteTrack the remote Track object.
* @param service the tracker service.
*/
fun syncChaptersWithTrackServiceTwoWay(
suspend fun syncChaptersWithTrackServiceTwoWay(
db: DatabaseHelper,
chapters: List<Chapter>,
remoteTrack: Track,
service: TrackService,
updateChapter: UpdateChapter = Injekt.get(),
) {
launchIO {
val sortedChapters = chapters.sortedBy { it.chapter_number }
sortedChapters
.filter { chapter -> chapter.chapter_number <= remoteTrack.last_chapter_read && !chapter.read }
.forEach { it.read = true }
updateChapter.awaitAll(sortedChapters.map(Chapter::toProgressUpdate))
) = withIOContext {
val sortedChapters = chapters.sortedBy { it.chapter_number }
sortedChapters
.filter { chapter -> chapter.chapter_number <= remoteTrack.last_chapter_read && !chapter.read }
.forEach { it.read = true }
updateChapter.awaitAll(sortedChapters.map(Chapter::toProgressUpdate))
// only take into account continuous reading
val localLastRead = sortedChapters.takeWhile { it.read }.lastOrNull()?.chapter_number ?: 0F
// only take into account continuous reading
val localLastRead = sortedChapters.takeWhile { it.read }.lastOrNull()?.chapter_number ?: 0F
// update remote
remoteTrack.last_chapter_read = localLastRead
// update remote
remoteTrack.last_chapter_read = localLastRead
try {
service.update(remoteTrack)
db.insertTrack(remoteTrack).executeAsBlocking()
} catch (e: Throwable) {
Logger.w(e)
}
try {
service.update(remoteTrack)
db.insertTrack(remoteTrack).executeAsBlocking()
} catch (e: Throwable) {
Logger.w(e)
}
}