Tweak flag classes for Library (#7829)

* Tweak flag classes for Library

- Add interface for Flag and Mask
- Merge Sort Type and Direction into one class
- Use custom serializers for preferences
  - Mainly to not break the old

* Review changes
This commit is contained in:
Andreas
2022-08-29 19:10:30 +02:00
committed by GitHub
parent 3b34a878a7
commit 880407442c
25 changed files with 442 additions and 250 deletions
@@ -0,0 +1,69 @@
package eu.kanade.tachiyomi.util.chapter
import eu.kanade.tachiyomi.ui.library.setting.LibraryDisplayMode
import eu.kanade.tachiyomi.ui.library.setting.LibrarySort
import eu.kanade.tachiyomi.ui.library.setting.plus
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)
}
}