Yokan / rebuild
Course 1 8624561

TrackerService.kt

composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/services/tracking/TrackerService.kt

implementation129 lines
1package eu.friedcutlet.yokan.services.tracking23import eu.friedcutlet.yokan.database.DbItemType4import eu.friedcutlet.yokan.database.DbManga5import eu.friedcutlet.yokan.database.DbSettings6import eu.friedcutlet.yokan.database.DbSource7import eu.friedcutlet.yokan.database.DbTrack8import eu.friedcutlet.yokan.database.DbTrackPreference9import eu.friedcutlet.yokan.database.DbTrackSearch10import eu.friedcutlet.yokan.database.DbTrackStatus1112interface TrackerService {13    val syncId: Int14    val name: String1516    /** The metadata-server tracker, configured with the metadata server instead of the tracker settings. */17    val isMetadataSource: Boolean get() = false1819    suspend fun checkRefresh(): Boolean20    suspend fun findLibraryItem(track: DbTrack, isManga: Boolean): DbTrack?2122    /**23     * Pushes [track]'s progress/status/score. Remote adapters return the provider's state for the caller24     * ([TrackerAccounts]) to store; the original local-only adapters store it themselves.25     */26    suspend fun update(track: DbTrack, isManga: Boolean): DbTrack27    fun statusList(isManga: Boolean): List<DbTrackStatus>2829    /** Free-text search; remote adapters resolve `id:<provider id>` (see [trackerSearchId]) to that entry. */30    suspend fun search(query: String, isManga: Boolean): List<DbTrackSearch>31    suspend fun fetchGeneralData(isManga: Boolean, rankingType: String): List<DbTrackSearch>32    suspend fun fetchUserData(isManga: Boolean): List<DbTrackSearch>33    suspend fun accountProfile(): TrackerAccountProfile? = null34    fun scoreValue(): Pair<Int, Int>35    fun displayScore(score: Int): String3637    /** Whether this tracker handles manga/novel ([isManga]) or anime entries. */38    fun supports(isManga: Boolean): Boolean = true3940    /** Connect-dialog fields. Empty keeps the original paste-a-token/OAuth dialog and [TrackerAccounts.connect]. */41    val loginFields: List<TrackerLoginField> get() = emptyList()4243    /** Browser sign-in page for the entered values (OAuth/approval), or null when the provider has none. */44    suspend fun authorizeUrl(values: Map<TrackerLoginField, String>): String? = null4546    /** Whether the connect dialog offers [authorizeUrl] ("Open sign-in"). */47    val browserSignIn: Boolean get() = TrackerLoginField.AuthorizationCode in loginFields4849    /**50     * Exchanges the entered credentials for the preference to store: the access token/session in51     * [DbTrackPreference.oauth], everything else in [TrackerCredentials] ([DbTrackPreference.prefs]).52     * Throws with a user-readable message on rejected credentials.53     */54    suspend fun login(values: Map<TrackerLoginField, String>): DbTrackPreference =55        throw UnsupportedOperationException("$name uses token sign-in")5657    /** Creates or fetches the remote list entry for a newly bound [track]; returns the state to store. */58    suspend fun bind(track: DbTrack, isManga: Boolean): DbTrack = track5960    /** Fetches the remote state of a bound [track]; returns the state to store. */61    suspend fun refresh(track: DbTrack, isManga: Boolean): DbTrack = track6263    /** Removes the remote list entry of [track]; local removal is the caller's. */64    suspend fun unbind(track: DbTrack, isManga: Boolean) {}65}6667/**68 * Animetail `EnhancedTracker`: a server-backed tracker that is never searched manually. It binds titles that69 * come from its own server's source ([accept]) by resolving the title's source URL ([match]), and its70 * progress is synchronized both ways with the local chapter/episode read state.71 */72interface EnhancedTracker {73    fun accept(source: DbSource): Boolean7475    /** Zero or one entry for [manga] on the configured server. */76    suspend fun match(manga: DbManga): DbTrackSearch?77}7879internal fun DbItemType.isMangaTracking(): Boolean = this != DbItemType.Anime8081data class TrackerAccountProfile(82    val name: String? = null,83    val avatarUrl: String? = null,84)8586// Sync ids the registry assigns; they key stored tracks and preferences, so they never change.87const val MalTrackerSyncId = 188const val AnilistTrackerSyncId = 289const val KitsuTrackerSyncId = 390const val SimklTrackerSyncId = 491const val TraktTrackerSyncId = 592const val MetadataTrackerSyncId = 693const val MangaUpdatesTrackerSyncId = 1094const val ShikimoriTrackerSyncId = 1195const val BangumiTrackerSyncId = 1296const val HikkaTrackerSyncId = 1397const val MangaBakaTrackerSyncId = 1498const val TmdbTrackerSyncId = 1599const val KomgaTrackerSyncId = 20100const val KavitaTrackerSyncId = 21101const val SuwayomiTrackerSyncId = 22102const val JellyfinTrackerSyncId = 23103104/** [all] in the user's configured tracker order, followed by unordered trackers in their default order. */105fun orderedTrackers(all: List<TrackerService>, db: DbSettings): List<TrackerService> {106    val configured = db.trackerOrder.orEmpty().mapNotNull { id -> all.firstOrNull { it.syncId == id } }107    return (configured + all).distinctBy { it.syncId }108}109110fun DbTrack.readableStatusList(isManga: Boolean): List<DbTrackStatus> =111    if (isManga) {112        listOf(113            DbTrackStatus.Reading,114            DbTrackStatus.Completed,115            DbTrackStatus.OnHold,116            DbTrackStatus.Dropped,117            DbTrackStatus.PlanToRead,118            DbTrackStatus.ReReading,119        )120    } else {121        listOf(122            DbTrackStatus.Watching,123            DbTrackStatus.Completed,124            DbTrackStatus.OnHold,125            DbTrackStatus.Dropped,126            DbTrackStatus.PlanToWatch,127            DbTrackStatus.ReWatching,128        )129    }130

Search chapters, reference and source filenames.