From 78a352541a623a1188cba73e89b1ff77be1f1b3a Mon Sep 17 00:00:00 2001 From: arkon Date: Mon, 25 May 2020 15:03:51 -0400 Subject: [PATCH] Use actual enum support for display mode preferences --- .../tachiyomi/data/preference/PreferenceKeys.kt | 4 ++-- .../data/preference/PreferenceValues.kt | 8 ++++---- .../data/preference/PreferencesHelper.kt | 4 ++-- .../source/browse/BrowseSourceController.kt | 11 +++++------ .../ui/browse/source/browse/SourceItem.kt | 16 +++++++--------- .../tachiyomi/ui/library/LibraryCategoryView.kt | 2 +- .../kanade/tachiyomi/ui/library/LibraryItem.kt | 16 +++++++--------- .../tachiyomi/ui/library/LibrarySettingsSheet.kt | 12 ++++++------ 8 files changed, 34 insertions(+), 39 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceKeys.kt b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceKeys.kt index 1ed80836db..f452045c98 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceKeys.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceKeys.kt @@ -73,7 +73,7 @@ object PreferenceKeys { const val lastUsedCategory = "last_used_category" - const val catalogueDisplayMode = "pref_catalogue_display_mode" + const val catalogueDisplayMode = "pref_display_mode_catalogue" const val enabledLanguages = "source_languages" @@ -131,7 +131,7 @@ object PreferenceKeys { const val downloadNewCategories = "download_new_categories" - const val libraryDisplayMode = "pref_library_display_mode" + const val libraryDisplayMode = "pref_display_mode_library" const val lang = "app_language" diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceValues.kt b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceValues.kt index dba9adc004..a9519335cd 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceValues.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceValues.kt @@ -22,9 +22,9 @@ object PreferenceValues { AMOLED("amoled"), } - enum class DisplayMode(val value: Int) { - COMPACT_GRID(0), - COMFORTABLE_GRID(1), - LIST(2), + enum class DisplayMode { + COMPACT_GRID, + COMFORTABLE_GRID, + LIST, } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt index cafef465c8..3baa708be8 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt @@ -139,7 +139,7 @@ class PreferencesHelper(val context: Context) { fun lastVersionCode() = flowPrefs.getInt("last_version_code", 0) - fun catalogueDisplayMode() = flowPrefs.getInt(Keys.catalogueDisplayMode, DisplayMode.COMPACT_GRID.value) + fun catalogueDisplayMode() = flowPrefs.getEnum(Keys.catalogueDisplayMode, DisplayMode.COMPACT_GRID) fun enabledLanguages() = flowPrefs.getStringSet(Keys.enabledLanguages, setOf("en", Locale.getDefault().language)) @@ -185,7 +185,7 @@ class PreferencesHelper(val context: Context) { fun libraryUpdatePrioritization() = flowPrefs.getInt(Keys.libraryUpdatePrioritization, 0) - fun libraryDisplayMode() = flowPrefs.getInt(Keys.libraryDisplayMode, DisplayMode.COMPACT_GRID.value) + fun libraryDisplayMode() = flowPrefs.getEnum(Keys.libraryDisplayMode, DisplayMode.COMPACT_GRID) fun downloadBadge() = flowPrefs.getBoolean(Keys.downloadBadge, false) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/BrowseSourceController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/BrowseSourceController.kt index 04ece72398..f9034bed1b 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/BrowseSourceController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/BrowseSourceController.kt @@ -188,7 +188,7 @@ open class BrowseSourceController(bundle: Bundle) : binding.catalogueView.removeView(oldRecycler) } - val recycler = if (preferences.catalogueDisplayMode().get() == DisplayMode.LIST.value) { + val recycler = if (preferences.catalogueDisplayMode().get() == DisplayMode.LIST) { RecyclerView(view.context).apply { id = R.id.recycler layoutManager = LinearLayoutManager(context) @@ -268,10 +268,9 @@ open class BrowseSourceController(bundle: Bundle) : ) val displayItem = when (preferences.catalogueDisplayMode().get()) { - DisplayMode.COMPACT_GRID.value -> R.id.action_compact_grid - DisplayMode.COMFORTABLE_GRID.value -> R.id.action_comfortable_grid - DisplayMode.LIST.value -> R.id.action_list - else -> throw NotImplementedError("Unknown display mode") + DisplayMode.COMPACT_GRID -> R.id.action_compact_grid + DisplayMode.COMFORTABLE_GRID -> R.id.action_comfortable_grid + DisplayMode.LIST -> R.id.action_list } menu.findItem(displayItem).isChecked = true } @@ -442,7 +441,7 @@ open class BrowseSourceController(bundle: Bundle) : val view = view ?: return val adapter = adapter ?: return - preferences.catalogueDisplayMode().set(mode.value) + preferences.catalogueDisplayMode().set(mode) presenter.refreshDisplayMode() activity?.invalidateOptionsMenu() setupRecycler(view) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/SourceItem.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/SourceItem.kt index 58568f15b6..93942f761a 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/SourceItem.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/SourceItem.kt @@ -17,15 +17,14 @@ import eu.kanade.tachiyomi.widget.AutofitRecyclerView import kotlinx.android.synthetic.main.source_compact_grid_item.view.card import kotlinx.android.synthetic.main.source_compact_grid_item.view.gradient -class SourceItem(val manga: Manga, private val catalogueDisplayMode: Preference) : +class SourceItem(val manga: Manga, private val catalogueDisplayMode: Preference) : AbstractFlexibleItem() { override fun getLayoutRes(): Int { return when (catalogueDisplayMode.get()) { - DisplayMode.COMPACT_GRID.value -> R.layout.source_compact_grid_item - DisplayMode.COMFORTABLE_GRID.value -> R.layout.source_comfortable_grid_item - DisplayMode.LIST.value -> R.layout.source_list_item - else -> throw NotImplementedError("Unknown display mode") + 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 } } @@ -34,7 +33,7 @@ class SourceItem(val manga: Manga, private val catalogueDisplayMode: Preference< adapter: FlexibleAdapter> ): SourceHolder { return when (catalogueDisplayMode.get()) { - DisplayMode.COMPACT_GRID.value -> { + DisplayMode.COMPACT_GRID -> { val parent = adapter.recyclerView as AutofitRecyclerView val coverHeight = parent.itemWidth / 3 * 4 view.apply { @@ -47,7 +46,7 @@ class SourceItem(val manga: Manga, private val catalogueDisplayMode: Preference< } SourceGridHolder(view, adapter) } - DisplayMode.COMFORTABLE_GRID.value -> { + DisplayMode.COMFORTABLE_GRID -> { val parent = adapter.recyclerView as AutofitRecyclerView val coverHeight = parent.itemWidth / 3 * 4 view.apply { @@ -57,10 +56,9 @@ class SourceItem(val manga: Manga, private val catalogueDisplayMode: Preference< } SourceComfortableGridHolder(view, adapter) } - DisplayMode.LIST.value -> { + DisplayMode.LIST -> { SourceListHolder(view, adapter) } - else -> throw NotImplementedError("Unknown display mode") } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryCategoryView.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryCategoryView.kt index 2268811171..a9d1bc9236 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryCategoryView.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryCategoryView.kt @@ -73,7 +73,7 @@ class LibraryCategoryView @JvmOverloads constructor(context: Context, attrs: Att fun onCreate(controller: LibraryController) { this.controller = controller - recycler = if (preferences.libraryDisplayMode().get() == DisplayMode.LIST.value) { + recycler = if (preferences.libraryDisplayMode().get() == DisplayMode.LIST) { (swipe_refresh.inflate(R.layout.library_list_recycler) as RecyclerView).apply { layoutManager = LinearLayoutManager(context) } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryItem.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryItem.kt index 0166944479..5c5450fb1d 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryItem.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryItem.kt @@ -21,7 +21,7 @@ import kotlinx.android.synthetic.main.source_compact_grid_item.view.gradient import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.api.get -class LibraryItem(val manga: LibraryManga, private val libraryDisplayMode: Preference) : +class LibraryItem(val manga: LibraryManga, private val libraryDisplayMode: Preference) : AbstractFlexibleItem(), IFilterable { private val sourceManager: SourceManager = Injekt.get() @@ -31,16 +31,15 @@ class LibraryItem(val manga: LibraryManga, private val libraryDisplayMode: Prefe override fun getLayoutRes(): Int { return when (libraryDisplayMode.get()) { - DisplayMode.COMPACT_GRID.value -> R.layout.source_compact_grid_item - DisplayMode.COMFORTABLE_GRID.value -> R.layout.source_comfortable_grid_item - DisplayMode.LIST.value -> R.layout.source_list_item - else -> throw NotImplementedError("Unknown display mode") + 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 } } override fun createViewHolder(view: View, adapter: FlexibleAdapter>): LibraryHolder { return when (libraryDisplayMode.get()) { - DisplayMode.COMPACT_GRID.value -> { + DisplayMode.COMPACT_GRID -> { val parent = adapter.recyclerView as AutofitRecyclerView val coverHeight = parent.itemWidth / 3 * 4 view.apply { @@ -51,7 +50,7 @@ class LibraryItem(val manga: LibraryManga, private val libraryDisplayMode: Prefe } LibraryGridHolder(view, adapter) } - DisplayMode.COMFORTABLE_GRID.value -> { + DisplayMode.COMFORTABLE_GRID -> { val parent = adapter.recyclerView as AutofitRecyclerView val coverHeight = parent.itemWidth / 3 * 4 view.apply { @@ -61,10 +60,9 @@ class LibraryItem(val manga: LibraryManga, private val libraryDisplayMode: Prefe } LibraryComfortableGridHolder(view, adapter) } - DisplayMode.LIST.value -> { + DisplayMode.LIST -> { LibraryListHolder(view, adapter) } - else -> throw NotImplementedError("Unknown display mode") } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibrarySettingsSheet.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibrarySettingsSheet.kt index 408591282e..437c07f446 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibrarySettingsSheet.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibrarySettingsSheet.kt @@ -193,9 +193,9 @@ class LibrarySettingsSheet( override fun initModels() { val mode = preferences.libraryDisplayMode().get() - compactGrid.checked = mode == DisplayMode.COMPACT_GRID.value - comfortableGrid.checked = mode == DisplayMode.COMFORTABLE_GRID.value - list.checked = mode == DisplayMode.LIST.value + compactGrid.checked = mode == DisplayMode.COMPACT_GRID + comfortableGrid.checked = mode == DisplayMode.COMFORTABLE_GRID + list.checked = mode == DisplayMode.LIST } override fun onItemClicked(item: Item) { @@ -207,9 +207,9 @@ class LibrarySettingsSheet( preferences.libraryDisplayMode().set( when (item) { - compactGrid -> DisplayMode.COMPACT_GRID.value - comfortableGrid -> DisplayMode.COMFORTABLE_GRID.value - list -> DisplayMode.LIST.value + compactGrid -> DisplayMode.COMPACT_GRID + comfortableGrid -> DisplayMode.COMFORTABLE_GRID + list -> DisplayMode.LIST else -> throw NotImplementedError("Unknown display mode") } )