From da5f10a2f1ac4f65c740c9838558456c43855678 Mon Sep 17 00:00:00 2001 From: arkon Date: Mon, 14 Sep 2020 16:13:28 -0400 Subject: [PATCH] Clean up global chapter settings code --- .../data/preference/PreferencesHelper.kt | 20 +++--- .../source/browse/BrowseSourcePresenter.kt | 3 +- .../tachiyomi/ui/library/LibraryController.kt | 2 +- .../ui/library/LibrarySettingsSheet.kt | 12 ++-- .../tachiyomi/ui/manga/MangaController.kt | 2 +- .../tachiyomi/ui/manga/MangaPresenter.kt | 2 +- .../ui/manga/chapter/ChaptersSettingsSheet.kt | 34 +++++++++-- .../manga/chapter/SetChapterSettingsDialog.kt | 34 ++++++----- .../util/chapter/ChapterSettingsHelper.kt | 61 ++++++++++--------- .../widget/TabbedBottomSheetDialog.kt | 38 ++---------- .../main/res/layout/common_tabbed_sheet.xml | 4 +- .../main/res/menu/default_chapter_filter.xml | 6 +- app/src/main/res/values/strings.xml | 8 +-- 13 files changed, 112 insertions(+), 114 deletions(-) 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 b1836fee9a..c8961a7873 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 @@ -9,8 +9,6 @@ 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.PreferenceKeys as Keys -import eu.kanade.tachiyomi.data.preference.PreferenceValues as Values import eu.kanade.tachiyomi.data.preference.PreferenceValues.DisplayMode import eu.kanade.tachiyomi.data.preference.PreferenceValues.NsfwAllowance import eu.kanade.tachiyomi.data.track.TrackService @@ -22,6 +20,8 @@ import java.io.File import java.text.DateFormat import java.text.SimpleDateFormat import java.util.Locale +import eu.kanade.tachiyomi.data.preference.PreferenceKeys as Keys +import eu.kanade.tachiyomi.data.preference.PreferenceValues as Values @OptIn(ExperimentalCoroutinesApi::class) fun Preference.asImmediateFlow(block: (value: T) -> Unit): Flow { @@ -264,16 +264,14 @@ class PreferencesHelper(val context: Context) { fun sortChapterByAscendingOrDescending() = prefs.getInt(Keys.defaultChapterSortByAscendingOrDescending, Manga.SORT_DESC) - fun setChapterSettingsDefault(m: Manga) { + fun setChapterSettingsDefault(manga: Manga) { prefs.edit { - putInt(Keys.defaultChapterFilterByRead, m.readFilter) - putInt(Keys.defaultChapterFilterByDownloaded, m.downloadedFilter) - putInt(Keys.defaultChapterFilterByBookmarked, m.bookmarkedFilter) - putInt(Keys.defaultChapterSortBySourceOrNumber, m.sorting) - putInt(Keys.defaultChapterDisplayByNameOrNumber, m.displayMode) + putInt(Keys.defaultChapterFilterByRead, manga.readFilter) + putInt(Keys.defaultChapterFilterByDownloaded, manga.downloadedFilter) + putInt(Keys.defaultChapterFilterByBookmarked, manga.bookmarkedFilter) + putInt(Keys.defaultChapterSortBySourceOrNumber, manga.sorting) + putInt(Keys.defaultChapterDisplayByNameOrNumber, manga.displayMode) + putInt(Keys.defaultChapterSortByAscendingOrDescending, if (manga.sortDescending()) Manga.SORT_DESC else Manga.SORT_ASC) } - - if (m.sortDescending()) prefs.edit { putInt(Keys.defaultChapterSortByAscendingOrDescending, Manga.SORT_DESC) } - else prefs.edit { putInt(Keys.defaultChapterSortByAscendingOrDescending, Manga.SORT_ASC) } } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/BrowseSourcePresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/BrowseSourcePresenter.kt index 39be964aaa..d406ca3fb0 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/BrowseSourcePresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/BrowseSourcePresenter.kt @@ -30,7 +30,6 @@ import eu.kanade.tachiyomi.ui.browse.source.filter.TriStateItem import eu.kanade.tachiyomi.ui.browse.source.filter.TriStateSectionItem import eu.kanade.tachiyomi.util.chapter.ChapterSettingsHelper import eu.kanade.tachiyomi.util.removeCovers -import kotlinx.coroutines.flow.subscribe import rx.Observable import rx.Subscription import rx.android.schedulers.AndroidSchedulers @@ -270,7 +269,7 @@ open class BrowseSourcePresenter( if (!manga.favorite) { manga.removeCovers(coverCache) } else { - ChapterSettingsHelper.applySettingDefaultsFromPreferences(manga) + ChapterSettingsHelper.applySettingDefaults(manga) } db.insertManga(manga).executeAsBlocking() diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryController.kt index 9ecefbd025..60cdfb9363 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryController.kt @@ -183,7 +183,7 @@ class LibraryController( createActionModeIfNeeded() } - settingsSheet = LibrarySettingsSheet(activity!!) { group -> + settingsSheet = LibrarySettingsSheet(router) { group -> when (group) { is LibrarySettingsSheet.Filter.FilterGroup -> onFilterChanged() is LibrarySettingsSheet.Sort.SortGroup -> onSortChanged() 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 d4b3d3af58..2491522324 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 @@ -1,9 +1,9 @@ package eu.kanade.tachiyomi.ui.library -import android.app.Activity import android.content.Context import android.util.AttributeSet import android.view.View +import com.bluelinelabs.conductor.Router import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.preference.PreferenceValues.DisplayMode import eu.kanade.tachiyomi.data.preference.PreferencesHelper @@ -12,22 +12,22 @@ import eu.kanade.tachiyomi.widget.TabbedBottomSheetDialog import uy.kohesive.injekt.injectLazy class LibrarySettingsSheet( - activity: Activity, + router: Router, onGroupClickListener: (ExtendedNavigationView.Group) -> Unit -) : TabbedBottomSheetDialog(activity) { +) : TabbedBottomSheetDialog(router) { val filters: Filter private val sort: Sort private val display: Display init { - filters = Filter(activity) + filters = Filter(router.activity!!) filters.onGroupClicked = onGroupClickListener - sort = Sort(activity) + sort = Sort(router.activity!!) sort.onGroupClicked = onGroupClickListener - display = Display(activity) + display = Display(router.activity!!) display.onGroupClicked = onGroupClickListener } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaController.kt index 40a9174581..0e46fb3932 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaController.kt @@ -237,7 +237,7 @@ class MangaController : binding.actionToolbar.offsetAppbarHeight(activity!!) - settingsSheet = ChaptersSettingsSheet(activity!!, presenter) { group -> + settingsSheet = ChaptersSettingsSheet(router, presenter) { group -> if (group is ChaptersSettingsSheet.Filter.FilterGroup) { updateFilterIconState() chaptersAdapter?.notifyDataSetChanged() diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaPresenter.kt index 103dd27247..24002f7fe4 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaPresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaPresenter.kt @@ -84,7 +84,7 @@ class MangaPresenter( super.onCreate(savedState) if (!manga.favorite) { - ChapterSettingsHelper.applySettingDefaultsFromPreferences(manga) + ChapterSettingsHelper.applySettingDefaults(manga) } // Manga info - start diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersSettingsSheet.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersSettingsSheet.kt index 60fb9a8697..321369039c 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersSettingsSheet.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersSettingsSheet.kt @@ -1,34 +1,39 @@ package eu.kanade.tachiyomi.ui.manga.chapter -import android.app.Activity import android.content.Context import android.util.AttributeSet import android.view.View +import androidx.core.view.isVisible +import com.bluelinelabs.conductor.Router import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.database.models.Manga import eu.kanade.tachiyomi.ui.manga.MangaPresenter +import eu.kanade.tachiyomi.util.view.popupMenu import eu.kanade.tachiyomi.widget.ExtendedNavigationView import eu.kanade.tachiyomi.widget.TabbedBottomSheetDialog class ChaptersSettingsSheet( - activity: Activity, + private val router: Router, private val presenter: MangaPresenter, onGroupClickListener: (ExtendedNavigationView.Group) -> Unit -) : TabbedBottomSheetDialog(activity, presenter.manga) { +) : TabbedBottomSheetDialog(router) { val filters: Filter private val sort: Sort private val display: Display init { - filters = Filter(activity) + filters = Filter(router.activity!!) filters.onGroupClicked = onGroupClickListener - sort = Sort(activity) + sort = Sort(router.activity!!) sort.onGroupClicked = onGroupClickListener - display = Display(activity) + display = Display(router.activity!!) display.onGroupClicked = onGroupClickListener + + binding.menu.isVisible = true + binding.menu.setOnClickListener { it.post { showPopupMenu(it) } } } override fun getTabViews(): List = listOf( @@ -43,6 +48,23 @@ class ChaptersSettingsSheet( R.string.action_display ) + private fun showPopupMenu(view: View) { + view.popupMenu( + R.menu.default_chapter_filter, + { + }, + { + when (this.itemId) { + R.id.set_as_default -> { + SetChapterSettingsDialog(presenter.manga).showDialog(router) + true + } + else -> true + } + } + ) + } + /** * Filters group (unread, downloaded, ...). */ diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/SetChapterSettingsDialog.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/SetChapterSettingsDialog.kt index 4ba7d83c6e..3230345365 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/SetChapterSettingsDialog.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/SetChapterSettingsDialog.kt @@ -1,46 +1,48 @@ package eu.kanade.tachiyomi.ui.manga.chapter import android.app.Dialog -import android.content.Context +import android.os.Bundle import com.afollestad.materialdialogs.MaterialDialog import com.afollestad.materialdialogs.customview.customView import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.database.models.Manga +import eu.kanade.tachiyomi.ui.base.controller.DialogController import eu.kanade.tachiyomi.util.chapter.ChapterSettingsHelper import eu.kanade.tachiyomi.util.system.toast import eu.kanade.tachiyomi.widget.DialogCheckboxView -class SetChapterSettingsDialog(val context: Context, val manga: Manga) { - private var dialog: Dialog +class SetChapterSettingsDialog(bundle: Bundle? = null) : DialogController(bundle) { - init { - this.dialog = buildDialog() - } + constructor(manga: Manga) : this( + Bundle().apply { + putSerializable(MANGA_KEY, manga) + } + ) - private fun buildDialog(): Dialog { - val view = DialogCheckboxView(context).apply { + override fun onCreateDialog(savedViewState: Bundle?): Dialog { + val view = DialogCheckboxView(activity!!).apply { setDescription(R.string.confirm_set_chapter_settings) setOptionDescription(R.string.also_set_chapter_settings_for_library) } - return MaterialDialog(context) - .title(R.string.action_chapter_settings) + return MaterialDialog(activity!!) + .title(R.string.chapter_settings) .customView( view = view, horizontalPadding = true ) .positiveButton(android.R.string.ok) { - ChapterSettingsHelper.setNewSettingDefaults(manga) + ChapterSettingsHelper.setGlobalSettings(args.getSerializable(MANGA_KEY)!! as Manga) if (view.isChecked()) { - ChapterSettingsHelper.updateAllMangasWithDefaultsFromPreferences() + ChapterSettingsHelper.updateAllMangasWithGlobalDefaults() } - context.toast(context.getString(R.string.chapter_settings_updated)) + activity?.toast(activity!!.getString(R.string.chapter_settings_updated)) } .negativeButton(android.R.string.cancel) } - fun showDialog() = dialog.show() - - fun dismissDialog() = dialog.dismiss() + private companion object { + const val MANGA_KEY = "manga" + } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/chapter/ChapterSettingsHelper.kt b/app/src/main/java/eu/kanade/tachiyomi/util/chapter/ChapterSettingsHelper.kt index a797c3799d..cccb214d6c 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/util/chapter/ChapterSettingsHelper.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/util/chapter/ChapterSettingsHelper.kt @@ -4,52 +4,55 @@ import eu.kanade.tachiyomi.data.database.DatabaseHelper import eu.kanade.tachiyomi.data.database.models.Manga import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.util.lang.launchIO -import uy.kohesive.injekt.Injekt -import uy.kohesive.injekt.api.get +import uy.kohesive.injekt.injectLazy object ChapterSettingsHelper { - private val prefs = Injekt.get() - private val db: DatabaseHelper = Injekt.get() + + private val prefs: PreferencesHelper by injectLazy() + private val db: DatabaseHelper by injectLazy() /** - * updates the Chapter Settings in Preferences + * Updates the global Chapter Settings in Preferences. */ - fun setNewSettingDefaults(m: Manga?) { - m?.let { + fun setGlobalSettings(manga: Manga?) { + manga?.let { prefs.setChapterSettingsDefault(it) db.updateFlags(it).executeAsBlocking() } } /** - * updates a single manga's Chapter Settings to match what's set in Preferences + * Updates a single manga's Chapter Settings to match what's set in Preferences. */ - fun applySettingDefaultsFromPreferences(m: Manga) { - m.readFilter = prefs.filterChapterByRead() - m.downloadedFilter = prefs.filterChapterByDownloaded() - m.bookmarkedFilter = prefs.filterChapterByBookmarked() - m.sorting = prefs.sortChapterBySourceOrNumber() - m.displayMode = prefs.displayChapterByNameOrNumber() - m.setChapterOrder(prefs.sortChapterByAscendingOrDescending()) - db.updateFlags(m).executeAsBlocking() + fun applySettingDefaults(manga: Manga) { + with(manga) { + readFilter = prefs.filterChapterByRead() + downloadedFilter = prefs.filterChapterByDownloaded() + bookmarkedFilter = prefs.filterChapterByBookmarked() + sorting = prefs.sortChapterBySourceOrNumber() + displayMode = prefs.displayChapterByNameOrNumber() + setChapterOrder(prefs.sortChapterByAscendingOrDescending()) + } + + db.updateFlags(manga).executeAsBlocking() } /** - * updates all mangas in database Chapter Settings to match what's set in Preferences + * Updates all mangas in library with global Chapter Settings. */ - fun updateAllMangasWithDefaultsFromPreferences() { + fun updateAllMangasWithGlobalDefaults() { launchIO { - val dbMangas = db.getMangas().executeAsBlocking().toMutableList() - - val updatedMangas = dbMangas.map { m -> - m.readFilter = prefs.filterChapterByRead() - m.downloadedFilter = prefs.filterChapterByDownloaded() - m.bookmarkedFilter = prefs.filterChapterByBookmarked() - m.sorting = prefs.sortChapterBySourceOrNumber() - m.displayMode = prefs.displayChapterByNameOrNumber() - m.setChapterOrder(prefs.sortChapterByAscendingOrDescending()) - m - }.toList() + val updatedMangas = db.getMangas().executeAsBlocking().map { manga -> + with(manga) { + readFilter = prefs.filterChapterByRead() + downloadedFilter = prefs.filterChapterByDownloaded() + bookmarkedFilter = prefs.filterChapterByBookmarked() + sorting = prefs.sortChapterBySourceOrNumber() + displayMode = prefs.displayChapterByNameOrNumber() + setChapterOrder(prefs.sortChapterByAscendingOrDescending()) + } + manga + } db.updateFlags(updatedMangas).executeAsBlocking() } diff --git a/app/src/main/java/eu/kanade/tachiyomi/widget/TabbedBottomSheetDialog.kt b/app/src/main/java/eu/kanade/tachiyomi/widget/TabbedBottomSheetDialog.kt index 07b72f6598..8b840ac294 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/widget/TabbedBottomSheetDialog.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/widget/TabbedBottomSheetDialog.kt @@ -1,17 +1,14 @@ package eu.kanade.tachiyomi.widget -import android.app.Activity import android.view.View import android.view.ViewGroup +import com.bluelinelabs.conductor.Router import com.google.android.material.bottomsheet.BottomSheetDialog -import eu.kanade.tachiyomi.R -import eu.kanade.tachiyomi.data.database.models.Manga import eu.kanade.tachiyomi.databinding.CommonTabbedSheetBinding -import eu.kanade.tachiyomi.ui.manga.chapter.SetChapterSettingsDialog -import eu.kanade.tachiyomi.util.view.popupMenu -abstract class TabbedBottomSheetDialog(private val activity: Activity, private val manga: Manga? = null) : BottomSheetDialog(activity) { - val binding: CommonTabbedSheetBinding = CommonTabbedSheetBinding.inflate(activity.layoutInflater) +abstract class TabbedBottomSheetDialog(private val router: Router) : BottomSheetDialog(router.activity!!) { + + val binding: CommonTabbedSheetBinding = CommonTabbedSheetBinding.inflate(router.activity!!.layoutInflater) init { val adapter = LibrarySettingsSheetAdapter() @@ -19,34 +16,9 @@ abstract class TabbedBottomSheetDialog(private val activity: Activity, private v binding.pager.adapter = adapter binding.tabs.setupWithViewPager(binding.pager) - // currently, we only need to show the overflow menu if this is a ChaptersSettingsSheet - if (manga != null) { - binding.menu.visibility = View.VISIBLE - binding.menu.setOnClickListener { it.post { showPopupMenu(it) } } - } else { - binding.menu.visibility = View.GONE - } - setContentView(binding.root) } - private fun showPopupMenu(view: View) { - view.popupMenu( - R.menu.default_chapter_filter, - { - }, - { - when (this.itemId) { - R.id.save_as_default -> { - manga?.let { SetChapterSettingsDialog(context, it).showDialog() } - true - } - else -> true - } - } - ) - } - abstract fun getTabViews(): List abstract fun getTabTitles(): List @@ -62,7 +34,7 @@ abstract class TabbedBottomSheetDialog(private val activity: Activity, private v } override fun getPageTitle(position: Int): CharSequence { - return activity.resources!!.getString(getTabTitles()[position]) + return router.activity!!.resources!!.getString(getTabTitles()[position]) } } } diff --git a/app/src/main/res/layout/common_tabbed_sheet.xml b/app/src/main/res/layout/common_tabbed_sheet.xml index 15792728fa..8c9cba8691 100644 --- a/app/src/main/res/layout/common_tabbed_sheet.xml +++ b/app/src/main/res/layout/common_tabbed_sheet.xml @@ -30,11 +30,13 @@ android:contentDescription="@string/action_menu" android:paddingStart="10dp" android:paddingEnd="10dp" + android:visibility="gone" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent" app:srcCompat="@drawable/ic_more_vert_24dp" - app:tint="?attr/colorOnBackground" /> + app:tint="?attr/colorOnBackground" + tools:visibility="visible" /> diff --git a/app/src/main/res/menu/default_chapter_filter.xml b/app/src/main/res/menu/default_chapter_filter.xml index 403d61c4a2..5cb503cfee 100644 --- a/app/src/main/res/menu/default_chapter_filter.xml +++ b/app/src/main/res/menu/default_chapter_filter.xml @@ -1,8 +1,8 @@ - + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index df8a41ea4d..cfb027c632 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -30,7 +30,6 @@ Settings - Chapter Settings Menu Filter Downloaded @@ -525,9 +524,10 @@ Unread Are you sure you want to delete the selected chapters? Invalid download location + Chapter settings Are you sure you want to save these settings as default? - Also apply to all manga in my Library - Set as Default + Also apply to all manga in my library + Set as default No chapters found @@ -710,5 +710,5 @@ Horizontal Vertical Both - +