Refactor custom manga info manager

This commit is contained in:
Jobobby04 2020-10-12 22:35:45 -04:00
parent 405b0580fc
commit 22f81758b0
4 changed files with 24 additions and 32 deletions

View File

@ -42,13 +42,13 @@ class AppModule(val app: Application) : InjektModule {
addSingletonFactory { DownloadManager(app) } addSingletonFactory { DownloadManager(app) }
addSingletonFactory { CustomMangaManager(app) }
addSingletonFactory { TrackManager(app) } addSingletonFactory { TrackManager(app) }
addSingletonFactory { Gson() } addSingletonFactory { Gson() }
// SY --> // SY -->
addSingletonFactory { CustomMangaManager(app) }
addSingletonFactory { EHentaiUpdateHelper(app) } addSingletonFactory { EHentaiUpdateHelper(app) }
// SY <-- // SY <--

View File

@ -7,13 +7,6 @@ import kotlinx.serialization.Serializable
import kotlinx.serialization.decodeFromString import kotlinx.serialization.decodeFromString
import kotlinx.serialization.encodeToString import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonArray
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.buildJsonObject
import kotlinx.serialization.json.contentOrNull
import kotlinx.serialization.json.encodeToJsonElement
import kotlinx.serialization.json.jsonPrimitive
import kotlinx.serialization.json.longOrNull
import java.io.File import java.io.File
import java.util.Scanner import java.util.Scanner
@ -33,25 +26,23 @@ class CustomMangaManager(val context: Context) {
if (!editJson.exists() || !editJson.isFile) return if (!editJson.exists() || !editJson.isFile) return
val json = try { val json = try {
Json.decodeFromString<JsonObject>( Json.decodeFromString<MangaList>(
Scanner(editJson).useDelimiter("\\Z").next() Scanner(editJson).useDelimiter("\\Z").next()
) )
} catch (e: Exception) { } catch (e: Exception) {
null null
} ?: return } ?: return
val mangasJson = json["mangas"] as? JsonArray ?: return val mangasJson = json.mangas ?: return
customMangaMap = mangasJson.mapNotNull { element -> customMangaMap = mangasJson.mapNotNull { mangaJson ->
val mangaObject = element as? JsonObject ?: return@mapNotNull null val id = mangaJson.id ?: return@mapNotNull null
val id = mangaObject["id"]?.jsonPrimitive?.longOrNull ?: return@mapNotNull null
val manga = MangaImpl().apply { val manga = MangaImpl().apply {
this.id = id this.id = id
title = mangaObject["title"]?.jsonPrimitive?.contentOrNull ?: "" title = mangaJson.title ?: ""
author = mangaObject["author"]?.jsonPrimitive?.contentOrNull author = mangaJson.author
artist = mangaObject["artist"]?.jsonPrimitive?.contentOrNull artist = mangaJson.artist
description = mangaObject["description"]?.jsonPrimitive?.contentOrNull description = mangaJson.description
genre = (mangaObject["genre"] as? JsonArray)?.mapNotNull { it.jsonPrimitive.contentOrNull } genre = mangaJson.genre?.joinToString(", ")
?.joinToString(", ")
} }
id to manga id to manga
}.toMap().toMutableMap() }.toMap().toMutableMap()
@ -59,9 +50,9 @@ class CustomMangaManager(val context: Context) {
fun saveMangaInfo(manga: MangaJson) { fun saveMangaInfo(manga: MangaJson) {
if (manga.title == null && manga.author == null && manga.artist == null && manga.description == null && manga.genre == null) { if (manga.title == null && manga.author == null && manga.artist == null && manga.description == null && manga.genre == null) {
customMangaMap.remove(manga.id) customMangaMap.remove(manga.id!!)
} else { } else {
customMangaMap[manga.id] = MangaImpl().apply { customMangaMap[manga.id!!] = MangaImpl().apply {
id = manga.id id = manga.id
title = manga.title ?: "" title = manga.title ?: ""
author = manga.author author = manga.author
@ -76,12 +67,8 @@ class CustomMangaManager(val context: Context) {
private fun saveCustomInfo() { private fun saveCustomInfo() {
val jsonElements = customMangaMap.values.map { it.toJson() } val jsonElements = customMangaMap.values.map { it.toJson() }
if (jsonElements.isNotEmpty()) { if (jsonElements.isNotEmpty()) {
val mangaEntries = Json.encodeToJsonElement(jsonElements)
val root = buildJsonObject {
put("mangas", mangaEntries)
}
editJson.delete() editJson.delete()
editJson.writeText(Json.encodeToString(root)) editJson.writeText(Json.encodeToString(MangaList(jsonElements)))
} }
} }
@ -92,18 +79,23 @@ class CustomMangaManager(val context: Context) {
author, author,
artist, artist,
description, description,
genre?.split(", ")?.toTypedArray() genre?.split(", ")
) )
} }
@Serializable
data class MangaList(
val mangas: List<MangaJson>? = null
)
@Serializable @Serializable
data class MangaJson( data class MangaJson(
val id: Long, val id: Long? = null,
val title: String? = null, val title: String? = null,
val author: String? = null, val author: String? = null,
val artist: String? = null, val artist: String? = null,
val description: String? = null, val description: String? = null,
val genre: Array<String>? = null val genre: List<String>? = null
) { ) {
override fun equals(other: Any?): Boolean { override fun equals(other: Any?): Boolean {

View File

@ -529,7 +529,7 @@ class LibraryPresenter(
(if (manga.author != manga.originalAuthor) manga.author else null), (if (manga.author != manga.originalAuthor) manga.author else null),
(if (manga.artist != manga.originalArtist) manga.artist else null), (if (manga.artist != manga.originalArtist) manga.artist else null),
(if (manga.description != manga.originalDescription) manga.description else null), (if (manga.description != manga.originalDescription) manga.description else null),
(if (manga.genre != manga.originalGenre) manga.getGenres()?.toTypedArray() else null) (if (manga.genre != manga.originalGenre) manga.getGenres() else null)
) )
} }
mangaJson?.let { mangaJson?.let {

View File

@ -295,7 +295,7 @@ class MangaPresenter(
db.updateMangaInfo(manga).executeAsBlocking() db.updateMangaInfo(manga).executeAsBlocking()
} else { } else {
val genre = if (!tags.isNullOrEmpty() && tags.joinToString() != manga.genre) { val genre = if (!tags.isNullOrEmpty() && tags.joinToString() != manga.genre) {
tags.toTypedArray() tags
} else { } else {
null null
} }