mirror of
https://github.com/mihonapp/mihon.git
synced 2025-10-20 01:59:43 +02:00
Move more models to domain module
This commit is contained in:
@@ -10,10 +10,13 @@ android {
|
||||
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
|
||||
consumerProguardFiles("consumer-rules.pro")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation(platform(kotlinx.coroutines.bom))
|
||||
implementation(kotlinx.bundles.coroutines)
|
||||
|
||||
implementation(project(":source-api"))
|
||||
|
||||
testImplementation(libs.junit)
|
||||
}
|
||||
|
@@ -0,0 +1,10 @@
|
||||
package tachiyomi.domain.history.model
|
||||
|
||||
import java.util.Date
|
||||
|
||||
data class History(
|
||||
val id: Long,
|
||||
val chapterId: Long,
|
||||
val readAt: Date?,
|
||||
val readDuration: Long,
|
||||
)
|
@@ -0,0 +1,9 @@
|
||||
package tachiyomi.domain.history.model
|
||||
|
||||
import java.util.Date
|
||||
|
||||
data class HistoryUpdate(
|
||||
val chapterId: Long,
|
||||
val readAt: Date,
|
||||
val sessionReadDuration: Long,
|
||||
)
|
@@ -0,0 +1,15 @@
|
||||
package tachiyomi.domain.history.model
|
||||
|
||||
import tachiyomi.domain.manga.model.MangaCover
|
||||
import java.util.Date
|
||||
|
||||
data class HistoryWithRelations(
|
||||
val id: Long,
|
||||
val chapterId: Long,
|
||||
val mangaId: Long,
|
||||
val title: String,
|
||||
val chapterNumber: Float,
|
||||
val readAt: Date?,
|
||||
val readDuration: Long,
|
||||
val coverData: MangaCover,
|
||||
)
|
35
domain/src/main/java/tachiyomi/domain/library/model/Flag.kt
Normal file
35
domain/src/main/java/tachiyomi/domain/library/model/Flag.kt
Normal file
@@ -0,0 +1,35 @@
|
||||
package tachiyomi.domain.library.model
|
||||
|
||||
interface Flag {
|
||||
val flag: Long
|
||||
}
|
||||
|
||||
interface Mask {
|
||||
val mask: Long
|
||||
}
|
||||
|
||||
interface FlagWithMask : Flag, Mask
|
||||
|
||||
operator fun Long.contains(other: Flag): Boolean {
|
||||
return if (other is Mask) {
|
||||
other.flag == this and other.mask
|
||||
} else {
|
||||
other.flag == this
|
||||
}
|
||||
}
|
||||
|
||||
operator fun Long.plus(other: Flag): Long {
|
||||
return if (other is Mask) {
|
||||
this and other.mask.inv() or (other.flag and other.mask)
|
||||
} else {
|
||||
this or other.flag
|
||||
}
|
||||
}
|
||||
|
||||
operator fun Flag.plus(other: Flag): Long {
|
||||
return if (other is Mask) {
|
||||
this.flag and other.mask.inv() or (other.flag and other.mask)
|
||||
} else {
|
||||
this.flag or other.flag
|
||||
}
|
||||
}
|
@@ -0,0 +1,59 @@
|
||||
package tachiyomi.domain.library.model
|
||||
|
||||
import tachiyomi.domain.category.model.Category
|
||||
|
||||
sealed class LibraryDisplayMode(
|
||||
override val flag: Long,
|
||||
) : FlagWithMask {
|
||||
|
||||
override val mask: Long = 0b00000011L
|
||||
|
||||
object CompactGrid : LibraryDisplayMode(0b00000000)
|
||||
object ComfortableGrid : LibraryDisplayMode(0b00000001)
|
||||
object List : LibraryDisplayMode(0b00000010)
|
||||
object CoverOnlyGrid : LibraryDisplayMode(0b00000011)
|
||||
|
||||
object Serializer {
|
||||
fun deserialize(serialized: String): LibraryDisplayMode {
|
||||
return LibraryDisplayMode.deserialize(serialized)
|
||||
}
|
||||
|
||||
fun serialize(value: LibraryDisplayMode): String {
|
||||
return value.serialize()
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
val values = setOf(CompactGrid, ComfortableGrid, List, CoverOnlyGrid)
|
||||
val default = CompactGrid
|
||||
|
||||
fun valueOf(flag: Long?): LibraryDisplayMode {
|
||||
if (flag == null) return default
|
||||
return values
|
||||
.find { mode -> mode.flag == flag and mode.mask }
|
||||
?: default
|
||||
}
|
||||
|
||||
fun deserialize(serialized: String): LibraryDisplayMode {
|
||||
return when (serialized) {
|
||||
"COMFORTABLE_GRID" -> ComfortableGrid
|
||||
"COMPACT_GRID" -> CompactGrid
|
||||
"COVER_ONLY_GRID" -> CoverOnlyGrid
|
||||
"LIST" -> List
|
||||
else -> default
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun serialize(): String {
|
||||
return when (this) {
|
||||
ComfortableGrid -> "COMFORTABLE_GRID"
|
||||
CompactGrid -> "COMPACT_GRID"
|
||||
CoverOnlyGrid -> "COVER_ONLY_GRID"
|
||||
List -> "LIST"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val Category.display: LibraryDisplayMode
|
||||
get() = LibraryDisplayMode.valueOf(flags)
|
@@ -0,0 +1,119 @@
|
||||
package tachiyomi.domain.library.model
|
||||
|
||||
import tachiyomi.domain.category.model.Category
|
||||
|
||||
data class LibrarySort(
|
||||
val type: Type,
|
||||
val direction: Direction,
|
||||
) : FlagWithMask {
|
||||
|
||||
override val flag: Long
|
||||
get() = type + direction
|
||||
|
||||
override val mask: Long
|
||||
get() = type.mask or direction.mask
|
||||
|
||||
val isAscending: Boolean
|
||||
get() = direction == Direction.Ascending
|
||||
|
||||
sealed class Type(
|
||||
override val flag: Long,
|
||||
) : FlagWithMask {
|
||||
|
||||
override val mask: Long = 0b00111100L
|
||||
|
||||
object Alphabetical : Type(0b00000000)
|
||||
object LastRead : Type(0b00000100)
|
||||
object LastUpdate : Type(0b00001000)
|
||||
object UnreadCount : Type(0b00001100)
|
||||
object TotalChapters : Type(0b00010000)
|
||||
object LatestChapter : Type(0b00010100)
|
||||
object ChapterFetchDate : Type(0b00011000)
|
||||
object DateAdded : Type(0b00011100)
|
||||
|
||||
companion object {
|
||||
fun valueOf(flag: Long): Type {
|
||||
return types.find { type -> type.flag == flag and type.mask } ?: default.type
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sealed class Direction(
|
||||
override val flag: Long,
|
||||
) : FlagWithMask {
|
||||
|
||||
override val mask: Long = 0b01000000L
|
||||
|
||||
object Ascending : Direction(0b01000000)
|
||||
object Descending : Direction(0b00000000)
|
||||
|
||||
companion object {
|
||||
fun valueOf(flag: Long): Direction {
|
||||
return directions.find { direction -> direction.flag == flag and direction.mask } ?: default.direction
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
object Serializer {
|
||||
fun deserialize(serialized: String): LibrarySort {
|
||||
return LibrarySort.deserialize(serialized)
|
||||
}
|
||||
|
||||
fun serialize(value: LibrarySort): String {
|
||||
return value.serialize()
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
val types = setOf(Type.Alphabetical, Type.LastRead, Type.LastUpdate, Type.UnreadCount, Type.TotalChapters, Type.LatestChapter, Type.ChapterFetchDate, Type.DateAdded)
|
||||
val directions = setOf(Direction.Ascending, Direction.Descending)
|
||||
val default = LibrarySort(Type.Alphabetical, Direction.Ascending)
|
||||
|
||||
fun valueOf(flag: Long): LibrarySort {
|
||||
return LibrarySort(
|
||||
Type.valueOf(flag),
|
||||
Direction.valueOf(flag),
|
||||
)
|
||||
}
|
||||
|
||||
fun deserialize(serialized: String): LibrarySort {
|
||||
if (serialized.isEmpty()) return default
|
||||
return try {
|
||||
val values = serialized.split(",")
|
||||
val type = when (values[0]) {
|
||||
"ALPHABETICAL" -> Type.Alphabetical
|
||||
"LAST_READ" -> Type.LastRead
|
||||
"LAST_MANGA_UPDATE" -> Type.LastUpdate
|
||||
"UNREAD_COUNT" -> Type.UnreadCount
|
||||
"TOTAL_CHAPTERS" -> Type.TotalChapters
|
||||
"LATEST_CHAPTER" -> Type.LatestChapter
|
||||
"CHAPTER_FETCH_DATE" -> Type.ChapterFetchDate
|
||||
"DATE_ADDED" -> Type.DateAdded
|
||||
else -> Type.Alphabetical
|
||||
}
|
||||
val ascending = if (values[1] == "ASCENDING") Direction.Ascending else Direction.Descending
|
||||
LibrarySort(type, ascending)
|
||||
} catch (e: Exception) {
|
||||
default
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun serialize(): String {
|
||||
val type = when (type) {
|
||||
Type.Alphabetical -> "ALPHABETICAL"
|
||||
Type.LastRead -> "LAST_READ"
|
||||
Type.LastUpdate -> "LAST_MANGA_UPDATE"
|
||||
Type.UnreadCount -> "UNREAD_COUNT"
|
||||
Type.TotalChapters -> "TOTAL_CHAPTERS"
|
||||
Type.LatestChapter -> "LATEST_CHAPTER"
|
||||
Type.ChapterFetchDate -> "CHAPTER_FETCH_DATE"
|
||||
Type.DateAdded -> "DATE_ADDED"
|
||||
}
|
||||
val direction = if (direction == Direction.Ascending) "ASCENDING" else "DESCENDING"
|
||||
return "$type,$direction"
|
||||
}
|
||||
}
|
||||
|
||||
val Category.sort: LibrarySort
|
||||
get() = LibrarySort.valueOf(flags)
|
@@ -0,0 +1,12 @@
|
||||
package tachiyomi.domain.manga.model
|
||||
|
||||
/**
|
||||
* Contains the required data for MangaCoverFetcher
|
||||
*/
|
||||
data class MangaCover(
|
||||
val mangaId: Long,
|
||||
val sourceId: Long,
|
||||
val isMangaFavorite: Boolean,
|
||||
val url: String?,
|
||||
val lastModified: Long,
|
||||
)
|
@@ -0,0 +1,24 @@
|
||||
package tachiyomi.domain.manga.model
|
||||
|
||||
import eu.kanade.tachiyomi.source.model.UpdateStrategy
|
||||
|
||||
data class MangaUpdate(
|
||||
val id: Long,
|
||||
val source: Long? = null,
|
||||
val favorite: Boolean? = null,
|
||||
val lastUpdate: Long? = 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,
|
||||
)
|
@@ -0,0 +1,10 @@
|
||||
package tachiyomi.domain.source.model
|
||||
|
||||
data class SourceData(
|
||||
val id: Long,
|
||||
val lang: String,
|
||||
val name: String,
|
||||
) {
|
||||
|
||||
val isMissingInfo: Boolean = name.isBlank() || lang.isBlank()
|
||||
}
|
@@ -0,0 +1,17 @@
|
||||
package tachiyomi.domain.updates.model
|
||||
|
||||
import tachiyomi.domain.manga.model.MangaCover
|
||||
|
||||
data class UpdatesWithRelations(
|
||||
val mangaId: Long,
|
||||
val mangaTitle: String,
|
||||
val chapterId: Long,
|
||||
val chapterName: String,
|
||||
val scanlator: String?,
|
||||
val read: Boolean,
|
||||
val bookmark: Boolean,
|
||||
val lastPageRead: Long,
|
||||
val sourceId: Long,
|
||||
val dateFetch: Long,
|
||||
val coverData: MangaCover,
|
||||
)
|
@@ -0,0 +1,66 @@
|
||||
package tachiyomi.domain.library.model
|
||||
|
||||
import org.junit.jupiter.api.Assertions.assertEquals
|
||||
import org.junit.jupiter.api.Assertions.assertNotEquals
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
class LibraryFlagsTest {
|
||||
|
||||
@Test
|
||||
fun `Check the amount of flags`() {
|
||||
assertEquals(4, LibraryDisplayMode.values.size)
|
||||
assertEquals(8, LibrarySort.types.size)
|
||||
assertEquals(2, LibrarySort.directions.size)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Test Flag plus operator (LibraryDisplayMode)`() {
|
||||
val current = LibraryDisplayMode.List
|
||||
val new = LibraryDisplayMode.CoverOnlyGrid
|
||||
val flag = current + new
|
||||
|
||||
assertEquals(0b00000011, flag)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Test Flag plus operator (LibrarySort)`() {
|
||||
val current = LibrarySort(LibrarySort.Type.LastRead, LibrarySort.Direction.Ascending)
|
||||
val new = LibrarySort(LibrarySort.Type.DateAdded, LibrarySort.Direction.Ascending)
|
||||
val flag = current + new
|
||||
|
||||
assertEquals(0b01011100, flag)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Test Flag plus operator`() {
|
||||
val display = LibraryDisplayMode.CoverOnlyGrid
|
||||
val sort = LibrarySort(LibrarySort.Type.DateAdded, LibrarySort.Direction.Ascending)
|
||||
val flag = display + sort
|
||||
|
||||
assertEquals(0b01011111, flag)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Test Flag plus operator with old flag as base`() {
|
||||
val currentDisplay = LibraryDisplayMode.List
|
||||
val currentSort = LibrarySort(LibrarySort.Type.UnreadCount, LibrarySort.Direction.Descending)
|
||||
val currentFlag = currentDisplay + currentSort
|
||||
|
||||
val display = LibraryDisplayMode.CoverOnlyGrid
|
||||
val sort = LibrarySort(LibrarySort.Type.DateAdded, LibrarySort.Direction.Ascending)
|
||||
val flag = currentFlag + display + sort
|
||||
|
||||
assertEquals(0b00001110, currentFlag)
|
||||
assertEquals(0b01011111, flag)
|
||||
assertNotEquals(currentFlag, flag)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Test default flags`() {
|
||||
val sort = LibrarySort.default
|
||||
val display = LibraryDisplayMode.default
|
||||
val flag = display + sort.type + sort.direction
|
||||
|
||||
assertEquals(0b01000000, flag)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user