Fix reading position not being saved when opening multi-versioned EH manga

This commit is contained in:
NerdNumber9
2019-08-04 21:27:59 -04:00
parent 3dac6366ff
commit cd5545284e
7 changed files with 95 additions and 34 deletions

View File

@@ -0,0 +1,22 @@
package exh.debug
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import uy.kohesive.injekt.injectLazy
enum class DebugToggles(val default: Boolean) {
ENABLE_EXH_ROOT_REDIRECT(true),
ENABLE_DEBUG_OVERLAY(true),
PULL_TO_ROOT_WHEN_LOADING_EXH_MANGA_DETAILS(true);
val prefKey = "eh_debug_toggle_${name.toLowerCase()}"
var enabled: Boolean
get() = prefs.rxPrefs.getBoolean(prefKey, default).get()!!
set(value) {
prefs.rxPrefs.getBoolean(prefKey).set(value)
}
companion object {
private val prefs: PreferencesHelper by injectLazy()
}
}

View File

@@ -2,13 +2,12 @@ package exh.debug
import android.annotation.SuppressLint
import android.support.v7.preference.PreferenceScreen
import android.text.Html
import android.util.Log
import android.widget.HorizontalScrollView
import android.widget.TextView
import com.afollestad.materialdialogs.MaterialDialog
import eu.kanade.tachiyomi.ui.setting.SettingsController
import eu.kanade.tachiyomi.ui.setting.onClick
import eu.kanade.tachiyomi.ui.setting.preference
import eu.kanade.tachiyomi.ui.setting.*
import kotlin.reflect.KVisibility
import kotlin.reflect.full.declaredFunctions
@@ -17,33 +16,55 @@ class SettingsDebugController : SettingsController() {
override fun setupPreferenceScreen(screen: PreferenceScreen) = with(screen) {
title = "DEBUG MENU"
DebugFunctions::class.declaredFunctions.filter {
it.visibility == KVisibility.PUBLIC
}.forEach {
preference {
title = it.name.replace(Regex("(.)(\\p{Upper})"), "$1 $2").toLowerCase().capitalize()
isPersistent = false
preferenceCategory {
title = "Functions"
onClick {
val view = TextView(context)
view.setHorizontallyScrolling(true)
view.setTextIsSelectable(true)
DebugFunctions::class.declaredFunctions.filter {
it.visibility == KVisibility.PUBLIC
}.forEach {
preference {
title = it.name.replace(Regex("(.)(\\p{Upper})"), "$1 $2").toLowerCase().capitalize()
isPersistent = false
val hView = HorizontalScrollView(context)
hView.addView(view)
onClick {
val view = TextView(context)
view.setHorizontallyScrolling(true)
view.setTextIsSelectable(true)
try {
val result = it.call(DebugFunctions)
view.text = "Function returned result:\n\n$result"
MaterialDialog.Builder(context)
.customView(hView, true)
} catch(t: Throwable) {
view.text = "Function threw exception:\n\n${Log.getStackTraceString(t)}"
MaterialDialog.Builder(context)
.customView(hView, true)
}.show()
val hView = HorizontalScrollView(context)
hView.addView(view)
try {
val result = it.call(DebugFunctions)
view.text = "Function returned result:\n\n$result"
MaterialDialog.Builder(context)
.customView(hView, true)
} catch(t: Throwable) {
view.text = "Function threw exception:\n\n${Log.getStackTraceString(t)}"
MaterialDialog.Builder(context)
.customView(hView, true)
}.show()
}
}
}
}
preferenceCategory {
title = "Toggles"
DebugToggles.values().forEach {
switchPreference {
title = it.name.replace('_', ' ').toLowerCase().capitalize()
key = it.prefKey
defaultValue = it.default
summaryOn = if(it.default) "" else MODIFIED_TEXT
summaryOff = if(it.default) MODIFIED_TEXT else ""
}
}
}
}
companion object {
private val MODIFIED_TEXT = Html.fromHtml("<font color='red'>MODIFIED</font>")
}
}

View File

@@ -57,6 +57,8 @@ class EHentaiUpdateHelper(context: Context) {
return chainsWithAccepted.map { (accepted, chains) ->
val toDiscard = chains.filter { it.manga.favorite && it.manga.id != accepted.manga.id }
val chainsAsChapters = chains.flatMap { it.chapters }
if(toDiscard.isNotEmpty()) {
// Copy chain chapters to curChapters
val newChapters = toDiscard
@@ -81,9 +83,16 @@ class EHentaiUpdateHelper(context: Context) {
.fold(accepted.chapters) { curChapters, chapter ->
val existing = curChapters.find { it.url == chapter.url }
val newLastPageRead = chainsAsChapters.filter { it.date_upload < chapter.date_upload }.maxBy {
it.last_page_read
}?.last_page_read
if (existing != null) {
existing.read = existing.read || chapter.read
existing.last_page_read = existing.last_page_read.coerceAtLeast(chapter.last_page_read)
if(newLastPageRead != null && existing.last_page_read <= 0) {
existing.last_page_read = newLastPageRead
}
existing.bookmark = existing.bookmark || chapter.bookmark
curChapters
} else if (chapter.date_upload > 0) { // Ignore chapters using the old system
@@ -93,19 +102,24 @@ class EHentaiUpdateHelper(context: Context) {
name = chapter.name
read = chapter.read
bookmark = chapter.bookmark
last_page_read = chapter.last_page_read
if(newLastPageRead != null && last_page_read <= 0) {
last_page_read = newLastPageRead
}
date_fetch = chapter.date_fetch
date_upload = chapter.date_upload
}
} else curChapters
}
.filter { it.date_upload <= 0 } // Ignore chapters using the old system (filter after to prevent dupes from insert)
.filter { it.date_upload > 0 } // Ignore chapters using the old system (filter after to prevent dupes from insert)
.sortedBy { it.date_upload }
.apply {
withIndex().map { (index, chapter) ->
mapIndexed { index, chapter ->
chapter.name = "v${index + 1}: " + chapter.name.substringAfter(" ")
chapter.chapter_number = index + 1f
chapter.source_order = index
chapter.source_order = lastIndex - index
}
}
@@ -119,7 +133,7 @@ class EHentaiUpdateHelper(context: Context) {
// Apply changes to all manga
db.insertMangas(rootsToMutate.map { it.manga }).executeAsBlocking()
// Insert new chapters for accepted manga
db.insertChapters(newAccepted.chapters)
db.insertChapters(newAccepted.chapters).executeAsBlocking()
// Copy categories from all chains to accepted manga
val newCategories = rootsToMutate.flatMap {
db.getCategoriesForManga(it.manga).executeAsBlocking()