Fix missing chapter warning when chapter number is not recognized (#3928)

* Fix missing chapter warning when chapter number is not recognized

* Add case where ch number is recognized as zero but has no ch number

Yes this will ignore ch 0

* Use RegEx to double check if ch. name contains potential ch. number
This commit is contained in:
Andreas E 2020-10-24 19:06:23 +02:00 committed by GitHub
parent 6af0eb4068
commit f1350bc33e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 56 additions and 25 deletions

View file

@ -1,15 +1,45 @@
package eu.kanade.tachiyomi.ui.reader.viewer
import eu.kanade.tachiyomi.data.database.models.Chapter
import eu.kanade.tachiyomi.ui.reader.model.ReaderChapter
import kotlin.math.floor
object MissingChapters {
private val pattern = Regex("""\d+""")
fun hasMissingChapters(higher: Chapter, lower: Chapter): Boolean {
return hasMissingChapters(higher.chapter_number, lower.chapter_number)
}
fun hasMissingChapters(higherChapterNumber: Float, lowerChapterNumber: Float): Boolean {
return floor(higherChapterNumber) - floor(lowerChapterNumber) - 1f > 0f
}
fun hasMissingChapters(higherReaderChapter: ReaderChapter?, lowerReaderChapter: ReaderChapter?): Boolean {
if (higherReaderChapter == null || lowerReaderChapter == null) return false
return hasMissingChapters(higherReaderChapter.chapter, lowerReaderChapter.chapter)
}
fun hasMissingChapters(higherChapter: Chapter?, lowerChapter: Chapter?): Boolean {
if (higherChapter == null || lowerChapter == null) return false
// Check if name contains a number that is potential chapter number
if (!pattern.containsMatchIn(higherChapter.name) || !pattern.containsMatchIn(lowerChapter.name)) return false
// Check if potential chapter number was recognized as chapter number
if (!higherChapter.isRecognizedNumber || !lowerChapter.isRecognizedNumber) return false
return hasMissingChapters(higherChapter.chapter_number, lowerChapter.chapter_number)
}
fun hasMissingChapters(higherChapterNumber: Float, lowerChapterNumber: Float): Boolean {
if (higherChapterNumber < 0f || lowerChapterNumber < 0f) return false
return calculateChapterDifference(higherChapterNumber, lowerChapterNumber) > 0f
}
fun calculateChapterDifference(higherReaderChapter: ReaderChapter?, lowerReaderChapter: ReaderChapter?): Float {
if (higherReaderChapter == null || lowerReaderChapter == null) return 0f
return calculateChapterDifference(higherReaderChapter.chapter, lowerReaderChapter.chapter)
}
fun calculateChapterDifference(higherChapter: Chapter?, lowerChapter: Chapter?): Float {
if (higherChapter == null || lowerChapter == null) return 0f
// Check if name contains a number that is potential chapter number
if (!pattern.containsMatchIn(higherChapter.name) || !pattern.containsMatchIn(lowerChapter.name)) return 0f
// Check if potential chapter number was recognized as chapter number
if (!higherChapter.isRecognizedNumber || !lowerChapter.isRecognizedNumber) return 0f
return calculateChapterDifference(higherChapter.chapter_number, lowerChapter.chapter_number)
}
fun calculateChapterDifference(higherChapterNumber: Float, lowerChapterNumber: Float): Float {
if (higherChapterNumber < 0f || lowerChapterNumber < 0f) return 0f
return floor(higherChapterNumber) - floor(lowerChapterNumber) - 1f
}

View file

@ -12,7 +12,6 @@ import kotlinx.android.synthetic.main.reader_transition_view.view.lower_text
import kotlinx.android.synthetic.main.reader_transition_view.view.upper_text
import kotlinx.android.synthetic.main.reader_transition_view.view.warning
import kotlinx.android.synthetic.main.reader_transition_view.view.warning_text
import kotlin.math.floor
class ReaderTransitionView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) :
LinearLayout(context, attrs) {
@ -85,20 +84,22 @@ class ReaderTransitionView @JvmOverloads constructor(context: Context, attrs: At
return
}
val fromChapterNumber: Float = floor(transition.from.chapter.chapter_number)
val toChapterNumber: Float = floor(transition.to!!.chapter.chapter_number)
val chapterDifference = when (transition) {
is ChapterTransition.Prev -> fromChapterNumber - toChapterNumber - 1f
is ChapterTransition.Next -> toChapterNumber - fromChapterNumber - 1f
val hasMissingChapters = when (transition) {
is ChapterTransition.Prev -> hasMissingChapters(transition.from, transition.to)
is ChapterTransition.Next -> hasMissingChapters(transition.to, transition.from)
}
val hasMissingChapters = when (transition) {
is ChapterTransition.Prev -> MissingChapters.hasMissingChapters(fromChapterNumber, toChapterNumber)
is ChapterTransition.Next -> MissingChapters.hasMissingChapters(toChapterNumber, fromChapterNumber)
if (!hasMissingChapters) {
warning.isVisible = false
return
}
val chapterDifference = when (transition) {
is ChapterTransition.Prev -> calculateChapterDifference(transition.from, transition.to)
is ChapterTransition.Next -> calculateChapterDifference(transition.to, transition.from)
}
warning_text.text = resources.getQuantityString(R.plurals.missing_chapters_warning, chapterDifference.toInt(), chapterDifference.toInt())
warning.isVisible = hasMissingChapters
warning.isVisible = true
}
}

View file

@ -6,7 +6,7 @@ import eu.kanade.tachiyomi.ui.reader.model.ChapterTransition
import eu.kanade.tachiyomi.ui.reader.model.ReaderChapter
import eu.kanade.tachiyomi.ui.reader.model.ReaderPage
import eu.kanade.tachiyomi.ui.reader.model.ViewerChapters
import eu.kanade.tachiyomi.ui.reader.viewer.MissingChapters
import eu.kanade.tachiyomi.ui.reader.viewer.hasMissingChapters
import eu.kanade.tachiyomi.widget.ViewPagerAdapter
import timber.log.Timber
@ -35,8 +35,8 @@ class PagerViewerAdapter(private val viewer: PagerViewer) : ViewPagerAdapter() {
val newItems = mutableListOf<Any>()
// Forces chapter transition if there is missing chapters
val prevHasMissingChapters = if (chapters.prevChapter != null) MissingChapters.hasMissingChapters(chapters.currChapter.chapter, chapters.prevChapter.chapter) else false
val nextHasMissingChapters = if (chapters.nextChapter != null) MissingChapters.hasMissingChapters(chapters.nextChapter.chapter, chapters.currChapter.chapter) else false
val prevHasMissingChapters = hasMissingChapters(chapters.currChapter, chapters.prevChapter)
val nextHasMissingChapters = hasMissingChapters(chapters.nextChapter, chapters.currChapter)
// Add previous chapter pages and transition.
if (chapters.prevChapter != null) {

View file

@ -9,7 +9,7 @@ import eu.kanade.tachiyomi.ui.reader.model.ChapterTransition
import eu.kanade.tachiyomi.ui.reader.model.ReaderChapter
import eu.kanade.tachiyomi.ui.reader.model.ReaderPage
import eu.kanade.tachiyomi.ui.reader.model.ViewerChapters
import eu.kanade.tachiyomi.ui.reader.viewer.MissingChapters
import eu.kanade.tachiyomi.ui.reader.viewer.hasMissingChapters
/**
* RecyclerView Adapter used by this [viewer] to where [ViewerChapters] updates are posted.
@ -31,8 +31,8 @@ class WebtoonAdapter(val viewer: WebtoonViewer) : RecyclerView.Adapter<RecyclerV
val newItems = mutableListOf<Any>()
// Forces chapter transition if there is missing chapters
val prevHasMissingChapters = if (chapters.prevChapter != null) MissingChapters.hasMissingChapters(chapters.currChapter.chapter, chapters.prevChapter.chapter) else false
val nextHasMissingChapters = if (chapters.nextChapter != null) MissingChapters.hasMissingChapters(chapters.nextChapter.chapter, chapters.currChapter.chapter) else false
val prevHasMissingChapters = hasMissingChapters(chapters.currChapter, chapters.prevChapter)
val nextHasMissingChapters = hasMissingChapters(chapters.nextChapter, chapters.currChapter)
// Add previous chapter pages and transition.
if (chapters.prevChapter != null) {