Merge Mihon 0.21.2 updates and use BookOasis updater
Build & Test / Build & Test App (push) Has been cancelled
Release / Extract tag name (push) Has been cancelled
Update website / update_website (release) Has been cancelled
Release / Build (push) Has been cancelled
Release / Build (FOSS) (push) Has been cancelled
Release / Create GitHub Release (push) Has been cancelled

This commit is contained in:
BookOasis Reader
2026-10-13 14:26:36 +09:00
583 changed files with 52681 additions and 30246 deletions
+2 -1
View File
@@ -19,6 +19,7 @@ kotlin {
dependencies {
implementation(projects.sourceApi)
implementation(projects.core.common)
implementation(projects.core.metro)
implementation(libs.metro.runtime)
implementation(libs.bundles.kotlinx.coroutines)
@@ -28,7 +29,7 @@ dependencies {
implementation(libs.unifile)
api(libs.sqldelight.androidxPaging)
api(libs.androidx.paging.runtime)
compileOnly(platform(libs.androidx.compose.bom))
compileOnly(libs.androidx.compose.runtimeAnnotation)
@@ -1,6 +1,5 @@
package eu.kanade.tachiyomi.extension.model
import android.graphics.drawable.Drawable
import eu.kanade.tachiyomi.source.Source
import mihon.domain.extension.model.ContentWarning
import mihon.domain.extension.model.ExtensionStore
@@ -21,6 +20,30 @@ sealed interface Extension {
*/
sealed interface Installed : Extension {
val isShared: Boolean
/** SHA-256 digests of the certificates its apk is signed with. */
val signatures: List<String>
val hasUpdate: Boolean
val store: ExtensionStore?
/**
* The newest listing of this extension among the stores whose signing key it's signed with. An
* apk from any other store can't replace it, so only these are where its updates come from.
*/
fun findListing(available: Collection<Available>): Available? {
return available
.filter { it.pkgName == pkgName && it.store.signingKey in signatures }
.maxWithOrNull(compareBy<Available> { it.versionCode }.thenBy { it.libVersion })
}
fun findUpdate(available: Collection<Available>): Available? {
val installedLibVersion = libVersion
return findListing(available)?.takeIf {
it.versionCode > versionCode || (installedLibVersion != null && it.libVersion > installedLibVersion)
}
}
}
/**
@@ -68,12 +91,12 @@ sealed interface Extension {
override val lang: String,
override val contentWarning: ContentWarning,
override val isShared: Boolean,
override val signatures: List<String>,
val pkgFactory: String?,
val sources: List<Source>,
val icon: Drawable?,
val hasUpdate: Boolean = false,
override val hasUpdate: Boolean = false,
val isObsolete: Boolean = false,
val store: ExtensionStore? = null,
override val store: ExtensionStore? = null,
) : Installed
/**
@@ -87,8 +110,11 @@ sealed interface Extension {
override val versionCode: Long,
override val isShared: Boolean,
override val contentWarning: ContentWarning,
override val signatures: List<String>,
override val libVersion: Double? = null,
override val lang: String? = null,
override val hasUpdate: Boolean = false,
override val store: ExtensionStore? = null,
val reason: Reason,
) : Installed {
@@ -0,0 +1,15 @@
package mihon.domain.common
import kotlin.properties.Delegates
import kotlin.reflect.KProperty
abstract class PartialUpdate {
private val assigned = mutableSetOf<String>()
fun isSet(property: KProperty<*>): Boolean = property.name in assigned
protected fun <T> field(initial: T) = Delegates.observable(initial) { property, _, _ ->
assigned += property.name
}
}
@@ -0,0 +1,17 @@
package mihon.domain.database.repository
import kotlinx.coroutines.flow.StateFlow
interface DatabaseRepository {
/**
* True while the database is being migrated from an older schema, which happens on its first use after an
* app update.
*/
val isMigrating: StateFlow<Boolean>
/**
* Opens the database, migrating it if needed, and throws what made the migration fail.
*/
suspend fun awaitOpen()
}
@@ -9,6 +9,8 @@ interface ExtensionStoreRepository {
suspend fun insertFromPreference(indexUrl: String, name: String)
suspend fun upsert(store: ExtensionStore)
suspend fun refreshAll()
suspend fun fetchExtensions(): List<Extension.Available>
@@ -0,0 +1,21 @@
package tachiyomi.domain.backup.model
import tachiyomi.domain.chapter.model.Chapter
import tachiyomi.domain.manga.model.Manga
import tachiyomi.domain.track.model.Track
import kotlin.time.Instant
data class RestoredManga(
val manga: Manga,
val chapters: List<Chapter>,
val categoryIds: List<Long>,
val history: List<RestoredHistory>,
val tracks: List<Track>,
val excludedScanlators: List<String>,
)
data class RestoredHistory(
val chapterUrl: String,
val readAt: Instant?,
val readDuration: Long,
)
@@ -0,0 +1,16 @@
package tachiyomi.domain.backup.repository
import tachiyomi.domain.backup.model.RestoredManga
import tachiyomi.domain.manga.model.Manga
import tachiyomi.domain.manga.model.MangaUpdate
interface RestoreRepository {
suspend fun getMangaUrlsBySourceId(): Map<Long, Set<String>>
/**
* Restores [entries] all together: either every one of them is restored or none is. [update] is what to change on each
* entry once its chapters and history are in place, and is part of the same all-or-nothing restore.
*/
suspend fun restoreManga(entries: List<RestoredManga>, update: suspend (Manga) -> MangaUpdate)
}
@@ -2,9 +2,8 @@ package tachiyomi.domain.category.interactor
import dev.zacsweers.metro.Inject
import logcat.LogPriority
import tachiyomi.core.common.util.lang.withNonCancellableContext
import tachiyomi.core.common.util.system.logcat
import tachiyomi.domain.category.model.Category
import tachiyomi.domain.category.model.NewCategory
import tachiyomi.domain.category.repository.CategoryRepository
import tachiyomi.domain.library.service.LibraryPreferences
@@ -20,18 +19,9 @@ class CreateCategoryWithName(
return sort.type.flag or sort.direction.flag
}
suspend fun await(name: String): Result = withNonCancellableContext {
val categories = categoryRepository.getAll()
val nextOrder = categories.maxOfOrNull { it.order }?.plus(1) ?: 0
val newCategory = Category(
id = 0,
name = name,
order = nextOrder,
flags = initialFlags,
)
try {
categoryRepository.insert(newCategory)
suspend fun await(name: String): Result {
return try {
categoryRepository.insert(NewCategory(name = name, flags = initialFlags))
Result.Success
} catch (e: Exception) {
logcat(LogPriority.ERROR, e)
@@ -2,8 +2,8 @@ package tachiyomi.domain.category.interactor
import dev.zacsweers.metro.Inject
import logcat.LogPriority
import tachiyomi.core.common.util.lang.withNonCancellableContext
import tachiyomi.core.common.util.system.logcat
import tachiyomi.domain.category.model.Category
import tachiyomi.domain.category.repository.CategoryRepository
import tachiyomi.domain.download.service.DownloadPreferences
import tachiyomi.domain.library.service.LibraryPreferences
@@ -15,15 +15,17 @@ class DeleteCategory(
private val downloadPreferences: DownloadPreferences,
) {
suspend fun await(categoryId: Long) = withNonCancellableContext {
suspend fun await(categoryId: Long): Result {
try {
categoryRepository.delete(categoryId)
} catch (e: Exception) {
logcat(LogPriority.ERROR, e)
return@withNonCancellableContext Result.InternalError(e)
return Result.InternalError(e)
}
val orderedIds = categoryRepository.getAll().map { it.id }
val orderedIds = categoryRepository.getAll()
.filterNot(Category::isSystemCategory)
.map { it.id }
val defaultCategory = libraryPreferences.defaultCategory.get()
if (defaultCategory == categoryId.toInt()) {
@@ -44,7 +46,7 @@ class DeleteCategory(
preference.set(ids.minus(categoryIdString))
}
try {
return try {
categoryRepository.updateAllOrders(orderedIds = orderedIds)
Result.Success
} catch (e: Exception) {
@@ -2,7 +2,6 @@ package tachiyomi.domain.category.interactor
import dev.zacsweers.metro.Inject
import logcat.LogPriority
import tachiyomi.core.common.util.lang.withNonCancellableContext
import tachiyomi.core.common.util.system.logcat
import tachiyomi.domain.category.model.Category
import tachiyomi.domain.category.repository.CategoryRepository
@@ -12,8 +11,8 @@ class RenameCategory(
private val categoryRepository: CategoryRepository,
) {
suspend fun await(categoryId: Long, name: String) = withNonCancellableContext {
try {
suspend fun await(categoryId: Long, name: String): Result {
return try {
categoryRepository.updateName(categoryId = categoryId, name = name)
Result.Success
} catch (e: Exception) {
@@ -4,7 +4,6 @@ import dev.zacsweers.metro.Inject
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import logcat.LogPriority
import tachiyomi.core.common.util.lang.withNonCancellableContext
import tachiyomi.core.common.util.system.logcat
import tachiyomi.domain.category.model.Category
import tachiyomi.domain.category.repository.CategoryRepository
@@ -15,26 +14,24 @@ class ReorderCategory(
) {
private val mutex = Mutex()
suspend fun await(category: Category, newIndex: Int) = withNonCancellableContext {
mutex.withLock {
val categories = categoryRepository.getAll()
.filterNot(Category::isSystemCategory)
.toMutableList()
suspend fun await(category: Category, newIndex: Int) = mutex.withLock {
val categories = categoryRepository.getAll()
.filterNot(Category::isSystemCategory)
.toMutableList()
val currentIndex = categories.indexOfFirst { it.id == category.id }
if (currentIndex == -1) {
return@withNonCancellableContext Result.Unchanged
}
val currentIndex = categories.indexOfFirst { it.id == category.id }
if (currentIndex == -1) {
return@withLock Result.Unchanged
}
try {
categories.add(newIndex, categories.removeAt(currentIndex))
try {
categories.add(newIndex, categories.removeAt(currentIndex))
categoryRepository.updateAllOrders(orderedIds = categories.map { it.id })
Result.Success
} catch (e: Exception) {
logcat(LogPriority.ERROR, e)
Result.InternalError(e)
}
categoryRepository.updateAllOrders(orderedIds = categories.map { it.id })
Result.Success
} catch (e: Exception) {
logcat(LogPriority.ERROR, e)
Result.InternalError(e)
}
}
@@ -0,0 +1,6 @@
package tachiyomi.domain.category.model
data class NewCategory(
val name: String,
val flags: Long,
)
@@ -2,6 +2,7 @@ package tachiyomi.domain.category.repository
import kotlinx.coroutines.flow.Flow
import tachiyomi.domain.category.model.Category
import tachiyomi.domain.category.model.NewCategory
interface CategoryRepository {
@@ -15,13 +16,17 @@ interface CategoryRepository {
fun getCategoriesByMangaIdAsFlow(mangaId: Long): Flow<List<Category>>
suspend fun insert(category: Category)
suspend fun getCategoriesByMangaIds(mangaIds: List<Long>): Map<Long, List<Category>>
suspend fun insert(category: NewCategory)
suspend fun insertAll(categories: List<NewCategory>)
suspend fun updateName(categoryId: Long, name: String)
suspend fun updateFlags(categoryId: Long, flags: Long)
suspend fun updateAllFlags(flags: Long?)
suspend fun updateAllFlags(flags: Long)
suspend fun updateAllOrders(orderedIds: List<Long>)
@@ -0,0 +1,15 @@
package tachiyomi.domain.chapter.interactor
import dev.zacsweers.metro.Inject
import tachiyomi.domain.chapter.model.ChapterSummary
import tachiyomi.domain.chapter.repository.ChapterRepository
@Inject
class GetChapterSummaryByMangaId(
private val chapterRepository: ChapterRepository,
) {
suspend fun await(mangaId: Long): ChapterSummary {
return chapterRepository.getChapterSummaryByMangaId(mangaId)
}
}
@@ -1,7 +1,6 @@
package tachiyomi.domain.chapter.interactor
import dev.zacsweers.metro.Inject
import tachiyomi.core.common.util.lang.withNonCancellableContext
import tachiyomi.domain.library.service.LibraryPreferences
import tachiyomi.domain.manga.interactor.GetFavorites
import tachiyomi.domain.manga.interactor.SetMangaChapterFlags
@@ -15,24 +14,24 @@ class SetMangaDefaultChapterFlags(
) {
suspend fun await(manga: Manga) {
withNonCancellableContext {
with(libraryPreferences) {
setMangaChapterFlags.awaitSetAllFlags(
mangaId = manga.id,
unreadFilter = filterChapterByRead.get(),
downloadedFilter = filterChapterByDownloaded.get(),
bookmarkedFilter = filterChapterByBookmarked.get(),
sortingMode = sortChapterBySourceOrNumber.get(),
sortingDirection = sortChapterByAscendingOrDescending.get(),
displayMode = displayChapterByNameOrNumber.get(),
)
}
}
setDefaultFlags(listOf(manga.id))
}
suspend fun awaitAll() {
withNonCancellableContext {
getFavorites.await().forEach { await(it) }
setDefaultFlags(getFavorites.await().map { it.id })
}
private suspend fun setDefaultFlags(mangaIds: List<Long>) {
with(libraryPreferences) {
setMangaChapterFlags.awaitSetAllFlags(
mangaIds = mangaIds,
unreadFilter = filterChapterByRead.get(),
downloadedFilter = filterChapterByDownloaded.get(),
bookmarkedFilter = filterChapterByBookmarked.get(),
sortingMode = sortChapterBySourceOrNumber.get(),
sortingDirection = sortChapterByAscendingOrDescending.get(),
displayMode = displayChapterByNameOrNumber.get(),
)
}
}
}
@@ -2,22 +2,21 @@ package tachiyomi.domain.chapter.model
import kotlinx.serialization.json.JsonObject
import mihon.core.common.extensions.EMPTY
import kotlin.time.Instant
data class Chapter(
val id: Long,
val mangaId: Long,
val read: Boolean,
val bookmark: Boolean,
val lastPageRead: Long,
val dateFetch: Long,
val lastPageRead: Int,
val dateFetch: Instant,
val sourceOrder: Long,
val url: String,
val name: String,
val dateUpload: Long,
val dateUpload: Instant?,
val chapterNumber: Double,
val scanlator: String?,
val lastModifiedAt: Long,
val version: Long,
val memo: JsonObject,
) {
val isRecognizedNumber: Boolean
@@ -40,15 +39,13 @@ data class Chapter(
read = false,
bookmark = false,
lastPageRead = 0,
dateFetch = 0,
dateFetch = Instant.fromEpochMilliseconds(0),
sourceOrder = 0,
url = "",
name = "",
dateUpload = -1,
dateUpload = null,
chapterNumber = -1.0,
scanlator = null,
lastModifiedAt = 0,
version = 1,
memo = JsonObject.EMPTY,
)
}
@@ -0,0 +1,14 @@
package tachiyomi.domain.chapter.model
import kotlinx.serialization.json.JsonObject
import kotlin.time.Instant
data class ChapterRemoteUpdate(
val id: Long,
val name: String,
val scanlator: String?,
val chapterNumber: Double,
val dateUpload: Instant?,
val sourceOrder: Long,
val memo: JsonObject,
)
@@ -0,0 +1,6 @@
package tachiyomi.domain.chapter.model
data class ChapterSummary(
val chapterCount: Long,
val latestChapterNumber: Double?,
)
@@ -1,39 +1,15 @@
package tachiyomi.domain.chapter.model
import kotlinx.serialization.json.JsonObject
import mihon.domain.common.PartialUpdate
import kotlin.time.Instant
data class ChapterUpdate(
val id: Long,
val mangaId: Long? = null,
val read: Boolean? = null,
val bookmark: Boolean? = null,
val lastPageRead: Long? = null,
val dateFetch: Long? = null,
val sourceOrder: Long? = null,
val url: String? = null,
val name: String? = null,
val dateUpload: Long? = null,
val chapterNumber: Double? = null,
val scanlator: String? = null,
val version: Long? = null,
val memo: JsonObject? = null,
)
class ChapterUpdate(val id: Long, block: ChapterUpdate.() -> Unit) : PartialUpdate() {
var read: Boolean? by field(null)
var bookmark: Boolean? by field(null)
var lastPageRead: Int? by field(null)
var dateFetch: Instant? by field(null)
fun Chapter.toChapterUpdate(): ChapterUpdate {
return ChapterUpdate(
id,
mangaId,
read,
bookmark,
lastPageRead,
dateFetch,
sourceOrder,
url,
name,
dateUpload,
chapterNumber,
scanlator,
version,
memo,
)
init {
block()
}
}
@@ -2,20 +2,28 @@ package tachiyomi.domain.chapter.repository
import kotlinx.coroutines.flow.Flow
import tachiyomi.domain.chapter.model.Chapter
import tachiyomi.domain.chapter.model.ChapterRemoteUpdate
import tachiyomi.domain.chapter.model.ChapterSummary
import tachiyomi.domain.chapter.model.ChapterUpdate
interface ChapterRepository {
suspend fun addAll(chapters: List<Chapter>): List<Chapter>
suspend fun update(chapterUpdate: ChapterUpdate)
suspend fun updateAll(chapterUpdates: List<ChapterUpdate>)
suspend fun removeChaptersWithIds(chapterIds: List<Long>)
suspend fun updateFromRemote(
removedIds: List<Long>,
added: List<Chapter>,
updated: List<ChapterRemoteUpdate>,
): List<Chapter>
suspend fun getChapterByMangaId(mangaId: Long, applyScanlatorFilter: Boolean = false): List<Chapter>
suspend fun getChaptersByMangaIds(mangaIds: List<Long>): Map<Long, List<Chapter>>
suspend fun getChapterSummaryByMangaId(mangaId: Long): ChapterSummary
suspend fun getScanlatorsByMangaId(mangaId: Long): List<String>
fun getScanlatorsByMangaIdAsFlow(mangaId: Long): Flow<List<String>>
@@ -1,15 +1,30 @@
package tachiyomi.domain.chapter.service
import tachiyomi.domain.chapter.model.Chapter
import java.text.DecimalFormat
import java.text.DecimalFormatSymbols
import java.util.Locale
import kotlin.math.floor
import kotlin.math.round
/**
* Recognizes the volume and chapter number of chapters from their names.
*
* Each name is first read on its own: the volume or season is taken out, a part becomes the letter of a sub-chapter,
* and the chapter number is the number after "ch." or else the first one left. Reading the whole chapter list then
* fills in what single names don't tell, see [parseChapterNumbers].
*
* -R> = regex conversion.
*/
object ChapterRecognition {
/**
* A number with optional decimals and a trailing word or letter, e.g. 12, 12.5, 12a, 12.extra
*/
private const val NUMBER_PATTERN = """([0-9]+)(\.[0-9]+)?(\.?[a-z]+)?"""
/**
* All cases with Ch.xx
* Mokushiroku Alice Vol.1 Ch. 4: Misrepresentation -R> 4
* Example: Mokushiroku Alice Vol.1 Ch. 4: Misrepresentation -R> 4
*/
private val basic = Regex("""(?<=ch\.) *$NUMBER_PATTERN""")
@@ -19,113 +34,301 @@ object ChapterRecognition {
private val number = Regex(NUMBER_PATTERN)
/**
* Regex used to remove unwanted tags
* Example Prison School 12 v.1 vol004 version1243 volume64 -R> Prison School 12
* Example: Onepunch-Man Punch Ver002 028 -R> Onepunch-Man Punch 028
*/
private val unwanted = Regex("""\b(?:v|ver|vol|version|volume|season|s)[^a-z]?[0-9]+""")
private val unwanted = Regex("""\b(?:ver|version)[^a-z]?[0-9]+""")
/**
* Regex used to remove unwanted whitespace
* Example One Piece 12 special -R> One Piece 12special
* Example: One Piece 12 special -R> One Piece 12special
*/
private val unwantedWhiteSpace = Regex("""\s(?=extra|special|omake)""")
/**
* A volume or season, the short forms only right before the number since "s" also ends words.
* Example: Mokushiroku Alice Vol.1 Ch. 4: Misrepresentation -R> 1, The Gamer S3 - Chapter 20 -R> 3
*/
private val volume = Regex("""\b(?:(?:vol(?:ume)?|season)[^a-z0-9]? *|[vs]\.?)([0-9]+(?:\.[0-9]+)?)\b""")
/**
* Example: Chapter 1.5 Part II -R> b
*/
private val part = Regex("""\bpart\.? *([0-9]+|i{1,3}|iv|vi{0,3}|ix|x)\b""")
private val romanNumerals = listOf("i", "ii", "iii", "iv", "v", "vi", "vii", "viii", "ix", "x")
/**
* Example: Group 2 Chapter 386 -R> Group # Chapter #
*/
private val anyNumber = Regex("""[0-9]+(?:\.[0-9]+)?""")
/**
* A number after this is the chapter number, even when every similar name has the same one.
* Example: Oshi no Ko Chapter 5 Part 1 -R> 5a
*/
private val chapterPrefix = Regex("""(?:\b(?:ch|chap|chapter|ep|episode)\.? *|#)$""")
/**
* Example: Bonus Chapter: Beach Episode
*/
private val bonus = Regex("""\b(?:extra|bonus|omake|special|side ?story|afterword|interlude)""")
/**
* Example: Prologue: The Beginning -R> 0
*/
private val prologue = Regex("""\b(?:prologue|one.?shot|pilot)\b""")
/**
* Formats numbers without trailing zeros, with ASCII digits and a dot whatever the locale, e.g. 10.5, 3, 24.005
*/
private val formatter = DecimalFormat("#.###", DecimalFormatSymbols(Locale.ROOT))
/**
* The volume and chapter number recognized for a chapter, e.g. "10.5" or "19a", `null` when there is none.
*/
data class ChapterNumbering(val volume: String?, val chapter: String?)
/**
* A recognized number, with the letter of a sub-chapter like the a in 19a. Kept apart from the value so that
* chapters can still be compared and numbered after each other.
*/
private data class RecognizedNumber(val value: Double, val letter: Char? = null) {
override fun toString(): String = formatter.format(value) + (letter?.toString() ?: "")
}
/**
* Recognizes the chapter number of a single chapter, keeping [chapterNumber] when the source gave one.
*/
fun parseChapterNumber(
mangaTitle: String,
chapterName: String,
chapterNumber: Double? = null,
): Double {
// If chapter number is known return.
if (chapterNumber != null && (chapterNumber == -2.0 || chapterNumber > -1.0)) {
return chapterNumber
if (chapterNumber != null && chapterNumber.isKnown()) return chapterNumber
return parseName(cleanName(mangaTitle, chapterName))?.let { toDouble(it.toString()) } ?: chapterNumber ?: -1.0
}
/**
* Recognizes the numbers of [chapters], given from the most to the least recent, using the whole list for what a
* name alone doesn't tell: numbers every similar name shares aren't the chapter number, and chapters without a
* number are numbered after the chapters around them. Numbers the source gave are kept.
*/
fun parseChapterNumbers(mangaTitle: String, chapters: List<Chapter>): List<ChapterNumbering> {
val names = chapters.map { cleanName(mangaTitle, it.name) }
// Taken out before reading the names, so a number like the 2 in "Group 2 Chapter 386" can't be picked
val sharedNumbers = findSharedNumbers(names, chapters)
val chapterNumbers = Array(chapters.size) { index ->
chapters[index].chapterNumber.takeIf { it.isKnown() }?.let(::RecognizedNumber)
?: parseName(names[index].removeRanges(sharedNumbers[index]))
}
// Only from the name: newer chapters may not be in a volume yet, so neighbours don't tell
val volumes = Array(chapters.size) { volume.find(names[it])?.groupValues?.get(1)?.toDouble() }
// Get chapter title with lower case
val cleanChapterName = chapterName.lowercase()
// Remove manga title from chapter title.
.replace(mangaTitle.lowercase(), "").trim()
// Remove comma's or hyphens.
.replace(',', '.')
.replace('-', '.')
// Remove unwanted white spaces.
.replace(unwantedWhiteSpace, "")
fillChapterNumbers(names, chapterNumbers, volumes)
return chapters.indices.map {
ChapterNumbering(volumes[it]?.let(formatter::format), chapterNumbers[it]?.toString())
}
}
val numberMatch = number.findAll(cleanChapterName)
/**
* Returns [number], a number of a [ChapterNumbering], as a double. A letter only counts without decimals, from .1
* for a up to .9 for i, e.g. 19a -R> 19.1 and 1.5a -R> 1.5.
*/
fun toDouble(number: String): Double {
val letter = number.last().takeIf { it.isLetter() }
val value = number.removeSuffix(letter?.toString() ?: "").toDouble()
val tenths = letter?.let { it - 'a' + 1 }
return if (tenths != null && tenths < 10 && '.' !in number) value + tenths / 10.0 else value
}
when {
numberMatch.none() -> {
return chapterNumber ?: -1.0
/**
* Whether the source gave a number: -1 and below mean it didn't, except -2 which some sources use on purpose.
*/
private fun Double.isKnown(): Boolean = this == -2.0 || this > -1.0
private fun cleanName(mangaTitle: String, chapterName: String): String {
return chapterName.lowercase().replace(mangaTitle.lowercase(), "")
}
/**
* Replaces [ranges] with spaces, from the last so the earlier ones stay where they are.
*/
private fun String.removeRanges(ranges: List<IntRange>): String {
return ranges.sortedByDescending { it.first }.fold(this) { name, range -> name.replaceRange(range, " ") }
}
/**
* Finds the numbers that are the same in every name of a group of at least three names that only differ in their
* numbers, e.g. the 2 in "Group 2 Chapter 385". Chapters with a number from the source are left out.
*/
private fun findSharedNumbers(names: List<String>, chapters: List<Chapter>): List<List<IntRange>> {
val matches = names.map { anyNumber.findAll(it).toList() }
val sharedNumbers = MutableList(names.size) { emptyList<IntRange>() }
names.indices
.filterNot { chapters[it].chapterNumber.isKnown() }
// Grouped by all but their numbers, so that the nth number of each name means the same thing
.groupBy { names[it].replace(anyNumber, "#") }
.values
// Fewer names could share a number by chance
.filter { it.size >= 3 }
.forEach { group ->
val first = matches[group.first()]
val (sharedSlots, otherSlots) = first.indices.partition { slot ->
group.all { matches[it][slot].value == first[slot].value }
}
// Something has to be left to tell the chapters apart
if (otherSlots.isEmpty()) return@forEach
// Parts of a chapter share its number, e.g. the 5 of "Chapter 5 Part 1" and "Chapter 5 Part 2"
val removableSlots = sharedSlots.filterNot { slot ->
chapterPrefix.containsMatchIn(names[group.first()].substring(0, first[slot].range.first))
}
group.forEach { index -> sharedNumbers[index] = removableSlots.map { matches[index][it].range } }
}
numberMatch.count() > 1 -> {
// Remove unwanted tags.
unwanted.replace(cleanChapterName, "").let { name ->
// Check base case ch.xx
basic.find(name)?.let { return getChapterNumberFromMatch(it) }
return sharedNumbers
}
// need to find again first number might already removed
number.find(name)?.let { return getChapterNumberFromMatch(it) }
/**
* Numbers runs of chapters without a number after the chapters around them, only within a volume since chapter
* numbers may start over in the next one:
* - between two chapters, the missing whole numbers when they fit, e.g. 2, ?, 4 -R> 3, or else 10, ?, 11 -R> 10.5
* - after the newest chapter, only bonus chapters, e.g. 100, Afterword -R> 100.5
* - before the oldest chapter, only a prologue, which gets 0, and volume 0 when the chapter after it has a volume
*/
private fun fillChapterNumbers(
names: List<String>,
chapterNumbers: Array<RecognizedNumber?>,
volumes: Array<Double?>,
) {
chapterNumbers.nullRuns().forEach { run ->
// A -2 from the source says nothing about where a chapter is, so it can't be numbered after
val newer = chapterNumbers.getOrNull(run.first - 1)?.takeIf { it.value >= 0 }
val older = chapterNumbers.getOrNull(run.last + 1)?.takeIf { it.value >= 0 }
val olderVolume = volumes.getOrNull(run.last + 1)
val bonuses = run.count { bonus.containsMatchIn(names[it]) }
when {
newer != null && older != null -> {
val isSameVolume = volumes[run.first - 1] == olderVolume && volumes.isWithinVolume(run, olderVolume)
// Out of order chapters tell nothing about the ones between them
if (newer.value <= older.value || !isSameVolume) return@forEach
val isWhole = listOf(newer, older).all { it.letter == null && it.value % 1.0 == 0.0 }
// Bonus chapters aren't missing chapters even when they would fit, e.g. 2, Bonus, 4 -R> 2.5
if (bonuses == 0 && isWhole && newer.value - older.value - 1 == run.count().toDouble()) {
run.forEach { chapterNumbers[it] = RecognizedNumber(older.value + (run.last + 1 - it)) }
} else {
// Never past the next whole number, which may still be a chapter to come
chapterNumbers.fillBetween(run, older.value, minOf(newer.value, floor(older.value) + 1))
}
}
older != null -> {
// Newer chapters without a number may as well be announcements
if (bonuses < run.count() || !volumes.isWithinVolume(run, olderVolume)) return@forEach
chapterNumbers.fillBetween(run, older.value, floor(older.value) + 1)
}
// When the first chapter is already 0, that's the prologue
newer != null && newer.value > 0 -> {
run.filter { prologue.containsMatchIn(names[it]) }.forEach {
chapterNumbers[it] = RecognizedNumber(0.0)
if (volumes[it] == null && volumes[run.first - 1] != null) volumes[it] = 0.0
}
}
}
}
// return the first number encountered
return getChapterNumberFromMatch(numberMatch.first())
}
/**
* Check if chapter number is found and return it
* @param match result of regex
* @return chapter number if found else null
* Returns the ranges of consecutive chapters without a number.
*/
private fun getChapterNumberFromMatch(match: MatchResult): Double {
return match.let {
val initial = it.groups[1]?.value?.toDouble()!!
val subChapterDecimal = it.groups[2]?.value
val subChapterAlpha = it.groups[3]?.value
val addition = checkForDecimal(subChapterDecimal, subChapterAlpha)
initial.plus(addition)
private fun Array<RecognizedNumber?>.nullRuns(): List<IntRange> {
val runs = mutableListOf<IntRange>()
var start = 0
while (start < size) {
if (this[start] != null) {
start++
continue
}
val end = (start..<size).firstOrNull { this[it] != null } ?: size
runs.add(start..<end)
start = end
}
return runs
}
/**
* Check for decimal in received strings
* @param decimal decimal value of regex
* @param alpha alpha value of regex
* @return decimal/alpha float value
* Whether the chapters of [run] are in no volume or in [volume], the one of the chapters their numbers come from.
*/
private fun checkForDecimal(decimal: String?, alpha: String?): Double {
if (!decimal.isNullOrEmpty()) {
return decimal.toDouble()
}
if (!alpha.isNullOrEmpty()) {
if (alpha.contains("extra")) {
return 0.99
}
if (alpha.contains("omake")) {
return 0.98
}
if (alpha.contains("special")) {
return 0.97
}
val trimmedAlpha = alpha.trimStart('.')
if (trimmedAlpha.length == 1) {
return parseAlphaPostFix(trimmedAlpha[0])
}
}
return 0.0
private fun Array<Double?>.isWithinVolume(run: IntRange, volume: Double?): Boolean {
return run.all { this[it] == null || this[it] == volume }
}
/**
* x.a -> x.1, x.b -> x.2, etc
* Numbers [run] between [lower] and [upper], the least recent getting the lowest: .5, .6 and so on after the whole
* number, e.g. 10, ?, ?, 11 -R> 10.5, 10.6, or evenly spread when those don't fit.
*/
private fun parseAlphaPostFix(alpha: Char): Double {
val number = alpha.code - ('a'.code - 1)
if (number >= 10) return 0.0
return number / 10.0
private fun Array<RecognizedNumber?>.fillBetween(run: IntRange, lower: Double, upper: Double) {
val tenths = (5..9).map { floor(lower) + it / 10.0 }.filter { it > lower && it < upper }
if (tenths.size >= run.count()) {
// Rounded since tenths can't be exact doubles, e.g. 10 + 0.6
run.reversed().forEachIndexed { index, chapter ->
this[chapter] = RecognizedNumber(round(tenths[index] * 10) / 10)
}
return
}
// Tenths run out after e.g. a 2.9 or past five chapters, and the chapters still have to fit below upper
val step = (upper - lower) / (run.count() + 1)
run.forEach { this[it] = RecognizedNumber(round((lower + step * (run.last + 1 - it)) * 1000) / 1000) }
}
/**
* Returns the chapter number in [name], a chapter name with the manga title removed, or `null` if it has none. A
* part after the chapter number becomes its letter, e.g. Chapter 1.5 Part I -R> 1.5a.
*/
private fun parseName(name: String): RecognizedNumber? {
// The volume can't also be the chapter number
val withoutVolume = volume.replace(name, " ")
val partMatch = part.find(withoutVolume)
val partLetter = partMatch?.groupValues?.get(1)?.let(::parsePart)?.takeIf { it in 1..26 }?.let { 'a' + it - 1 }
if (partMatch != null && partLetter != null) {
parseNumber(withoutVolume.removeRange(partMatch.range))?.let { number ->
// A letter written in the number is what the source numbers the chapter by
return if (number.letter == null) number.copy(letter = partLetter) else number
}
}
// Without another number, e.g. "Part 3", the part is the chapter number
return parseNumber(withoutVolume)
}
private fun parsePart(part: String): Int = part.toIntOrNull() ?: (romanNumerals.indexOf(part) + 1)
/**
* Returns the number after "ch." or else the first one, leaving out version tags when there are several.
*/
private fun parseNumber(name: String): RecognizedNumber? {
val cleanName = name
.trim()
// Some sources write decimals and letters with these, e.g. 300,a and 122-a
.replace(',', '.')
.replace('-', '.')
.replace(unwantedWhiteSpace, "")
val numbers = number.findAll(cleanName).toList()
if (numbers.size > 1) {
val withoutTags = unwanted.replace(cleanName, "")
(basic.find(withoutTags) ?: number.find(withoutTags))?.let { return it.toRecognizedNumber() }
}
return numbers.firstOrNull()?.toRecognizedNumber()
}
/**
* Example: 12.5 -R> 12.5, 12.extra -R> 12.99, 12.a -R> 12a
*/
private fun MatchResult.toRecognizedNumber(): RecognizedNumber {
val (whole, decimal, alpha) = destructured
val value = whole.toDouble() + (decimal.toDoubleOrNull() ?: 0.0)
return when {
decimal.isEmpty() && "extra" in alpha -> RecognizedNumber(value + 0.99)
decimal.isEmpty() && "omake" in alpha -> RecognizedNumber(value + 0.98)
decimal.isEmpty() && "special" in alpha -> RecognizedNumber(value + 0.97)
// Only up to i, like the decimals they used to stand for, since others are rather words like 5v2
else -> RecognizedNumber(value, alpha.trimStart('.').singleOrNull()?.takeIf { it in 'a'..'i' })
}
}
}
@@ -21,8 +21,8 @@ fun getChapterSort(
false -> { c1, c2 -> c1.chapterNumber.compareTo(c2.chapterNumber) }
}
Manga.CHAPTER_SORTING_UPLOAD_DATE -> when (sortDescending) {
true -> { c1, c2 -> c2.dateUpload.compareTo(c1.dateUpload) }
false -> { c1, c2 -> c1.dateUpload.compareTo(c2.dateUpload) }
true -> { c1, c2 -> compareValues(c2.dateUpload, c1.dateUpload) }
false -> { c1, c2 -> compareValues(c1.dateUpload, c2.dateUpload) }
}
Manga.CHAPTER_SORTING_ALPHABET -> when (sortDescending) {
true -> { c1, c2 -> c2.name.compareToWithCollator(c1.name) }
@@ -1,11 +1,11 @@
package tachiyomi.domain.history.model
import java.util.Date
import kotlin.time.Instant
data class History(
val id: Long,
val chapterId: Long,
val readAt: Date?,
val readAt: Instant?,
val readDuration: Long,
) {
companion object {
@@ -1,9 +1,9 @@
package tachiyomi.domain.history.model
import java.util.Date
import kotlin.time.Instant
data class HistoryUpdate(
val chapterId: Long,
val readAt: Date,
val readAt: Instant,
val sessionReadDuration: Long,
)
@@ -1,7 +1,7 @@
package tachiyomi.domain.history.model
import tachiyomi.domain.manga.model.MangaCover
import java.util.Date
import kotlin.time.Instant
data class HistoryWithRelations(
val id: Long,
@@ -9,7 +9,7 @@ data class HistoryWithRelations(
val mangaId: Long,
val title: String,
val chapterNumber: Double,
val readAt: Date?,
val readAt: Instant?,
val readDuration: Long,
val coverData: MangaCover,
)
@@ -15,11 +15,15 @@ interface HistoryRepository {
suspend fun getHistoryByMangaId(mangaId: Long): List<History>
suspend fun getHistoryByMangaIds(mangaIds: List<Long>): Map<Long, List<History>>
suspend fun resetHistory(historyId: Long)
suspend fun resetHistoryByMangaId(mangaId: Long)
suspend fun deleteAllHistory(): Boolean
suspend fun deleteResetHistory()
suspend fun upsertHistory(historyUpdate: HistoryUpdate)
}
@@ -1,6 +1,7 @@
package tachiyomi.domain.library.model
import tachiyomi.domain.manga.model.Manga
import kotlin.time.Instant
data class LibraryManga(
val manga: Manga,
@@ -8,9 +9,9 @@ data class LibraryManga(
val totalChapters: Long,
val readCount: Long,
val bookmarkCount: Long,
val latestUpload: Long,
val chapterFetchedAt: Long,
val lastRead: Long,
val latestUpload: Instant?,
val chapterFetchedAt: Instant?,
val lastRead: Instant?,
) {
val id: Long = manga.id
@@ -219,6 +219,10 @@ class LibraryPreferences(
"disallow_non_ascii_filenames",
false,
)
val enableChapterNameHash: Preference<Boolean> = preferenceStore.getBoolean(
"pref_enable_chapter_name_hash",
false,
)
// endregion
@@ -5,16 +5,13 @@ import kotlinx.datetime.LocalDate
import kotlinx.datetime.LocalDateTime
import kotlinx.datetime.TimeZone
import kotlinx.datetime.atStartOfDayIn
import kotlinx.datetime.daysUntil
import kotlinx.datetime.toInstant
import kotlinx.datetime.toLocalDateTime
import tachiyomi.domain.chapter.interactor.GetChaptersByMangaId
import tachiyomi.domain.chapter.model.Chapter
import tachiyomi.domain.manga.model.Manga
import tachiyomi.domain.manga.model.MangaUpdate
import kotlin.math.absoluteValue
import kotlin.time.Clock
import kotlin.time.Duration.Companion.days
import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.Instant
@Inject
@@ -22,113 +19,51 @@ class FetchInterval(
private val getChaptersByMangaId: GetChaptersByMangaId,
) {
suspend fun toMangaUpdate(
/**
* Works out the entry's fetch interval and the next day to check it for new chapters.
*
* @param foundRelease whether the check that led here found new chapters.
*/
suspend fun withFetchInterval(
manga: Manga,
dateTime: LocalDateTime,
timeZone: TimeZone,
window: Pair<Long, Long>,
window: ClosedRange<Instant>?,
foundRelease: Boolean = false,
): MangaUpdate {
val interval = manga.fetchInterval.takeIf { it < 0 } ?: calculateInterval(
chapters = getChaptersByMangaId.await(manga.id, applyScanlatorFilter = true),
zone = timeZone,
)
val currentWindow = if (window.first == 0L && window.second == 0L) {
getWindow(Clock.System.now().toLocalDateTime(timeZone).date, timeZone)
val today = dateTime.date
val currentWindow = window ?: getWindow(Clock.System.now().toLocalDateTime(timeZone).date, timeZone)
val schedule = if (manga.fetchInterval < 0) {
// The check that found a release is the entry's last update
val lastUpdate = if (foundRelease) today else manga.lastUpdate?.toLocalDateTime(timeZone)?.date
ReleaseSchedule.fixed(-manga.fetchInterval, lastUpdate ?: today)
} else {
window
val chapters = getChaptersByMangaId.await(manga.id, applyScanlatorFilter = true)
ReleaseSchedule.estimate(chapters, timeZone, today)
}
val nextUpdate = calculateNextUpdate(manga, interval, dateTime, timeZone, currentWindow)
return MangaUpdate(id = manga.id, nextUpdate = nextUpdate, fetchInterval = interval)
val expected = manga.nextUpdate
val expectedDay = expected?.toLocalDateTime(timeZone)?.date
// A release on or after the expected day means the next one is counted from it instead
val isAwaitingRelease = expected != null &&
expectedDay != null &&
expected in currentWindow.start..currentWindow.endInclusive + 1.milliseconds &&
(schedule.lastRelease == null || schedule.lastRelease < expectedDay)
val nextUpdate = when {
foundRelease -> schedule.nextCheckAfterRelease(today).atStartOfDayIn(timeZone)
isAwaitingRelease -> expected
else -> schedule.nextCheck(today).atStartOfDayIn(timeZone)
}
return MangaUpdate(manga.id) {
this.nextUpdate = nextUpdate
fetchInterval = if (manga.fetchInterval < 0) manga.fetchInterval else schedule.interval
}
}
fun getWindow(localDateTime: LocalDate, timeZone: TimeZone): Pair<Long, Long> {
fun getWindow(localDateTime: LocalDate, timeZone: TimeZone): ClosedRange<Instant> {
val today = localDateTime.atStartOfDayIn(timeZone)
val lowerBound = today - GRACE_PERIOD.days
val upperBound = today + GRACE_PERIOD.days
return Pair(lowerBound.toEpochMilliseconds(), upperBound.toEpochMilliseconds())
}
internal fun calculateInterval(chapters: List<Chapter>, zone: TimeZone): Int {
val chapterWindow = if (chapters.size <= 8) 3 else 10
val uploadDates = chapters.asSequence()
.filter { it.dateUpload > 0L }
.sortedByDescending { it.dateUpload }
.map {
Instant.fromEpochMilliseconds(it.dateUpload)
.toLocalDateTime(zone)
.date
.atStartOfDayIn(zone)
}
.distinct()
.take(chapterWindow)
.toList()
val fetchDates = chapters.asSequence()
.sortedByDescending { it.dateFetch }
.map {
Instant.fromEpochMilliseconds(it.dateFetch)
.toLocalDateTime(zone)
.date
.atStartOfDayIn(zone)
}
.distinct()
.take(chapterWindow)
.toList()
val interval = when {
// Enough upload date from source
uploadDates.size >= 3 -> {
val ranges = uploadDates.windowed(2).map { x -> x[1].daysUntil(x[0], zone) }.sorted()
ranges[(ranges.size - 1) / 2]
}
// Enough fetch date from client
fetchDates.size >= 3 -> {
val ranges = fetchDates.windowed(2).map { x -> x[1].daysUntil(x[0], zone) }.sorted()
ranges[(ranges.size - 1) / 2]
}
// Default to 7 days
else -> 7
}
return interval.coerceIn(1, MAX_INTERVAL)
}
private fun calculateNextUpdate(
manga: Manga,
interval: Int,
dateTime: LocalDateTime,
timeZone: TimeZone,
window: Pair<Long, Long>,
): Long {
if (manga.nextUpdate in window.first.rangeTo(window.second + 1)) {
return manga.nextUpdate
}
val instant = if (manga.lastUpdate > 0) Instant.fromEpochMilliseconds(manga.lastUpdate) else Clock.System.now()
val latestDate = instant.toLocalDateTime(timeZone).date.atStartOfDayIn(timeZone)
val daysSinceLatest = (dateTime.toInstant(timeZone) - latestDate).inWholeDays
val cycle = daysSinceLatest.floorDiv(
interval.absoluteValue.takeIf { interval < 0 }
?: increaseInterval(interval, daysSinceLatest, increaseWhenOver = 10),
)
val offsetDays = ((cycle + 1) * interval.absoluteValue.toLong()).days
return latestDate.plus(offsetDays).toEpochMilliseconds()
}
private fun increaseInterval(delta: Int, daysSinceLatest: Long, increaseWhenOver: Int): Int {
if (delta >= MAX_INTERVAL) return MAX_INTERVAL
// double delta again if missed more than 9 check in new delta
val cycle = daysSinceLatest.floorDiv(delta) + 1
return if (cycle > increaseWhenOver) {
increaseInterval(delta * 2, daysSinceLatest, increaseWhenOver)
} else {
delta
}
return (today - GRACE_PERIOD.days)..(today + GRACE_PERIOD.days)
}
companion object {
@@ -0,0 +1,212 @@
package tachiyomi.domain.manga.interactor
import kotlinx.datetime.DatePeriod
import kotlinx.datetime.DayOfWeek
import kotlinx.datetime.LocalDate
import kotlinx.datetime.daysUntil
import kotlinx.datetime.isoDayNumber
import kotlinx.datetime.minus
import kotlinx.datetime.plus
import kotlin.math.abs
/** The days an entry is expected to release on. */
internal sealed interface ReleasePattern {
/** The first expected release day after [date]. */
fun nextAfter(date: LocalDate): LocalDate
/** The last expected release day before [date]. */
fun previousBefore(date: LocalDate): LocalDate
/** The expected release day closest to [date], which a release on [date] stands in for. */
fun nearest(date: LocalDate): LocalDate {
val before = previousBefore(date.plus(DatePeriod(days = 1)))
val after = nextAfter(date.minus(DatePeriod(days = 1)))
// A release halfway between is taken as late, so the next check comes sooner
return if (before.daysUntil(date) <= date.daysUntil(after)) before else after
}
/** Releases every [days] days, in step with [onSchedule]. */
data class EveryDays(val days: Int, val onSchedule: LocalDate) : ReleasePattern {
override fun nextAfter(date: LocalDate): LocalDate {
return date.plus(DatePeriod(days = days - onSchedule.daysUntil(date).mod(days)))
}
override fun previousBefore(date: LocalDate): LocalDate {
return date.minus(DatePeriod(days = (onSchedule.daysUntil(date) - 1).mod(days) + 1))
}
}
data class Weekdays(val days: Set<DayOfWeek>) : ReleasePattern {
override fun nextAfter(date: LocalDate): LocalDate {
return generateSequence(date.plus(DatePeriod(days = 1))) { it.plus(DatePeriod(days = 1)) }
.first { it.dayOfWeek in days }
}
override fun previousBefore(date: LocalDate): LocalDate {
return generateSequence(date.minus(DatePeriod(days = 1))) { it.minus(DatePeriod(days = 1)) }
.first { it.dayOfWeek in days }
}
// Release days are a day or two apart, so a release between them is more likely an extra than an early one
override fun nearest(date: LocalDate) = date
}
/** Releases for [length] days in a row, every [every] days in step with [onSchedule]. */
data class Batches(val every: Int, val onSchedule: LocalDate, val length: Int) : ReleasePattern {
override fun nextAfter(date: LocalDate): LocalDate {
val offset = onSchedule.daysUntil(date).mod(every)
return date.plus(DatePeriod(days = if (offset + 1 < length) 1 else every - offset))
}
override fun previousBefore(date: LocalDate): LocalDate {
val dayBefore = date.minus(DatePeriod(days = 1))
val offset = onSchedule.daysUntil(dayBefore).mod(every)
return dayBefore.minus(DatePeriod(days = (offset - length + 1).coerceAtLeast(0)))
}
}
/** A pattern that repeats every month. */
sealed interface Monthly : ReleasePattern {
/** The release days in the month starting on [firstOfMonth]. */
fun daysIn(firstOfMonth: LocalDate): List<LocalDate>
override fun nextAfter(date: LocalDate): LocalDate {
return generateSequence(date.firstOfMonth()) { it.plus(DatePeriod(months = 1)) }
.flatMap { daysIn(it).sorted() }
.first { it > date }
}
override fun previousBefore(date: LocalDate): LocalDate {
return generateSequence(date.firstOfMonth()) { it.minus(DatePeriod(months = 1)) }
.flatMap { daysIn(it).sortedDescending() }
.first { it < date }
}
}
/** Releases on [days] of every month, or a month's last day for those past it. */
data class DaysOfMonth(val days: Set<Int>) : Monthly {
override fun daysIn(firstOfMonth: LocalDate): List<LocalDate> {
val lastDay = firstOfMonth.lastOfMonth().day
return days.map { firstOfMonth.plus(DatePeriod(days = it.coerceAtMost(lastDay) - 1)) }.distinct()
}
}
/** Releases on [dayOfWeek] of the [week]th week of every month, or its last week for [LAST_WEEK]. */
data class WeekdayOfMonth(val week: Int, val dayOfWeek: DayOfWeek) : Monthly {
override fun daysIn(firstOfMonth: LocalDate): List<LocalDate> {
if (week == LAST_WEEK) {
val lastOfMonth = firstOfMonth.lastOfMonth()
val daysBack = (lastOfMonth.dayOfWeek.isoDayNumber - dayOfWeek.isoDayNumber).mod(7)
return listOf(lastOfMonth.minus(DatePeriod(days = daysBack)))
}
val daysAhead = (dayOfWeek.isoDayNumber - firstOfMonth.dayOfWeek.isoDayNumber).mod(7)
return listOf(firstOfMonth.plus(DatePeriod(days = daysAhead + (week - 1) * 7)))
}
}
companion object {
const val LAST_WEEK = -1
private const val MIN_RELEASES = 4
private const val MAX_WEEKDAYS = 3
private const val COVERAGE = 0.8
private const val DAY_OF_MONTH_TOLERANCE = 3
private val MONTHLY_GAPS = 26..35
private val TWICE_MONTHLY_GAPS = 12..18
private const val BATCH_HISTORY = 40
private const val BATCH_GAP = 2
private const val BATCHES = 6
private const val MIN_BATCHES = 3
/**
* Finds a calendar pattern a plain day interval can't follow in [days], newest first: a few fixed weekdays,
* days or a weekday of the month, or releases in batches, which can take longer than [days] to show up in
* [history]. They all need a few releases to be told apart from chance.
*/
fun detect(days: List<LocalDate>, history: List<LocalDate>, medianGap: Int): ReleasePattern? {
if (days.size < MIN_RELEASES) return null
// Batches come first, since their days can look like a few weekdays or a monthly schedule
return batches(history) ?: when (medianGap) {
in MONTHLY_GAPS -> monthly(days)
in TWICE_MONTHLY_GAPS -> twiceMonthly(days)
else -> weekdays(days, medianGap)
}
}
private fun monthly(days: List<LocalDate>): ReleasePattern? {
val daysOfMonth = days.map { it.day }
return when {
daysOfMonth.max() - daysOfMonth.min() <= DAY_OF_MONTH_TOLERANCE -> DaysOfMonth(
setOf(daysOfMonth.median()),
)
// The last day of the month moves between the 28th and the 31st
daysOfMonth.all { it >= 28 } -> DaysOfMonth(setOf(31))
else -> weekdayOfMonth(days)
}
}
private fun weekdayOfMonth(days: List<LocalDate>): ReleasePattern? {
val dayOfWeek = days.mostCommon { it.dayOfWeek } ?: return null
// A fifth week isn't in every month, so those days only count as the last week
val week = days.mostCommon { (it.day - 1) / 7 + 1 }?.takeIf { it < 5 }
if (week != null) return WeekdayOfMonth(week, dayOfWeek)
val isLastWeek = days.count { it.day + 7 > it.lastOfMonth().day } >= days.size * COVERAGE
return if (isLastWeek) WeekdayOfMonth(LAST_WEEK, dayOfWeek) else null
}
private fun twiceMonthly(days: List<LocalDate>): ReleasePattern? {
val daysOfMonth = days.map { it.day }.sorted()
val split = (1..<daysOfMonth.size).maxBy { daysOfMonth[it] - daysOfMonth[it - 1] }
val halves = listOf(daysOfMonth.subList(0, split), daysOfMonth.subList(split, daysOfMonth.size))
if (halves.any { it.size < 2 || it.last() - it.first() > DAY_OF_MONTH_TOLERANCE }) return null
return DaysOfMonth(halves.map { it.median() }.toSet())
}
private fun weekdays(days: List<LocalDate>, medianGap: Int): ReleasePattern? {
val byFrequency = days.groupingBy { it.dayOfWeek }.eachCount().entries.sortedByDescending { it.value }
val weekdays = mutableSetOf<DayOfWeek>()
var covered = 0
for ((dayOfWeek, count) in byFrequency) {
if (covered >= days.size * COVERAGE) break
weekdays += dayOfWeek
covered += count
}
// One weekday is a weekly interval, and an irregular schedule spreads over most of the week
if (weekdays.size !in 2..MAX_WEEKDAYS || covered < days.size * COVERAGE) return null
// Releasing on several weekdays means more than one release a week
if (medianGap > 7 / weekdays.size + 1) return null
return Weekdays(weekdays)
}
/** Runs of releases a day or two apart, starting at a steady interval and mostly longer than a day. */
private fun batches(history: List<LocalDate>): ReleasePattern? {
val runs = mutableListOf<MutableList<LocalDate>>()
for (day in history.take(BATCH_HISTORY).asReversed()) {
val run = runs.lastOrNull()
if (run != null && run.last().daysUntil(day) <= BATCH_GAP) run += day else runs += mutableListOf(day)
}
val recent = runs.takeLast(BATCHES)
if (recent.size < MIN_BATCHES || recent.count { it.size > 1 } * 2 < recent.size) return null
val gaps = recent.zipWithNext { older, newer -> older.first().daysUntil(newer.first()) }
val every = gaps.median()
val length = recent.map { it.first().daysUntil(it.last()) + 1 }.median()
if (every < length * 2 || gaps.any { abs(it - every) > every / 4 }) return null
return Batches(every, recent.last().first(), length)
}
private fun List<Int>.median() = sorted()[(size - 1) / 2]
/** The most common key of [selector], if at least [COVERAGE] of the days share it. */
private fun <T> List<LocalDate>.mostCommon(selector: (LocalDate) -> T): T? {
val (key, count) = groupingBy(selector).eachCount().maxBy { it.value }
return key.takeIf { count >= size * COVERAGE }
}
}
}
private fun LocalDate.firstOfMonth() = LocalDate(year, month, 1)
private fun LocalDate.lastOfMonth() = firstOfMonth().plus(DatePeriod(months = 1)).minus(DatePeriod(days = 1))
@@ -0,0 +1,151 @@
package tachiyomi.domain.manga.interactor
import kotlinx.datetime.DatePeriod
import kotlinx.datetime.LocalDate
import kotlinx.datetime.TimeZone
import kotlinx.datetime.daysUntil
import kotlinx.datetime.plus
import kotlinx.datetime.toLocalDateTime
import tachiyomi.domain.chapter.model.Chapter
import kotlin.time.Instant
/** How often an entry releases, and so when to check it for the next release. */
internal data class ReleaseSchedule(
/** Days between releases, which is what the fetch interval shows. */
val interval: Int,
/** The most recent release day, which the next ones are counted from. */
val lastRelease: LocalDate?,
val pattern: ReleasePattern,
/** Days past the expected day a release can still come, from how late recent ones were. */
val lateness: Int = 0,
/** Whether checks spread out once releases stop coming, which a schedule set by the user doesn't. */
val backsOff: Boolean = true,
/** Lengths in days of earlier breaks in releases, which a break like a season's end is expected to last again. */
val pastBreaks: List<Int> = emptyList(),
) {
/** The next day to check for a release, as of [today]. Today counts, so a due release keeps being checked for. */
fun nextCheck(today: LocalDate): LocalDate {
val anchor = lastRelease ?: today
val daysSinceRelease = anchor.daysUntil(today)
val isOnBreak = backsOff && daysSinceRelease > interval * HIATUS_CYCLES
// Around when earlier breaks ended, this one likely ends too, so the schedule is followed again
if (isOnBreak && pastBreaks.none { daysSinceRelease in it - interval..it + interval }) {
// Past a few missed releases the schedule no longer holds, so checks spread out until it resumes
val backedOff = backOff(daysSinceRelease)
val next = anchor.plus(DatePeriod(days = ceilDiv(daysSinceRelease, backedOff) * backedOff))
val breakEnd = pastBreaks.map {
anchor.plus(DatePeriod(days = it - interval))
}.filter { it > today }.minOrNull()
return minOf(next, breakEnd ?: next)
}
val due = pattern.nextAfter(pattern.nearest(anchor))
if (today in due..due.plus(DatePeriod(days = lateness))) return today
return generateSequence(due, pattern::nextAfter).first { it >= today }.cappedFrom(today)
}
/**
* The next day to check after a check [today] found a release.
*
* The release is dated by its newest chapter, since a check can turn up one that came out on an earlier day. It
* stands in for the expected day nearest to it, so an early, late or extra release leaves the schedule in place.
*/
fun nextCheckAfterRelease(today: LocalDate): LocalDate {
val release = lastRelease?.coerceAtMost(today) ?: today
// What was found is out already, so checking again today would only find it again
val next = pattern.nextAfter(pattern.nearest(release))
return generateSequence(next, pattern::nextAfter).first { it > today }.cappedFrom(today)
}
// Months and batches can be further apart than a check should ever wait
private fun LocalDate.cappedFrom(today: LocalDate): LocalDate {
return coerceAtMost(today.plus(DatePeriod(days = FetchInterval.MAX_INTERVAL)))
}
private fun backOff(daysSinceRelease: Int): Int {
var backedOff = interval
while (backedOff < FetchInterval.MAX_INTERVAL && daysSinceRelease > backedOff * HIATUS_CYCLES) {
backedOff *= 2
}
return backedOff.coerceAtMost(FetchInterval.MAX_INTERVAL)
}
private fun ceilDiv(a: Int, b: Int) = -Math.floorDiv(-a, b)
companion object {
private const val DEFAULT_INTERVAL = 7
private const val HIATUS_CYCLES = 4
private const val MIN_RELEASES = 3
private const val FEW_CHAPTERS = 8
private const val FEW_CHAPTERS_RELEASES = 3
private const val RECENT_RELEASES = 10
private const val PHASE_RELEASES = 5
/** A schedule the user set to every [interval] days, counted from [lastRelease]. */
fun fixed(interval: Int, lastRelease: LocalDate): ReleaseSchedule {
val pattern = ReleasePattern.EveryDays(interval, onSchedule = lastRelease)
return ReleaseSchedule(interval, lastRelease, pattern, backsOff = false)
}
/** Estimates the schedule from the recent release days of [chapters], by upload date or else fetch date. */
fun estimate(chapters: List<Chapter>, zone: TimeZone, today: LocalDate): ReleaseSchedule {
val recentReleases = if (chapters.size <= FEW_CHAPTERS) FEW_CHAPTERS_RELEASES else RECENT_RELEASES
// Sources that date chapters ahead of their release would otherwise pull the schedule into the future
val latestPlausibleDay = today.plus(DatePeriod(days = 1))
fun releaseDays(dates: Sequence<Instant>): List<LocalDate> {
return dates
.map { it.toLocalDateTime(zone).date }
.filter { it <= latestPlausibleDay }
.distinct()
.sortedDescending()
.toList()
}
val uploadDays = releaseDays(chapters.asSequence().mapNotNull { it.dateUpload })
val fetchDays = releaseDays(chapters.asSequence().map { it.dateFetch })
val history = uploadDays.takeIf { it.size >= MIN_RELEASES }
?: fetchDays.takeIf { it.size >= MIN_RELEASES }
?: run {
val lastRelease = uploadDays.firstOrNull() ?: fetchDays.firstOrNull()
val pattern = ReleasePattern.EveryDays(DEFAULT_INTERVAL, onSchedule = lastRelease ?: today)
return ReleaseSchedule(DEFAULT_INTERVAL, lastRelease, pattern)
}
val days = history.take(recentReleases)
val gaps = days.zipWithNext { newer, older -> older.daysUntil(newer) }.sorted()
val medianGap = gaps[(gaps.size - 1) / 2]
val detected = ReleasePattern.detect(days, history, medianGap)
// Within a batch releases are a day apart, but it's the batches that come every so often
val interval = ((detected as? ReleasePattern.Batches)?.every ?: medianGap)
.coerceIn(1, FetchInterval.MAX_INTERVAL)
return ReleaseSchedule(
interval = interval,
lastRelease = days.first(),
pattern = detected
?: ReleasePattern.EveryDays(
interval,
onSchedule = medianPhase(days.take(PHASE_RELEASES), interval),
),
lateness = (gaps.last() - interval).coerceIn(0, interval / 2),
pastBreaks = history
.zipWithNext { newer, older -> older.daysUntil(newer) }
.filter { it > interval * HIATUS_CYCLES },
)
}
/**
* The release in [days], newest first, that the others land closest to in step with [interval], so one early
* or late release doesn't shift the schedule. Ties go to the newest, which follows a schedule that moved.
*/
private fun medianPhase(days: List<LocalDate>, interval: Int): LocalDate {
fun offset(
from: LocalDate,
to: LocalDate,
) = from.daysUntil(to).mod(interval).let { minOf(it, interval - it) }
val phase = days.minBy { candidate -> days.sumOf { offset(candidate, it) } }
val onPhase = days.count { offset(phase, it) == 0 }
return if (onPhase * 5 >= days.size * 3) phase else days.first()
}
}
}
@@ -12,37 +12,33 @@ class SetMangaChapterFlags(
suspend fun awaitSetDownloadedFilter(manga: Manga, flag: Long): Boolean {
return mangaRepository.update(
MangaUpdate(
id = manga.id,
chapterFlags = manga.chapterFlags.setFlag(flag, Manga.CHAPTER_DOWNLOADED_MASK),
),
MangaUpdate(manga.id) {
chapterFlags = manga.chapterFlags.setFlag(flag, Manga.CHAPTER_DOWNLOADED_MASK)
},
)
}
suspend fun awaitSetUnreadFilter(manga: Manga, flag: Long): Boolean {
return mangaRepository.update(
MangaUpdate(
id = manga.id,
chapterFlags = manga.chapterFlags.setFlag(flag, Manga.CHAPTER_UNREAD_MASK),
),
MangaUpdate(manga.id) {
chapterFlags = manga.chapterFlags.setFlag(flag, Manga.CHAPTER_UNREAD_MASK)
},
)
}
suspend fun awaitSetBookmarkFilter(manga: Manga, flag: Long): Boolean {
return mangaRepository.update(
MangaUpdate(
id = manga.id,
chapterFlags = manga.chapterFlags.setFlag(flag, Manga.CHAPTER_BOOKMARKED_MASK),
),
MangaUpdate(manga.id) {
chapterFlags = manga.chapterFlags.setFlag(flag, Manga.CHAPTER_BOOKMARKED_MASK)
},
)
}
suspend fun awaitSetDisplayMode(manga: Manga, flag: Long): Boolean {
return mangaRepository.update(
MangaUpdate(
id = manga.id,
chapterFlags = manga.chapterFlags.setFlag(flag, Manga.CHAPTER_DISPLAY_MASK),
),
MangaUpdate(manga.id) {
chapterFlags = manga.chapterFlags.setFlag(flag, Manga.CHAPTER_DISPLAY_MASK)
},
)
}
@@ -64,15 +60,14 @@ class SetMangaChapterFlags(
}
}
return mangaRepository.update(
MangaUpdate(
id = manga.id,
chapterFlags = newFlags,
),
MangaUpdate(manga.id) {
chapterFlags = newFlags
},
)
}
suspend fun awaitSetAllFlags(
mangaId: Long,
mangaIds: List<Long>,
unreadFilter: Long,
downloadedFilter: Long,
bookmarkedFilter: Long,
@@ -80,17 +75,13 @@ class SetMangaChapterFlags(
sortingDirection: Long,
displayMode: Long,
): Boolean {
return mangaRepository.update(
MangaUpdate(
id = mangaId,
chapterFlags = 0L.setFlag(unreadFilter, Manga.CHAPTER_UNREAD_MASK)
.setFlag(downloadedFilter, Manga.CHAPTER_DOWNLOADED_MASK)
.setFlag(bookmarkedFilter, Manga.CHAPTER_BOOKMARKED_MASK)
.setFlag(sortingMode, Manga.CHAPTER_SORTING_MASK)
.setFlag(sortingDirection, Manga.CHAPTER_SORT_DIR_MASK)
.setFlag(displayMode, Manga.CHAPTER_DISPLAY_MASK),
),
)
val flags = 0L.setFlag(unreadFilter, Manga.CHAPTER_UNREAD_MASK)
.setFlag(downloadedFilter, Manga.CHAPTER_DOWNLOADED_MASK)
.setFlag(bookmarkedFilter, Manga.CHAPTER_BOOKMARKED_MASK)
.setFlag(sortingMode, Manga.CHAPTER_SORTING_MASK)
.setFlag(sortingDirection, Manga.CHAPTER_SORT_DIR_MASK)
.setFlag(displayMode, Manga.CHAPTER_DISPLAY_MASK)
return mangaRepository.updateAll(mangaIds.map { MangaUpdate(it) { chapterFlags = flags } })
}
private fun Long.setFlag(flag: Long, mask: Long): Long {
@@ -11,10 +11,9 @@ class UpdateMangaNotes(
suspend operator fun invoke(mangaId: Long, notes: String): Boolean {
return mangaRepository.update(
MangaUpdate(
id = mangaId,
notes = notes,
),
MangaUpdate(mangaId) {
this.notes = notes
},
)
}
}
@@ -19,14 +19,13 @@ import java.io.Serializable as JavaSerializable
data class Manga(
val id: Long,
val source: Long,
val favorite: Boolean,
val lastUpdate: Long,
val nextUpdate: Long,
val lastUpdate: Instant?,
val nextUpdate: Instant?,
val fetchInterval: Int,
val dateAdded: Long,
val favoriteAt: Instant?,
val viewerFlags: Long,
val chapterFlags: Long,
val coverLastModified: Long,
val coverLastModified: Instant?,
val url: String,
val title: String,
val artist: String?,
@@ -37,17 +36,15 @@ data class Manga(
val thumbnailUrl: String?,
val updateStrategy: UpdateStrategy,
val initialized: Boolean,
val lastModifiedAt: Long,
val favoriteModifiedAt: Long?,
val version: Long,
val notes: String,
val memo: JsonObject,
) : JavaSerializable {
val favorite: Boolean
get() = favoriteAt != null
val expectedNextUpdate: Instant?
get() = nextUpdate
.takeIf { status != SManga.COMPLETED.toLong() }
?.let { Instant.fromEpochMilliseconds(it) }
get() = nextUpdate?.takeIf { status != SManga.COMPLETED.toLong() }
val sorting: Long
get() = chapterFlags and CHAPTER_SORTING_MASK
@@ -112,19 +109,23 @@ data class Manga(
const val CHAPTER_DISPLAY_NUMBER = 0x00100000L
const val CHAPTER_DISPLAY_MASK = 0x00100000L
/**
* [favoriteAt] of an entry added to the library before the time it was added was recorded.
*/
val UNKNOWN_FAVORITE_AT = Instant.fromEpochMilliseconds(0)
fun create() = Manga(
id = -1L,
url = "",
title = "",
source = -1L,
favorite = false,
lastUpdate = 0L,
nextUpdate = 0L,
lastUpdate = null,
nextUpdate = null,
fetchInterval = 0,
dateAdded = 0L,
favoriteAt = null,
viewerFlags = 0L,
chapterFlags = 0L,
coverLastModified = 0L,
coverLastModified = null,
artist = null,
author = null,
description = null,
@@ -133,9 +134,6 @@ data class Manga(
thumbnailUrl = null,
updateStrategy = UpdateStrategy.ALWAYS_UPDATE,
initialized = false,
lastModifiedAt = 0L,
favoriteModifiedAt = null,
version = 0L,
notes = "",
memo = JsonObject.EMPTY,
)
@@ -1,5 +1,7 @@
package tachiyomi.domain.manga.model
import kotlin.time.Instant
/**
* Contains the required data for MangaCoverFetcher
*/
@@ -8,7 +10,7 @@ data class MangaCover(
val sourceId: Long,
val isMangaFavorite: Boolean,
val url: String?,
val lastModified: Long,
val lastModified: Instant?,
)
fun Manga.asMangaCover(): MangaCover {
@@ -0,0 +1,20 @@
package tachiyomi.domain.manga.model
import eu.kanade.tachiyomi.source.model.UpdateStrategy
import kotlinx.serialization.json.JsonObject
import kotlin.time.Instant
data class MangaRemoteUpdate(
val id: Long,
val title: String?,
val author: String?,
val artist: String?,
val description: String?,
val genre: List<String>?,
val status: Long,
val thumbnailUrl: String?,
val updateStrategy: UpdateStrategy,
val memo: JsonObject,
val initialized: Boolean,
val coverLastModified: Instant?,
)
@@ -1,58 +1,19 @@
package tachiyomi.domain.manga.model
import eu.kanade.tachiyomi.source.model.UpdateStrategy
import kotlinx.serialization.json.JsonObject
import mihon.domain.common.PartialUpdate
import kotlin.time.Instant
data class MangaUpdate(
val id: Long,
val source: Long? = null,
val favorite: Boolean? = null,
val lastUpdate: Long? = null,
val nextUpdate: Long? = null,
val fetchInterval: Int? = null,
val dateAdded: Long? = null,
val viewerFlags: Long? = null,
val chapterFlags: Long? = null,
val coverLastModified: Long? = null,
val url: String? = null,
val title: String? = null,
val artist: String? = null,
val author: String? = null,
val description: String? = null,
val genre: List<String>? = null,
val status: Long? = null,
val thumbnailUrl: String? = null,
val updateStrategy: UpdateStrategy? = null,
val initialized: Boolean? = null,
val version: Long? = null,
val notes: String? = null,
val memo: JsonObject? = null,
)
class MangaUpdate(val id: Long, block: MangaUpdate.() -> Unit) : PartialUpdate() {
var favoriteAt: Instant? by field(null)
var lastUpdate: Instant? by field(null)
var nextUpdate: Instant? by field(null)
var fetchInterval: Int? by field(null)
var viewerFlags: Long? by field(null)
var chapterFlags: Long? by field(null)
var coverLastModified: Instant? by field(null)
var notes: String? by field(null)
fun Manga.toMangaUpdate(): MangaUpdate {
return MangaUpdate(
id = id,
source = source,
favorite = favorite,
lastUpdate = lastUpdate,
nextUpdate = nextUpdate,
fetchInterval = fetchInterval,
dateAdded = dateAdded,
viewerFlags = viewerFlags,
chapterFlags = chapterFlags,
coverLastModified = coverLastModified,
url = url,
title = title,
artist = artist,
author = author,
description = description,
genre = genre,
status = status,
thumbnailUrl = thumbnailUrl,
updateStrategy = updateStrategy,
initialized = initialized,
version = version,
notes = notes,
memo = memo,
)
init {
block()
}
}
@@ -3,6 +3,7 @@ package tachiyomi.domain.manga.repository
import kotlinx.coroutines.flow.Flow
import tachiyomi.domain.library.model.LibraryManga
import tachiyomi.domain.manga.model.Manga
import tachiyomi.domain.manga.model.MangaRemoteUpdate
import tachiyomi.domain.manga.model.MangaUpdate
import tachiyomi.domain.manga.model.MangaWithChapterCount
@@ -36,10 +37,22 @@ interface MangaRepository {
suspend fun resetViewerFlags(): Boolean
suspend fun deleteNonLibraryManga(sourceIds: List<Long>, keepReadManga: Boolean)
suspend fun setMangaCategories(mangaId: Long, categoryIds: List<Long>)
suspend fun getExcludedScanlators(mangaId: Long): Set<String>
suspend fun getExcludedScanlatorsByMangaIds(mangaIds: List<Long>): Map<Long, List<String>>
fun getExcludedScanlatorsAsFlow(mangaId: Long): Flow<Set<String>>
suspend fun setExcludedScanlators(mangaId: Long, excludedScanlators: Set<String>)
suspend fun update(update: MangaUpdate): Boolean
suspend fun updateRemote(update: MangaRemoteUpdate): Boolean
suspend fun updateAll(mangaUpdates: List<MangaUpdate>): Boolean
suspend fun insertNetworkManga(manga: List<Manga>): List<Manga>
@@ -30,30 +30,38 @@ class GetApplicationRelease(
versionName: String,
versionTag: String,
): Boolean {
// Removes prefixes like "r" or "v"
val newVersion = versionTag.replace("[^\\d.]".toRegex(), "")
return if (isNightly) {
// Nightly builds: based on releases in "mihonapp/mihon-preview" repo
// tagged as something like "r1234"
newVersion.toInt() > commitCount
} else {
// Release builds: based on releases in "mihonapp/mihon" repo
// tagged as something like "v0.1.2"
val oldVersion = versionName.replace("[^\\d.]".toRegex(), "")
val newSemVer = newVersion.split(".").map { it.toInt() }
val oldSemVer = oldVersion.split(".").map { it.toInt() }
oldSemVer.mapIndexed { index, i ->
if (newSemVer[index] > i) {
return true
}
}
false
if (isNightly) {
val nightlyRevision = versionTag.substringAfterLast("r", "").toIntOrNull()
return nightlyRevision != null && nightlyRevision > commitCount
}
val current = parseReleaseVersion(versionName) ?: return false
val latest = parseReleaseVersion(versionTag) ?: return false
val componentCount = maxOf(current.components.size, latest.components.size)
repeat(componentCount) { index ->
val oldPart = current.components.getOrElse(index) { 0 }
val newPart = latest.components.getOrElse(index) { 0 }
if (newPart != oldPart) return newPart > oldPart
}
return latest.bookOasisRevision > current.bookOasisRevision
}
private fun parseReleaseVersion(value: String): ReleaseVersion? {
val match = RELEASE_VERSION_REGEX.find(value) ?: return null
val components = match.groupValues[1]
.split(".")
.map { it.toIntOrNull() ?: return null }
val revision = match.groupValues[2].toIntOrNull() ?: 0
return ReleaseVersion(components, revision)
}
private data class ReleaseVersion(
val components: List<Int>,
val bookOasisRevision: Int,
)
data class Arguments(
val isFoss: Boolean,
val isNightly: Boolean,
@@ -63,6 +71,11 @@ class GetApplicationRelease(
val forceCheck: Boolean = false,
)
companion object {
private val RELEASE_VERSION_REGEX =
"""(?:^|v)(\d+(?:\.\d+)+)(?:-bookoasis-r(\d+))?""".toRegex(RegexOption.IGNORE_CASE)
}
sealed interface Result {
data class NewUpdate(val release: Release) : Result
data object NoNewUpdate : Result
@@ -8,7 +8,7 @@ import eu.kanade.tachiyomi.source.model.SChapter
import eu.kanade.tachiyomi.source.model.SManga
import eu.kanade.tachiyomi.source.model.SMangaUpdate
class StubSource(
data class StubSource(
override val id: Long,
override val lang: String,
override val name: String,
@@ -8,7 +8,6 @@ import dev.zacsweers.metro.Inject
import dev.zacsweers.metro.SingleIn
import eu.kanade.tachiyomi.util.storage.DiskUtil
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.distinctUntilChanged
@@ -17,17 +16,25 @@ import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.receiveAsFlow
import kotlinx.coroutines.flow.shareIn
import mihon.core.metro.AppCoroutineScope
import java.util.concurrent.ConcurrentHashMap
@Inject
@SingleIn(AppScope::class)
class StorageManager(
@AppCoroutineScope private val scope: CoroutineScope,
private val context: Context,
storagePreferences: StoragePreferences,
) {
private val scope = CoroutineScope(Dispatchers.IO)
@Volatile
private var baseDirLazy: Lazy<UniFile?> = lazy { getBaseDir(storagePreferences.baseStorageDirectory.get()) }
private val baseDir: UniFile?
get() = baseDirLazy.value
private var baseDir: UniFile? = getBaseDir(storagePreferences.baseStorageDirectory.get())
// On SAF, createDirectory lists the whole base directory to find an existing child, so a resolved child
// is reused while it's under the current base directory and still exists; it can be deleted outside the app
private val childDirectories = ConcurrentHashMap<String, Pair<UniFile, UniFile>>()
private val _changes: Channel<Unit> = Channel(Channel.UNLIMITED)
val changes = _changes.receiveAsFlow()
@@ -38,7 +45,7 @@ class StorageManager(
.drop(1)
.distinctUntilChanged()
.onEach { uri ->
baseDir = getBaseDir(uri)
baseDirLazy = lazyOf(getBaseDir(uri))
baseDir?.let { parent ->
parent.createDirectory(AUTOMATIC_BACKUPS_PATH)
parent.createDirectory(LOCAL_SOURCE_PATH)
@@ -57,15 +64,23 @@ class StorageManager(
}
fun getAutomaticBackupsDirectory(): UniFile? {
return baseDir?.createDirectory(AUTOMATIC_BACKUPS_PATH)
return getChildDirectory(AUTOMATIC_BACKUPS_PATH)
}
fun getDownloadsDirectory(): UniFile? {
return baseDir?.createDirectory(DOWNLOADS_PATH)
return getChildDirectory(DOWNLOADS_PATH)
}
fun getLocalSourceDirectory(): UniFile? {
return baseDir?.createDirectory(LOCAL_SOURCE_PATH)
return getChildDirectory(LOCAL_SOURCE_PATH)
}
private fun getChildDirectory(name: String): UniFile? {
val parent = baseDir ?: return null
childDirectories[name]
?.takeIf { (cachedParent, directory) -> cachedParent === parent && directory.exists() }
?.let { (_, directory) -> return directory }
return parent.createDirectory(name)?.also { childDirectories[name] = parent to it }
}
}
@@ -11,6 +11,10 @@ class GetTracksPerManga(
private val trackRepository: TrackRepository,
) {
suspend fun await(): Map<Long, List<Track>> {
return trackRepository.getTracks().groupBy { it.mangaId }
}
fun subscribe(): Flow<Map<Long, List<Track>>> {
return trackRepository.getTracksAsFlow().map { tracks -> tracks.groupBy { it.mangaId } }
}
@@ -7,13 +7,13 @@ import tachiyomi.domain.track.model.Track
import tachiyomi.domain.track.repository.TrackRepository
@Inject
class InsertTrack(
class UpsertTrack(
private val trackRepository: TrackRepository,
) {
suspend fun await(track: Track) {
try {
trackRepository.insert(track)
trackRepository.upsert(track)
} catch (e: Exception) {
logcat(LogPriority.ERROR, e)
}
@@ -21,7 +21,7 @@ class InsertTrack(
suspend fun awaitAll(tracks: List<Track>) {
try {
trackRepository.insertAll(tracks)
trackRepository.upsertAll(tracks)
} catch (e: Exception) {
logcat(LogPriority.ERROR, e)
}
@@ -1,6 +1,7 @@
package tachiyomi.domain.track.model
import java.io.Serializable
import kotlin.time.Instant
data class Track(
val id: Long,
@@ -14,7 +15,7 @@ data class Track(
val status: Long,
val score: Double,
val remoteUrl: String,
val startDate: Long,
val finishDate: Long,
val startDate: Instant?,
val finishDate: Instant?,
val private: Boolean,
) : Serializable
@@ -9,13 +9,17 @@ interface TrackRepository {
suspend fun getTracksByMangaId(mangaId: Long): List<Track>
suspend fun getTracksByMangaIds(mangaIds: List<Long>): Map<Long, List<Track>>
suspend fun getTracks(): List<Track>
fun getTracksAsFlow(): Flow<List<Track>>
fun getTracksByMangaIdAsFlow(mangaId: Long): Flow<List<Track>>
suspend fun delete(mangaId: Long, trackerId: Long)
suspend fun insert(track: Track)
suspend fun upsert(track: Track)
suspend fun insertAll(tracks: List<Track>)
suspend fun upsertAll(tracks: List<Track>)
}
@@ -11,7 +11,7 @@ class GetUpdates(
private val repository: UpdatesRepository,
) {
suspend fun await(read: Boolean, after: Long): List<UpdatesWithRelations> {
suspend fun await(read: Boolean, after: Instant): List<UpdatesWithRelations> {
return repository.awaitWithRead(read, after, limit = 500)
}
@@ -25,7 +25,7 @@ class GetUpdates(
excludedCategories: List<Long>,
): Flow<List<UpdatesWithRelations>> {
return repository.subscribeAll(
instant.toEpochMilliseconds(),
instant,
limit = 500,
unread = unread,
started = started,
@@ -36,7 +36,7 @@ class GetUpdates(
)
}
fun subscribe(read: Boolean, after: Long): Flow<List<UpdatesWithRelations>> {
fun subscribe(read: Boolean, after: Instant): Flow<List<UpdatesWithRelations>> {
return repository.subscribeWithRead(read, after, limit = 500)
}
}
@@ -1,6 +1,7 @@
package tachiyomi.domain.updates.model
import tachiyomi.domain.manga.model.MangaCover
import kotlin.time.Instant
data class UpdatesWithRelations(
val mangaId: Long,
@@ -11,8 +12,8 @@ data class UpdatesWithRelations(
val chapterUrl: String,
val read: Boolean,
val bookmark: Boolean,
val lastPageRead: Long,
val lastPageRead: Int,
val sourceId: Long,
val dateFetch: Long,
val dateFetch: Instant,
val coverData: MangaCover,
)
@@ -2,13 +2,14 @@ package tachiyomi.domain.updates.repository
import kotlinx.coroutines.flow.Flow
import tachiyomi.domain.updates.model.UpdatesWithRelations
import kotlin.time.Instant
interface UpdatesRepository {
suspend fun awaitWithRead(read: Boolean, after: Long, limit: Long): List<UpdatesWithRelations>
suspend fun awaitWithRead(read: Boolean, after: Instant, limit: Long): List<UpdatesWithRelations>
fun subscribeAll(
after: Long,
after: Instant,
limit: Long,
unread: Boolean?,
started: Boolean?,
@@ -18,5 +19,5 @@ interface UpdatesRepository {
excludedCategories: List<Long>,
): Flow<List<UpdatesWithRelations>>
fun subscribeWithRead(read: Boolean, after: Long, limit: Long): Flow<List<UpdatesWithRelations>>
fun subscribeWithRead(read: Boolean, after: Instant, limit: Long): Flow<List<UpdatesWithRelations>>
}
@@ -4,160 +4,149 @@ import io.kotest.matchers.shouldBe
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.parallel.Execution
import org.junit.jupiter.api.parallel.ExecutionMode
import tachiyomi.domain.chapter.model.Chapter
import tachiyomi.domain.chapter.service.ChapterRecognition.ChapterNumbering
@Execution(ExecutionMode.CONCURRENT)
class ChapterRecognitionTest {
@Test
fun `Basic Ch prefix`() {
val mangaTitle = "Mokushiroku Alice"
assertChapter(mangaTitle, "Mokushiroku Alice Vol.1 Ch.4: Misrepresentation", 4.0)
assertNumbers(
"Mokushiroku Alice",
"Mokushiroku Alice Vol.1 Ch.4: Misrepresentation" to ChapterNumbering("1", "4"),
)
}
@Test
fun `Basic Ch prefix with space after period`() {
val mangaTitle = "Mokushiroku Alice"
assertChapter(mangaTitle, "Mokushiroku Alice Vol. 1 Ch. 4: Misrepresentation", 4.0)
assertNumbers(
"Mokushiroku Alice",
"Mokushiroku Alice Vol. 1 Ch. 4: Misrepresentation" to ChapterNumbering("1", "4"),
)
}
@Test
fun `Basic Ch prefix with decimal`() {
val mangaTitle = "Mokushiroku Alice"
assertChapter(mangaTitle, "Mokushiroku Alice Vol.1 Ch.4.1: Misrepresentation", 4.1)
assertChapter(mangaTitle, "Mokushiroku Alice Vol.1 Ch.4.4: Misrepresentation", 4.4)
assertNumbers(
"Mokushiroku Alice",
"Mokushiroku Alice Vol.1 Ch.4.1: Misrepresentation" to ChapterNumbering("1", "4.1"),
)
assertNumbers(
"Mokushiroku Alice",
"Mokushiroku Alice Vol.1 Ch.4.4: Misrepresentation" to ChapterNumbering("1", "4.4"),
)
}
@Test
fun `Basic Ch prefix with alpha postfix`() {
val mangaTitle = "Mokushiroku Alice"
assertChapter(mangaTitle, "Mokushiroku Alice Vol.1 Ch.4.a: Misrepresentation", 4.1)
assertChapter(mangaTitle, "Mokushiroku Alice Vol.1 Ch.4.b: Misrepresentation", 4.2)
assertChapter(mangaTitle, "Mokushiroku Alice Vol.1 Ch.4.extra: Misrepresentation", 4.99)
assertNumbers(
"Mokushiroku Alice",
"Mokushiroku Alice Vol.1 Ch.4.a: Misrepresentation" to ChapterNumbering("1", "4a"),
)
assertNumbers(
"Mokushiroku Alice",
"Mokushiroku Alice Vol.1 Ch.4.b: Misrepresentation" to ChapterNumbering("1", "4b"),
)
assertNumbers(
"Mokushiroku Alice",
"Mokushiroku Alice Vol.1 Ch.4.extra: Misrepresentation" to ChapterNumbering("1", "4.99"),
)
}
@Test
fun `Name containing one number`() {
val mangaTitle = "Bleach"
assertChapter(mangaTitle, "Bleach 567 Down With Snowwhite", 567.0)
assertNumbers("Bleach", "Bleach 567 Down With Snowwhite" to ChapterNumbering(null, "567"))
}
@Test
fun `Name containing one number and decimal`() {
val mangaTitle = "Bleach"
assertChapter(mangaTitle, "Bleach 567.1 Down With Snowwhite", 567.1)
assertChapter(mangaTitle, "Bleach 567.4 Down With Snowwhite", 567.4)
assertNumbers("Bleach", "Bleach 567.1 Down With Snowwhite" to ChapterNumbering(null, "567.1"))
assertNumbers("Bleach", "Bleach 567.4 Down With Snowwhite" to ChapterNumbering(null, "567.4"))
}
@Test
fun `Name containing one number and alpha`() {
val mangaTitle = "Bleach"
assertChapter(mangaTitle, "Bleach 567.a Down With Snowwhite", 567.1)
assertChapter(mangaTitle, "Bleach 567.b Down With Snowwhite", 567.2)
assertChapter(mangaTitle, "Bleach 567.extra Down With Snowwhite", 567.99)
assertNumbers("Bleach", "Bleach 567.a Down With Snowwhite" to ChapterNumbering(null, "567a"))
assertNumbers("Bleach", "Bleach 567.b Down With Snowwhite" to ChapterNumbering(null, "567b"))
assertNumbers("Bleach", "Bleach 567.extra Down With Snowwhite" to ChapterNumbering(null, "567.99"))
}
@Test
fun `Chapter containing manga title and number`() {
val mangaTitle = "Solanin"
assertChapter(mangaTitle, "Solanin 028 Vol. 2", 28.0)
assertNumbers("Solanin", "Solanin 028 Vol. 2" to ChapterNumbering("2", "28"))
}
@Test
fun `Chapter containing manga title and number decimal`() {
val mangaTitle = "Solanin"
assertChapter(mangaTitle, "Solanin 028.1 Vol. 2", 28.1)
assertChapter(mangaTitle, "Solanin 028.4 Vol. 2", 28.4)
assertNumbers("Solanin", "Solanin 028.1 Vol. 2" to ChapterNumbering("2", "28.1"))
assertNumbers("Solanin", "Solanin 028.4 Vol. 2" to ChapterNumbering("2", "28.4"))
}
@Test
fun `Chapter containing manga title and number alpha`() {
val mangaTitle = "Solanin"
assertChapter(mangaTitle, "Solanin 028.a Vol. 2", 28.1)
assertChapter(mangaTitle, "Solanin 028.b Vol. 2", 28.2)
assertChapter(mangaTitle, "Solanin 028.extra Vol. 2", 28.99)
assertNumbers("Solanin", "Solanin 028.a Vol. 2" to ChapterNumbering("2", "28a"))
assertNumbers("Solanin", "Solanin 028.b Vol. 2" to ChapterNumbering("2", "28b"))
assertNumbers("Solanin", "Solanin 028.extra Vol. 2" to ChapterNumbering("2", "28.99"))
}
@Test
fun `Extreme case`() {
val mangaTitle = "Onepunch-Man"
assertChapter(mangaTitle, "Onepunch-Man Punch Ver002 028", 28.0)
assertNumbers("Onepunch-Man", "Onepunch-Man Punch Ver002 028" to ChapterNumbering(null, "28"))
}
@Test
fun `Extreme case with decimal`() {
val mangaTitle = "Onepunch-Man"
assertChapter(mangaTitle, "Onepunch-Man Punch Ver002 028.1", 28.1)
assertChapter(mangaTitle, "Onepunch-Man Punch Ver002 028.4", 28.4)
assertNumbers("Onepunch-Man", "Onepunch-Man Punch Ver002 028.1" to ChapterNumbering(null, "28.1"))
assertNumbers("Onepunch-Man", "Onepunch-Man Punch Ver002 028.4" to ChapterNumbering(null, "28.4"))
}
@Test
fun `Extreme case with alpha`() {
val mangaTitle = "Onepunch-Man"
assertChapter(mangaTitle, "Onepunch-Man Punch Ver002 028.a", 28.1)
assertChapter(mangaTitle, "Onepunch-Man Punch Ver002 028.b", 28.2)
assertChapter(mangaTitle, "Onepunch-Man Punch Ver002 028.extra", 28.99)
assertNumbers("Onepunch-Man", "Onepunch-Man Punch Ver002 028.a" to ChapterNumbering(null, "28a"))
assertNumbers("Onepunch-Man", "Onepunch-Man Punch Ver002 028.b" to ChapterNumbering(null, "28b"))
assertNumbers("Onepunch-Man", "Onepunch-Man Punch Ver002 028.extra" to ChapterNumbering(null, "28.99"))
}
@Test
fun `Chapter containing dot v2`() {
val mangaTitle = "random"
assertChapter(mangaTitle, "Vol.1 Ch.5v.2: Alones", 5.0)
assertNumbers("random", "Vol.1 Ch.5v.2: Alones" to ChapterNumbering("1", "5"))
}
@Test
fun `Number in manga title`() {
val mangaTitle = "Ayame 14"
assertChapter(mangaTitle, "Ayame 14 1 - The summer of 14", 1.0)
assertNumbers("Ayame 14", "Ayame 14 1 - The summer of 14" to ChapterNumbering(null, "1"))
}
@Test
fun `Space between ch x`() {
val mangaTitle = "Mokushiroku Alice"
assertChapter(mangaTitle, "Mokushiroku Alice Vol.1 Ch. 4: Misrepresentation", 4.0)
assertNumbers(
"Mokushiroku Alice",
"Mokushiroku Alice Vol.1 Ch. 4: Misrepresentation" to ChapterNumbering("1", "4"),
)
}
@Test
fun `Chapter title with ch substring`() {
val mangaTitle = "Ayame 14"
assertChapter(mangaTitle, "Vol.1 Ch.1: March 25 (First Day Cohabiting)", 1.0)
assertNumbers("Ayame 14", "Vol.1 Ch.1: March 25 (First Day Cohabiting)" to ChapterNumbering("1", "1"))
}
@Test
fun `Chapter containing multiple zeros`() {
val mangaTitle = "random"
assertChapter(mangaTitle, "Vol.001 Ch.003: Kaguya Doesn't Know Much", 3.0)
assertNumbers("random", "Vol.001 Ch.003: Kaguya Doesn't Know Much" to ChapterNumbering("1", "3"))
}
@Test
fun `Chapter with version before number`() {
val mangaTitle = "Onepunch-Man"
assertChapter(mangaTitle, "Onepunch-Man Punch Ver002 086 : Creeping Darkness [3]", 86.0)
assertNumbers(
"Onepunch-Man",
"Onepunch-Man Punch Ver002 086 : Creeping Darkness [3]" to ChapterNumbering(null, "86"),
)
}
@Test
fun `Version attached to chapter number`() {
val mangaTitle = "Ansatsu Kyoushitsu"
assertChapter(mangaTitle, "Ansatsu Kyoushitsu 011v002: Assembly Time", 11.0)
assertNumbers("Ansatsu Kyoushitsu", "Ansatsu Kyoushitsu 011v002: Assembly Time" to ChapterNumbering(null, "11"))
}
/**
@@ -166,9 +155,7 @@ class ChapterRecognitionTest {
*/
@Test
fun `Number after manga title with chapter in chapter title case`() {
val mangaTitle = "Tokyo ESP"
assertChapter(mangaTitle, "Tokyo ESP 027: Part 002: Chapter 001", 027.0)
assertNumbers("Tokyo ESP", "Tokyo ESP 027: Part 002: Chapter 001" to ChapterNumbering(null, "27b"))
}
/**
@@ -177,101 +164,276 @@ class ChapterRecognitionTest {
*/
@Test
fun `Number after unwanted tag`() {
val mangaTitle = "One-punch Man"
assertChapter(mangaTitle, "Mag Version 195.5", 195.5)
assertNumbers("One-punch Man", "Mag Version 195.5" to ChapterNumbering(null, "195.5"))
}
@Test
fun `Unparseable chapter`() {
val mangaTitle = "random"
assertChapter(mangaTitle, "Foo", -1.0)
assertNumbers("random", "Foo" to ChapterNumbering(null, null))
}
@Test
fun `Chapter with time in title`() {
val mangaTitle = "random"
assertChapter(mangaTitle, "Fairy Tail 404: 00:00", 404.0)
assertNumbers("random", "Fairy Tail 404: 00:00" to ChapterNumbering(null, "404"))
}
@Test
fun `Chapter with alpha without dot`() {
val mangaTitle = "random"
assertChapter(mangaTitle, "Asu No Yoichi 19a", 19.1)
assertNumbers("random", "Asu No Yoichi 19a" to ChapterNumbering(null, "19a"))
}
@Test
fun `Chapter title containing extra and vol`() {
val mangaTitle = "Fairy Tail"
assertChapter(mangaTitle, "Fairy Tail 404.extravol002", 404.99)
assertChapter(mangaTitle, "Fairy Tail 404 extravol002", 404.99)
assertNumbers("Fairy Tail", "Fairy Tail 404.extravol002" to ChapterNumbering(null, "404.99"))
assertNumbers("Fairy Tail", "Fairy Tail 404 extravol002" to ChapterNumbering(null, "404.99"))
}
@Test
fun `Chapter title containing omake (japanese extra) and vol`() {
val mangaTitle = "Fairy Tail"
assertChapter(mangaTitle, "Fairy Tail 404.omakevol002", 404.98)
assertChapter(mangaTitle, "Fairy Tail 404 omakevol002", 404.98)
assertNumbers("Fairy Tail", "Fairy Tail 404.omakevol002" to ChapterNumbering(null, "404.98"))
assertNumbers("Fairy Tail", "Fairy Tail 404 omakevol002" to ChapterNumbering(null, "404.98"))
}
@Test
fun `Chapter title containing special and vol`() {
val mangaTitle = "Fairy Tail"
assertChapter(mangaTitle, "Fairy Tail 404.specialvol002", 404.97)
assertChapter(mangaTitle, "Fairy Tail 404 specialvol002", 404.97)
assertNumbers("Fairy Tail", "Fairy Tail 404.specialvol002" to ChapterNumbering(null, "404.97"))
assertNumbers("Fairy Tail", "Fairy Tail 404 specialvol002" to ChapterNumbering(null, "404.97"))
}
@Test
fun `Chapter title containing commas`() {
val mangaTitle = "One Piece"
assertChapter(mangaTitle, "One Piece 300,a", 300.1)
assertChapter(mangaTitle, "One Piece Ch,123,extra", 123.99)
assertChapter(mangaTitle, "One Piece the sunny, goes swimming 024,005", 24.005)
assertNumbers("One Piece", "One Piece 300,a" to ChapterNumbering(null, "300a"))
assertNumbers("One Piece", "One Piece Ch,123,extra" to ChapterNumbering(null, "123.99"))
assertNumbers("One Piece", "One Piece the sunny, goes swimming 024,005" to ChapterNumbering(null, "24.005"))
}
@Test
fun `Chapter title containing hyphens`() {
val mangaTitle = "Solo Leveling"
assertChapter(mangaTitle, "ch 122-a", 122.1)
assertChapter(mangaTitle, "Solo Leveling Ch.123-extra", 123.99)
assertChapter(mangaTitle, "Solo Leveling, 024-005", 24.005)
assertChapter(mangaTitle, "Ch.191-200 Read Online", 191.200)
assertNumbers("Solo Leveling", "ch 122-a" to ChapterNumbering(null, "122a"))
assertNumbers("Solo Leveling", "Solo Leveling Ch.123-extra" to ChapterNumbering(null, "123.99"))
assertNumbers("Solo Leveling", "Solo Leveling, 024-005" to ChapterNumbering(null, "24.005"))
assertNumbers("Solo Leveling", "Ch.191-200 Read Online" to ChapterNumbering(null, "191.2"))
}
@Test
fun `Chapters containing season`() {
assertChapter("D.I.C.E", "D.I.C.E[Season 001] Ep. 007", 7.0)
assertNumbers("D.I.C.E", "D.I.C.E[Season 001] Ep. 007" to ChapterNumbering("1", "7"))
}
@Test
fun `Chapters in format sx - chapter xx`() {
assertChapter("The Gamer", "S3 - Chapter 20", 20.0)
assertNumbers("The Gamer", "S3 - Chapter 20" to ChapterNumbering("3", "20"))
}
@Test
fun `Chapters ending with s`() {
assertChapter("One Outs", "One Outs 001", 1.0)
assertNumbers("One Outs", "One Outs 001" to ChapterNumbering(null, "1"))
}
@Test
fun `Chapters containing ordinals`() {
val mangaTitle = "The Sister of the Woods with a Thousand Young"
assertChapter(mangaTitle, "The 1st Night", 1.0)
assertChapter(mangaTitle, "The 2nd Night", 2.0)
assertChapter(mangaTitle, "The 3rd Night", 3.0)
assertChapter(mangaTitle, "The 4th Night", 4.0)
assertNumbers(mangaTitle, "The 1st Night" to ChapterNumbering(null, "1"))
assertNumbers(mangaTitle, "The 2nd Night" to ChapterNumbering(null, "2"))
assertNumbers(mangaTitle, "The 3rd Night" to ChapterNumbering(null, "3"))
assertNumbers(mangaTitle, "The 4th Night" to ChapterNumbering(null, "4"))
}
private fun assertChapter(mangaTitle: String, name: String, expected: Double) {
ChapterRecognition.parseChapterNumber(mangaTitle, name) shouldBe expected
@Test
fun `Parts are letters`() {
assertNumbers("Chapter 1.5 Part I" to ChapterNumbering(null, "1.5a"))
assertNumbers("Chapter 1.5 Part II" to ChapterNumbering(null, "1.5b"))
assertNumbers("Chapter 12 Part 3" to ChapterNumbering(null, "12c"))
assertNumbers("Vol.2 Chapter 7 - Part IV" to ChapterNumbering("2", "7d"))
assertNumbers("Part 3" to ChapterNumbering(null, "3"))
}
@Test
fun `Numbers are turned into doubles`() {
ChapterRecognition.toDouble("12") shouldBe 12.0
ChapterRecognition.toDouble("10.5") shouldBe 10.5
ChapterRecognition.toDouble("19a") shouldBe 19.1
ChapterRecognition.toDouble("19i") shouldBe 19.9
ChapterRecognition.toDouble("1.5a") shouldBe 1.5
ChapterRecognition.toDouble("-2") shouldBe -2.0
}
@Test
fun `Volume is not taken as the chapter number`() {
assertNumbers("Mokushiroku Alice", "Mokushiroku Alice Vol.3" to ChapterNumbering("3", null))
assertNumbers("Mokushiroku Alice", "Vol.3 12" to ChapterNumbering("3", "12"))
}
@Test
fun `Seasons are volumes`() {
assertNumbers("Vol-3 Chapter 2" to ChapterNumbering("3", "2"))
assertNumbers("Mob's 100" to ChapterNumbering(null, "100"))
}
@Test
fun `Volumes are only taken from the name`() {
assertNumbers(
"Vol.2 Ch.5" to ChapterNumbering("2", "5"),
"Ch.4" to ChapterNumbering(null, "4"),
"Vol.2 Ch.3" to ChapterNumbering("2", "3"),
)
}
@Test
fun `Numbers every similar name shares are ignored`() {
assertNumbers(
"Group 2 Chapter 387" to ChapterNumbering(null, "387"),
"Group 2 Chapter 386" to ChapterNumbering(null, "386"),
"Group 2 Chapter 385" to ChapterNumbering(null, "385"),
)
}
@Test
fun `Shared numbers after a chapter prefix are kept`() {
assertNumbers(
"Chapter 5 Part 3" to ChapterNumbering(null, "5c"),
"Chapter 5 Part 2" to ChapterNumbering(null, "5b"),
"Chapter 5 Part 1" to ChapterNumbering(null, "5a"),
)
}
@Test
fun `Chapters without a number fill the missing whole numbers`() {
assertNumbers(
"Chapter 4" to ChapterNumbering(null, "4"),
"The Return" to ChapterNumbering(null, "3"),
"Chapter 2" to ChapterNumbering(null, "2"),
)
assertNumbers(
"Chapter 5" to ChapterNumbering(null, "5"),
"Rain" to ChapterNumbering(null, "4"),
"Snow" to ChapterNumbering(null, "3"),
"Chapter 2" to ChapterNumbering(null, "2"),
)
}
@Test
fun `Chapters without a number go between the chapters around them`() {
assertNumbers(
"Chapter 11" to ChapterNumbering(null, "11"),
"Bonus: Beach Episode" to ChapterNumbering(null, "10.5"),
"Chapter 10" to ChapterNumbering(null, "10"),
)
assertNumbers(
"Chapter 13" to ChapterNumbering(null, "13"),
"Intermission" to ChapterNumbering(null, "10.5"),
"Chapter 10" to ChapterNumbering(null, "10"),
)
assertNumbers(
"Chapter 11" to ChapterNumbering(null, "11"),
"Rain" to ChapterNumbering(null, "10.6"),
"Snow" to ChapterNumbering(null, "10.5"),
"Chapter 10" to ChapterNumbering(null, "10"),
)
assertNumbers(
"Chapter 4" to ChapterNumbering(null, "4"),
"Side Story" to ChapterNumbering(null, "2.5"),
"Chapter 2" to ChapterNumbering(null, "2"),
)
assertNumbers(
"Chapter 3" to ChapterNumbering(null, "3"),
"Extra" to ChapterNumbering(null, "2.6"),
"Chapter 2.5" to ChapterNumbering(null, "2.5"),
)
assertNumbers(
"Chapter 3" to ChapterNumbering(null, "3"),
"Extra" to ChapterNumbering(null, "2.95"),
"Chapter 2.9" to ChapterNumbering(null, "2.9"),
)
}
@Test
fun `Chapters without a number at the ends`() {
assertNumbers(
"Afterword" to ChapterNumbering(null, "100.5"),
"Chapter 100" to ChapterNumbering(null, "100"),
)
assertNumbers(
"Hiatus Announcement" to ChapterNumbering(null, null),
"Chapter 100" to ChapterNumbering(null, "100"),
)
assertNumbers(
"Chapter 1" to ChapterNumbering(null, "1"),
"Prologue" to ChapterNumbering(null, "0"),
)
assertNumbers(
"Chapter 1" to ChapterNumbering(null, "1"),
"Character Profiles" to ChapterNumbering(null, null),
)
}
@Test
fun `A prologue before chapters in volumes is in volume 0`() {
assertNumbers(
"Vol.1 Ch.1" to ChapterNumbering("1", "1"),
"Prologue" to ChapterNumbering("0", "0"),
)
assertNumbers(
"Vol.1 Ch.1" to ChapterNumbering("1", "1"),
"Vol.2 Prologue" to ChapterNumbering("2", "0"),
)
}
@Test
fun `Chapters without a number are only numbered within a volume`() {
assertNumbers(
"Vol.1 Ch.3" to ChapterNumbering("1", "3"),
"Rain" to ChapterNumbering(null, "2"),
"Vol.1 Ch.1" to ChapterNumbering("1", "1"),
)
assertNumbers(
"Vol.2 Ch.3" to ChapterNumbering("2", "3"),
"Rain" to ChapterNumbering(null, null),
"Vol.1 Ch.1" to ChapterNumbering("1", "1"),
)
assertNumbers(
"Vol.2 Ch.3" to ChapterNumbering("2", "3"),
"Vol.2 Rain" to ChapterNumbering("2", null),
"Ch.1" to ChapterNumbering(null, "1"),
)
assertNumbers(
"Vol.2 Ch.3" to ChapterNumbering("2", "3"),
"Vol.1 Rain" to ChapterNumbering("1", null),
"Vol.2 Ch.1" to ChapterNumbering("2", "1"),
)
assertNumbers(
"Extra" to ChapterNumbering(null, "10.5"),
"Vol.2 Ch.10" to ChapterNumbering("2", "10"),
)
assertNumbers(
"Vol.2 Extra" to ChapterNumbering("2", "10.5"),
"Vol.2 Ch.10" to ChapterNumbering("2", "10"),
)
assertNumbers(
"Vol.3 Extra" to ChapterNumbering("3", null),
"Vol.2 Ch.10" to ChapterNumbering("2", "10"),
)
}
@Test
fun `Numbers from the source are kept`() {
val chapters = listOf(
Chapter.create().copy(name = "Vol.1 Chapter 3", chapterNumber = 7.0),
Chapter.create().copy(name = "Chapter 2", chapterNumber = -1.0),
)
ChapterRecognition.parseChapterNumbers("", chapters) shouldBe
listOf(ChapterNumbering("1", "7"), ChapterNumbering(null, "2"))
}
private fun assertNumbers(vararg expected: Pair<String, ChapterNumbering>) = assertNumbers("", *expected)
/**
* Asserts the numbers recognized for chapters with the given names, listed from the most to the least recent.
*/
private fun assertNumbers(mangaTitle: String, vararg expected: Pair<String, ChapterNumbering>) {
val chapters = expected.map { (name, _) -> Chapter.create().copy(name = name, chapterNumber = -1.0) }
ChapterRecognition.parseChapterNumbers(mangaTitle, chapters) shouldBe expected.map { it.second }
}
}
@@ -1,19 +1,29 @@
package tachiyomi.domain.manga.interactor
import io.kotest.matchers.shouldBe
import io.mockk.coEvery
import io.mockk.mockk
import kotlinx.coroutines.test.runTest
import kotlinx.datetime.DatePeriod
import kotlinx.datetime.DayOfWeek
import kotlinx.datetime.LocalDate
import kotlinx.datetime.LocalDateTime
import kotlinx.datetime.TimeZone
import kotlinx.datetime.atStartOfDayIn
import kotlinx.datetime.atTime
import kotlinx.datetime.plus
import kotlinx.datetime.toInstant
import kotlinx.datetime.toLocalDateTime
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.parallel.Execution
import org.junit.jupiter.api.parallel.ExecutionMode
import tachiyomi.domain.chapter.interactor.GetChaptersByMangaId
import tachiyomi.domain.chapter.model.Chapter
import tachiyomi.domain.manga.model.Manga
import kotlin.time.Clock
import kotlin.time.Duration
import kotlin.time.Duration.Companion.days
import kotlin.time.Duration.Companion.hours
import kotlin.time.DurationUnit
import kotlin.time.toDuration
@Execution(ExecutionMode.CONCURRENT)
class FetchIntervalTest {
@@ -21,23 +31,21 @@ class FetchIntervalTest {
private val testTime = LocalDateTime.parse("2020-01-01T00:00:00")
private val testTimeZone = TimeZone.UTC
private var chapter = Chapter.create().copy(
dateFetch = testTime.toInstant(testTimeZone).toEpochMilliseconds(),
dateUpload = testTime.toInstant(testTimeZone).toEpochMilliseconds(),
dateFetch = testTime.toInstant(testTimeZone),
dateUpload = testTime.toInstant(testTimeZone),
)
private val fetchInterval = FetchInterval(mockk())
@Test
fun `returns default interval of 7 days when not enough distinct days`() {
val chaptersWithUploadDate = (1..50).map {
chapterWithTime(chapter, 1.days)
}
fetchInterval.calculateInterval(chaptersWithUploadDate, testTimeZone) shouldBe 7
interval(chaptersWithUploadDate) shouldBe 7
val chaptersWithoutUploadDate = chaptersWithUploadDate.map {
it.copy(dateUpload = 0L)
it.copy(dateUpload = null)
}
fetchInterval.calculateInterval(chaptersWithoutUploadDate, testTimeZone) shouldBe 7
interval(chaptersWithoutUploadDate) shouldBe 7
}
@Test
@@ -51,7 +59,7 @@ class FetchIntervalTest {
val chapters = oldChapters + newChapters
fetchInterval.calculateInterval(chapters, testTimeZone) shouldBe 1
interval(chapters) shouldBe 1
}
@Test
@@ -66,7 +74,7 @@ class FetchIntervalTest {
val chapters = oldChapters + newChapters
fetchInterval.calculateInterval(chapters, testTimeZone) shouldBe 7
interval(chapters) shouldBe 7
}
@Test
@@ -74,7 +82,7 @@ class FetchIntervalTest {
val chapters = (1..10).map {
chapterWithTime(chapter, 10.hours)
}
fetchInterval.calculateInterval(chapters, testTimeZone) shouldBe 7
interval(chapters) shouldBe 7
}
@Test
@@ -84,7 +92,7 @@ class FetchIntervalTest {
} + (1..5).map {
chapterWithTime(chapter, 2.days)
}
fetchInterval.calculateInterval(chapters, testTimeZone) shouldBe 7
interval(chapters) shouldBe 7
}
@Test
@@ -92,7 +100,7 @@ class FetchIntervalTest {
val chapters = (1..20).map {
chapterWithTime(chapter, it.days)
}
fetchInterval.calculateInterval(chapters, testTimeZone) shouldBe 1
interval(chapters) shouldBe 1
}
@Test
@@ -100,7 +108,7 @@ class FetchIntervalTest {
val chapters = (1..20).map {
chapterWithTime(chapter, (15 * it).hours)
}
fetchInterval.calculateInterval(chapters, testTimeZone) shouldBe 1
interval(chapters) shouldBe 1
}
@Test
@@ -108,7 +116,7 @@ class FetchIntervalTest {
val chapters = (1..20).map {
chapterWithTime(chapter, (2 * it).days)
}
fetchInterval.calculateInterval(chapters, testTimeZone) shouldBe 2
interval(chapters) shouldBe 2
}
@Test
@@ -116,12 +124,12 @@ class FetchIntervalTest {
val chaptersWithUploadDate = (1..5).map {
chapterWithTime(chapter, (25 * it).hours)
}
fetchInterval.calculateInterval(chaptersWithUploadDate, testTimeZone) shouldBe 1
interval(chaptersWithUploadDate) shouldBe 1
val chaptersWithoutUploadDate = chaptersWithUploadDate.map {
it.copy(dateUpload = 0L)
it.copy(dateUpload = null)
}
fetchInterval.calculateInterval(chaptersWithoutUploadDate, testTimeZone) shouldBe 1
interval(chaptersWithoutUploadDate) shouldBe 1
}
@Test
@@ -129,14 +137,256 @@ class FetchIntervalTest {
val chapters = (1..20).map {
chapterWithTime(chapter, (43 * it).hours)
}
fetchInterval.calculateInterval(chapters, testTimeZone) shouldBe 2
interval(chapters) shouldBe 2
}
@Test
fun `follows releases on a few fixed weekdays`() = runTest {
val mondaysAndThursdays = generateSequence(LocalDate.parse("2019-12-02")) { it.plus(DatePeriod(days = 1)) }
.takeWhile { it <= LocalDate.parse("2020-01-20") }
.filter { it.dayOfWeek == DayOfWeek.MONDAY || it.dayOfWeek == DayOfWeek.THURSDAY }
.toList()
val chapters = chaptersOn(mondaysAndThursdays)
ReleaseSchedule.estimate(chapters, testTimeZone, LocalDate.parse("2020-01-21")).pattern shouldBe
ReleasePattern.Weekdays(setOf(DayOfWeek.MONDAY, DayOfWeek.THURSDAY))
nextUpdate(today = "2020-01-21", chapters) shouldBe LocalDate.parse("2020-01-23")
}
@Test
fun `follows releases on a day of the month`() = runTest {
val chapters = chaptersOn(
listOf("2019-09-15", "2019-10-15", "2019-11-14", "2019-12-15", "2020-01-16").map(LocalDate::parse),
perDay = 2,
)
nextUpdate(today = "2020-01-20", chapters) shouldBe LocalDate.parse("2020-02-15")
}
@Test
fun `follows releases on the last day of the month`() = runTest {
val chapters = chaptersOn(
listOf("2019-09-30", "2019-10-31", "2019-11-30", "2019-12-31", "2020-01-31").map(LocalDate::parse),
perDay = 2,
)
nextUpdate(today = "2020-02-05", chapters) shouldBe LocalDate.parse("2020-02-29")
}
@Test
fun `follows releases on two days of the month`() = runTest {
val days =
listOf("2019-10-01", "2019-10-15", "2019-11-01", "2019-11-16", "2019-12-01", "2019-12-15", "2020-01-01")
val chapters = chaptersOn((days + "2020-01-15").map(LocalDate::parse), perDay = 2)
ReleaseSchedule.estimate(chapters, testTimeZone, LocalDate.parse("2020-01-20")).pattern shouldBe
ReleasePattern.DaysOfMonth(setOf(1, 15))
nextUpdate(today = "2020-01-20", chapters) shouldBe LocalDate.parse("2020-02-01")
}
@Test
fun `follows releases on a weekday of the month`() = runTest {
val firstSaturdays = listOf("2019-09-07", "2019-10-05", "2019-11-02", "2019-12-07", "2020-01-04")
val chapters = chaptersOn(firstSaturdays.map(LocalDate::parse), perDay = 2)
nextUpdate(today = "2020-01-10", chapters) shouldBe LocalDate.parse("2020-02-01")
}
@Test
fun `follows releases on a weekday of the last week of the month`() = runTest {
val lastFridays = listOf("2019-09-27", "2019-10-25", "2019-11-29", "2019-12-27", "2020-01-31")
val chapters = chaptersOn(lastFridays.map(LocalDate::parse), perDay = 2)
nextUpdate(today = "2020-02-03", chapters) shouldBe LocalDate.parse("2020-02-28")
}
@Test
fun `follows releases in batches`() = runTest {
// Three days of releases every three weeks
val batches = every(days = 21, from = "2019-09-02", to = "2019-12-16")
.flatMap { start -> (0..2).map { start.plus(DatePeriod(days = it)) } }
val chapters = chaptersOn(batches.filter { it <= LocalDate.parse("2019-12-17") })
nextUpdate(today = "2019-12-17", chapters, foundRelease = true) shouldBe LocalDate.parse("2019-12-18")
nextUpdate(today = "2019-12-18", chaptersOn(batches), foundRelease = true) shouldBe
LocalDate.parse("2020-01-06")
}
@Test
fun `checks again around when an earlier break ended`() = runTest {
// A season of weekly releases, a 98 day break, and another season
val chapters = chaptersOn(weekly(from = "2019-01-02", to = "2019-03-27") + weekly("2019-07-03", "2019-09-25"))
nextUpdate(today = "2019-12-20", chapters) shouldBe LocalDate.parse("2019-12-25")
nextUpdate(today = "2019-12-26", chapters) shouldBe LocalDate.parse("2020-01-01")
}
@Test
fun `never waits more than 28 days between checks`() = runTest {
val lastDays = listOf("2019-09-30", "2019-10-31", "2019-11-30", "2019-12-31", "2020-01-31")
val chapters = chaptersOn(lastDays.map(LocalDate::parse), perDay = 2)
nextUpdate(today = "2020-01-31", chapters, foundRelease = true) shouldBe LocalDate.parse("2020-02-28")
}
@Test
fun `keeps checking on the expected day`() = runTest {
val chapters = chaptersOn(weekly(from = "2019-12-04", to = "2019-12-25"))
nextUpdate(today = "2020-01-01", chapters) shouldBe LocalDate.parse("2020-01-01")
}
@Test
fun `keeps a next update in the window while nothing new came out`() = runTest {
val chapters = chaptersOn(weekly(from = "2019-12-04", to = "2019-12-25"))
val manga = Manga.create().copy(nextUpdate = LocalDate.parse("2020-01-01").atStartOfDayIn(testTimeZone))
nextUpdate(today = "2020-01-02", chapters, manga) shouldBe LocalDate.parse("2020-01-01")
}
@Test
fun `counts from a release on the expected day`() = runTest {
val chapters = chaptersOn(weekly(from = "2019-12-04", to = "2020-01-01"))
val manga = Manga.create().copy(nextUpdate = LocalDate.parse("2020-01-01").atStartOfDayIn(testTimeZone))
nextUpdate(today = "2020-01-01", chapters, manga) shouldBe LocalDate.parse("2020-01-08")
}
@Test
fun `keeps the schedule for a release uploaded the day before it was found`() = runTest {
val chapters = chaptersOn(weekly(from = "2019-10-02", to = "2019-12-25") + LocalDate.parse("2019-12-31"))
nextUpdate(today = "2020-01-01", chapters, expecting("2020-01-01"), foundRelease = true) shouldBe
LocalDate.parse("2020-01-08")
}
@Test
fun `keeps the schedule after a release shortly before the expected day`() = runTest {
val chapters = chaptersOn(weekly(from = "2019-10-02", to = "2019-12-25") + LocalDate.parse("2019-12-30"))
nextUpdate(today = "2019-12-30", chapters, expecting("2020-01-01"), foundRelease = true) shouldBe
LocalDate.parse("2020-01-08")
}
@Test
fun `keeps the schedule after a late release`() = runTest {
val chapters = chaptersOn(weekly(from = "2019-12-04", to = "2019-12-25") + LocalDate.parse("2020-01-03"))
nextUpdate(today = "2020-01-03", chapters, expecting("2020-01-01"), foundRelease = true) shouldBe
LocalDate.parse("2020-01-08")
}
@Test
fun `keeps expecting the release after an extra one well before it`() = runTest {
val chapters = chaptersOn(weekly(from = "2019-10-02", to = "2020-01-01") + LocalDate.parse("2020-01-02"))
nextUpdate(today = "2020-01-02", chapters, expecting("2020-01-08"), foundRelease = true) shouldBe
LocalDate.parse("2020-01-08")
}
@Test
fun `follows the schedule again when releases resume after a hiatus`() = runTest {
val chapters = chaptersOn(weekly(from = "2019-09-11", to = "2019-10-02") + LocalDate.parse("2020-01-01"))
nextUpdate(today = "2020-01-01", chapters, expecting("2020-01-22"), foundRelease = true) shouldBe
LocalDate.parse("2020-01-08")
}
@Test
fun `follows the schedule once releases keep coming early`() = runTest {
val history = every(days = 5, from = "2019-10-01", to = "2019-12-15")
val chapters = chaptersOn(history + every(days = 5, from = "2019-12-19", to = "2019-12-29"))
nextUpdate(today = "2019-12-29", chapters, expecting("2019-12-30"), foundRelease = true) shouldBe
LocalDate.parse("2020-01-03")
}
@Test
fun `keeps checking for an overdue release while recent ones came that late`() = runTest {
val chapters = chaptersOn(afterGaps("2019-10-18", 6, 5, 8, 6, 5, 6, 8, 6, 5))
// Due on the 18th from a median gap of 6, and gaps have run to 8
nextUpdate(today = "2019-12-20", chapters, expecting("2019-12-18")) shouldBe LocalDate.parse("2019-12-20")
}
@Test
fun `counts from an early release with a custom interval`() = runTest {
val manga = expecting("2020-01-01").copy(fetchInterval = -7)
nextUpdate(today = "2019-12-31", emptyList(), manga, foundRelease = true) shouldBe
LocalDate.parse("2020-01-07")
}
@Test
fun `spreads out checks during a hiatus`() = runTest {
val chapters = chaptersOn(weekly(from = "2019-09-11", to = "2019-10-02"))
nextUpdate(today = "2020-01-01", chapters) shouldBe LocalDate.parse("2020-01-22")
}
@Test
fun `ignores chapters dated in the future`() = runTest {
val chapters = chaptersOn(weekly(from = "2019-12-04", to = "2019-12-25") + LocalDate.parse("2020-06-01"))
nextUpdate(today = "2020-01-01", chapters) shouldBe LocalDate.parse("2020-01-01")
}
private suspend fun nextUpdate(
today: String,
chapters: List<Chapter>,
manga: Manga = Manga.create(),
foundRelease: Boolean = false,
): LocalDate {
val getChaptersByMangaId = mockk<GetChaptersByMangaId>()
coEvery { getChaptersByMangaId.await(any(), any()) } returns chapters
val fetchInterval = FetchInterval(getChaptersByMangaId)
val date = LocalDate.parse(today)
val update = fetchInterval.withFetchInterval(
manga = manga,
dateTime = date.atTime(10, 0),
timeZone = testTimeZone,
window = fetchInterval.getWindow(date, testTimeZone),
foundRelease = foundRelease,
)
return update.nextUpdate!!.toLocalDateTime(testTimeZone).date
}
private fun expecting(day: String): Manga {
return Manga.create().copy(nextUpdate = LocalDate.parse(day).atStartOfDayIn(testTimeZone))
}
private fun interval(chapters: List<Chapter>): Int {
return ReleaseSchedule.estimate(
chapters,
testTimeZone,
Clock.System.now().toLocalDateTime(testTimeZone).date,
).interval
}
private fun weekly(from: String, to: String) = every(days = 7, from, to)
private fun every(days: Int, from: String, to: String): List<LocalDate> {
return generateSequence(LocalDate.parse(from)) { it.plus(DatePeriod(days = days)) }
.takeWhile { it <= LocalDate.parse(to) }
.toList()
}
private fun afterGaps(from: String, vararg gaps: Int): List<LocalDate> {
return gaps.runningFold(LocalDate.parse(from)) { day, gap -> day.plus(DatePeriod(days = gap)) }
}
private fun chaptersOn(days: List<LocalDate>, perDay: Int = 1): List<Chapter> {
return days.flatMap { day ->
val time = day.atStartOfDayIn(testTimeZone) + 12.hours
List(perDay) { chapter.copy(dateFetch = time, dateUpload = time) }
}
}
private fun chapterWithTime(chapter: Chapter, duration: Duration): Chapter {
val newTime = testTime.toInstant(testTimeZone).plus(duration).toEpochMilliseconds()
val newTime = testTime.toInstant(testTimeZone) + duration
return chapter.copy(dateFetch = newTime, dateUpload = newTime)
}
private fun List<Chapter>.lastUploadDate() =
last().dateUpload.toDuration(DurationUnit.MILLISECONDS)
last().dateUpload!! - testTime.toInstant(testTimeZone)
}