Add support for using the user's chosen Kitsu rating scales (#3818)

Add support for different Kitsu rating scales

Includes a database migration because we have been scaling Kitsu's
2-20 `ratingTwenty` integer value to a 1-10 (step 0.5) value and
stored that in the database.

I'm also "rounding" the values to the next lowest valid one in the
current rating system to mirror Kitsu's behaviour for this. Both Kitsu
and the app keep the misfit value around until the score is otherwise
edited, at which point the interface constrains the user selection to
only valid values (for the current system).

Maybe the RatingSystem data class is overkill, but a previous version
of mine had three constants for each system that had to be manually
associated at each corner. Encapsulating those in a little map of data
classes seemed the more ergonomic solution.
This commit is contained in:
MajorTanya
2026-08-20 20:11:27 +02:00
committed by GitHub
parent 4b5a0539e5
commit 01bfaa99ad
8 changed files with 74 additions and 12 deletions
@@ -8,6 +8,8 @@ import eu.kanade.tachiyomi.data.track.DeletableTracker
import eu.kanade.tachiyomi.data.track.kitsu.dto.KitsuOAuth
import eu.kanade.tachiyomi.data.track.model.TrackSearch
import kotlinx.serialization.json.Json
import logcat.LogPriority
import tachiyomi.core.common.util.system.logcat
import tachiyomi.i18n.MR
import uy.kohesive.injekt.injectLazy
import java.text.DecimalFormat
@@ -15,6 +17,12 @@ import tachiyomi.domain.track.model.Track as DomainTrack
class Kitsu(id: Long) : BaseTracker(id, "Kitsu"), DeletableTracker {
private data class RatingSystem(
val name: String,
val scoreList: List<String>,
val twentyScale: List<Int>,
)
companion object {
const val READING = 1L
const val COMPLETED = 2L
@@ -22,6 +30,33 @@ class Kitsu(id: Long) : BaseTracker(id, "Kitsu"), DeletableTracker {
const val DROPPED = 4L
const val PLAN_TO_READ = 5L
const val RATING_SIMPLE = "simple"
const val RATING_REGULAR = "regular"
const val RATING_ADVANCED = "advanced"
private val ratingSystems = mapOf(
// Smileys
RATING_SIMPLE to RatingSystem(
name = RATING_SIMPLE,
scoreList = listOf("-", "😡", "😐", "😊", "😀"),
twentyScale = (2..20 step 6).toList(), // 2, 8, 14, 20
),
// DecimalFormatter is not thread safe, so new formatters for each map instead of extracted val attribute
// to not incite reuse
// Stars (0.5-5 step 0.5)
RATING_REGULAR to RatingSystem(
name = RATING_REGULAR,
scoreList = (0..10).map { it / 2.0 }.map(DecimalFormat("0.#")::format).map { "$it" },
twentyScale = (2..20 step 2).toList(), // 2, 4, ..., 18, 20
),
// 10 point decimal (step 0.5, starting at 1) + 0 for our "not rated" placeholder
RATING_ADVANCED to RatingSystem(
name = RATING_ADVANCED,
scoreList = listOf("0") + (2..20).map { it / 2.0 }.map(DecimalFormat("0.#")::format),
twentyScale = (2..20).toList(), // 2, 3, ..., 19, 20
),
)
private const val SEARCH_ID_PREFIX = "id:"
}
@@ -35,6 +70,8 @@ class Kitsu(id: Long) : BaseTracker(id, "Kitsu"), DeletableTracker {
private val api by lazy { KitsuApi(client, interceptor) }
private val scorePreference by lazy { trackPreferences.kitsuScoreType }
override fun getLogo() = R.drawable.brand_kitsu
override fun getStatusList(): List<Long> {
@@ -56,18 +93,29 @@ class Kitsu(id: Long) : BaseTracker(id, "Kitsu"), DeletableTracker {
override fun getCompletionStatus(): Long = COMPLETED
override fun getScoreList(): List<String> {
val df = DecimalFormat("0.#")
return (listOf("0") + IntRange(2, 20).map { df.format(it / 2f) })
private fun getCurrentRatingSystem(): RatingSystem {
val ratingSystem = scorePreference.get()
return ratingSystems[ratingSystem] ?: throw Exception("Unknown score type $ratingSystem")
}
override fun getScoreList(): List<String> = getCurrentRatingSystem().scoreList
override fun get10PointScore(track: DomainTrack): Double {
// score is stored in Kitsu's native 2-20 scale
return track.score / 2.0
}
override fun indexToScore(index: Int): Double {
return if (index > 0) (index + 1) / 2.0 else 0.0
if (index == 0) return 0.0
return getCurrentRatingSystem().twentyScale[index - 1].toDouble()
}
override fun displayScore(track: DomainTrack): String {
val df = DecimalFormat("0.#")
return df.format(track.score)
val ratingSystem = getCurrentRatingSystem()
// Since Kitsu's valid score range is 2-20, unset values of -1.0 or 0.0 will both return -1 from indexOfLast
// which is turned into index 0 of the scoreList, giving us the "unset" display score (- or 0).
// Proper scores are "rounded down" to the nearest value of the scale (also what Kitsu's website does)
return ratingSystem.scoreList[ratingSystem.twentyScale.indexOfLast { it <= track.score } + 1]
}
private suspend fun add(track: Track): Track {
@@ -136,6 +184,14 @@ class Kitsu(id: Long) : BaseTracker(id, "Kitsu"), DeletableTracker {
val token = api.login(username, password)
interceptor.newAuth(token)
val currentUser = api.getCurrentUser()
val ratingSystem = currentUser.attributes.ratingSystem
if (ratingSystem in listOf(RATING_SIMPLE, RATING_REGULAR, RATING_ADVANCED)) {
scorePreference.set(ratingSystem)
} else {
logcat(LogPriority.ERROR) { "Unsupported Kitsu score type: $ratingSystem" }
scorePreference.set(RATING_ADVANCED)
}
saveDisplayUsername(currentUser.attributes.name)
saveCredentials(username, currentUser.id)
}
@@ -96,7 +96,7 @@ class KitsuApi(private val client: OkHttpClient, interceptor: KitsuInterceptor)
putJsonObject("attributes") {
put("status", track.toApiStatus())
put("progress", track.last_chapter_read.toInt())
put("ratingTwenty", track.toApiScore())
put("ratingTwenty", track.score.takeIf { it > 0 }?.toInt())
put("startedAt", KitsuDateHelper.convert(track.started_reading_date))
put("finishedAt", KitsuDateHelper.convert(track.finished_reading_date))
put("private", track.private)
@@ -10,7 +10,3 @@ fun Track.toApiStatus() = when (status) {
Kitsu.PLAN_TO_READ -> "planned"
else -> throw Exception("Unknown status")
}
fun Track.toApiScore(): String? {
return if (score > 0) (score * 2).toInt().toString() else null
}
@@ -41,7 +41,7 @@ data class KitsuListSearchResult(
"planned" -> Kitsu.PLAN_TO_READ
else -> throw Exception("Unknown status")
}
score = userDataAttrs.ratingTwenty?.let { it / 2.0 } ?: 0.0
score = userDataAttrs.ratingTwenty?.toDouble() ?: 0.0
last_chapter_read = userDataAttrs.progress.toDouble()
private = userDataAttrs.private
}
@@ -16,4 +16,5 @@ data class KitsuUser(
@Serializable
data class KitsuUserAttributes(
val name: String,
val ratingSystem: String,
)