Clean up global chapter settings code

This commit is contained in:
arkon 2020-09-14 16:13:28 -04:00
parent 64050e8266
commit da5f10a2f1
13 changed files with 112 additions and 114 deletions

View file

@ -9,8 +9,6 @@ import com.tfcporciuncula.flow.FlowSharedPreferences
import com.tfcporciuncula.flow.Preference import com.tfcporciuncula.flow.Preference
import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.database.models.Manga 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.DisplayMode
import eu.kanade.tachiyomi.data.preference.PreferenceValues.NsfwAllowance import eu.kanade.tachiyomi.data.preference.PreferenceValues.NsfwAllowance
import eu.kanade.tachiyomi.data.track.TrackService import eu.kanade.tachiyomi.data.track.TrackService
@ -22,6 +20,8 @@ import java.io.File
import java.text.DateFormat import java.text.DateFormat
import java.text.SimpleDateFormat import java.text.SimpleDateFormat
import java.util.Locale 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) @OptIn(ExperimentalCoroutinesApi::class)
fun <T> Preference<T>.asImmediateFlow(block: (value: T) -> Unit): Flow<T> { fun <T> Preference<T>.asImmediateFlow(block: (value: T) -> Unit): Flow<T> {
@ -264,16 +264,14 @@ class PreferencesHelper(val context: Context) {
fun sortChapterByAscendingOrDescending() = prefs.getInt(Keys.defaultChapterSortByAscendingOrDescending, Manga.SORT_DESC) fun sortChapterByAscendingOrDescending() = prefs.getInt(Keys.defaultChapterSortByAscendingOrDescending, Manga.SORT_DESC)
fun setChapterSettingsDefault(m: Manga) { fun setChapterSettingsDefault(manga: Manga) {
prefs.edit { prefs.edit {
putInt(Keys.defaultChapterFilterByRead, m.readFilter) putInt(Keys.defaultChapterFilterByRead, manga.readFilter)
putInt(Keys.defaultChapterFilterByDownloaded, m.downloadedFilter) putInt(Keys.defaultChapterFilterByDownloaded, manga.downloadedFilter)
putInt(Keys.defaultChapterFilterByBookmarked, m.bookmarkedFilter) putInt(Keys.defaultChapterFilterByBookmarked, manga.bookmarkedFilter)
putInt(Keys.defaultChapterSortBySourceOrNumber, m.sorting) putInt(Keys.defaultChapterSortBySourceOrNumber, manga.sorting)
putInt(Keys.defaultChapterDisplayByNameOrNumber, m.displayMode) 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) }
} }
} }

View file

@ -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.ui.browse.source.filter.TriStateSectionItem
import eu.kanade.tachiyomi.util.chapter.ChapterSettingsHelper import eu.kanade.tachiyomi.util.chapter.ChapterSettingsHelper
import eu.kanade.tachiyomi.util.removeCovers import eu.kanade.tachiyomi.util.removeCovers
import kotlinx.coroutines.flow.subscribe
import rx.Observable import rx.Observable
import rx.Subscription import rx.Subscription
import rx.android.schedulers.AndroidSchedulers import rx.android.schedulers.AndroidSchedulers
@ -270,7 +269,7 @@ open class BrowseSourcePresenter(
if (!manga.favorite) { if (!manga.favorite) {
manga.removeCovers(coverCache) manga.removeCovers(coverCache)
} else { } else {
ChapterSettingsHelper.applySettingDefaultsFromPreferences(manga) ChapterSettingsHelper.applySettingDefaults(manga)
} }
db.insertManga(manga).executeAsBlocking() db.insertManga(manga).executeAsBlocking()

View file

@ -183,7 +183,7 @@ class LibraryController(
createActionModeIfNeeded() createActionModeIfNeeded()
} }
settingsSheet = LibrarySettingsSheet(activity!!) { group -> settingsSheet = LibrarySettingsSheet(router) { group ->
when (group) { when (group) {
is LibrarySettingsSheet.Filter.FilterGroup -> onFilterChanged() is LibrarySettingsSheet.Filter.FilterGroup -> onFilterChanged()
is LibrarySettingsSheet.Sort.SortGroup -> onSortChanged() is LibrarySettingsSheet.Sort.SortGroup -> onSortChanged()

View file

@ -1,9 +1,9 @@
package eu.kanade.tachiyomi.ui.library package eu.kanade.tachiyomi.ui.library
import android.app.Activity
import android.content.Context import android.content.Context
import android.util.AttributeSet import android.util.AttributeSet
import android.view.View import android.view.View
import com.bluelinelabs.conductor.Router
import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.preference.PreferenceValues.DisplayMode import eu.kanade.tachiyomi.data.preference.PreferenceValues.DisplayMode
import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.data.preference.PreferencesHelper
@ -12,22 +12,22 @@ import eu.kanade.tachiyomi.widget.TabbedBottomSheetDialog
import uy.kohesive.injekt.injectLazy import uy.kohesive.injekt.injectLazy
class LibrarySettingsSheet( class LibrarySettingsSheet(
activity: Activity, router: Router,
onGroupClickListener: (ExtendedNavigationView.Group) -> Unit onGroupClickListener: (ExtendedNavigationView.Group) -> Unit
) : TabbedBottomSheetDialog(activity) { ) : TabbedBottomSheetDialog(router) {
val filters: Filter val filters: Filter
private val sort: Sort private val sort: Sort
private val display: Display private val display: Display
init { init {
filters = Filter(activity) filters = Filter(router.activity!!)
filters.onGroupClicked = onGroupClickListener filters.onGroupClicked = onGroupClickListener
sort = Sort(activity) sort = Sort(router.activity!!)
sort.onGroupClicked = onGroupClickListener sort.onGroupClicked = onGroupClickListener
display = Display(activity) display = Display(router.activity!!)
display.onGroupClicked = onGroupClickListener display.onGroupClicked = onGroupClickListener
} }

View file

@ -237,7 +237,7 @@ class MangaController :
binding.actionToolbar.offsetAppbarHeight(activity!!) binding.actionToolbar.offsetAppbarHeight(activity!!)
settingsSheet = ChaptersSettingsSheet(activity!!, presenter) { group -> settingsSheet = ChaptersSettingsSheet(router, presenter) { group ->
if (group is ChaptersSettingsSheet.Filter.FilterGroup) { if (group is ChaptersSettingsSheet.Filter.FilterGroup) {
updateFilterIconState() updateFilterIconState()
chaptersAdapter?.notifyDataSetChanged() chaptersAdapter?.notifyDataSetChanged()

View file

@ -84,7 +84,7 @@ class MangaPresenter(
super.onCreate(savedState) super.onCreate(savedState)
if (!manga.favorite) { if (!manga.favorite) {
ChapterSettingsHelper.applySettingDefaultsFromPreferences(manga) ChapterSettingsHelper.applySettingDefaults(manga)
} }
// Manga info - start // Manga info - start

View file

@ -1,34 +1,39 @@
package eu.kanade.tachiyomi.ui.manga.chapter package eu.kanade.tachiyomi.ui.manga.chapter
import android.app.Activity
import android.content.Context import android.content.Context
import android.util.AttributeSet import android.util.AttributeSet
import android.view.View import android.view.View
import androidx.core.view.isVisible
import com.bluelinelabs.conductor.Router
import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.database.models.Manga import eu.kanade.tachiyomi.data.database.models.Manga
import eu.kanade.tachiyomi.ui.manga.MangaPresenter 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.ExtendedNavigationView
import eu.kanade.tachiyomi.widget.TabbedBottomSheetDialog import eu.kanade.tachiyomi.widget.TabbedBottomSheetDialog
class ChaptersSettingsSheet( class ChaptersSettingsSheet(
activity: Activity, private val router: Router,
private val presenter: MangaPresenter, private val presenter: MangaPresenter,
onGroupClickListener: (ExtendedNavigationView.Group) -> Unit onGroupClickListener: (ExtendedNavigationView.Group) -> Unit
) : TabbedBottomSheetDialog(activity, presenter.manga) { ) : TabbedBottomSheetDialog(router) {
val filters: Filter val filters: Filter
private val sort: Sort private val sort: Sort
private val display: Display private val display: Display
init { init {
filters = Filter(activity) filters = Filter(router.activity!!)
filters.onGroupClicked = onGroupClickListener filters.onGroupClicked = onGroupClickListener
sort = Sort(activity) sort = Sort(router.activity!!)
sort.onGroupClicked = onGroupClickListener sort.onGroupClicked = onGroupClickListener
display = Display(activity) display = Display(router.activity!!)
display.onGroupClicked = onGroupClickListener display.onGroupClicked = onGroupClickListener
binding.menu.isVisible = true
binding.menu.setOnClickListener { it.post { showPopupMenu(it) } }
} }
override fun getTabViews(): List<View> = listOf( override fun getTabViews(): List<View> = listOf(
@ -43,6 +48,23 @@ class ChaptersSettingsSheet(
R.string.action_display 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, ...). * Filters group (unread, downloaded, ...).
*/ */

View file

@ -1,46 +1,48 @@
package eu.kanade.tachiyomi.ui.manga.chapter package eu.kanade.tachiyomi.ui.manga.chapter
import android.app.Dialog import android.app.Dialog
import android.content.Context import android.os.Bundle
import com.afollestad.materialdialogs.MaterialDialog import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.customview.customView import com.afollestad.materialdialogs.customview.customView
import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.database.models.Manga 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.chapter.ChapterSettingsHelper
import eu.kanade.tachiyomi.util.system.toast import eu.kanade.tachiyomi.util.system.toast
import eu.kanade.tachiyomi.widget.DialogCheckboxView import eu.kanade.tachiyomi.widget.DialogCheckboxView
class SetChapterSettingsDialog(val context: Context, val manga: Manga) { class SetChapterSettingsDialog(bundle: Bundle? = null) : DialogController(bundle) {
private var dialog: Dialog
init { constructor(manga: Manga) : this(
this.dialog = buildDialog() Bundle().apply {
putSerializable(MANGA_KEY, manga)
} }
)
private fun buildDialog(): Dialog { override fun onCreateDialog(savedViewState: Bundle?): Dialog {
val view = DialogCheckboxView(context).apply { val view = DialogCheckboxView(activity!!).apply {
setDescription(R.string.confirm_set_chapter_settings) setDescription(R.string.confirm_set_chapter_settings)
setOptionDescription(R.string.also_set_chapter_settings_for_library) setOptionDescription(R.string.also_set_chapter_settings_for_library)
} }
return MaterialDialog(context) return MaterialDialog(activity!!)
.title(R.string.action_chapter_settings) .title(R.string.chapter_settings)
.customView( .customView(
view = view, view = view,
horizontalPadding = true horizontalPadding = true
) )
.positiveButton(android.R.string.ok) { .positiveButton(android.R.string.ok) {
ChapterSettingsHelper.setNewSettingDefaults(manga) ChapterSettingsHelper.setGlobalSettings(args.getSerializable(MANGA_KEY)!! as Manga)
if (view.isChecked()) { 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) .negativeButton(android.R.string.cancel)
} }
fun showDialog() = dialog.show() private companion object {
const val MANGA_KEY = "manga"
fun dismissDialog() = dialog.dismiss() }
} }

View file

@ -4,52 +4,55 @@ import eu.kanade.tachiyomi.data.database.DatabaseHelper
import eu.kanade.tachiyomi.data.database.models.Manga import eu.kanade.tachiyomi.data.database.models.Manga
import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.util.lang.launchIO import eu.kanade.tachiyomi.util.lang.launchIO
import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.injectLazy
import uy.kohesive.injekt.api.get
object ChapterSettingsHelper { object ChapterSettingsHelper {
private val prefs = Injekt.get<PreferencesHelper>()
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?) { fun setGlobalSettings(manga: Manga?) {
m?.let { manga?.let {
prefs.setChapterSettingsDefault(it) prefs.setChapterSettingsDefault(it)
db.updateFlags(it).executeAsBlocking() 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) { fun applySettingDefaults(manga: Manga) {
m.readFilter = prefs.filterChapterByRead() with(manga) {
m.downloadedFilter = prefs.filterChapterByDownloaded() readFilter = prefs.filterChapterByRead()
m.bookmarkedFilter = prefs.filterChapterByBookmarked() downloadedFilter = prefs.filterChapterByDownloaded()
m.sorting = prefs.sortChapterBySourceOrNumber() bookmarkedFilter = prefs.filterChapterByBookmarked()
m.displayMode = prefs.displayChapterByNameOrNumber() sorting = prefs.sortChapterBySourceOrNumber()
m.setChapterOrder(prefs.sortChapterByAscendingOrDescending()) displayMode = prefs.displayChapterByNameOrNumber()
db.updateFlags(m).executeAsBlocking() 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 { launchIO {
val dbMangas = db.getMangas().executeAsBlocking().toMutableList() val updatedMangas = db.getMangas().executeAsBlocking().map { manga ->
with(manga) {
val updatedMangas = dbMangas.map { m -> readFilter = prefs.filterChapterByRead()
m.readFilter = prefs.filterChapterByRead() downloadedFilter = prefs.filterChapterByDownloaded()
m.downloadedFilter = prefs.filterChapterByDownloaded() bookmarkedFilter = prefs.filterChapterByBookmarked()
m.bookmarkedFilter = prefs.filterChapterByBookmarked() sorting = prefs.sortChapterBySourceOrNumber()
m.sorting = prefs.sortChapterBySourceOrNumber() displayMode = prefs.displayChapterByNameOrNumber()
m.displayMode = prefs.displayChapterByNameOrNumber() setChapterOrder(prefs.sortChapterByAscendingOrDescending())
m.setChapterOrder(prefs.sortChapterByAscendingOrDescending()) }
m manga
}.toList() }
db.updateFlags(updatedMangas).executeAsBlocking() db.updateFlags(updatedMangas).executeAsBlocking()
} }

View file

@ -1,17 +1,14 @@
package eu.kanade.tachiyomi.widget package eu.kanade.tachiyomi.widget
import android.app.Activity
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import com.bluelinelabs.conductor.Router
import com.google.android.material.bottomsheet.BottomSheetDialog 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.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) { abstract class TabbedBottomSheetDialog(private val router: Router) : BottomSheetDialog(router.activity!!) {
val binding: CommonTabbedSheetBinding = CommonTabbedSheetBinding.inflate(activity.layoutInflater)
val binding: CommonTabbedSheetBinding = CommonTabbedSheetBinding.inflate(router.activity!!.layoutInflater)
init { init {
val adapter = LibrarySettingsSheetAdapter() val adapter = LibrarySettingsSheetAdapter()
@ -19,34 +16,9 @@ abstract class TabbedBottomSheetDialog(private val activity: Activity, private v
binding.pager.adapter = adapter binding.pager.adapter = adapter
binding.tabs.setupWithViewPager(binding.pager) 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) 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<View> abstract fun getTabViews(): List<View>
abstract fun getTabTitles(): List<Int> abstract fun getTabTitles(): List<Int>
@ -62,7 +34,7 @@ abstract class TabbedBottomSheetDialog(private val activity: Activity, private v
} }
override fun getPageTitle(position: Int): CharSequence { override fun getPageTitle(position: Int): CharSequence {
return activity.resources!!.getString(getTabTitles()[position]) return router.activity!!.resources!!.getString(getTabTitles()[position])
} }
} }
} }

View file

@ -30,11 +30,13 @@
android:contentDescription="@string/action_menu" android:contentDescription="@string/action_menu"
android:paddingStart="10dp" android:paddingStart="10dp"
android:paddingEnd="10dp" android:paddingEnd="10dp"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_more_vert_24dp" app:srcCompat="@drawable/ic_more_vert_24dp"
app:tint="?attr/colorOnBackground" /> app:tint="?attr/colorOnBackground"
tools:visibility="visible" />
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>

View file

@ -1,8 +1,8 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" <menu xmlns:android="http://schemas.android.com/apk/res/android">
xmlns:app="http://schemas.android.com/apk/res-auto">
<item <item
android:id="@+id/save_as_default" android:id="@+id/set_as_default"
android:title="@string/set_chapter_settings_as_default" /> android:title="@string/set_chapter_settings_as_default" />
</menu> </menu>

View file

@ -30,7 +30,6 @@
<!-- Actions --> <!-- Actions -->
<string name="action_settings">Settings</string> <string name="action_settings">Settings</string>
<string name="action_chapter_settings">Chapter Settings</string>
<string name="action_menu">Menu</string> <string name="action_menu">Menu</string>
<string name="action_filter">Filter</string> <string name="action_filter">Filter</string>
<string name="action_filter_downloaded">Downloaded</string> <string name="action_filter_downloaded">Downloaded</string>
@ -525,9 +524,10 @@
<string name="download_unread">Unread</string> <string name="download_unread">Unread</string>
<string name="confirm_delete_chapters">Are you sure you want to delete the selected chapters?</string> <string name="confirm_delete_chapters">Are you sure you want to delete the selected chapters?</string>
<string name="invalid_download_dir">Invalid download location</string> <string name="invalid_download_dir">Invalid download location</string>
<string name="chapter_settings">Chapter settings</string>
<string name="confirm_set_chapter_settings">Are you sure you want to save these settings as default?</string> <string name="confirm_set_chapter_settings">Are you sure you want to save these settings as default?</string>
<string name="also_set_chapter_settings_for_library">Also apply to all manga in my Library</string> <string name="also_set_chapter_settings_for_library">Also apply to all manga in my library</string>
<string name="set_chapter_settings_as_default">Set as Default</string> <string name="set_chapter_settings_as_default">Set as default</string>
<string name="no_chapters_error">No chapters found</string> <string name="no_chapters_error">No chapters found</string>
<!-- Tracking Screen --> <!-- Tracking Screen -->