Backing up custom data for manga as well

Using 800s in BackupManga for this
This commit is contained in:
Jays2Kings 2021-03-26 01:49:39 -04:00
parent f65460be05
commit c21b91bc02
9 changed files with 79 additions and 17 deletions

View File

@ -6,6 +6,7 @@ import eu.kanade.tachiyomi.data.database.DatabaseHelper
import eu.kanade.tachiyomi.data.database.models.Chapter
import eu.kanade.tachiyomi.data.database.models.Manga
import eu.kanade.tachiyomi.data.database.models.toMangaInfo
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.source.Source
@ -20,6 +21,7 @@ abstract class AbstractBackupManager(protected val context: Context) {
internal val sourceManager: SourceManager by injectLazy()
internal val trackManager: TrackManager by injectLazy()
protected val preferences: PreferencesHelper by injectLazy()
protected val customMangaManager: CustomMangaManager by injectLazy()
abstract fun createBackup(uri: Uri, flags: Int, isJob: Boolean): String?

View File

@ -7,6 +7,7 @@ import eu.kanade.tachiyomi.data.database.DatabaseHelper
import eu.kanade.tachiyomi.data.database.models.Chapter
import eu.kanade.tachiyomi.data.database.models.Manga
import eu.kanade.tachiyomi.data.database.models.Track
import eu.kanade.tachiyomi.data.library.CustomMangaManager
import eu.kanade.tachiyomi.data.track.TrackManager
import eu.kanade.tachiyomi.source.Source
import eu.kanade.tachiyomi.util.chapter.NoChaptersException
@ -22,6 +23,7 @@ abstract class AbstractBackupRestore<T : AbstractBackupManager>(protected val co
protected val db: DatabaseHelper by injectLazy()
protected val trackManager: TrackManager by injectLazy()
protected val customMangaManager: CustomMangaManager by injectLazy()
var job: Job? = null

View File

@ -30,7 +30,9 @@ class BackupCreateService : Service() {
internal const val BACKUP_HISTORY_MASK = 0x4
internal const val BACKUP_TRACK = 0x8
internal const val BACKUP_TRACK_MASK = 0x8
internal const val BACKUP_ALL = 0xF
internal const val BACKUP_CUSTOM_INFO = 0x10
internal const val BACKUP_CUSTOM_INFO_MASK = 0x10
internal const val BACKUP_ALL = 0x1F
/**
* Returns the status of the service.

View File

@ -8,6 +8,8 @@ import eu.kanade.tachiyomi.data.backup.BackupCreateService.Companion.BACKUP_CATE
import eu.kanade.tachiyomi.data.backup.BackupCreateService.Companion.BACKUP_CATEGORY_MASK
import eu.kanade.tachiyomi.data.backup.BackupCreateService.Companion.BACKUP_CHAPTER
import eu.kanade.tachiyomi.data.backup.BackupCreateService.Companion.BACKUP_CHAPTER_MASK
import eu.kanade.tachiyomi.data.backup.BackupCreateService.Companion.BACKUP_CUSTOM_INFO
import eu.kanade.tachiyomi.data.backup.BackupCreateService.Companion.BACKUP_CUSTOM_INFO_MASK
import eu.kanade.tachiyomi.data.backup.BackupCreateService.Companion.BACKUP_HISTORY
import eu.kanade.tachiyomi.data.backup.BackupCreateService.Companion.BACKUP_HISTORY_MASK
import eu.kanade.tachiyomi.data.backup.BackupCreateService.Companion.BACKUP_TRACK
@ -126,7 +128,7 @@ class FullBackupManager(context: Context) : AbstractBackupManager(context) {
*/
private fun backupMangaObject(manga: Manga, options: Int): BackupManga {
// Entry for this manga
val mangaObject = BackupManga.copyFrom(manga)
val mangaObject = BackupManga.copyFrom(manga, if (options and BACKUP_CUSTOM_INFO_MASK == BACKUP_CUSTOM_INFO) customMangaManager else null)
// Check if user wants chapter information in backup
if (options and BACKUP_CHAPTER_MASK == BACKUP_CHAPTER) {

View File

@ -12,6 +12,8 @@ import eu.kanade.tachiyomi.data.backup.full.models.BackupSerializer
import eu.kanade.tachiyomi.data.database.models.Chapter
import eu.kanade.tachiyomi.data.database.models.Manga
import eu.kanade.tachiyomi.data.database.models.Track
import eu.kanade.tachiyomi.data.library.CustomMangaManager
import eu.kanade.tachiyomi.data.library.LibraryUpdateService
import okio.buffer
import okio.gzip
import okio.source
@ -64,9 +66,10 @@ class FullBackupRestore(context: Context, notifier: BackupNotifier) : AbstractBa
val categories = backupManga.categories
val history = backupManga.history
val tracks = backupManga.getTrackingImpl()
val customManga = backupManga.getCustomMangaInfo()
try {
restoreMangaData(manga, chapters, categories, history, tracks, backupCategories)
restoreMangaData(manga, chapters, categories, history, tracks, backupCategories, customManga)
} catch (e: Exception) {
val sourceName = sourceMapping[manga.source] ?: manga.source.toString()
errors.add(Date() to "${manga.title} [$sourceName]: ${e.message}")
@ -91,19 +94,20 @@ class FullBackupRestore(context: Context, notifier: BackupNotifier) : AbstractBa
categories: List<Int>,
history: List<BackupHistory>,
tracks: List<Track>,
backupCategories: List<BackupCategory>
backupCategories: List<BackupCategory>,
customManga: CustomMangaManager.MangaJson?
) {
db.inTransaction {
val dbManga = backupManager.getMangaFromDatabase(manga)
if (dbManga == null) {
// Manga not in database
restoreMangaFetch(manga, chapters, categories, history, tracks, backupCategories)
restoreMangaFetch(manga, chapters, categories, history, tracks, backupCategories, customManga)
} else {
// Manga in database
// Copy information from manga already in database
backupManager.restoreMangaNoFetch(manga, dbManga)
// Fetch rest of manga information
restoreMangaNoFetch(manga, chapters, categories, history, tracks, backupCategories)
restoreMangaNoFetch(manga, chapters, categories, history, tracks, backupCategories, customManga)
}
}
}
@ -121,15 +125,15 @@ class FullBackupRestore(context: Context, notifier: BackupNotifier) : AbstractBa
categories: List<Int>,
history: List<BackupHistory>,
tracks: List<Track>,
backupCategories: List<BackupCategory>
backupCategories: List<BackupCategory>,
customManga: CustomMangaManager.MangaJson?
) {
try {
val fetchedManga = backupManager.restoreManga(manga)
fetchedManga.id ?: return
backupManager.restoreChaptersForManga(fetchedManga, chapters)
restoreExtraForManga(fetchedManga, categories, history, tracks, backupCategories)
restoreExtraForManga(fetchedManga, categories, history, tracks, backupCategories, customManga)
} catch (e: Exception) {
errors.add(Date() to "${manga.title} - ${e.message}")
}
@ -141,14 +145,22 @@ class FullBackupRestore(context: Context, notifier: BackupNotifier) : AbstractBa
categories: List<Int>,
history: List<BackupHistory>,
tracks: List<Track>,
backupCategories: List<BackupCategory>
backupCategories: List<BackupCategory>,
customManga: CustomMangaManager.MangaJson?
) {
backupManager.restoreChaptersForManga(backupManga, chapters)
restoreExtraForManga(backupManga, categories, history, tracks, backupCategories)
restoreExtraForManga(backupManga, categories, history, tracks, backupCategories, customManga)
}
private fun restoreExtraForManga(manga: Manga, categories: List<Int>, history: List<BackupHistory>, tracks: List<Track>, backupCategories: List<BackupCategory>) {
private fun restoreExtraForManga(
manga: Manga,
categories: List<Int>,
history: List<BackupHistory>,
tracks: List<Track>,
backupCategories: List<BackupCategory>,
customManga: CustomMangaManager.MangaJson?
) {
// Restore categories
backupManager.restoreCategoriesForManga(manga, categories, backupCategories)
@ -157,5 +169,9 @@ class FullBackupRestore(context: Context, notifier: BackupNotifier) : AbstractBa
// Restore tracking
backupManager.restoreTrackForManga(manga, tracks)
// Restore Custom Info
customManga?.id = manga.id!!
customManga?.let { customMangaManager.saveMangaInfo(it) }
}
}

View File

@ -4,6 +4,7 @@ import eu.kanade.tachiyomi.data.database.models.ChapterImpl
import eu.kanade.tachiyomi.data.database.models.Manga
import eu.kanade.tachiyomi.data.database.models.MangaImpl
import eu.kanade.tachiyomi.data.database.models.TrackImpl
import eu.kanade.tachiyomi.data.library.CustomMangaManager
import kotlinx.serialization.Serializable
import kotlinx.serialization.protobuf.ProtoNumber
@ -34,6 +35,13 @@ data class BackupManga(
@ProtoNumber(100) var favorite: Boolean = true,
@ProtoNumber(101) var chapterFlags: Int = 0,
@ProtoNumber(102) var history: List<BackupHistory> = emptyList(),
// J2K specific values
@ProtoNumber(800) var customTitle: String? = null,
@ProtoNumber(801) var customArtist: String? = null,
@ProtoNumber(802) var customAuthor: String? = null,
@ProtoNumber(803) var customDescription: String? = null,
@ProtoNumber(803) var customGenre: List<String>? = null
) {
fun getMangaImpl(): MangaImpl {
return MangaImpl().apply {
@ -59,6 +67,25 @@ data class BackupManga(
}
}
fun getCustomMangaInfo(): CustomMangaManager.MangaJson? {
if (customTitle != null ||
customArtist != null ||
customAuthor != null ||
customDescription != null ||
customGenre != null
) {
return CustomMangaManager.MangaJson(
id = 0L,
title = customTitle,
author = customAuthor,
artist = customArtist,
description = customDescription,
genre = customGenre?.toTypedArray()
)
}
return null
}
fun getTrackingImpl(): List<TrackImpl> {
return tracking.map {
it.getTrackingImpl()
@ -66,7 +93,7 @@ data class BackupManga(
}
companion object {
fun copyFrom(manga: Manga): BackupManga {
fun copyFrom(manga: Manga, customMangaManager: CustomMangaManager?): BackupManga {
return BackupManga(
url = manga.url,
title = manga.originalTitle,
@ -81,7 +108,15 @@ data class BackupManga(
dateAdded = manga.date_added,
viewer = manga.viewer,
chapterFlags = manga.chapter_flags
)
).also { backupManga ->
customMangaManager?.getManga(manga)?.let {
backupManga.customTitle = it.title
backupManga.customArtist = it.artist
backupManga.customAuthor = it.author
backupManga.customDescription = it.description
backupManga.customGenre = it.getGenres()
}
}
}
}
}

View File

@ -94,7 +94,7 @@ class CustomMangaManager(val context: Context) {
}
data class MangaJson(
val id: Long,
var id: Long,
val title: String? = null,
val author: String? = null,
val artist: String? = null,

View File

@ -271,7 +271,8 @@ class SettingsBackupController : SettingsController() {
R.string.categories,
R.string.chapters,
R.string.tracking,
R.string.history
R.string.history,
R.string.custom_manga_info
)
.map { activity.getString(it) }
@ -281,7 +282,7 @@ class SettingsBackupController : SettingsController() {
.listItemsMultiChoice(
items = options,
disabledIndices = intArrayOf(0),
initialSelection = intArrayOf(0, 1, 2, 3, 4)
initialSelection = intArrayOf(0, 1, 2, 3, 4, 5)
) { _, positions, _ ->
var flags = 0
for (i in 1 until positions.size) {
@ -290,6 +291,7 @@ class SettingsBackupController : SettingsController() {
2 -> flags = flags or BackupCreateService.BACKUP_CHAPTER
3 -> flags = flags or BackupCreateService.BACKUP_TRACK
4 -> flags = flags or BackupCreateService.BACKUP_HISTORY
5 -> flags = flags or BackupCreateService.BACKUP_CUSTOM_INFO
}
}

View File

@ -426,6 +426,7 @@
<string name="cover_saved">Cover saved</string>
<string name="error_saving_cover">Error saving cover</string>
<string name="error_sharing_cover">Error sharing cover</string>
<string name="custom_manga_info">Custom manga info</string>
<plurals name="deleted_chapters">
<item quantity="one">A chapter has been removed from the source:\n%2$s\nDelete
its download?</item>