mirror of
https://github.com/null2264/yokai.git
synced 2025-06-21 10:44:42 +00:00
refactor: Migrate more stuff to use SQLDelight
This commit is contained in:
parent
4d2909340e
commit
4af54a906a
3 changed files with 29 additions and 22 deletions
|
@ -82,6 +82,7 @@ import kotlinx.coroutines.sync.Semaphore
|
||||||
import kotlinx.coroutines.sync.withPermit
|
import kotlinx.coroutines.sync.withPermit
|
||||||
import uy.kohesive.injekt.Injekt
|
import uy.kohesive.injekt.Injekt
|
||||||
import uy.kohesive.injekt.api.get
|
import uy.kohesive.injekt.api.get
|
||||||
|
import yokai.domain.chapter.interactor.GetChapter
|
||||||
import yokai.domain.manga.interactor.GetLibraryManga
|
import yokai.domain.manga.interactor.GetLibraryManga
|
||||||
import yokai.domain.manga.interactor.UpdateManga
|
import yokai.domain.manga.interactor.UpdateManga
|
||||||
import yokai.domain.manga.models.cover
|
import yokai.domain.manga.models.cover
|
||||||
|
@ -92,6 +93,9 @@ class LibraryUpdateJob(private val context: Context, workerParams: WorkerParamet
|
||||||
CoroutineWorker(context, workerParams) {
|
CoroutineWorker(context, workerParams) {
|
||||||
|
|
||||||
private val db: DatabaseHelper = Injekt.get()
|
private val db: DatabaseHelper = Injekt.get()
|
||||||
|
|
||||||
|
private val getChapter: GetChapter = Injekt.get()
|
||||||
|
|
||||||
private val coverCache: CoverCache = Injekt.get()
|
private val coverCache: CoverCache = Injekt.get()
|
||||||
private val sourceManager: SourceManager = Injekt.get()
|
private val sourceManager: SourceManager = Injekt.get()
|
||||||
private val preferences: PreferencesHelper = Injekt.get()
|
private val preferences: PreferencesHelper = Injekt.get()
|
||||||
|
@ -319,7 +323,7 @@ class LibraryUpdateJob(private val context: Context, workerParams: WorkerParamet
|
||||||
db.insertTrack(newTrack).executeAsBlocking()
|
db.insertTrack(newTrack).executeAsBlocking()
|
||||||
|
|
||||||
if (service is EnhancedTrackService) {
|
if (service is EnhancedTrackService) {
|
||||||
syncChaptersWithTrackServiceTwoWay(db, db.getChapters(manga).executeAsBlocking(), track, service)
|
syncChaptersWithTrackServiceTwoWay(db, getChapter.awaitAll(manga.id!!, false), track, service)
|
||||||
}
|
}
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
Logger.e(e)
|
Logger.e(e)
|
||||||
|
|
|
@ -40,11 +40,13 @@ import eu.kanade.tachiyomi.util.view.setTitle
|
||||||
import eu.kanade.tachiyomi.util.view.snack
|
import eu.kanade.tachiyomi.util.view.snack
|
||||||
import eu.kanade.tachiyomi.util.view.withFadeTransaction
|
import eu.kanade.tachiyomi.util.view.withFadeTransaction
|
||||||
import eu.kanade.tachiyomi.widget.TriStateCheckBox
|
import eu.kanade.tachiyomi.widget.TriStateCheckBox
|
||||||
|
import java.util.Date
|
||||||
|
import java.util.Locale
|
||||||
import uy.kohesive.injekt.Injekt
|
import uy.kohesive.injekt.Injekt
|
||||||
import uy.kohesive.injekt.api.get
|
import uy.kohesive.injekt.api.get
|
||||||
|
import yokai.domain.chapter.interactor.GetChapter
|
||||||
import yokai.i18n.MR
|
import yokai.i18n.MR
|
||||||
import yokai.util.lang.getString
|
import yokai.util.lang.getString
|
||||||
import java.util.*
|
|
||||||
import android.R as AR
|
import android.R as AR
|
||||||
|
|
||||||
fun Manga.isLocal() = source == LocalSource.ID
|
fun Manga.isLocal() = source == LocalSource.ID
|
||||||
|
@ -411,6 +413,7 @@ private fun showAddDuplicateDialog(
|
||||||
fun Manga.autoAddTrack(db: DatabaseHelper, onMangaMoved: () -> Unit) {
|
fun Manga.autoAddTrack(db: DatabaseHelper, onMangaMoved: () -> Unit) {
|
||||||
val loggedServices = Injekt.get<TrackManager>().services.filter { it.isLogged }
|
val loggedServices = Injekt.get<TrackManager>().services.filter { it.isLogged }
|
||||||
val source = Injekt.get<SourceManager>().getOrStub(this.source)
|
val source = Injekt.get<SourceManager>().getOrStub(this.source)
|
||||||
|
val getChapter = Injekt.get<GetChapter>()
|
||||||
loggedServices
|
loggedServices
|
||||||
.filterIsInstance<EnhancedTrackService>()
|
.filterIsInstance<EnhancedTrackService>()
|
||||||
.filter { it.accept(source) }
|
.filter { it.accept(source) }
|
||||||
|
@ -418,11 +421,12 @@ fun Manga.autoAddTrack(db: DatabaseHelper, onMangaMoved: () -> Unit) {
|
||||||
launchIO {
|
launchIO {
|
||||||
try {
|
try {
|
||||||
service.match(this@autoAddTrack)?.let { track ->
|
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)
|
(service as TrackService).bind(track)
|
||||||
db.insertTrack(track).executeAsBlocking()
|
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 {
|
withUIContext {
|
||||||
onMangaMoved()
|
onMangaMoved()
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,6 +12,7 @@ import eu.kanade.tachiyomi.util.system.e
|
||||||
import eu.kanade.tachiyomi.util.system.isOnline
|
import eu.kanade.tachiyomi.util.system.isOnline
|
||||||
import eu.kanade.tachiyomi.util.system.launchIO
|
import eu.kanade.tachiyomi.util.system.launchIO
|
||||||
import eu.kanade.tachiyomi.util.system.w
|
import eu.kanade.tachiyomi.util.system.w
|
||||||
|
import eu.kanade.tachiyomi.util.system.withIOContext
|
||||||
import kotlinx.coroutines.Job
|
import kotlinx.coroutines.Job
|
||||||
import kotlinx.coroutines.delay
|
import kotlinx.coroutines.delay
|
||||||
import uy.kohesive.injekt.Injekt
|
import uy.kohesive.injekt.Injekt
|
||||||
|
@ -26,32 +27,30 @@ import yokai.domain.chapter.interactor.UpdateChapter
|
||||||
* @param remoteTrack the remote Track object.
|
* @param remoteTrack the remote Track object.
|
||||||
* @param service the tracker service.
|
* @param service the tracker service.
|
||||||
*/
|
*/
|
||||||
fun syncChaptersWithTrackServiceTwoWay(
|
suspend fun syncChaptersWithTrackServiceTwoWay(
|
||||||
db: DatabaseHelper,
|
db: DatabaseHelper,
|
||||||
chapters: List<Chapter>,
|
chapters: List<Chapter>,
|
||||||
remoteTrack: Track,
|
remoteTrack: Track,
|
||||||
service: TrackService,
|
service: TrackService,
|
||||||
updateChapter: UpdateChapter = Injekt.get(),
|
updateChapter: UpdateChapter = Injekt.get(),
|
||||||
) {
|
) = withIOContext {
|
||||||
launchIO {
|
val sortedChapters = chapters.sortedBy { it.chapter_number }
|
||||||
val sortedChapters = chapters.sortedBy { it.chapter_number }
|
sortedChapters
|
||||||
sortedChapters
|
.filter { chapter -> chapter.chapter_number <= remoteTrack.last_chapter_read && !chapter.read }
|
||||||
.filter { chapter -> chapter.chapter_number <= remoteTrack.last_chapter_read && !chapter.read }
|
.forEach { it.read = true }
|
||||||
.forEach { it.read = true }
|
updateChapter.awaitAll(sortedChapters.map(Chapter::toProgressUpdate))
|
||||||
updateChapter.awaitAll(sortedChapters.map(Chapter::toProgressUpdate))
|
|
||||||
|
|
||||||
// only take into account continuous reading
|
// only take into account continuous reading
|
||||||
val localLastRead = sortedChapters.takeWhile { it.read }.lastOrNull()?.chapter_number ?: 0F
|
val localLastRead = sortedChapters.takeWhile { it.read }.lastOrNull()?.chapter_number ?: 0F
|
||||||
|
|
||||||
// update remote
|
// update remote
|
||||||
remoteTrack.last_chapter_read = localLastRead
|
remoteTrack.last_chapter_read = localLastRead
|
||||||
|
|
||||||
try {
|
try {
|
||||||
service.update(remoteTrack)
|
service.update(remoteTrack)
|
||||||
db.insertTrack(remoteTrack).executeAsBlocking()
|
db.insertTrack(remoteTrack).executeAsBlocking()
|
||||||
} catch (e: Throwable) {
|
} catch (e: Throwable) {
|
||||||
Logger.w(e)
|
Logger.w(e)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue