From db11ac8cf6e15dba533e62165bf56c436138b35a Mon Sep 17 00:00:00 2001 From: Jay Date: Mon, 20 Jan 2020 15:37:15 -0800 Subject: [PATCH] Threw local manga editing into a json --- .../eu/kanade/tachiyomi/source/LocalSource.kt | 38 +++++++ .../ui/manga/info/EditMangaDialog.kt | 57 ++++++---- .../ui/manga/info/MangaInfoPresenter.kt | 101 ++++++++++-------- 3 files changed, 130 insertions(+), 66 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/source/LocalSource.kt b/app/src/main/java/eu/kanade/tachiyomi/source/LocalSource.kt index 90b06d86e6..bba5eec640 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/source/LocalSource.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/source/LocalSource.kt @@ -2,6 +2,7 @@ package eu.kanade.tachiyomi.source import android.content.Context import com.google.gson.Gson +import com.google.gson.GsonBuilder import com.google.gson.JsonObject import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.source.model.Filter @@ -21,6 +22,7 @@ import rx.Observable import timber.log.Timber import java.io.File import java.io.FileInputStream +import java.io.FileWriter import java.io.InputStream import java.util.Locale import java.util.Scanner @@ -148,6 +150,42 @@ class LocalSource(private val context: Context) : CatalogueSource { return Observable.just(manga) } + fun updateMangaInfo(manga: SManga) { + val directory = getBaseDirectories(context).mapNotNull { File(it, manga.url) }.find { it + .exists() } ?: return + val gson = GsonBuilder().setPrettyPrinting().create() + val file = File(directory, "info.json") + file.writeText(gson.toJson(manga.toJson())) + } + + fun SManga.toJson():MangaJson { + return MangaJson(title, author, artist, description, genre?.split(", ")?.toTypedArray()) + } + + data class MangaJson( + val title:String, + val author:String?, + val artist:String?, + val description:String?, + val genre:Array? + ) { + + override fun equals(other: Any?): Boolean { + if (this === other) return true + if (javaClass != other?.javaClass) return false + + other as MangaJson + + if (title != other.title) return false + + return true + } + + override fun hashCode(): Int { + return title.hashCode() + } + } + override fun fetchChapterList(manga: SManga): Observable> { val chapters = getBaseDirectories(context) .mapNotNull { File(it, manga.url).listFiles()?.toList() } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/info/EditMangaDialog.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/info/EditMangaDialog.kt index 264f7d0226..20d5466699 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/info/EditMangaDialog.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/info/EditMangaDialog.kt @@ -20,6 +20,7 @@ import eu.kanade.tachiyomi.data.database.DatabaseHelper import eu.kanade.tachiyomi.data.database.models.Manga import eu.kanade.tachiyomi.data.database.models.MangaImpl import eu.kanade.tachiyomi.data.glide.GlideApp +import eu.kanade.tachiyomi.source.LocalSource import eu.kanade.tachiyomi.ui.base.controller.DialogController import eu.kanade.tachiyomi.ui.library.LibraryController import eu.kanade.tachiyomi.util.chop @@ -83,38 +84,52 @@ class EditMangaDialog : DialogController { .signature(ObjectKey(MangaImpl.getLastCoverFetch(manga.id!!).toString())) .dontAnimate() .into(view.manga_cover) + val isLocal = manga.source == LocalSource.ID - if (manga.currentTitle() != manga.originalTitle()) - view.manga_title.append(manga.currentTitle()) - view.manga_title.hint = "${resources?.getString(R.string.title)}: ${manga.originalTitle()}" + if (isLocal) { + if (manga.title != manga.url) + view.manga_title.append(manga.title) + view.manga_title.hint = "${resources?.getString(R.string.title)}: ${manga.url}" + view.manga_author.append(manga.author ?: "") + view.manga_artist.append(manga.artist ?: "") + view.manga_description.append(manga.description ?: "") + view.manga_genres_tags.setTags(manga.genre?.split(", ") ?: emptyList()) + } + else { + if (manga.currentTitle() != manga.originalTitle()) + view.manga_title.append(manga.currentTitle()) + view.manga_title.hint = "${resources?.getString(R.string.title)}: ${manga + .originalTitle()}" - if (manga.currentAuthor() != manga.originalAuthor()) - view.manga_author.append(manga.currentAuthor()) - if (!manga.originalAuthor().isNullOrBlank()) - view.manga_author.hint = "${resources?.getString(R.string.manga_info_author_label)}: ${manga.originalAuthor()}" + if (manga.currentAuthor() != manga.originalAuthor()) + view.manga_author.append(manga.currentAuthor()) + if (!manga.originalAuthor().isNullOrBlank()) + view.manga_author.hint = "${resources?.getString(R.string + .manga_info_author_label)}: ${manga.originalAuthor()}" - if (manga.currentArtist() != manga.originalArtist()) - view.manga_artist.append(manga.currentArtist()) - if (!manga.originalArtist().isNullOrBlank()) - view.manga_artist.hint = "${resources?.getString(R.string.manga_info_artist_label)}: ${manga.originalArtist()}" + if (manga.currentArtist() != manga.originalArtist()) + view.manga_artist.append(manga.currentArtist()) + if (!manga.originalArtist().isNullOrBlank()) + view.manga_artist.hint = "${resources?.getString(R.string + .manga_info_artist_label)}: ${manga.originalArtist()}" + if (manga.currentDesc() != manga.originalDesc()) + view.manga_description.append(manga.currentDesc()) + if (!manga.originalDesc().isNullOrBlank()) + view.manga_description.hint = "${resources?.getString(R.string.description)}: ${manga + .originalDesc()?.chop(15)}" + if (manga.currentGenres().isNullOrBlank().not()) { + view.manga_genres_tags.setTags(manga.currentGenres()?.split(", ")) + } + } view.cover_layout.setOnClickListener { changeCover() } - - if (manga.currentDesc() != manga.originalDesc()) - view.manga_description.append(manga.currentDesc()) - if (!manga.originalDesc().isNullOrBlank()) - view.manga_description.hint = "${resources?.getString(R.string.description)}: ${manga - .originalDesc()?.chop(15)}" - if (manga.currentGenres().isNullOrBlank().not()) { - view.manga_genres_tags.setTags(manga.currentGenres()?.split(", ")) - } view.reset_tags.setOnClickListener { resetTags() } } private fun resetTags() { - if (manga.originalGenres().isNullOrBlank()) + if (manga.originalGenres().isNullOrBlank() || manga.source == LocalSource.ID) dialogView?.manga_genres_tags?.setTags(emptyList()) else dialogView?.manga_genres_tags?.setTags(manga.originalGenres()?.split(", ")) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/info/MangaInfoPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/info/MangaInfoPresenter.kt index b878175600..181f0c2bcf 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/info/MangaInfoPresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/info/MangaInfoPresenter.kt @@ -203,56 +203,67 @@ class MangaInfoPresenter( fun updateManga(title:String?, author:String?, artist: String?, uri: Uri?, description: String?, tags: Array?) { - var changed = false - val title = title?.trim() - if (title.isNullOrBlank() && manga.currentTitle() != manga.originalTitle()) { - manga.title = manga.originalTitle() - changed = true - } else if (!title.isNullOrBlank() && title != manga.currentTitle()) { - manga.title = "${title}${SManga.splitter}${manga.originalTitle()}" - changed = true + if (manga.source == LocalSource.ID) { + manga.title = if (title.isNullOrBlank()) manga.url else title.trim() + manga.author = author?.trim() + manga.artist = artist?.trim() + manga.description = description?.trim() + val tagsString = tags?.joinToString(", ") { it.capitalize() } + manga.genre = if (tags.isNullOrEmpty()) null else tagsString?.trim() + LocalSource(downloadManager.context).updateMangaInfo(manga) + db.updateMangaInfo(manga).executeAsBlocking() } + else { + var changed = false + val title = title?.trim() + if (title.isNullOrBlank() && manga.currentTitle() != manga.originalTitle()) { + manga.title = manga.originalTitle() + changed = true + } else if (!title.isNullOrBlank() && title != manga.currentTitle()) { + manga.title = "${title}${SManga.splitter}${manga.originalTitle()}" + changed = true + } - val author = author?.trim() - if (author.isNullOrBlank() && manga.currentAuthor() != manga.originalAuthor()) { - manga.author = manga.originalAuthor() - changed = true - } else if (!author.isNullOrBlank() && author != manga.currentAuthor()) { - manga.author = "${author}${SManga.splitter}${manga.originalAuthor() ?: ""}" - changed = true + val author = author?.trim() + if (author.isNullOrBlank() && manga.currentAuthor() != manga.originalAuthor()) { + manga.author = manga.originalAuthor() + changed = true + } else if (!author.isNullOrBlank() && author != manga.currentAuthor()) { + manga.author = "${author}${SManga.splitter}${manga.originalAuthor() ?: ""}" + changed = true + } + + val artist = artist?.trim() + if (artist.isNullOrBlank() && manga.currentArtist() != manga.originalArtist()) { + manga.artist = manga.originalArtist() + changed = true + } else if (!artist.isNullOrBlank() && artist != manga.currentArtist()) { + manga.artist = "${artist}${SManga.splitter}${manga.originalArtist() ?: ""}" + changed = true + } + + val description = description?.trim() + if (description.isNullOrBlank() && manga.currentDesc() != manga.originalDesc()) { + manga.description = manga.originalDesc() + changed = true + } else if (!description.isNullOrBlank() && description != manga.currentDesc()) { + manga.description = "${description}${SManga.splitter}${manga.originalDesc() ?: ""}" + changed = true + } + + var tagsString = tags?.joinToString(", ") + if (tagsString.isNullOrBlank() && manga.currentGenres() != manga.originalGenres()) { + manga.genre = manga.originalGenres() + changed = true + } else if (!tagsString.isNullOrBlank() && tagsString != manga.currentGenres()) { + tagsString = tags?.joinToString(", ") { it.capitalize() } + manga.genre = "${tagsString}${SManga.splitter}${manga.originalGenres() ?: ""}" + changed = true + } + if (changed) db.updateMangaInfo(manga).executeAsBlocking() } - - val artist = artist?.trim() - if (artist.isNullOrBlank() && manga.currentArtist() != manga.currentArtist()) { - manga.artist = manga.originalArtist() - changed = true - } else if (!artist.isNullOrBlank() && artist != manga.currentArtist()) { - manga.artist = "${artist}${SManga.splitter}${manga.originalArtist() ?: ""}" - changed = true - } - - val description = description?.trim() - if (description.isNullOrBlank() && manga.currentDesc() != manga.originalDesc()) { - manga.description = manga.originalDesc() - changed = true - } else if (!description.isNullOrBlank() && description != manga.currentDesc()) { - manga.description = "${description}${SManga.splitter}${manga.originalDesc() ?: ""}" - changed = true - } - - var tagsString = tags?.joinToString(", ") - if (tagsString.isNullOrBlank() && manga.currentGenres() != manga.originalGenres()) { - manga.genre = manga.originalGenres() - changed = true - } else if (!tagsString.isNullOrBlank() && tagsString != manga.currentGenres()) { - tagsString = tags?.joinToString(", ") { it.capitalize() } - manga.genre = "${tagsString}${SManga.splitter}${manga.originalGenres() ?: ""}" - changed = true - } - if (uri != null) editCoverWithStream(uri) - if (changed) db.updateMangaInfo(manga).executeAsBlocking() } private fun editCoverWithStream(uri: Uri): Boolean {