Skip to content

Commit

Permalink
Sort per category (#5408)
Browse files Browse the repository at this point in the history
* Add flags for sorting per category

* Add logic to LibrarySettingsSheet

* Add  logic to LibraryPresenter

* Minor tweaks

* Use enum instead of variables

Also deprecates LibrarySort in favour of the new enum classes

* Remove debug log and suppress deprecation

* Convert DisplayMode setting to enum

Also fix bug were adapter would get de-synced with the current per category setting

* Fix migration crashing app due to values being access before migration
  • Loading branch information
ghostbear authored Jun 26, 2021
1 parent 64c9530 commit 6089014
Show file tree
Hide file tree
Showing 18 changed files with 289 additions and 124 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ android {
minSdkVersion(AndroidConfig.minSdk)
targetSdkVersion(AndroidConfig.targetSdk)
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
versionCode = 63
versionCode = 64
versionName = "0.11.1"

buildConfigField("String", "COMMIT_COUNT", "\"${getCommitCount()}\"")
Expand Down
46 changes: 44 additions & 2 deletions app/src/main/java/eu/kanade/tachiyomi/Migrations.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import eu.kanade.tachiyomi.data.updater.UpdaterJob
import eu.kanade.tachiyomi.extension.ExtensionUpdateJob
import eu.kanade.tachiyomi.network.PREF_DOH_CLOUDFLARE
import eu.kanade.tachiyomi.ui.library.LibrarySort
import eu.kanade.tachiyomi.ui.library.setting.SortDirectionSetting
import eu.kanade.tachiyomi.ui.library.setting.SortModeSetting
import eu.kanade.tachiyomi.ui.reader.setting.OrientationType
import eu.kanade.tachiyomi.util.system.toast
import eu.kanade.tachiyomi.widget.ExtendedNavigationView
Expand Down Expand Up @@ -96,9 +98,15 @@ object Migrations {
}
if (oldVersion < 44) {
// Reset sorting preference if using removed sort by source
val prefs = PreferenceManager.getDefaultSharedPreferences(context)

val oldSortingMode = prefs.getInt(PreferenceKeys.librarySortingMode, 0)

@Suppress("DEPRECATION")
if (preferences.librarySortingMode().get() == LibrarySort.SOURCE) {
preferences.librarySortingMode().set(LibrarySort.ALPHA)
if (oldSortingMode == LibrarySort.SOURCE) {
prefs.edit {
putInt(PreferenceKeys.librarySortingMode, LibrarySort.ALPHA)
}
}
}
if (oldVersion < 52) {
Expand Down Expand Up @@ -190,6 +198,40 @@ object Migrations {
LibraryUpdateJob.setupTask(context, 3)
}
}
if (oldVersion < 64) {
val prefs = PreferenceManager.getDefaultSharedPreferences(context)

val oldSortingMode = prefs.getInt(PreferenceKeys.librarySortingMode, 0)
val oldSortingDirection = prefs.getBoolean(PreferenceKeys.librarySortingDirection, true)

@Suppress("DEPRECATION")
val newSortingMode = when (oldSortingMode) {
LibrarySort.ALPHA -> SortModeSetting.ALPHABETICAL
LibrarySort.LAST_READ -> SortModeSetting.LAST_READ
LibrarySort.LAST_CHECKED -> SortModeSetting.LAST_CHECKED
LibrarySort.UNREAD -> SortModeSetting.UNREAD
LibrarySort.TOTAL -> SortModeSetting.TOTAL_CHAPTERS
LibrarySort.LATEST_CHAPTER -> SortModeSetting.LATEST_CHAPTER
LibrarySort.CHAPTER_FETCH_DATE -> SortModeSetting.DATE_FETCHED
LibrarySort.DATE_ADDED -> SortModeSetting.DATE_ADDED
else -> SortModeSetting.ALPHABETICAL
}

val newSortingDirection = when (oldSortingDirection) {
true -> SortDirectionSetting.ASCENDING
else -> SortDirectionSetting.DESCENDING
}

prefs.edit(commit = true) {
remove(PreferenceKeys.librarySortingMode)
remove(PreferenceKeys.librarySortingDirection)
}

prefs.edit {
putString(PreferenceKeys.librarySortingMode, newSortingMode.name)
putString(PreferenceKeys.librarySortingDirection, newSortingDirection.name)
}
}
return true
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package eu.kanade.tachiyomi.data.database.models

import eu.kanade.tachiyomi.ui.library.setting.DisplayModeSetting
import eu.kanade.tachiyomi.ui.library.setting.SortDirectionSetting
import eu.kanade.tachiyomi.ui.library.setting.SortModeSetting
import java.io.Serializable

interface Category : Serializable {
Expand All @@ -17,15 +20,18 @@ interface Category : Serializable {
}

var displayMode: Int
get() = flags and MASK
set(mode) = setFlags(mode, MASK)
get() = flags and DisplayModeSetting.MASK
set(mode) = setFlags(mode, DisplayModeSetting.MASK)

companion object {
var sortMode: Int
get() = flags and SortModeSetting.MASK
set(mode) = setFlags(mode, SortModeSetting.MASK)

var sortDirection: Int
get() = flags and SortDirectionSetting.MASK
set(mode) = setFlags(mode, SortDirectionSetting.MASK)

const val COMPACT_GRID = 0b00000000
const val COMFORTABLE_GRID = 0b00000001
const val LIST = 0b00000010
const val MASK = 0b00000011
companion object {

fun create(name: String): Category = CategoryImpl().apply {
this.name = name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ object PreferenceKeys {
const val filterTracked = "pref_filter_library_tracked"

const val librarySortingMode = "library_sorting_mode"
const val librarySortingDirection = "library_sorting_ascending"

const val automaticExtUpdates = "automatic_ext_updates"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,6 @@ object PreferenceValues {

/* ktlint-enable experimental:enum-entry-name-case */

enum class DisplayMode {
COMPACT_GRID,
COMFORTABLE_GRID,
LIST,
}

enum class TappingInvertMode(val shouldInvertHorizontal: Boolean = false, val shouldInvertVertical: Boolean = false) {
NONE,
HORIZONTAL(shouldInvertHorizontal = true),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ import com.tfcporciuncula.flow.FlowSharedPreferences
import com.tfcporciuncula.flow.Preference
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.database.models.Manga
import eu.kanade.tachiyomi.data.preference.PreferenceValues.DisplayMode
import eu.kanade.tachiyomi.data.preference.PreferenceValues.ThemeMode.*
import eu.kanade.tachiyomi.data.track.TrackService
import eu.kanade.tachiyomi.data.track.anilist.Anilist
import eu.kanade.tachiyomi.ui.library.setting.DisplayModeSetting
import eu.kanade.tachiyomi.ui.library.setting.SortDirectionSetting
import eu.kanade.tachiyomi.ui.library.setting.SortModeSetting
import eu.kanade.tachiyomi.ui.reader.setting.OrientationType
import eu.kanade.tachiyomi.ui.reader.setting.ReadingModeType
import eu.kanade.tachiyomi.widget.ExtendedNavigationView
Expand Down Expand Up @@ -184,7 +186,7 @@ class PreferencesHelper(val context: Context) {

fun lastVersionCode() = flowPrefs.getInt("last_version_code", 0)

fun sourceDisplayMode() = flowPrefs.getEnum(Keys.sourceDisplayMode, DisplayMode.COMPACT_GRID)
fun sourceDisplayMode() = flowPrefs.getEnum(Keys.sourceDisplayMode, DisplayModeSetting.COMPACT_GRID)

fun enabledLanguages() = flowPrefs.getStringSet(Keys.enabledLanguages, setOf("en", Locale.getDefault().language))

Expand Down Expand Up @@ -235,7 +237,7 @@ class PreferencesHelper(val context: Context) {

fun libraryUpdatePrioritization() = flowPrefs.getInt(Keys.libraryUpdatePrioritization, 0)

fun libraryDisplayMode() = flowPrefs.getEnum(Keys.libraryDisplayMode, DisplayMode.COMPACT_GRID)
fun libraryDisplayMode() = flowPrefs.getEnum(Keys.libraryDisplayMode, DisplayModeSetting.COMPACT_GRID)

fun downloadBadge() = flowPrefs.getBoolean(Keys.downloadBadge, false)

Expand All @@ -257,9 +259,8 @@ class PreferencesHelper(val context: Context) {

fun filterTracking(name: Int) = flowPrefs.getInt("${Keys.filterTracked}_$name", ExtendedNavigationView.Item.TriStateGroup.State.IGNORE.value)

fun librarySortingMode() = flowPrefs.getInt(Keys.librarySortingMode, 0)

fun librarySortingAscending() = flowPrefs.getBoolean("library_sorting_ascending", true)
fun librarySortingMode() = flowPrefs.getEnum(Keys.librarySortingMode, SortModeSetting.ALPHABETICAL)
fun librarySortingAscending() = flowPrefs.getEnum(Keys.librarySortingDirection, SortDirectionSetting.ASCENDING)

fun automaticExtUpdates() = flowPrefs.getBoolean(Keys.automaticExtUpdates, true)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import eu.davidea.flexibleadapter.items.IFlexible
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.database.models.Category
import eu.kanade.tachiyomi.data.database.models.Manga
import eu.kanade.tachiyomi.data.preference.PreferenceValues.DisplayMode
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.data.preference.asImmediateFlow
import eu.kanade.tachiyomi.databinding.SourceControllerBinding
Expand All @@ -37,6 +36,7 @@ import eu.kanade.tachiyomi.ui.base.controller.SearchableNucleusController
import eu.kanade.tachiyomi.ui.base.controller.withFadeTransaction
import eu.kanade.tachiyomi.ui.browse.source.globalsearch.GlobalSearchController
import eu.kanade.tachiyomi.ui.library.ChangeMangaCategoriesDialog
import eu.kanade.tachiyomi.ui.library.setting.DisplayModeSetting
import eu.kanade.tachiyomi.ui.main.MainActivity
import eu.kanade.tachiyomi.ui.manga.MangaController
import eu.kanade.tachiyomi.ui.more.MoreController
Expand Down Expand Up @@ -205,7 +205,7 @@ open class BrowseSourceController(bundle: Bundle) :
binding.catalogueView.removeView(oldRecycler)
}

val recycler = if (preferences.sourceDisplayMode().get() == DisplayMode.LIST) {
val recycler = if (preferences.sourceDisplayMode().get() == DisplayModeSetting.LIST) {
RecyclerView(view.context).apply {
id = R.id.recycler
layoutManager = LinearLayoutManager(context)
Expand Down Expand Up @@ -273,9 +273,9 @@ open class BrowseSourceController(bundle: Bundle) :
)

val displayItem = when (preferences.sourceDisplayMode().get()) {
DisplayMode.COMPACT_GRID -> R.id.action_compact_grid
DisplayMode.COMFORTABLE_GRID -> R.id.action_comfortable_grid
DisplayMode.LIST -> R.id.action_list
DisplayModeSetting.COMPACT_GRID -> R.id.action_compact_grid
DisplayModeSetting.COMFORTABLE_GRID -> R.id.action_comfortable_grid
DisplayModeSetting.LIST -> R.id.action_list
}
menu.findItem(displayItem).isChecked = true
}
Expand All @@ -297,9 +297,9 @@ open class BrowseSourceController(bundle: Bundle) :
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.action_search -> expandActionViewFromInteraction = true
R.id.action_compact_grid -> setDisplayMode(DisplayMode.COMPACT_GRID)
R.id.action_comfortable_grid -> setDisplayMode(DisplayMode.COMFORTABLE_GRID)
R.id.action_list -> setDisplayMode(DisplayMode.LIST)
R.id.action_compact_grid -> setDisplayMode(DisplayModeSetting.COMPACT_GRID)
R.id.action_comfortable_grid -> setDisplayMode(DisplayModeSetting.COMFORTABLE_GRID)
R.id.action_list -> setDisplayMode(DisplayModeSetting.LIST)
R.id.action_open_in_web_view -> openInWebView()
R.id.action_local_source_help -> openLocalSourceHelpGuide()
}
Expand Down Expand Up @@ -446,7 +446,7 @@ open class BrowseSourceController(bundle: Bundle) :
*
* @param mode the mode to change to
*/
private fun setDisplayMode(mode: DisplayMode) {
private fun setDisplayMode(mode: DisplayModeSetting) {
val view = view ?: return
val adapter = adapter ?: return

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ import eu.davidea.flexibleadapter.items.AbstractFlexibleItem
import eu.davidea.flexibleadapter.items.IFlexible
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.database.models.Manga
import eu.kanade.tachiyomi.data.preference.PreferenceValues.DisplayMode
import eu.kanade.tachiyomi.databinding.SourceComfortableGridItemBinding
import eu.kanade.tachiyomi.databinding.SourceCompactGridItemBinding
import eu.kanade.tachiyomi.ui.library.setting.DisplayModeSetting
import eu.kanade.tachiyomi.widget.AutofitRecyclerView

class SourceItem(val manga: Manga, private val displayMode: Preference<DisplayMode>) :
class SourceItem(val manga: Manga, private val displayMode: Preference<DisplayModeSetting>) :
AbstractFlexibleItem<SourceHolder<*>>() {

override fun getLayoutRes(): Int {
return when (displayMode.get()) {
DisplayMode.COMPACT_GRID -> R.layout.source_compact_grid_item
DisplayMode.COMFORTABLE_GRID -> R.layout.source_comfortable_grid_item
DisplayMode.LIST -> R.layout.source_list_item
DisplayModeSetting.COMPACT_GRID -> R.layout.source_compact_grid_item
DisplayModeSetting.COMFORTABLE_GRID -> R.layout.source_comfortable_grid_item
DisplayModeSetting.LIST -> R.layout.source_list_item
}
}

Expand All @@ -33,7 +33,7 @@ class SourceItem(val manga: Manga, private val displayMode: Preference<DisplayMo
adapter: FlexibleAdapter<IFlexible<RecyclerView.ViewHolder>>
): SourceHolder<*> {
return when (displayMode.get()) {
DisplayMode.COMPACT_GRID -> {
DisplayModeSetting.COMPACT_GRID -> {
val binding = SourceCompactGridItemBinding.bind(view)
val parent = adapter.recyclerView as AutofitRecyclerView
val coverHeight = parent.itemWidth / 3 * 4
Expand All @@ -50,7 +50,7 @@ class SourceItem(val manga: Manga, private val displayMode: Preference<DisplayMo
}
SourceGridHolder(view, adapter)
}
DisplayMode.COMFORTABLE_GRID -> {
DisplayModeSetting.COMFORTABLE_GRID -> {
val binding = SourceComfortableGridItemBinding.bind(view)
val parent = adapter.recyclerView as AutofitRecyclerView
val coverHeight = parent.itemWidth / 3 * 4
Expand All @@ -62,7 +62,7 @@ class SourceItem(val manga: Manga, private val displayMode: Preference<DisplayMo
}
SourceComfortableGridHolder(view, adapter)
}
DisplayMode.LIST -> {
DisplayModeSetting.LIST -> {
SourceListHolder(view, adapter)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.database.models.Category
import eu.kanade.tachiyomi.data.database.models.Manga
import eu.kanade.tachiyomi.data.library.LibraryUpdateService
import eu.kanade.tachiyomi.data.preference.PreferenceValues.DisplayMode
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.databinding.LibraryCategoryBinding
import eu.kanade.tachiyomi.util.lang.plusAssign
Expand All @@ -28,6 +27,7 @@ import reactivecircus.flowbinding.swiperefreshlayout.refreshes
import rx.subscriptions.CompositeSubscription
import uy.kohesive.injekt.injectLazy
import java.util.ArrayDeque
import eu.kanade.tachiyomi.ui.library.setting.DisplayModeSetting as DisplayMode

/**
* Fragment containing the library manga for a certain category.
Expand Down Expand Up @@ -125,7 +125,7 @@ class LibraryCategoryView @JvmOverloads constructor(context: Context, attrs: Att

// If displayMode should be set from category adjust manga count per row
if (preferences.categorisedDisplaySettings().get()) {
recycler.spanCount = if (category.displayMode == Category.LIST || (preferences.libraryDisplayMode().get() == DisplayMode.LIST && category.id == 0)) {
recycler.spanCount = if (DisplayMode.fromFlag(category.displayMode) == DisplayMode.LIST || (preferences.libraryDisplayMode().get() == DisplayMode.LIST && category.id == 0)) {
1
} else {
controller.mangaPerRow
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,11 @@ class LibraryController(
.map { (it.id ?: -1) to (mangaMap[it.id]?.size ?: 0) }
.toMap()

if (preferences.categorisedDisplaySettings().get()) {
// Reattach adapter so it doesn't get de-synced
reattachAdapter()
}

// Restore active category.
binding.libraryPager.setCurrentItem(activeCat, false)

Expand Down
Loading

0 comments on commit 6089014

Please sign in to comment.