Skip to content

Commit

Permalink
Edit info for online manga + Custom covers update
Browse files Browse the repository at this point in the history
Yes you read that right. It's back! Oh god it's back

Instead of modifying the db, an external json file is made holding the custom info for your library (meaning it's even easier to remove should I so choose)
Reworking to just override the variable and use said var instead of having the current/original logic that existed before

Custom covers are now saved in a new folder, likewise to upstream
Also like upstream, custom covers can be added to manga without covers (closes #49)

(I'm so sorry Carlos)
  • Loading branch information
Jays2Kings committed May 20, 2020
1 parent 0809a7b commit d3ec230
Show file tree
Hide file tree
Showing 25 changed files with 407 additions and 181 deletions.
5 changes: 5 additions & 0 deletions app/src/main/java/eu/kanade/tachiyomi/AppModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import eu.kanade.tachiyomi.data.cache.ChapterCache
import eu.kanade.tachiyomi.data.cache.CoverCache
import eu.kanade.tachiyomi.data.database.DatabaseHelper
import eu.kanade.tachiyomi.data.download.DownloadManager
import eu.kanade.tachiyomi.data.library.CustomMangaManager
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.data.track.TrackManager
import eu.kanade.tachiyomi.extension.ExtensionManager
Expand Down Expand Up @@ -41,6 +42,8 @@ class AppModule(val app: Application) : InjektModule {

addSingletonFactory { DownloadManager(app) }

addSingletonFactory { CustomMangaManager(app) }

addSingletonFactory { TrackManager(app) }

addSingletonFactory { Gson() }
Expand All @@ -56,5 +59,7 @@ class AppModule(val app: Application) : InjektModule {
GlobalScope.launch { get<DatabaseHelper>() }

GlobalScope.launch { get<DownloadManager>() }

GlobalScope.launch { get<CustomMangaManager>() }
}
}
3 changes: 3 additions & 0 deletions app/src/main/java/eu/kanade/tachiyomi/Migrations.kt
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ object Migrations {
BackupCreatorJob.setupTask()
ExtensionUpdateJob.setupTask()
}
if (oldVersion < 66) {
LibraryPresenter.updateCustoms()
}
return true
}
return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ object MangaTypeAdapter {
write {
beginArray()
value(it.url)
value(it.title)
value(it.originalTitle)
value(it.source)
value(max(0, it.viewer))
value(it.chapter_flags)
Expand Down
81 changes: 63 additions & 18 deletions app/src/main/java/eu/kanade/tachiyomi/data/cache/CoverCache.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import coil.Coil
import eu.kanade.tachiyomi.R
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.util.storage.DiskUtil
import eu.kanade.tachiyomi.util.system.executeOnIO
import eu.kanade.tachiyomi.util.system.toast
Expand All @@ -29,11 +30,16 @@ import java.io.InputStream
*/
class CoverCache(val context: Context) {

/**
* Cache directory used for cache management.
*/
private val cacheDir = context.getExternalFilesDir("covers")
?: File(context.filesDir, "covers").also { it.mkdirs() }
companion object {
private const val COVERS_DIR = "covers"
private const val CUSTOM_COVERS_DIR = "covers/custom"
}

/** Cache directory used for cache management.*/
private val cacheDir = getCacheDir(COVERS_DIR)

/** Cache directory used for custom cover cache management.*/
private val customCoverCacheDir = getCacheDir(CUSTOM_COVERS_DIR)

fun getChapterCacheSize(): String {
return Formatter.formatFileSize(context, DiskUtil.getDirectorySize(cacheDir))
Expand All @@ -50,7 +56,7 @@ class CoverCache(val context: Context) {
val files = cacheDir.listFiles()?.iterator() ?: return@launch
while (files.hasNext()) {
val file = files.next()
if (file.name !in urls) {
if (file.isFile && file.name !in urls) {
deletedSize += file.length()
file.delete()
}
Expand All @@ -66,28 +72,59 @@ class CoverCache(val context: Context) {
}

/**
* Returns the cover from cache.
* Returns the custom cover from cache.
*
* @param thumbnailUrl the thumbnail url.
* @param manga the manga.
* @return cover image.
*/
fun getCoverFile(manga: Manga): File {
return File(cacheDir, manga.key())
fun getCustomCoverFile(manga: Manga): File {
return File(customCoverCacheDir, DiskUtil.hashKeyForDisk(manga.id.toString()))
}

/**
* Copy the given stream to this cache.
* Saves the given stream as the manga's custom cover to cache.
*
* @param thumbnailUrl url of the thumbnail.
* @param manga the manga.
* @param inputStream the stream to copy.
* @throws IOException if there's any error.
*/
@Throws(IOException::class)
fun copyToCache(manga: Manga, inputStream: InputStream) {
// Get destination file.
val destFile = getCoverFile(manga)
fun setCustomCoverToCache(manga: Manga, inputStream: InputStream) {
getCustomCoverFile(manga).outputStream().use {
inputStream.copyTo(it)
Coil.imageLoader(context).invalidate(manga.key())
}
}

/**
* Delete custom cover of the manga from the cache
*
* @param manga the manga.
* @return whether the cover was deleted.
*/
fun deleteCustomCover(manga: Manga): Boolean {
val result = getCustomCoverFile(manga).let {
it.exists() && it.delete()
}
Coil.imageLoader(context).invalidate(manga.key())
return result
}

destFile.outputStream().use { inputStream.copyTo(it) }
/**
* Returns the cover from cache.
*
* @param thumbnailUrl the thumbnail url.
* @return cover image.
*/
fun getCoverFile(manga: Manga): File {
return File(cacheDir, manga.key())
}

fun deleteFromCache(name: String?) {
if (name.isNullOrEmpty()) return
val file = getCoverFile(MangaImpl().apply { thumbnail_url = name })
Coil.imageLoader(context).invalidate(file.name)
if (file.exists()) file.delete()
}

/**
Expand All @@ -96,13 +133,21 @@ class CoverCache(val context: Context) {
* @param thumbnailUrl the thumbnail url.
* @return status of deletion.
*/
fun deleteFromCache(manga: Manga, deleteMemoryCache: Boolean = true) {
fun deleteFromCache(
manga: Manga,
deleteCustom: Boolean = true
) {
// Check if url is empty.
if (manga.thumbnail_url.isNullOrEmpty()) return

// Remove file
val file = getCoverFile(manga)
if (deleteMemoryCache) Coil.imageLoader(context).invalidate(file.name)
if (deleteCustom) deleteCustomCover(manga)
if (file.exists()) file.delete()
}

private fun getCacheDir(dir: String): File {
return context.getExternalFilesDir(dir)
?: File(context.filesDir, dir).also { it.mkdirs() }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ class MangaPutResolver : DefaultPutResolver<Manga>() {
put(COL_ID, obj.id)
put(COL_SOURCE, obj.source)
put(COL_URL, obj.url)
put(COL_ARTIST, obj.artist)
put(COL_AUTHOR, obj.author)
put(COL_DESCRIPTION, obj.description)
put(COL_GENRE, obj.genre)
put(COL_TITLE, obj.title)
put(COL_ARTIST, obj.originalArtist)
put(COL_AUTHOR, obj.originalAuthor)
put(COL_DESCRIPTION, obj.originalDescription)
put(COL_GENRE, obj.originalGenre)
put(COL_TITLE, obj.originalTitle)
put(COL_STATUS, obj.status)
put(COL_THUMBNAIL_URL, obj.thumbnail_url)
put(COL_FAVORITE, obj.favorite)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import eu.kanade.tachiyomi.util.storage.DiskUtil
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
import java.util.Locale
import kotlin.random.Random

interface Manga : SManga {

Expand Down Expand Up @@ -149,15 +148,6 @@ interface Manga : SManga {
return DiskUtil.hashKeyForDisk(thumbnail_url.orEmpty())
}

fun setCustomThumbnailUrl() {
removeCustomThumbnailUrl()
thumbnail_url = "Custom-${Random.nextInt(0, 1000)}-J2K-${thumbnail_url ?: id!!}"
}

fun removeCustomThumbnailUrl() {
thumbnail_url = thumbnail_url?.substringAfter("-J2K-")?.substringAfter("Custom-")
}

// Used to display the chapter's title one way or another
var displayMode: Int
get() = chapter_flags and DISPLAY_MASK
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package eu.kanade.tachiyomi.data.database.models

import eu.kanade.tachiyomi.data.download.DownloadManager
import eu.kanade.tachiyomi.data.download.DownloadProvider
import eu.kanade.tachiyomi.data.library.CustomMangaManager
import eu.kanade.tachiyomi.source.model.SManga
import uy.kohesive.injekt.injectLazy
import kotlin.collections.set

open class MangaImpl : Manga {

Expand All @@ -14,15 +14,34 @@ open class MangaImpl : Manga {

override lateinit var url: String

override lateinit var title: String
private val customMangaManager: CustomMangaManager by injectLazy()

override var artist: String? = null
override var title: String
get() = if (favorite) {
val customTitle = customMangaManager.getManga(this)?.title
if (customTitle.isNullOrBlank()) ogTitle else customTitle
} else {
ogTitle
}
set(value) {
ogTitle = value
}

override var author: String? = null
override var author: String?
get() = if (favorite) customMangaManager.getManga(this)?.author ?: ogAuthor else ogAuthor
set(value) { ogAuthor = value }

override var description: String? = null
override var artist: String?
get() = if (favorite) customMangaManager.getManga(this)?.artist ?: ogArtist else ogArtist
set(value) { ogArtist = value }

override var genre: String? = null
override var description: String?
get() = if (favorite) customMangaManager.getManga(this)?.description ?: ogDesc else ogDesc
set(value) { ogDesc = value }

override var genre: String?
get() = if (favorite) customMangaManager.getManga(this)?.genre ?: ogGenre else ogGenre
set(value) { ogGenre = value }

override var status: Int = 0

Expand All @@ -42,14 +61,25 @@ open class MangaImpl : Manga {

override var date_added: Long = 0

lateinit var ogTitle: String
private set
var ogAuthor: String? = null
private set
var ogArtist: String? = null
private set
var ogDesc: String? = null
private set
var ogGenre: String? = null
private set

override fun copyFrom(other: SManga) {
if (other is MangaImpl && (other as MangaImpl)::title.isInitialized &&
!other.title.isBlank() && other.title != title) {
val oldTitle = title
title = other.title
if (other is MangaImpl && other::ogTitle.isInitialized &&
!other.title.isBlank() && other.ogTitle != ogTitle) {
val oldTitle = ogTitle
title = other.ogTitle
val db: DownloadManager by injectLazy()
val provider = DownloadProvider(db.context)
provider.renameMangaFolder(oldTitle, title, source)
provider.renameMangaFolder(oldTitle, ogTitle, source)
}
super.copyFrom(other)
}
Expand All @@ -64,7 +94,7 @@ open class MangaImpl : Manga {
}

override fun hashCode(): Int {
if (::url.isInitialized) return url.hashCode()
else return (id ?: 0L).hashCode()
return if (::url.isInitialized) url.hashCode()
else (id ?: 0L).hashCode()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ class MangaInfoPutResolver(val reset: Boolean = false) : PutResolver<Manga>() {
.build()

fun mapToContentValues(manga: Manga) = ContentValues(1).apply {
put(MangaTable.COL_TITLE, manga.title)
put(MangaTable.COL_GENRE, manga.genre)
put(MangaTable.COL_AUTHOR, manga.author)
put(MangaTable.COL_ARTIST, manga.artist)
put(MangaTable.COL_DESCRIPTION, manga.description)
put(MangaTable.COL_TITLE, manga.originalTitle)
put(MangaTable.COL_GENRE, manga.originalGenre)
put(MangaTable.COL_AUTHOR, manga.originalAuthor)
put(MangaTable.COL_ARTIST, manga.originalArtist)
put(MangaTable.COL_DESCRIPTION, manga.originalDescription)
}

fun resetToContentValues(manga: Manga) = ContentValues(1).apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,11 @@ class DownloadCache(
val trueMangaDirs = mangaDirs.mapNotNull { mangaDir ->
val manga = sourceMangas.firstOrNull()?.find {
DiskUtil.buildValidFilename(
it.title
it.originalTitle
).toLowerCase() == mangaDir.key.toLowerCase() && it.source == sourceValue.key
} ?: sourceMangas.lastOrNull()?.find {
DiskUtil.buildValidFilename(
it.title
it.originalTitle
).toLowerCase() == mangaDir.key.toLowerCase() && it.source == sourceValue.key
}
val id = manga?.id ?: return@mapNotNull null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ class DownloadProvider(private val context: Context) {
* @param manga the manga to query.
*/
fun getMangaDirName(manga: Manga): String {
return DiskUtil.buildValidFilename(manga.title)
return DiskUtil.buildValidFilename(manga.originalTitle)
}

/**
Expand Down
Loading

0 comments on commit d3ec230

Please sign in to comment.