Skip to content

Commit

Permalink
Barebones setup (only AniList works)
Browse files Browse the repository at this point in the history
  • Loading branch information
NGB-Was-Taken committed Sep 7, 2024
1 parent dea3891 commit c8c4a6b
Show file tree
Hide file tree
Showing 8 changed files with 172 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import eu.kanade.domain.track.interactor.AddTracks
import eu.kanade.domain.track.model.toDomainTrack
import eu.kanade.domain.track.service.TrackPreferences
import eu.kanade.tachiyomi.data.database.models.Track
import eu.kanade.tachiyomi.data.track.model.TrackMangaMetadata
import eu.kanade.tachiyomi.network.NetworkHelper
import eu.kanade.tachiyomi.util.system.toast
import kotlinx.coroutines.flow.Flow
Expand Down Expand Up @@ -120,6 +121,12 @@ abstract class BaseTracker(
updateRemote(track)
}

override suspend fun getMangaMetadata(track: DomainTrack): TrackMangaMetadata {
return TrackMangaMetadata(
0, "test", "test", "test", "test", "test",
)
}

private suspend fun updateRemote(track: Track): Unit = withIOContext {
try {
update(track)
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/java/eu/kanade/tachiyomi/data/track/Tracker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import androidx.annotation.ColorInt
import androidx.annotation.DrawableRes
import dev.icerock.moko.resources.StringResource
import eu.kanade.tachiyomi.data.database.models.Track
import eu.kanade.tachiyomi.data.track.model.TrackMangaMetadata
import eu.kanade.tachiyomi.data.track.model.TrackSearch
import kotlinx.collections.immutable.ImmutableList
import kotlinx.coroutines.flow.Flow
Expand Down Expand Up @@ -82,4 +83,6 @@ interface Tracker {
suspend fun setRemoteStartDate(track: Track, epochMillis: Long)

suspend fun setRemoteFinishDate(track: Track, epochMillis: Long)

suspend fun getMangaMetadata(track: DomainTrack): TrackMangaMetadata
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ 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.DeletableTracker
import eu.kanade.tachiyomi.data.track.model.TrackMangaMetadata
import eu.kanade.tachiyomi.data.track.model.TrackSearch
import kotlinx.collections.immutable.ImmutableList
import kotlinx.collections.immutable.persistentListOf
Expand Down Expand Up @@ -229,6 +230,10 @@ class Anilist(id: Long) : BaseTracker(id, "AniList"), DeletableTracker {
interceptor.setAuth(null)
}

override suspend fun getMangaMetadata(track: DomainTrack): TrackMangaMetadata {
return api.getMangaMetadata(track)
}

fun saveOAuth(oAuth: OAuth?) {
trackPreferences.trackToken(this).set(json.encodeToString(oAuth))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package eu.kanade.tachiyomi.data.track.anilist
import android.net.Uri
import androidx.core.net.toUri
import eu.kanade.tachiyomi.data.database.models.Track
import eu.kanade.tachiyomi.data.track.model.TrackMangaMetadata
import eu.kanade.tachiyomi.data.track.model.TrackSearch
import eu.kanade.tachiyomi.network.POST
import eu.kanade.tachiyomi.network.awaitSuccess
Expand Down Expand Up @@ -304,6 +305,79 @@ class AnilistApi(val client: OkHttpClient, interceptor: AnilistInterceptor) {
}
}

suspend fun getMangaMetadata(track: DomainTrack): TrackMangaMetadata {
return withIOContext {
val query = """
|query (${'$'}mangaId: Int!) {
|Media (id: ${'$'}mangaId) {
|id
|title {
|userPreferred
|}
|coverImage {
|extraLarge
|}
|description
|staff {
|edges {
|role
|node {
|name {
|userPreferred
|}
|}
|}
|}
|}
|}
|
""".trimMargin()
val payload = buildJsonObject {
put("query", query)
putJsonObject("variables") {
put("mangaId", track.remoteId)
}
}
with(json) {
authClient.newCall(
POST(
API_URL,
body = payload.toString().toRequestBody(jsonMime),
),
)
.awaitSuccess()
.parseAs<JsonObject>()
.let {
val data = it["data"]!!.jsonObject
val media = data["Media"]!!.jsonObject
jsonToTrackMangaMetadata(media)
}
}
}
}

private fun jsonToTrackMangaMetadata(struct: JsonObject): TrackMangaMetadata {
val edges = struct["staff"]!!.jsonObject["edges"]!!.jsonArray
val authors = edges.filter {
it.jsonObject["role"]!!.jsonPrimitive.content == "Story"
}.map {
it.jsonObject["node"]!!.jsonObject["name"]!!.jsonObject["userPreferred"]!!.jsonPrimitive.content
}.joinToString(", ")
val artists = edges.filter {
it.jsonObject["role"]!!.jsonPrimitive.content == "Art"
}.map {
it.jsonObject["node"]!!.jsonObject["name"]!!.jsonObject["userPreferred"]!!.jsonPrimitive.content
}.joinToString(", ")
return TrackMangaMetadata(
struct["id"]!!.jsonPrimitive.long,
struct["title"]!!.jsonObject["userPreferred"]!!.jsonPrimitive.content,
struct["coverImage"]!!.jsonObject["extraLarge"]!!.jsonPrimitive.contentOrNull,
struct["description"]!!.jsonPrimitive.contentOrNull,
authors,
artists,
)
}

private fun jsonToALManga(struct: JsonObject): ALManga {
return ALManga(
struct["id"]!!.jsonPrimitive.long,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package eu.kanade.tachiyomi.data.track.model

data class TrackMangaMetadata(
val remoteId: Long,
val title: String,
val thumbnailUrl: String?,
val description: String?,
val authors: String?,
val artists: String?,
)
35 changes: 33 additions & 2 deletions app/src/main/java/eu/kanade/tachiyomi/ui/manga/EditMangaDialog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,27 @@ import com.google.android.material.chip.Chip
import com.google.android.material.chip.ChipGroup
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.track.TrackerManager
import eu.kanade.tachiyomi.databinding.EditMangaDialogBinding
import eu.kanade.tachiyomi.source.model.SManga
import eu.kanade.tachiyomi.util.lang.chop
import eu.kanade.tachiyomi.util.system.dpToPx
import eu.kanade.tachiyomi.util.system.toast
import eu.kanade.tachiyomi.widget.materialdialogs.setTextInput
import exh.ui.metadata.adapters.MetadataUIUtil.getResourceColor
import exh.util.dropBlank
import exh.util.trimOrNull
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import tachiyomi.core.common.i18n.stringResource
import tachiyomi.domain.manga.model.Manga
import tachiyomi.domain.track.interactor.GetTracks
import tachiyomi.i18n.MR
import tachiyomi.i18n.sy.SYMR
import tachiyomi.presentation.core.i18n.stringResource
import tachiyomi.source.local.isLocal
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get

@Composable
fun EditMangaDialog(
Expand All @@ -61,6 +67,8 @@ fun EditMangaDialog(
var binding by remember {
mutableStateOf<EditMangaDialogBinding?>(null)
}
val getTracks = Injekt.get<GetTracks>()
val trackerManager = Injekt.get<TrackerManager>()
AlertDialog(
onDismissRequest = onDismissRequest,
confirmButton = {
Expand Down Expand Up @@ -109,7 +117,7 @@ fun EditMangaDialog(
EditMangaDialogBinding.inflate(LayoutInflater.from(factoryContext))
.also { binding = it }
.apply {
onViewCreated(manga, factoryContext, this, scope)
onViewCreated(manga, factoryContext, this, scope, getTracks, trackerManager)
}
.root
},
Expand All @@ -120,7 +128,7 @@ fun EditMangaDialog(
)
}

private fun onViewCreated(manga: Manga, context: Context, binding: EditMangaDialogBinding, scope: CoroutineScope) {
private fun onViewCreated(manga: Manga, context: Context, binding: EditMangaDialogBinding, scope: CoroutineScope, getTracks: GetTracks, trackerManager: TrackerManager) {
loadCover(manga, binding)

val statusAdapter: ArrayAdapter<String> = ArrayAdapter(
Expand Down Expand Up @@ -203,6 +211,29 @@ private fun onViewCreated(manga: Manga, context: Context, binding: EditMangaDial

binding.resetTags.setOnClickListener { resetTags(manga, binding, scope) }
binding.resetInfo.setOnClickListener { resetInfo(manga, binding, scope) }
binding.autofillFromTracker.setOnClickListener {
scope.launch {
autofillFromTracker(manga, binding, getTracks, trackerManager, context)
}
}
}

private suspend fun autofillFromTracker(manga: Manga, binding: EditMangaDialogBinding, getTracks: GetTracks, trackerManager: TrackerManager, context: Context) {
val tracks = getTracks.await(manga.id)

if (tracks.isEmpty()) {
context.toast("Entry not tracked.")
return
}
val track = tracks[0]
val tracker = trackerManager.get(track.trackerId)!!
val trackerMangaMetadata = tracker.getMangaMetadata(track)

binding.title.setText(trackerMangaMetadata.title)
binding.mangaAuthor.setText(trackerMangaMetadata.authors)
binding.mangaArtist.setText(trackerMangaMetadata.artists)
binding.thumbnailUrl.setText(trackerMangaMetadata.thumbnailUrl)
binding.mangaDescription.setText(trackerMangaMetadata.description)
}

private fun resetTags(manga: Manga, binding: EditMangaDialogBinding, scope: CoroutineScope) {
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/java/eu/kanade/test/DummyTracker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,10 @@ data class DummyTracker(
track: eu.kanade.tachiyomi.data.database.models.Track,
epochMillis: Long,
) = Unit

override suspend fun getMangaMetadata(
track: tachiyomi.domain.track.model.Track,
): eu.kanade.tachiyomi.data.track.model.TrackMangaMetadata = eu.kanade.tachiyomi.data.track.model.TrackMangaMetadata(
0, "test", "test", "test", "test", "test",
)
}
51 changes: 34 additions & 17 deletions app/src/main/res/layout/edit_manga_dialog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -129,25 +129,42 @@
android:layout_marginEnd="16dp"
android:layout_marginBottom="12dp" />

<Button
android:id="@+id/reset_tags"
android:layout_width="wrap_content"
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="8dp"
android:text="@string/reset_tags"
android:textAllCaps="false" />
android:orientation="horizontal">

<Button
android:id="@+id/reset_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="8dp"
android:text="@string/reset_info"
android:textAllCaps="false" />
<Button
android:id="@+id/autofill_from_tracker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="8dp"
android:text="Autofill from tracker"
android:textAllCaps="false" />


<Button
android:id="@+id/reset_tags"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="8dp"
android:text="@string/reset_tags"
android:textAllCaps="false" />

<Button
android:id="@+id/reset_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="8dp"
android:text="@string/reset_info"
android:textAllCaps="false" />
</LinearLayout>

<View
android:layout_width="match_parent"
Expand Down

0 comments on commit c8c4a6b

Please sign in to comment.