Skip to content

Commit

Permalink
Apply review changes
Browse files Browse the repository at this point in the history
- Renamed `BangumiX` classes to `BGMX` classes.
- Renamed `toXStatus` and `toXScore` to `toApiStatus` and `toApiScore`
  • Loading branch information
MajorTanya committed Aug 19, 2024
1 parent 815c63f commit e9a1b7a
Show file tree
Hide file tree
Showing 13 changed files with 42 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ class Anilist(id: Long) : BaseTracker(id, "AniList"), DeletableTracker {
else -> "😊"
}

else -> track.toAnilistScore()
else -> track.toApiScore()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class AnilistApi(val client: OkHttpClient, interceptor: AnilistInterceptor) {
putJsonObject("variables") {
put("mangaId", track.remote_id)
put("progress", track.last_chapter_read.toInt())
put("status", track.toAnilistStatus())
put("status", track.toApiStatus())
}
}
with(json) {
Expand Down Expand Up @@ -98,7 +98,7 @@ class AnilistApi(val client: OkHttpClient, interceptor: AnilistInterceptor) {
putJsonObject("variables") {
put("listId", track.library_id)
put("progress", track.last_chapter_read.toInt())
put("status", track.toAnilistStatus())
put("status", track.toApiStatus())
put("score", track.score.toInt())
put("startedAt", createDate(track.started_reading_date))
put("completedAt", createDate(track.finished_reading_date))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import eu.kanade.tachiyomi.data.database.models.Track
import uy.kohesive.injekt.injectLazy
import tachiyomi.domain.track.model.Track as DomainTrack

fun Track.toAnilistStatus() = when (status) {
fun Track.toApiStatus() = when (status) {
Anilist.READING -> "CURRENT"
Anilist.COMPLETED -> "COMPLETED"
Anilist.ON_HOLD -> "PAUSED"
Expand All @@ -18,7 +18,7 @@ fun Track.toAnilistStatus() = when (status) {
private val preferences: TrackPreferences by injectLazy()

@Suppress("MagicNumber", "CyclomaticComplexMethod")
fun DomainTrack.toAnilistScore(): String = when (preferences.anilistScoreType().get()) {
fun DomainTrack.toApiScore(): String = when (preferences.anilistScoreType().get()) {
// 10 point
"POINT_10" -> (score.toInt() / 10).toString()
// 100 point
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import dev.icerock.moko.resources.StringResource
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.database.models.Track
import eu.kanade.tachiyomi.data.track.BaseTracker
import eu.kanade.tachiyomi.data.track.bangumi.dto.BangumiOAuth
import eu.kanade.tachiyomi.data.track.bangumi.dto.BGMOAuth
import eu.kanade.tachiyomi.data.track.model.TrackSearch
import kotlinx.collections.immutable.ImmutableList
import kotlinx.collections.immutable.toImmutableList
Expand Down Expand Up @@ -119,13 +119,13 @@ class Bangumi(id: Long) : BaseTracker(id, "Bangumi") {
}
}

fun saveToken(oauth: BangumiOAuth?) {
fun saveToken(oauth: BGMOAuth?) {
trackPreferences.trackToken(this).set(json.encodeToString(oauth))
}

fun restoreToken(): BangumiOAuth? {
fun restoreToken(): BGMOAuth? {
return try {
json.decodeFromString<BangumiOAuth>(trackPreferences.trackToken(this).get())
json.decodeFromString<BGMOAuth>(trackPreferences.trackToken(this).get())
} catch (e: Exception) {
null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package eu.kanade.tachiyomi.data.track.bangumi
import android.net.Uri
import androidx.core.net.toUri
import eu.kanade.tachiyomi.data.database.models.Track
import eu.kanade.tachiyomi.data.track.bangumi.dto.BangumiCollectionResponse
import eu.kanade.tachiyomi.data.track.bangumi.dto.BangumiOAuth
import eu.kanade.tachiyomi.data.track.bangumi.dto.BangumiSearchItem
import eu.kanade.tachiyomi.data.track.bangumi.dto.BangumiSearchResult
import eu.kanade.tachiyomi.data.track.bangumi.dto.BGMCollectionResponse
import eu.kanade.tachiyomi.data.track.bangumi.dto.BGMOAuth
import eu.kanade.tachiyomi.data.track.bangumi.dto.BGMSearchItem
import eu.kanade.tachiyomi.data.track.bangumi.dto.BGMSearchResult
import eu.kanade.tachiyomi.data.track.model.TrackSearch
import eu.kanade.tachiyomi.network.GET
import eu.kanade.tachiyomi.network.POST
Expand Down Expand Up @@ -36,7 +36,7 @@ class BangumiApi(
return withIOContext {
val body = FormBody.Builder()
.add("rating", track.score.toInt().toString())
.add("status", track.toBangumiStatus())
.add("status", track.toApiStatus())
.build()
authClient.newCall(POST("$API_URL/collection/${track.remote_id}/update", body = body))
.awaitSuccess()
Expand All @@ -49,7 +49,7 @@ class BangumiApi(
// read status update
val sbody = FormBody.Builder()
.add("rating", track.score.toInt().toString())
.add("status", track.toBangumiStatus())
.add("status", track.toApiStatus())
.build()
authClient.newCall(POST("$API_URL/collection/${track.remote_id}/update", body = sbody))
.awaitSuccess()
Expand Down Expand Up @@ -77,7 +77,7 @@ class BangumiApi(
with(json) {
authClient.newCall(GET(url.toString()))
.awaitSuccess()
.parseAs<BangumiSearchResult>()
.parseAs<BGMSearchResult>()
.let { result ->
if (result.code == 404) emptyList<TrackSearch>()

Expand All @@ -95,7 +95,7 @@ class BangumiApi(
with(json) {
authClient.newCall(GET("$API_URL/subject/${track.remote_id}"))
.awaitSuccess()
.parseAs<BangumiSearchItem>()
.parseAs<BGMSearchItem>()
.toTrackSearch(trackId)
}
}
Expand All @@ -115,7 +115,7 @@ class BangumiApi(
with(json) {
authClient.newCall(requestUserRead)
.awaitSuccess()
.parseAs<BangumiCollectionResponse>()
.parseAs<BGMCollectionResponse>()
.let {
if (it.code == 400) return@let null

Expand All @@ -128,7 +128,7 @@ class BangumiApi(
}
}

suspend fun accessToken(code: String): BangumiOAuth {
suspend fun accessToken(code: String): BGMOAuth {
return withIOContext {
with(json) {
client.newCall(accessTokenRequest(code))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package eu.kanade.tachiyomi.data.track.bangumi

import eu.kanade.tachiyomi.BuildConfig
import eu.kanade.tachiyomi.data.track.bangumi.dto.BangumiOAuth
import eu.kanade.tachiyomi.data.track.bangumi.dto.BGMOAuth
import eu.kanade.tachiyomi.data.track.bangumi.dto.isExpired
import kotlinx.serialization.json.Json
import okhttp3.FormBody
Expand All @@ -16,7 +16,7 @@ class BangumiInterceptor(private val bangumi: Bangumi) : Interceptor {
/**
* OAuth object used for authenticated requests.
*/
private var oauth: BangumiOAuth? = bangumi.restoreToken()
private var oauth: BGMOAuth? = bangumi.restoreToken()

override fun intercept(chain: Interceptor.Chain): Response {
val originalRequest = chain.request()
Expand All @@ -26,7 +26,7 @@ class BangumiInterceptor(private val bangumi: Bangumi) : Interceptor {
if (currAuth.isExpired()) {
val response = chain.proceed(BangumiApi.refreshTokenRequest(currAuth.refreshToken!!))
if (response.isSuccessful) {
newAuth(json.decodeFromString<BangumiOAuth>(response.body.string()))
newAuth(json.decodeFromString<BGMOAuth>(response.body.string()))
} else {
response.close()
}
Expand All @@ -51,11 +51,11 @@ class BangumiInterceptor(private val bangumi: Bangumi) : Interceptor {
.let(chain::proceed)
}

fun newAuth(oauth: BangumiOAuth?) {
fun newAuth(oauth: BGMOAuth?) {
this.oauth = if (oauth == null) {
null
} else {
BangumiOAuth(
BGMOAuth(
oauth.accessToken,
oauth.tokenType,
System.currentTimeMillis() / 1000,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package eu.kanade.tachiyomi.data.track.bangumi

import eu.kanade.tachiyomi.data.database.models.Track

fun Track.toBangumiStatus() = when (status) {
fun Track.toApiStatus() = when (status) {
Bangumi.READING -> "do"
Bangumi.COMPLETED -> "collect"
Bangumi.ON_HOLD -> "on_hold"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class BangumiCollectionResponse(
data class BGMCollectionResponse(
val code: Int?,
val `private`: Int? = 0,
val comment: String? = "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class BangumiOAuth(
data class BGMOAuth(
@SerialName("access_token")
val accessToken: String,
@SerialName("token_type")
Expand All @@ -22,4 +22,4 @@ data class BangumiOAuth(

// Access token refresh before expired
@Suppress("MagicNumber")
fun BangumiOAuth.isExpired() = (System.currentTimeMillis() / 1000) > (createdAt + expiresIn - 3600)
fun BGMOAuth.isExpired() = (System.currentTimeMillis() / 1000) > (createdAt + expiresIn - 3600)
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,41 @@ import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class BangumiSearchResult(
val list: List<BangumiSearchItem>?,
data class BGMSearchResult(
val list: List<BGMSearchItem>?,
val code: Int?,
)

@Serializable
data class BangumiSearchItem(
data class BGMSearchItem(
val id: Long,
@SerialName("name_cn")
val nameCn: String,
val name: String,
val type: Int,
val images: BangumiSearchItemCovers?,
val images: BGMSearchItemCovers?,
@SerialName("eps_count")
val epsCount: Long?,
val rating: BangumiSearchItemRating?,
val rating: BGMSearchItemRating?,
val url: String,
) {
fun toTrackSearch(trackId: Long): TrackSearch = TrackSearch.create(trackId).apply {
remote_id = this@BangumiSearchItem.id
remote_id = this@BGMSearchItem.id
title = nameCn
cover_url = images?.common ?: ""
summary = this@BangumiSearchItem.name
summary = this@BGMSearchItem.name
score = rating?.score ?: -1.0
tracking_url = url
total_chapters = epsCount ?: 0
}
}

@Serializable
data class BangumiSearchItemCovers(
data class BGMSearchItemCovers(
val common: String?,
)

@Serializable
data class BangumiSearchItemRating(
data class BGMSearchItemRating(
val score: Double?,
)
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class KitsuApi(private val client: OkHttpClient, interceptor: KitsuInterceptor)
putJsonObject("data") {
put("type", "libraryEntries")
putJsonObject("attributes") {
put("status", track.toKitsuStatus())
put("status", track.toApiStatus())
put("progress", track.last_chapter_read.toInt())
}
putJsonObject("relationships") {
Expand Down Expand Up @@ -89,9 +89,9 @@ class KitsuApi(private val client: OkHttpClient, interceptor: KitsuInterceptor)
put("type", "libraryEntries")
put("id", track.remote_id)
putJsonObject("attributes") {
put("status", track.toKitsuStatus())
put("status", track.toApiStatus())
put("progress", track.last_chapter_read.toInt())
put("ratingTwenty", track.toKitsuScore())
put("ratingTwenty", track.toApiScore())
put("startedAt", KitsuDateHelper.convert(track.started_reading_date))
put("finishedAt", KitsuDateHelper.convert(track.finished_reading_date))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package eu.kanade.tachiyomi.data.track.kitsu
import eu.kanade.tachiyomi.data.database.models.Track

@Suppress("TooGenericExceptionThrown")
fun Track.toKitsuStatus() = when (status) {
fun Track.toApiStatus() = when (status) {
Kitsu.READING -> "current"
Kitsu.COMPLETED -> "completed"
Kitsu.ON_HOLD -> "on_hold"
Expand All @@ -12,6 +12,6 @@ fun Track.toKitsuStatus() = when (status) {
else -> throw Exception("Unknown status")
}

fun Track.toKitsuScore(): String? {
fun Track.toApiScore(): String? {
return if (score > 0) (score * 2).toInt().toString() else null
}

0 comments on commit e9a1b7a

Please sign in to comment.