Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update all trackers progress when new tracker is added #1372

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 41 additions & 9 deletions app/src/main/java/eu/kanade/domain/track/interactor/AddTracks.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package eu.kanade.domain.track.interactor

import android.app.Application
import eu.kanade.domain.track.model.toDbTrack
import eu.kanade.domain.track.model.toDomainTrack
import eu.kanade.tachiyomi.data.database.models.Track
Expand All @@ -8,26 +9,29 @@ import eu.kanade.tachiyomi.data.track.Tracker
import eu.kanade.tachiyomi.data.track.TrackerManager
import eu.kanade.tachiyomi.source.Source
import eu.kanade.tachiyomi.util.lang.convertEpochMillisZone
import eu.kanade.tachiyomi.util.system.toast
import logcat.LogPriority
import tachiyomi.core.common.i18n.stringResource
import tachiyomi.core.common.util.lang.withIOContext
import tachiyomi.core.common.util.lang.withNonCancellableContext
import tachiyomi.core.common.util.lang.withUIContext
import tachiyomi.core.common.util.system.logcat
import tachiyomi.domain.chapter.interactor.GetChaptersByMangaId
import tachiyomi.domain.history.interactor.GetHistory
import tachiyomi.domain.manga.model.Manga
import tachiyomi.domain.track.interactor.InsertTrack
import tachiyomi.i18n.MR
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
import java.time.ZoneOffset

class AddTracks(
private val insertTrack: InsertTrack,
private val syncChapterProgressWithTrack: SyncChapterProgressWithTrack,
private val refreshTracks: RefreshTracks,
private val getChaptersByMangaId: GetChaptersByMangaId,
private val trackerManager: TrackerManager,
) {

// TODO: update all trackers based on common data
suspend fun bind(tracker: Tracker, item: Track, mangaId: Long) = withNonCancellableContext {
withIOContext {
val allChapters = getChaptersByMangaId.await(mangaId)
Expand Down Expand Up @@ -73,7 +77,23 @@ class AddTracks(
}
}

syncChapterProgressWithTrack.await(mangaId, track, tracker)
val context = Injekt.get<Application>()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move to constructor

refreshTracks.await(mangaId)
.filter { it.first != null }
.forEach { (track, e) ->
Comment on lines +82 to +83
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.filter { it.first != null }
.forEach { (track, e) ->
.forEach { (track, e) ->
if (track == null) return@forEach

logcat(LogPriority.ERROR, e) {
"Failed to refresh track data mangaId=$mangaId for service ${track!!.id}"
}
withUIContext {
context.toast(
context.stringResource(
MR.strings.track_error,
track!!.name,
e.message ?: "",
),
)
Comment on lines +88 to +94
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
context.toast(
context.stringResource(
MR.strings.track_error,
track!!.name,
e.message ?: "",
),
)
context.toast(context.stringResource(MR.strings.track_error, track.name, e.message ?: ""))

}
}
}
}

Expand All @@ -88,12 +108,6 @@ class AddTracks(
track.manga_id = manga.id
(service as Tracker).bind(track)
insertTrack.await(track.toDomainTrack(idRequired = false)!!)

syncChapterProgressWithTrack.await(
manga.id,
track.toDomainTrack(idRequired = false)!!,
service,
)
}
} catch (e: Exception) {
logcat(
Expand All @@ -102,6 +116,24 @@ class AddTracks(
) { "Could not match manga: ${manga.title} with service $service" }
}
}

val context = Injekt.get<Application>()
refreshTracks.await(manga.id)
.filter { it.first != null }
.forEach { (track, e) ->
logcat(LogPriority.ERROR, e) {
"Failed to refresh track data mangaId=${manga.id} for service ${track!!.id}"
}
withUIContext {
context.toast(
context.stringResource(
MR.strings.track_error,
track!!.name,
e.message ?: "",
),
)
}
}
}
}
}