From f1350bc33e2d20c7cff9e3aadb604ab3498b9672 Mon Sep 17 00:00:00 2001 From: Andreas E Date: Sat, 24 Oct 2020 19:06:23 +0200 Subject: [PATCH] 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 --- .../ui/reader/viewer/MissingChapters.kt | 46 +++++++++++++++---- .../ui/reader/viewer/ReaderTransitionView.kt | 23 +++++----- .../reader/viewer/pager/PagerViewerAdapter.kt | 6 +-- .../reader/viewer/webtoon/WebtoonAdapter.kt | 6 +-- 4 files changed, 56 insertions(+), 25 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/MissingChapters.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/MissingChapters.kt index 4ee2543eac..64853058c2 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/MissingChapters.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/MissingChapters.kt @@ -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 } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/ReaderTransitionView.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/ReaderTransitionView.kt index 8ab6a6f262..453352b20b 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/ReaderTransitionView.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/ReaderTransitionView.kt @@ -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 } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/pager/PagerViewerAdapter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/pager/PagerViewerAdapter.kt index fe2244cfbd..b1dfb70f94 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/pager/PagerViewerAdapter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/pager/PagerViewerAdapter.kt @@ -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() // 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) { diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonAdapter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonAdapter.kt index 0aa19a2f1f..d699079bc7 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonAdapter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonAdapter.kt @@ -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() // 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) {