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 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 <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 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)
}
if (m.sortDescending()) prefs.edit { putInt(Keys.defaultChapterSortByAscendingOrDescending, Manga.SORT_DESC) }
else prefs.edit { putInt(Keys.defaultChapterSortByAscendingOrDescending, Manga.SORT_ASC) }
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)
}
}
}

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.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()

View File

@ -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()

View File

@ -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
}

View File

@ -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()

View File

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

View File

@ -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<View> = 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, ...).
*/

View File

@ -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"
}
}

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.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<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?) {
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()
}

View File

@ -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<View>
abstract fun getTabTitles(): List<Int>
@ -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])
}
}
}

View File

@ -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" />
</androidx.constraintlayout.widget.ConstraintLayout>

View File

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

View File

@ -30,7 +30,6 @@
<!-- Actions -->
<string name="action_settings">Settings</string>
<string name="action_chapter_settings">Chapter Settings</string>
<string name="action_menu">Menu</string>
<string name="action_filter">Filter</string>
<string name="action_filter_downloaded">Downloaded</string>
@ -525,9 +524,10 @@
<string name="download_unread">Unread</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="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="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="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="no_chapters_error">No chapters found</string>
<!-- Tracking Screen -->