Using enum for filter order instead of random string and chars

This commit is contained in:
Jays2Kings 2021-04-16 02:54:13 -04:00
parent c002a23da2
commit 681ceccf2e
4 changed files with 38 additions and 39 deletions

View File

@ -11,6 +11,7 @@ import com.f2prateek.rx.preferences.RxSharedPreferences
import com.tfcporciuncula.flow.FlowSharedPreferences
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.track.TrackService
import eu.kanade.tachiyomi.ui.library.filter.FilterBottomSheet
import eu.kanade.tachiyomi.ui.reader.ReaderActivity
import eu.kanade.tachiyomi.ui.reader.viewer.ViewerNavigation
import eu.kanade.tachiyomi.ui.reader.viewer.pager.PageLayout
@ -340,7 +341,7 @@ class PreferencesHelper(val context: Context) {
fun hopperGravity() = flowPrefs.getInt("hopper_gravity", 1)
fun filterOrder() = flowPrefs.getString("filter_order", "rudcmt")
fun filterOrder() = flowPrefs.getString("filter_order", FilterBottomSheet.Filters.DEFAULT_ORDER)
fun hopperLongPressAction() = flowPrefs.getInt(Keys.hopperLongPress, 0)

View File

@ -12,6 +12,7 @@ import eu.davidea.flexibleadapter.FlexibleAdapter
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.databinding.LibraryCategoryLayoutBinding
import eu.kanade.tachiyomi.ui.library.filter.FilterBottomSheet
import eu.kanade.tachiyomi.ui.library.filter.FilterBottomSheet.Filters.Companion.DEFAULT_ORDER
import eu.kanade.tachiyomi.ui.library.filter.ManageFilterItem
import eu.kanade.tachiyomi.util.bindToPreference
import eu.kanade.tachiyomi.util.system.toInt
@ -57,7 +58,7 @@ class LibraryCategoryView @JvmOverloads constructor(context: Context, attrs: Att
val recycler = RecyclerView(context)
var filterOrder = preferences.filterOrder().get()
if (filterOrder.count() != 6) {
filterOrder = "urdcmt"
filterOrder = DEFAULT_ORDER
}
val adapter = FlexibleAdapter(
filterOrder.toCharArray().map(::ManageFilterItem),
@ -71,7 +72,7 @@ class LibraryCategoryView @JvmOverloads constructor(context: Context, attrs: Att
MaterialDialog(context).title(R.string.reorder_filters)
.customView(view = recycler, scrollable = false)
.negativeButton(android.R.string.cancel)
.positiveButton(android.R.string.ok) {
.positiveButton(R.string.reorder) {
val order = adapter.currentItems.map { it.char }.joinToString("")
preferences.filterOrder().set(order)
recycler.adapter = null

View File

@ -6,6 +6,7 @@ import android.util.AttributeSet
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import androidx.annotation.StringRes
import com.google.android.material.bottomsheet.BottomSheetBehavior
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.database.DatabaseHelper
@ -324,37 +325,19 @@ class FilterBottomSheet @JvmOverloads constructor(context: Context, attrs: Attri
}
}
}
private fun indexOf(filterTagGroup: FilterTagGroup): Int {
charOfFilter(filterTagGroup)?.let {
return filterOrder.indexOf(it)
}
return 0
}
private fun addForClear(): Int {
return if (clearButton.parent != null) 1 else 0
}
private fun charOfFilter(filterTagGroup: FilterTagGroup): Char? {
return when (filterTagGroup) {
unreadProgress -> 'u'
unread -> 'r'
downloaded -> 'd'
completed -> 'c'
mangaType -> 'm'
tracked -> 't'
else -> null
}
}
private fun mapOfFilters(char: Char): FilterTagGroup? {
return when (char) {
'u' -> unreadProgress
'r' -> unread
'd' -> downloaded
'c' -> completed
'm' -> mangaType
't' -> if (hasTracking) tracked else null
return when (Filters.filterOf(char)) {
Filters.UnreadProgress -> unreadProgress
Filters.Unread -> unread
Filters.Downloaded -> downloaded
Filters.Completed -> completed
Filters.SeriesType -> mangaType
Filters.Tracked -> if (hasTracking) tracked else null
else -> null
}
}
@ -470,4 +453,28 @@ class FilterBottomSheet @JvmOverloads constructor(context: Context, attrs: Attri
var FILTER_TRACKER = ""
private set
}
enum class Filters(val value: Char, @StringRes val stringRes: Int) {
UnreadProgress('u', R.string.read_progress),
Unread('r', R.string.unread),
Downloaded('d', R.string.downloaded),
Completed('c', R.string.status),
SeriesType('m', R.string.series_type),
Tracked('t', R.string.tracked);
companion object {
val DEFAULT_ORDER = listOf(
UnreadProgress,
Unread,
Downloaded,
Completed,
SeriesType,
Tracked
).joinToString("")
fun filterOf(char: Char): Filters? {
return Filters::class.java.enumConstants?.find { it.value == char }
}
}
}
}

View File

@ -83,17 +83,7 @@ class ManageFilterItem(val char: Char) : AbstractFlexibleItem<ManageFilterItem.H
}
fun bind(char: Char) {
binding.title.setText(
when (char) {
'u' -> R.string.read_progress
'r' -> R.string.unread
'd' -> R.string.downloaded
'c' -> R.string.status
'm' -> R.string.series_type
't' -> R.string.tracked
else -> R.string.unread
}
)
binding.title.setText(FilterBottomSheet.Filters.filterOf(char)?.stringRes ?: 0)
}
}
}