More bottom sheet work

Title og bottom sheet now shows filter status and sort method
This commit is contained in:
Jay 2020-02-06 06:20:50 -08:00
parent 40e4adc23b
commit cb599704a9
6 changed files with 50 additions and 25 deletions

View File

@ -9,6 +9,7 @@ import android.widget.LinearLayout
import com.google.android.material.bottomsheet.BottomSheetBehavior
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.database.DatabaseHelper
import eu.kanade.tachiyomi.data.database.models.Category
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.data.preference.getOrDefault
import eu.kanade.tachiyomi.data.track.TrackManager
@ -18,6 +19,7 @@ import kotlinx.android.synthetic.main.filter_buttons.view.*
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
import uy.kohesive.injekt.injectLazy
import java.util.Locale
import kotlin.math.roundToInt
class FilterBottomSheet @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null)
@ -38,6 +40,8 @@ class FilterBottomSheet @JvmOverloads constructor(context: Context, attrs: Attri
private lateinit var categories:FilterTagGroup
var lastCategory:Category? = null
val filterItems:List<FilterTagGroup> by lazy {
val list = mutableListOf<FilterTagGroup>()
if (Injekt.get<DatabaseHelper>().getCategories().executeAsBlocking().isNotEmpty())
@ -107,7 +111,7 @@ class FilterBottomSheet @JvmOverloads constructor(context: Context, attrs: Attri
if (filters.isEmpty()) {
sortText.text = context.getString(
R.string.sorting_by_, context.getString(
when (preferences.librarySortingMode().getOrDefault()) {
when (sorting()) {
LibrarySort.LAST_UPDATED -> R.string.action_sort_last_updated
LibrarySort.DRAG_AND_DROP -> R.string.action_sort_drag_and_drop
LibrarySort.TOTAL -> R.string.action_sort_total
@ -119,7 +123,7 @@ class FilterBottomSheet @JvmOverloads constructor(context: Context, attrs: Attri
)
}
else {
filters.add(0, when (preferences.librarySortingMode().getOrDefault()) {
filters.add(0, when (sorting()) {
LibrarySort.LAST_UPDATED -> R.string.action_sort_last_updated
LibrarySort.DRAG_AND_DROP -> R.string.action_sort_drag_and_drop
LibrarySort.TOTAL -> R.string.action_sort_total
@ -131,18 +135,32 @@ class FilterBottomSheet @JvmOverloads constructor(context: Context, attrs: Attri
}
}
fun sorting(): Int {
return if (lastCategory != null && preferences.showCategories().getOrDefault()) {
when (lastCategory?.mangaSort) {
'a', 'b' -> LibrarySort.ALPHA
'c', 'd' -> LibrarySort.LAST_UPDATED
'e', 'f' -> LibrarySort.UNREAD
'g', 'h' -> LibrarySort.LAST_READ
else -> LibrarySort.DRAG_AND_DROP
}
}
else {
preferences.librarySortingMode().getOrDefault()
}
}
fun getFilters(): List<Int> {
val filters = mutableListOf<Int>()
var categoriesOn = preferences.showCategories().getOrDefault()
if (!categoriesOn) {
filters.add(R.string.hiding_categories)
}
var filter = preferences.filterDownloaded().getOrDefault()
if (filter > 0) {
filters.add(if (filter == 1) R.string.action_filter_downloaded else R.string
.action_filter_not_downloaded)
}
filter = preferences.filterCompleted().getOrDefault()
if (filter > 0) {
filters.add(if (filter == 1) R.string.completed else R.string
.ongoing)
}
filter = preferences.filterUnread().getOrDefault()
if (filter > 0) {
filters.add(when (filter) {
@ -151,6 +169,11 @@ class FilterBottomSheet @JvmOverloads constructor(context: Context, attrs: Attri
else -> R.string.action_filter_not_started
})
}
filter = preferences.filterCompleted().getOrDefault()
if (filter > 0) {
filters.add(if (filter == 1) R.string.completed else R.string
.ongoing)
}
filter = preferences.filterTracked().getOrDefault()
if (filter > 0) {
filters.add(if (filter == 1) R.string.action_filter_tracked else R.string
@ -163,29 +186,29 @@ class FilterBottomSheet @JvmOverloads constructor(context: Context, attrs: Attri
fun createTags() {
categories = inflate(R.layout.filter_buttons) as FilterTagGroup
categories.setup(this, R.string.categories)
categories.onItemClicked = { view, index -> onFilterClicked(view, index) }
categories.firstButton.isActivated = preferences.showCategories().getOrDefault()
categories.onItemClicked = { view, index -> onFilterClicked(view, index) }
downloaded = inflate(R.layout.filter_buttons) as FilterTagGroup
downloaded.setup(this, R.string.action_filter_downloaded, R.string.action_filter_not_downloaded)
downloaded.onItemClicked = { view, index -> onFilterClicked(view, index) }
downloaded.setState(preferences.filterDownloaded())
downloaded.onItemClicked = { view, index -> onFilterClicked(view, index) }
completed = inflate(R.layout.filter_buttons) as FilterTagGroup
completed.setup(this, R.string.completed, R.string.ongoing)
completed.onItemClicked = { view, index -> onFilterClicked(view, index) }
completed.setState(preferences.filterCompleted())
completed.onItemClicked = { view, index -> onFilterClicked(view, index) }
unread = inflate(R.layout.filter_buttons) as FilterTagGroup
unread.setup(this, R.string.action_filter_not_started, R.string.action_filter_in_progress,
R.string.action_filter_read)
unread.onItemClicked = { view, index -> onFilterClicked(view, index) }
unread.setState(preferences.filterUnread())
unread.onItemClicked = { view, index -> onFilterClicked(view, index) }
tracked = inflate(R.layout.filter_buttons) as FilterTagGroup
tracked.setup(this, R.string.action_filter_tracked, R.string.action_filter_not_tracked)
tracked.onItemClicked = { view, index -> onFilterClicked(view, index) }
tracked.setState(preferences.filterTracked())
tracked.onItemClicked = { view, index -> onFilterClicked(view, index) }
filterItems.forEach {
filterLayout.addView(it)

View File

@ -62,14 +62,14 @@ class FilterTagGroup@JvmOverloads constructor(context: Context, attrs: Attribute
fun setState(preference: Preference<Int>) {
val index = preference.getOrDefault() - 1
if (index > -1)
toggleButton(index)
toggleButton(index, false)
}
private fun toggleButton(index: Int) {
private fun toggleButton(index: Int, callBack: Boolean = true) {
if (itemCount == 0) return
if (itemCount == 1) {
firstButton.isActivated = !firstButton.isActivated
onItemClicked(this, if (firstButton.isActivated) index else -1)
if (callBack) onItemClicked(this, if (firstButton.isActivated) index else -1)
return
}
val buttons = mutableListOf(firstButton, secondButton)
@ -83,14 +83,14 @@ class FilterTagGroup@JvmOverloads constructor(context: Context, attrs: Attribute
if (mainButton.isActivated) {
mainButton.isActivated = false
separator1.visible()
onItemClicked(this, -1)
if (callBack) onItemClicked(this, -1)
if (itemCount >= 3)
separator2.visible()
buttons.forEach{ it.visible() }
}
else {
mainButton.isActivated = true
onItemClicked(this, index)
if (callBack) onItemClicked(this, index)
buttons.forEach{ it.gone() }
separator1.gone()
if (itemCount >= 3) {

View File

@ -187,6 +187,8 @@ class LibraryController(
library_pager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
override fun onPageSelected(position: Int) {
enableReorderItems(position)
bottom_sheet.lastCategory = adapter?.categories?.getOrNull(position)
bottom_sheet.updateTitle()
}
override fun onPageScrolled(
@ -351,6 +353,9 @@ class LibraryController(
// Restore active category.
library_pager.setCurrentItem(activeCat, false)
bottom_sheet.lastCategory = adapter.categories.getOrNull(activeCat)
bottom_sheet.updateTitle()
tabsVisibilityRelay.call(categories.size > 1)
// Delay the scroll position to allow the view to be properly measured.

View File

@ -139,19 +139,14 @@ class LibraryPresenter(
val filterTracked = preferences.filterTracked().getOrDefault()
val lastReadManga by lazy {
db.getLastReadManga().executeAsBlocking()
}
val filterFn: (LibraryItem) -> Boolean = f@ { item ->
// Filter when there isn't unread chapters.
if (MainActivity.bottomNav) {
if (filterUnread == STATE_INCLUDE &&
(item.manga.unread == 0 || lastReadManga.find { it.id == item.manga.id } ==
null)) return@f false
(item.manga.unread == 0 || db.getChapters(item.manga).executeAsBlocking()
.size != item.manga.unread)) return@f false
if (filterUnread == STATE_EXCLUDE &&
(item.manga.unread == 0 || lastReadManga.find { it.id == item.manga.id } !=
null)) return@f false
(item.manga.unread == 0 || db.getChapters(item.manga).executeAsBlocking().size == item.manga.unread)) return@f false
if (filterUnread == STATE_REALLY_EXCLUDE && item.manga.unread > 0) return@f false
}
else {

View File

@ -33,6 +33,7 @@
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginTop="15dp"
android:textColor="?attr/actionBarTintColor"
android:text="Filter &amp; Sort"

View File

@ -43,6 +43,7 @@
<string name="action_filter_read">Read</string>
<string name="action_filter_tracked">Tracked</string>
<string name="action_filter_not_tracked">Not tracked</string>
<string name="hiding_categories">Hiding categories</string>
<string name="sorting_by_">Sorting by %1$s</string>
<string name="action_filter_empty">Remove filter</string>