Refactor custom manga info manager
This commit is contained in:
parent
405b0580fc
commit
22f81758b0
@ -42,13 +42,13 @@ class AppModule(val app: Application) : InjektModule {
|
||||
|
||||
addSingletonFactory { DownloadManager(app) }
|
||||
|
||||
addSingletonFactory { CustomMangaManager(app) }
|
||||
|
||||
addSingletonFactory { TrackManager(app) }
|
||||
|
||||
addSingletonFactory { Gson() }
|
||||
|
||||
// SY -->
|
||||
addSingletonFactory { CustomMangaManager(app) }
|
||||
|
||||
addSingletonFactory { EHentaiUpdateHelper(app) }
|
||||
// SY <--
|
||||
|
||||
|
@ -7,13 +7,6 @@ import kotlinx.serialization.Serializable
|
||||
import kotlinx.serialization.decodeFromString
|
||||
import kotlinx.serialization.encodeToString
|
||||
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.util.Scanner
|
||||
|
||||
@ -33,25 +26,23 @@ class CustomMangaManager(val context: Context) {
|
||||
if (!editJson.exists() || !editJson.isFile) return
|
||||
|
||||
val json = try {
|
||||
Json.decodeFromString<JsonObject>(
|
||||
Json.decodeFromString<MangaList>(
|
||||
Scanner(editJson).useDelimiter("\\Z").next()
|
||||
)
|
||||
} catch (e: Exception) {
|
||||
null
|
||||
} ?: return
|
||||
|
||||
val mangasJson = json["mangas"] as? JsonArray ?: return
|
||||
customMangaMap = mangasJson.mapNotNull { element ->
|
||||
val mangaObject = element as? JsonObject ?: return@mapNotNull null
|
||||
val id = mangaObject["id"]?.jsonPrimitive?.longOrNull ?: return@mapNotNull null
|
||||
val mangasJson = json.mangas ?: return
|
||||
customMangaMap = mangasJson.mapNotNull { mangaJson ->
|
||||
val id = mangaJson.id ?: return@mapNotNull null
|
||||
val manga = MangaImpl().apply {
|
||||
this.id = id
|
||||
title = mangaObject["title"]?.jsonPrimitive?.contentOrNull ?: ""
|
||||
author = mangaObject["author"]?.jsonPrimitive?.contentOrNull
|
||||
artist = mangaObject["artist"]?.jsonPrimitive?.contentOrNull
|
||||
description = mangaObject["description"]?.jsonPrimitive?.contentOrNull
|
||||
genre = (mangaObject["genre"] as? JsonArray)?.mapNotNull { it.jsonPrimitive.contentOrNull }
|
||||
?.joinToString(", ")
|
||||
title = mangaJson.title ?: ""
|
||||
author = mangaJson.author
|
||||
artist = mangaJson.artist
|
||||
description = mangaJson.description
|
||||
genre = mangaJson.genre?.joinToString(", ")
|
||||
}
|
||||
id to manga
|
||||
}.toMap().toMutableMap()
|
||||
@ -59,9 +50,9 @@ class CustomMangaManager(val context: Context) {
|
||||
|
||||
fun saveMangaInfo(manga: MangaJson) {
|
||||
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 {
|
||||
customMangaMap[manga.id] = MangaImpl().apply {
|
||||
customMangaMap[manga.id!!] = MangaImpl().apply {
|
||||
id = manga.id
|
||||
title = manga.title ?: ""
|
||||
author = manga.author
|
||||
@ -76,12 +67,8 @@ class CustomMangaManager(val context: Context) {
|
||||
private fun saveCustomInfo() {
|
||||
val jsonElements = customMangaMap.values.map { it.toJson() }
|
||||
if (jsonElements.isNotEmpty()) {
|
||||
val mangaEntries = Json.encodeToJsonElement(jsonElements)
|
||||
val root = buildJsonObject {
|
||||
put("mangas", mangaEntries)
|
||||
}
|
||||
editJson.delete()
|
||||
editJson.writeText(Json.encodeToString(root))
|
||||
editJson.writeText(Json.encodeToString(MangaList(jsonElements)))
|
||||
}
|
||||
}
|
||||
|
||||
@ -92,18 +79,23 @@ class CustomMangaManager(val context: Context) {
|
||||
author,
|
||||
artist,
|
||||
description,
|
||||
genre?.split(", ")?.toTypedArray()
|
||||
genre?.split(", ")
|
||||
)
|
||||
}
|
||||
|
||||
@Serializable
|
||||
data class MangaList(
|
||||
val mangas: List<MangaJson>? = null
|
||||
)
|
||||
|
||||
@Serializable
|
||||
data class MangaJson(
|
||||
val id: Long,
|
||||
val id: Long? = null,
|
||||
val title: String? = null,
|
||||
val author: String? = null,
|
||||
val artist: String? = null,
|
||||
val description: String? = null,
|
||||
val genre: Array<String>? = null
|
||||
val genre: List<String>? = null
|
||||
) {
|
||||
|
||||
override fun equals(other: Any?): Boolean {
|
||||
|
@ -529,7 +529,7 @@ class LibraryPresenter(
|
||||
(if (manga.author != manga.originalAuthor) manga.author else null),
|
||||
(if (manga.artist != manga.originalArtist) manga.artist 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 {
|
||||
|
@ -295,7 +295,7 @@ class MangaPresenter(
|
||||
db.updateMangaInfo(manga).executeAsBlocking()
|
||||
} else {
|
||||
val genre = if (!tags.isNullOrEmpty() && tags.joinToString() != manga.genre) {
|
||||
tags.toTypedArray()
|
||||
tags
|
||||
} else {
|
||||
null
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user