6d69903a56
* Replace java.time APIs with kotlin(x).(date)time kotlinx-datetime explicitly says in its README that it does not cover i18n, so we have to stick with using their toJavaX converters wherever we localise dates and times. Yes, some of these replacements are... questionable. But I wanted to try and maximise the replacement for now. * Bump kotlinx-datetime to 0.8.0 * More kotlin.time/kotlinx-datetime replacements * Remove redundant init block & make attributes val * Replace new use of java.time.Instant
148 lines
5.4 KiB
Kotlin
148 lines
5.4 KiB
Kotlin
package mihon.feature.library
|
|
|
|
import eu.kanade.tachiyomi.ui.library.LibraryItem
|
|
import kotlinx.datetime.LocalDate
|
|
import kotlinx.datetime.TimeZone
|
|
import kotlinx.datetime.toLocalDateTime
|
|
import mihon.domain.library.model.search.AndNode
|
|
import mihon.domain.library.model.search.ComparisonField
|
|
import mihon.domain.library.model.search.ComparisonQueryNode
|
|
import mihon.domain.library.model.search.EmptyQueryNode
|
|
import mihon.domain.library.model.search.FieldQueryNode
|
|
import mihon.domain.library.model.search.GeneralQueryNode
|
|
import mihon.domain.library.model.search.MangaField
|
|
import mihon.domain.library.model.search.NotNode
|
|
import mihon.domain.library.model.search.OrNode
|
|
import mihon.domain.library.model.search.QueryNode
|
|
import tachiyomi.source.local.LocalSource
|
|
import kotlin.math.abs
|
|
import kotlin.time.Instant
|
|
|
|
fun QueryNode.matches(item: LibraryItem): Boolean {
|
|
return when (this) {
|
|
is AndNode -> children.all { it.matches(item) }
|
|
is OrNode -> children.any { it.matches(item) }
|
|
is NotNode -> !child.matches(item)
|
|
is EmptyQueryNode -> true
|
|
is GeneralQueryNode -> matches(item)
|
|
is FieldQueryNode -> matches(item)
|
|
is ComparisonQueryNode -> matches(item)
|
|
}
|
|
}
|
|
|
|
private fun GeneralQueryNode.matches(item: LibraryItem): Boolean {
|
|
val manga = item.libraryManga.manga
|
|
|
|
// Use when so each added field has to be handled explicitly
|
|
val match = MangaField.entries.any { field ->
|
|
if (field.fieldOnly) return@any false
|
|
|
|
when (field) {
|
|
MangaField.TITLE -> manga.title.contains(value, ignoreCase = true)
|
|
MangaField.AUTHOR -> manga.author?.contains(value, ignoreCase = true) ?: false
|
|
MangaField.ARTIST -> manga.artist?.contains(value, ignoreCase = true) ?: false
|
|
MangaField.DESCRIPTION -> manga.description?.contains(value, ignoreCase = true) ?: false
|
|
MangaField.GENRE -> manga.genre?.any { it.contains(value, ignoreCase = true) } ?: false
|
|
MangaField.SOURCE -> {
|
|
item.sourceName.contains(value, ignoreCase = true) ||
|
|
(value.equals("local", ignoreCase = true) && manga.source == LocalSource.ID)
|
|
}
|
|
MangaField.NOTES -> manga.notes.contains(value, ignoreCase = true)
|
|
|
|
// field-only queries; unreachable; added here to make `when` exhaustive
|
|
MangaField.LANGUAGE, MangaField.SOURCE_ID -> error("How did we get here?")
|
|
}
|
|
}
|
|
return if (negated) !match else match
|
|
}
|
|
|
|
private fun FieldQueryNode.matches(item: LibraryItem): Boolean {
|
|
val manga = item.libraryManga.manga
|
|
|
|
val match = when (field) {
|
|
MangaField.GENRE -> {
|
|
if (value.isEmpty()) {
|
|
manga.genre.isNullOrEmpty()
|
|
} else {
|
|
manga.genre?.any { it.contains(value, ignoreCase = true) } ?: false
|
|
}
|
|
}
|
|
|
|
MangaField.SOURCE -> {
|
|
if (value.isEmpty()) {
|
|
item.sourceName.isEmpty()
|
|
} else {
|
|
item.sourceName.contains(value, ignoreCase = true) ||
|
|
(value.equals("local", ignoreCase = true) && manga.source == LocalSource.ID)
|
|
}
|
|
}
|
|
|
|
MangaField.SOURCE_ID -> {
|
|
value.toLongOrNull()?.let { it == manga.source } ?: false
|
|
}
|
|
|
|
else -> {
|
|
val text = when (field) {
|
|
MangaField.TITLE -> manga.title
|
|
MangaField.AUTHOR -> manga.author
|
|
MangaField.ARTIST -> manga.artist
|
|
MangaField.DESCRIPTION -> manga.description
|
|
MangaField.NOTES -> manga.notes
|
|
MangaField.LANGUAGE -> item.sourceLanguage
|
|
|
|
// unreachable; added here to make `when` exhaustive
|
|
MangaField.GENRE, MangaField.SOURCE, MangaField.SOURCE_ID -> error("How did we get here?")
|
|
}
|
|
|
|
if (value.isEmpty()) {
|
|
text.isNullOrEmpty()
|
|
} else {
|
|
text?.contains(value, ignoreCase = true) ?: false
|
|
}
|
|
}
|
|
}
|
|
|
|
return if (negated) !match else match
|
|
}
|
|
|
|
private fun ComparisonQueryNode.matches(item: LibraryItem): Boolean {
|
|
val manga = item.libraryManga.manga
|
|
|
|
fun compareDates(timestamp: Long, value: String): Boolean? {
|
|
val inputDate = runCatching { LocalDate.parse(value) }.getOrNull() ?: return null
|
|
val mangaDate = Instant.fromEpochMilliseconds(timestamp).toLocalDateTime(TimeZone.currentSystemDefault()).date
|
|
return queryComparator.apply(mangaDate, inputDate)
|
|
}
|
|
|
|
val match = when (field) {
|
|
ComparisonField.ID -> value.toLongOrNull()?.let { queryComparator.apply(manga.id, it) }
|
|
|
|
ComparisonField.DATE_ADDED -> compareDates(manga.dateAdded, value)
|
|
|
|
ComparisonField.FETCH_INTERVAL -> value.toIntOrNull()
|
|
?.let { queryComparator.apply(abs(manga.fetchInterval), it) }
|
|
|
|
ComparisonField.NEXT_UPDATE -> compareDates(manga.nextUpdate, value)
|
|
|
|
ComparisonField.UNREAD -> {
|
|
value.toLongOrNull()?.let {
|
|
queryComparator.apply(item.unreadCount, it)
|
|
}
|
|
}
|
|
|
|
ComparisonField.READ -> {
|
|
value.toLongOrNull()?.let {
|
|
queryComparator.apply(item.libraryManga.readCount, it)
|
|
}
|
|
}
|
|
|
|
ComparisonField.TOTAL -> {
|
|
value.toLongOrNull()?.let {
|
|
queryComparator.apply(item.libraryManga.totalChapters, it)
|
|
}
|
|
}
|
|
} ?: false
|
|
|
|
return if (negated) !match else match
|
|
}
|