Skip to content

Commit

Permalink
Update types of legacy tracker model to match to domain one (#245)
Browse files Browse the repository at this point in the history
* `score` to Double

* `tracker_id` to Long

* `last_chapter_read` to Double

* `total_chapters` to Long

* `status` to Long
  • Loading branch information
AntsyLich authored Jan 27, 2024
1 parent 65bfa08 commit 05efc4e
Show file tree
Hide file tree
Showing 36 changed files with 249 additions and 244 deletions.
20 changes: 9 additions & 11 deletions app/src/main/java/eu/kanade/domain/track/model/Track.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ fun Track.toDbTrack(): DbTrack = DbTrack.create(trackerId).also {
it.remote_id = remoteId
it.library_id = libraryId
it.title = title
it.last_chapter_read = lastChapterRead.toFloat()
it.total_chapters = totalChapters.toInt()
it.status = status.toInt()
it.score = score.toFloat()
it.last_chapter_read = lastChapterRead
it.total_chapters = totalChapters
it.status = status
it.score = score
it.tracking_url = remoteUrl
it.started_reading_date = startDate
it.finished_reading_date = finishDate
Expand All @@ -33,16 +33,14 @@ fun DbTrack.toDomainTrack(idRequired: Boolean = true): Track? {
return Track(
id = trackId,
mangaId = manga_id,
trackerId = tracker_id.toLong(),
trackerId = tracker_id,
remoteId = remote_id,
libraryId = library_id,
title = title,
lastChapterRead = last_chapter_read.toDouble(),
totalChapters = total_chapters.toLong(),
status = status.toLong(),
// Jank workaround due to precision issues while converting
// See https://github.com/tachiyomiorg/tachiyomi/issues/10343
score = score.toString().toDouble(),
lastChapterRead = last_chapter_read,
totalChapters = total_chapters,
status = status,
score = score,
remoteUrl = tracking_url,
startDate = started_reading_date,
finishDate = finished_reading_date,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ fun TrackInfoDialogHome(
TrackInfoItem(
title = item.track.title,
tracker = item.tracker,
status = item.tracker.getStatus(item.track.status.toInt()),
status = item.tracker.getStatus(item.track.status),
onStatusClick = { onStatusClick(item) },
chapters = "${item.track.lastChapterRead.toInt()}".let {
val totalChapters = item.track.totalChapters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ import tachiyomi.presentation.core.util.isScrolledToStart

@Composable
fun TrackStatusSelector(
selection: Int,
onSelectionChange: (Int) -> Unit,
selections: Map<Int, StringResource?>,
selection: Long,
onSelectionChange: (Long) -> Unit,
selections: Map<Long, StringResource?>,
onConfirm: () -> Unit,
onDismissRequest: () -> Unit,
) {
Expand Down Expand Up @@ -236,12 +236,12 @@ private fun TrackStatusSelectorPreviews() {
onSelectionChange = {},
selections = persistentMapOf(
// Anilist values
1 to MR.strings.reading,
2 to MR.strings.plan_to_read,
3 to MR.strings.completed,
4 to MR.strings.on_hold,
5 to MR.strings.dropped,
6 to MR.strings.repeating,
1L to MR.strings.reading,
2L to MR.strings.plan_to_read,
3L to MR.strings.completed,
4L to MR.strings.on_hold,
5L to MR.strings.dropped,
6L to MR.strings.repeating,
),
onConfirm = {},
onDismissRequest = {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ private fun SearchResultItem(
text = status,
)
}
if (trackSearch.score != -1f) {
if (trackSearch.score != -1.0) {
SearchResultItemDetails(
title = stringResource(MR.strings.score),
text = trackSearch.score.toString(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,14 @@ internal class TrackerSearchPreviewProvider : PreviewParameterProvider<@Composab
private fun randTrackSearch() = TrackSearch().let {
it.id = Random.nextLong()
it.manga_id = Random.nextLong()
it.tracker_id = Random.nextInt()
it.tracker_id = Random.nextLong()
it.remote_id = Random.nextLong()
it.library_id = Random.nextLong()
it.title = lorem((1..10).random()).joinToString()
it.last_chapter_read = (0..100).random().toFloat()
it.total_chapters = (100..1000).random()
it.score = (0..10).random().toFloat()
it.status = Random.nextInt()
it.last_chapter_read = (0..100).random().toDouble()
it.total_chapters = (100L..1000L).random()
it.score = (0..10).random().toDouble()
it.status = Random.nextLong()
it.started_reading_date = 0L
it.finished_reading_date = 0L
it.tracking_url = "https://example.com/tracker-example"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@ interface Track : Serializable {

var manga_id: Long

var tracker_id: Int
var tracker_id: Long

var remote_id: Long

var library_id: Long?

var title: String

var last_chapter_read: Float
var last_chapter_read: Double

var total_chapters: Int
var total_chapters: Long

var score: Float
var score: Double

var status: Int
var status: Long

var started_reading_date: Long

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

companion object {
fun create(serviceId: Long): Track = TrackImpl().apply {
tracker_id = serviceId.toInt()
tracker_id = serviceId
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@ class TrackImpl : Track {

override var manga_id: Long = 0

override var tracker_id: Int = 0
override var tracker_id: Long = 0

override var remote_id: Long = 0

override var library_id: Long? = null

override lateinit var title: String

override var last_chapter_read: Float = 0F
override var last_chapter_read: Double = 0.0

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

override var score: Float = 0f
override var score: Double = 0.0

override var status: Int = 0
override var status: Long = 0

override var started_reading_date: Long = 0

Expand Down
16 changes: 8 additions & 8 deletions app/src/main/java/eu/kanade/tachiyomi/data/track/BaseTracker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ abstract class BaseTracker(
return track.score
}

override fun indexToScore(index: Int): Float {
return index.toFloat()
override fun indexToScore(index: Int): Double {
return index.toDouble()
}

@CallSuper
Expand Down Expand Up @@ -70,24 +70,24 @@ abstract class BaseTracker(
}
}

override suspend fun setRemoteStatus(track: Track, status: Int) {
override suspend fun setRemoteStatus(track: Track, status: Long) {
track.status = status
if (track.status == getCompletionStatus() && track.total_chapters != 0) {
track.last_chapter_read = track.total_chapters.toFloat()
if (track.status == getCompletionStatus() && track.total_chapters != 0L) {
track.last_chapter_read = track.total_chapters.toDouble()
}
updateRemote(track)
}

override suspend fun setRemoteLastChapterRead(track: Track, chapterNumber: Int) {
if (
track.last_chapter_read == 0f &&
track.last_chapter_read == 0.0 &&
track.last_chapter_read < chapterNumber &&
track.status != getRereadingStatus()
) {
track.status = getReadingStatus()
}
track.last_chapter_read = chapterNumber.toFloat()
if (track.total_chapters != 0 && track.last_chapter_read.toInt() == track.total_chapters) {
track.last_chapter_read = chapterNumber.toDouble()
if (track.total_chapters != 0L && track.last_chapter_read.toLong() == track.total_chapters) {
track.status = getCompletionStatus()
track.finished_reading_date = System.currentTimeMillis()
}
Expand Down
14 changes: 7 additions & 7 deletions app/src/main/java/eu/kanade/tachiyomi/data/track/Tracker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,22 @@ interface Tracker {
@DrawableRes
fun getLogo(): Int

fun getStatusList(): List<Int>
fun getStatusList(): List<Long>

fun getStatus(status: Int): StringResource?
fun getStatus(status: Long): StringResource?

fun getReadingStatus(): Int
fun getReadingStatus(): Long

fun getRereadingStatus(): Int
fun getRereadingStatus(): Long

fun getCompletionStatus(): Int
fun getCompletionStatus(): Long

fun getScoreList(): ImmutableList<String>

// TODO: Store all scores as 10 point in the future maybe?
fun get10PointScore(track: DomainTrack): Double

fun indexToScore(index: Int): Float
fun indexToScore(index: Int): Double

fun displayScore(track: DomainTrack): String

Expand Down Expand Up @@ -70,7 +70,7 @@ interface Tracker {
// TODO: move this to an interactor, and update all trackers based on common data
suspend fun register(item: Track, mangaId: Long)

suspend fun setRemoteStatus(track: Track, status: Int)
suspend fun setRemoteStatus(track: Track, status: Long)

suspend fun setRemoteLastChapterRead(track: Track, chapterNumber: Int)

Expand Down
44 changes: 22 additions & 22 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 @@ -20,12 +20,12 @@ import tachiyomi.domain.track.model.Track as DomainTrack
class Anilist(id: Long) : BaseTracker(id, "AniList"), DeletableTracker {

companion object {
const val READING = 1
const val COMPLETED = 2
const val ON_HOLD = 3
const val DROPPED = 4
const val PLAN_TO_READ = 5
const val REREADING = 6
const val READING = 1L
const val COMPLETED = 2L
const val ON_HOLD = 3L
const val DROPPED = 4L
const val PLAN_TO_READ = 5L
const val REREADING = 6L

const val POINT_100 = "POINT_100"
const val POINT_10 = "POINT_10"
Expand Down Expand Up @@ -58,11 +58,11 @@ class Anilist(id: Long) : BaseTracker(id, "AniList"), DeletableTracker {

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

override fun getStatusList(): List<Int> {
override fun getStatusList(): List<Long> {
return listOf(READING, COMPLETED, ON_HOLD, DROPPED, PLAN_TO_READ, REREADING)
}

override fun getStatus(status: Int): StringResource? = when (status) {
override fun getStatus(status: Long): StringResource? = when (status) {
READING -> MR.strings.reading
PLAN_TO_READ -> MR.strings.plan_to_read
COMPLETED -> MR.strings.completed
Expand All @@ -72,11 +72,11 @@ class Anilist(id: Long) : BaseTracker(id, "AniList"), DeletableTracker {
else -> null
}

override fun getReadingStatus(): Int = READING
override fun getReadingStatus(): Long = READING

override fun getRereadingStatus(): Int = REREADING
override fun getRereadingStatus(): Long = REREADING

override fun getCompletionStatus(): Int = COMPLETED
override fun getCompletionStatus(): Long = COMPLETED

override fun getScoreList(): ImmutableList<String> {
return when (scorePreference.get()) {
Expand All @@ -99,24 +99,24 @@ class Anilist(id: Long) : BaseTracker(id, "AniList"), DeletableTracker {
return track.score / 10.0
}

override fun indexToScore(index: Int): Float {
override fun indexToScore(index: Int): Double {
return when (scorePreference.get()) {
// 10 point
POINT_10 -> index * 10f
POINT_10 -> index * 10.0
// 100 point
POINT_100 -> index.toFloat()
POINT_100 -> index.toDouble()
// 5 stars
POINT_5 -> when (index) {
0 -> 0f
else -> index * 20f - 10f
0 -> 0.0
else -> index * 20.0 - 10.0
}
// Smiley
POINT_3 -> when (index) {
0 -> 0f
else -> index * 25f + 10f
0 -> 0.0
else -> index * 25.0 + 10.0
}
// 10 point decimal
POINT_10_DECIMAL -> index.toFloat()
POINT_10_DECIMAL -> index.toDouble()
else -> throw Exception("Unknown score type")
}
}
Expand Down Expand Up @@ -153,12 +153,12 @@ class Anilist(id: Long) : BaseTracker(id, "AniList"), DeletableTracker {

if (track.status != COMPLETED) {
if (didReadChapter) {
if (track.last_chapter_read.toInt() == track.total_chapters && track.total_chapters > 0) {
if (track.last_chapter_read.toLong() == track.total_chapters && track.total_chapters > 0) {
track.status = COMPLETED
track.finished_reading_date = System.currentTimeMillis()
} else if (track.status != REREADING) {
track.status = READING
if (track.last_chapter_read == 1F) {
if (track.last_chapter_read == 1.0) {
track.started_reading_date = System.currentTimeMillis()
}
}
Expand Down Expand Up @@ -192,7 +192,7 @@ class Anilist(id: Long) : BaseTracker(id, "AniList"), DeletableTracker {
} else {
// Set default fields if it's not found in the list
track.status = if (hasReadChapters) READING else PLAN_TO_READ
track.score = 0F
track.score = 0.0
add(track)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import kotlinx.serialization.json.jsonArray
import kotlinx.serialization.json.jsonObject
import kotlinx.serialization.json.jsonPrimitive
import kotlinx.serialization.json.long
import kotlinx.serialization.json.longOrNull
import kotlinx.serialization.json.put
import kotlinx.serialization.json.putJsonObject
import okhttp3.OkHttpClient
Expand Down Expand Up @@ -312,7 +313,7 @@ class AnilistApi(val client: OkHttpClient, interceptor: AnilistInterceptor) {
struct["format"]!!.jsonPrimitive.content.replace("_", "-"),
struct["status"]!!.jsonPrimitive.contentOrNull ?: "",
parseDate(struct, "startDate"),
struct["chapters"]!!.jsonPrimitive.intOrNull ?: 0,
struct["chapters"]!!.jsonPrimitive.longOrNull ?: 0,
struct["averageScore"]?.jsonPrimitive?.intOrNull ?: -1,
)
}
Expand Down
Loading

0 comments on commit 05efc4e

Please sign in to comment.