Skip to content

Commit

Permalink
refactor: Use DTOs to parse tracking API responses
Browse files Browse the repository at this point in the history
  • Loading branch information
MajorTanya authored and null2264 committed Sep 3, 2024
1 parent 6ee9a41 commit 93738ff
Show file tree
Hide file tree
Showing 95 changed files with 1,452 additions and 1,125 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class BackupFileValidator(

val trackers = backup.backupManga
.flatMap { it.tracking }
.map { it.syncId }
.map { it.syncId.toLong() }
.distinct()
val missingTrackers = trackers
.mapNotNull { trackManager.getService(it) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ data class BackupTracking(

fun getTrackingImpl(): TrackImpl {
return TrackImpl().apply {
sync_id = this@BackupTracking.syncId
sync_id = this@BackupTracking.syncId.toLong()
media_id = if (this@BackupTracking.mediaIdInt != 0) {
this@BackupTracking.mediaIdInt.toLong()
} else {
Expand All @@ -41,7 +41,7 @@ data class BackupTracking(
library_id = this@BackupTracking.libraryId
title = this@BackupTracking.title
last_chapter_read = this@BackupTracking.lastChapterRead
total_chapters = this@BackupTracking.totalChapters
total_chapters = this@BackupTracking.totalChapters.toLong()
score = this@BackupTracking.score
status = this@BackupTracking.status
started_reading_date = this@BackupTracking.startedReadingDate
Expand All @@ -53,13 +53,13 @@ data class BackupTracking(
companion object {
fun copyFrom(track: Track): BackupTracking {
return BackupTracking(
syncId = track.sync_id,
syncId = track.sync_id.toInt(),
mediaId = track.media_id,
// forced not null so its compatible with 1.x backup system
libraryId = track.library_id!!,
title = track.title,
lastChapterRead = track.last_chapter_read,
totalChapters = track.total_chapters,
totalChapters = track.total_chapters.toInt(),
score = track.score,
status = track.status,
startedReadingDate = track.started_reading_date,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ class TrackGetResolver : DefaultGetResolver<Track>() {
override fun mapFromCursor(cursor: Cursor): Track = TrackImpl().apply {
id = cursor.getLong(cursor.getColumnIndexOrThrow(COL_ID))
manga_id = cursor.getLong(cursor.getColumnIndexOrThrow(COL_MANGA_ID))
sync_id = cursor.getInt(cursor.getColumnIndexOrThrow(COL_SYNC_ID))
sync_id = cursor.getLong(cursor.getColumnIndexOrThrow(COL_SYNC_ID))
media_id = cursor.getLong(cursor.getColumnIndexOrThrow(COL_MEDIA_ID))
library_id = cursor.getLong(cursor.getColumnIndexOrThrow(COL_LIBRARY_ID))
title = cursor.getString(cursor.getColumnIndexOrThrow(COL_TITLE))
last_chapter_read = cursor.getFloat(cursor.getColumnIndexOrThrow(COL_LAST_CHAPTER_READ))
total_chapters = cursor.getInt(cursor.getColumnIndexOrThrow(COL_TOTAL_CHAPTERS))
total_chapters = cursor.getLong(cursor.getColumnIndexOrThrow(COL_TOTAL_CHAPTERS))
status = cursor.getInt(cursor.getColumnIndexOrThrow(COL_STATUS))
score = cursor.getFloat(cursor.getColumnIndexOrThrow(COL_SCORE))
tracking_url = cursor.getString(cursor.getColumnIndexOrThrow(COL_TRACKING_URL))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ interface Track : Serializable {

var manga_id: Long

var sync_id: Int
var sync_id: Long

var media_id: Long

Expand All @@ -18,7 +18,7 @@ interface Track : Serializable {

var last_chapter_read: Float

var total_chapters: Int
var total_chapters: Long

var score: Float

Expand All @@ -39,7 +39,7 @@ interface Track : Serializable {
}

companion object {
fun create(serviceId: Int): Track = TrackImpl().apply {
fun create(serviceId: Long): Track = TrackImpl().apply {
sync_id = serviceId
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ class TrackImpl : Track {

override var id: Long? = null

override var manga_id: Long = 0
override var manga_id: Long = 0L

override var sync_id: Int = 0
override var sync_id: Long = 0L

override var media_id: Long = 0
override var media_id: Long = 0L

override var library_id: Long? = null

override lateinit var title: String

override var last_chapter_read: Float = 0F

override var total_chapters: Int = 0
override var total_chapters: Long = 0L

override var score: Float = 0f

Expand All @@ -43,7 +43,7 @@ class TrackImpl : Track {

override fun hashCode(): Int {
var result = manga_id.hashCode()
result = 31 * result + sync_id
result = 31 * result + sync_id.hashCode()
result = 31 * result + media_id.hashCode()
return result
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ import co.touchlab.kermit.Logger
import eu.kanade.tachiyomi.data.database.DatabaseHelper
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.util.system.e
import java.util.concurrent.TimeUnit
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
import java.util.concurrent.TimeUnit

class DelayedTrackingUpdateJob(context: Context, workerParams: WorkerParameters) :
CoroutineWorker(context, workerParams) {
Expand All @@ -32,7 +32,7 @@ class DelayedTrackingUpdateJob(context: Context, workerParams: WorkerParameters)
null
} else {
val mangaId = items[0].toLongOrNull() ?: return@mapNotNull null
val trackId = items[1].toIntOrNull() ?: return@mapNotNull null
val trackId = items[1].toLongOrNull() ?: return@mapNotNull null
val chapterNumber = items[2].toFloatOrNull() ?: return@mapNotNull null
mangaId to (trackId to chapterNumber)
}
Expand Down
20 changes: 10 additions & 10 deletions app/src/main/java/eu/kanade/tachiyomi/data/track/TrackManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ import eu.kanade.tachiyomi.data.track.suwayomi.Suwayomi
class TrackManager(context: Context) {

companion object {
const val MYANIMELIST = 1
const val ANILIST = 2
const val KITSU = 3
const val SHIKIMORI = 4
const val BANGUMI = 5
const val KOMGA = 6
const val MANGA_UPDATES = 7
const val KAVITA = 8
const val SUWAYOMI = 9
const val MYANIMELIST = 1L
const val ANILIST = 2L
const val KITSU = 3L
const val SHIKIMORI = 4L
const val BANGUMI = 5L
const val KOMGA = 6L
const val MANGA_UPDATES = 7L
const val KAVITA = 8L
const val SUWAYOMI = 9L
}

val myAnimeList = MyAnimeList(context, MYANIMELIST)
Expand All @@ -37,7 +37,7 @@ class TrackManager(context: Context) {

val services = listOf(myAnimeList, aniList, kitsu, shikimori, bangumi, komga, mangaUpdates, kavita, suwayomi)

fun getService(id: Int) = services.find { it.id == id }
fun getService(id: Long) = services.find { it.id == id }

fun hasLoggedServices() = services.any { it.isLogged }
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ class TrackPreferences(
fun autoUpdateTrack() = preferenceStore.getBoolean("pref_auto_update_manga_sync_key", true)

companion object {
fun trackUsername(syncId: Int) = Preference.privateKey("pref_mangasync_username_$syncId")
fun trackUsername(syncId: Long) = Preference.privateKey("pref_mangasync_username_$syncId")

private fun trackPassword(syncId: Int) =
private fun trackPassword(syncId: Long) =
Preference.privateKey("pref_mangasync_password_$syncId")

private fun trackToken(syncId: Int) = Preference.privateKey("track_token_$syncId")
private fun trackToken(syncId: Long) = Preference.privateKey("track_token_$syncId")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import eu.kanade.tachiyomi.data.database.models.isOneShotOrCompleted
import eu.kanade.tachiyomi.data.track.model.TrackSearch
import eu.kanade.tachiyomi.network.NetworkHelper
import eu.kanade.tachiyomi.util.system.executeOnIO
import kotlinx.collections.immutable.ImmutableList
import okhttp3.OkHttpClient
import uy.kohesive.injekt.injectLazy

abstract class TrackService(val id: Int) {
abstract class TrackService(val id: Long) {

val trackPreferences: TrackPreferences by injectLazy()
val networkService: NetworkHelper by injectLazy()
Expand Down Expand Up @@ -46,7 +47,7 @@ abstract class TrackService(val id: Int) {

abstract fun getGlobalStatus(status: Int): String

abstract fun getScoreList(): List<String>
abstract fun getScoreList(): ImmutableList<String>

open fun indexToScore(index: Int): Float {
return index.toFloat()
Expand Down Expand Up @@ -83,8 +84,8 @@ abstract class TrackService(val id: Int) {
}
if (setToComplete &&
(!mustReadToComplete || track.status == readingStatus()) &&
track.total_chapters != 0 &&
track.last_chapter_read.toInt() == track.total_chapters
track.total_chapters != 0L &&
track.last_chapter_read.toLong() == track.total_chapters
) {
track.status = completedStatus()
}
Expand Down
39 changes: 22 additions & 17 deletions app/src/main/java/eu/kanade/tachiyomi/data/track/anilist/Anilist.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,23 @@ import co.touchlab.kermit.Logger
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.database.models.Track
import eu.kanade.tachiyomi.data.track.TrackService
import eu.kanade.tachiyomi.data.track.anilist.dto.ALOAuth
import eu.kanade.tachiyomi.data.track.updateNewTrackInfo
import eu.kanade.tachiyomi.util.system.e
import kotlinx.collections.immutable.ImmutableList
import kotlinx.collections.immutable.toImmutableList
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import uy.kohesive.injekt.injectLazy
import yokai.i18n.MR
import yokai.util.lang.getString

class Anilist(private val context: Context, id: Int) : TrackService(id) {
class Anilist(private val context: Context, id: Long) : TrackService(id) {

companion object {
const val READING = 1
const val COMPLETED = 2
const val PAUSED = 3
const val ON_HOLD = 3
const val DROPPED = 4
const val PLAN_TO_READ = 5
const val REREADING = 6
Expand Down Expand Up @@ -62,7 +65,7 @@ class Anilist(private val context: Context, id: Int) : TrackService(id) {

override fun getLogoColor() = Color.rgb(18, 25, 35)

override fun getStatusList() = listOf(READING, PLAN_TO_READ, COMPLETED, REREADING, PAUSED, DROPPED)
override fun getStatusList() = listOf(READING, PLAN_TO_READ, COMPLETED, REREADING, ON_HOLD, DROPPED)

override fun isCompletedStatus(index: Int) = getStatusList()[index] == COMPLETED

Expand All @@ -75,7 +78,7 @@ class Anilist(private val context: Context, id: Int) : TrackService(id) {
READING -> getString(MR.strings.reading)
PLAN_TO_READ -> getString(MR.strings.plan_to_read)
COMPLETED -> getString(MR.strings.completed)
PAUSED -> getString(MR.strings.paused)
ON_HOLD -> getString(MR.strings.paused)
DROPPED -> getString(MR.strings.dropped)
REREADING -> getString(MR.strings.rereading)
else -> ""
Expand All @@ -87,25 +90,25 @@ class Anilist(private val context: Context, id: Int) : TrackService(id) {
READING -> getString(MR.strings.reading)
PLAN_TO_READ -> getString(MR.strings.plan_to_read)
COMPLETED -> getString(MR.strings.completed)
PAUSED -> getString(MR.strings.on_hold)
ON_HOLD -> getString(MR.strings.on_hold)
DROPPED -> getString(MR.strings.dropped)
REREADING -> getString(MR.strings.rereading)
else -> ""
}
}

override fun getScoreList(): List<String> {
override fun getScoreList(): ImmutableList<String> {
return when (scorePreference.get()) {
// 10 point
POINT_10 -> IntRange(0, 10).map(Int::toString)
POINT_10 -> IntRange(0, 10).map(Int::toString).toImmutableList()
// 100 point
POINT_100 -> IntRange(0, 100).map(Int::toString)
POINT_100 -> IntRange(0, 100).map(Int::toString).toImmutableList()
// 5 stars
POINT_5 -> IntRange(0, 5).map { "$it" }
POINT_5 -> IntRange(0, 5).map { "$it" }.toImmutableList()
// Smiley
POINT_3 -> listOf("-", "😦", "😐", "😊")
POINT_3 -> listOf("-", "😦", "😐", "😊").toImmutableList()
// 10 point decimal
POINT_10_DECIMAL -> IntRange(0, 100).map { (it / 10f).toString() }
POINT_10_DECIMAL -> IntRange(0, 100).map { (it / 10f).toString() }.toImmutableList()
else -> throw Exception("Unknown score type")
}
}
Expand Down Expand Up @@ -142,13 +145,15 @@ class Anilist(private val context: Context, id: Int) : TrackService(id) {
0f -> "0 ★"
else -> "${((score + 10) / 20).toInt()}"
}

POINT_3 -> when {
score == 0f -> "0"
score <= 35 -> "😦"
score <= 60 -> "😐"
else -> "😊"
}
else -> track.toAnilistScore()

else -> track.toApiScore()
}
}

Expand Down Expand Up @@ -207,7 +212,7 @@ class Anilist(private val context: Context, id: Int) : TrackService(id) {
interceptor.setAuth(oauth)
val (username, scoreType) = api.getCurrentUser()
scorePreference.set(scoreType)
saveCredentials(username.toString(), oauth.access_token)
saveCredentials(username.toString(), oauth.accessToken)
true
} catch (e: Exception) {
Logger.e(e)
Expand All @@ -233,13 +238,13 @@ class Anilist(private val context: Context, id: Int) : TrackService(id) {
interceptor.setAuth(null)
}

fun saveOAuth(oAuth: OAuth?) {
trackPreferences.trackToken(this).set(json.encodeToString(oAuth))
fun saveOAuth(alOAuth: ALOAuth?) {
trackPreferences.trackToken(this).set(json.encodeToString(alOAuth))
}

fun loadOAuth(): OAuth? {
fun loadOAuth(): ALOAuth? {
return try {
json.decodeFromString<OAuth>(trackPreferences.trackToken(this).get())
json.decodeFromString<ALOAuth>(trackPreferences.trackToken(this).get())
} catch (e: Exception) {
Logger.e(e)
null
Expand Down
Loading

0 comments on commit 93738ff

Please sign in to comment.