Using new bottom sheet menu for sorting categories

This commit is contained in:
Jay 2020-05-17 02:48:01 -04:00
parent 68cf3cab10
commit 59c108a972
8 changed files with 103 additions and 163 deletions

View File

@ -1,18 +1,12 @@
package eu.kanade.tachiyomi.ui.library
import android.annotation.SuppressLint
import android.app.Activity
import android.graphics.Color
import android.graphics.drawable.Drawable
import android.text.SpannableString
import android.text.style.ForegroundColorSpan
import android.util.TypedValue
import android.view.View
import android.widget.ImageView
import android.widget.ProgressBar
import android.widget.TextView
import androidx.appcompat.view.menu.MenuBuilder
import androidx.appcompat.widget.PopupMenu
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
@ -145,9 +139,6 @@ class LibraryHeaderItem(
if (category.isAlone) sectionText.text = ""
else sectionText.text = category.name
sortText.text = itemView.context.getString(
R.string.sort_by_, itemView.context.getString(category.sortRes())
)
val isAscending = category.isAscending()
val sortingMode = category.sortingMode()
@ -198,72 +189,71 @@ class LibraryHeaderItem(
}
}
@SuppressLint("RestrictedApi")
private fun showCatSortOptions() {
val category =
(adapter.getItem(adapterPosition) as? LibraryHeaderItem)?.category ?: return
// Create a PopupMenu, giving it the clicked view for an anchor
val popup = PopupMenu(itemView.context, sortText)
// Inflate our menu resource into the PopupMenu's Menu
popup.menuInflater.inflate(
if (category.id == -1) R.menu.main_sort
else R.menu.cat_sort, popup.menu)
// Set a listener so we are notified if a menu item is clicked
popup.setOnMenuItemClickListener { menuItem ->
onCatSortClicked(category, menuItem.itemId)
true
}
val sortingMode = category.sortingMode()
val currentItem = if (sortingMode == null) null
else popup.menu.findItem(
when (sortingMode) {
LibrarySort.DRAG_AND_DROP -> R.id.action_drag_and_drop
LibrarySort.TOTAL -> R.id.action_total_chaps
LibrarySort.LAST_READ -> R.id.action_last_read
LibrarySort.UNREAD -> R.id.action_unread
LibrarySort.LATEST_CHAPTER -> R.id.action_update
LibrarySort.DATE_ADDED -> R.id.action_date_added
else -> R.id.action_alpha
adapter.controller.activity?.let { activity ->
val items = mutableListOf(
MaterialMenuSheet.MenuSheetItem(
LibrarySort.ALPHA, R.drawable.ic_sort_by_alpha_24dp, R.string.title
), MaterialMenuSheet.MenuSheetItem(
LibrarySort.LAST_READ,
R.drawable.ic_recent_read_outline_24dp,
R.string.last_read
), MaterialMenuSheet.MenuSheetItem(
LibrarySort.LATEST_CHAPTER,
R.drawable.ic_new_releases_24dp,
R.string.latest_chapter
), MaterialMenuSheet.MenuSheetItem(
LibrarySort.UNREAD, R.drawable.ic_eye_24dp, R.string.unread
), MaterialMenuSheet.MenuSheetItem(
LibrarySort.TOTAL,
R.drawable.ic_sort_by_numeric_24dp,
R.string.total_chapters
), MaterialMenuSheet.MenuSheetItem(
LibrarySort.DATE_ADDED,
R.drawable.ic_heart_outline_24dp,
R.string.date_added
)
)
if (category.isDynamic) {
items.add(
MaterialMenuSheet.MenuSheetItem(
LibrarySort.DRAG_AND_DROP,
R.drawable.ic_label_outline_white_24dp,
R.string.category
)
)
}
)
if (category.id == -1)
popup.menu.findItem(R.id.action_drag_and_drop).title = contentView.context.getString(
R.string.category
)
if (sortingMode != null && popup.menu is MenuBuilder) {
val m = popup.menu as MenuBuilder
m.setOptionalIconsVisible(true)
}
val isAscending = category.isAscending()
currentItem?.icon = tintVector(
when {
sortingMode == LibrarySort.DRAG_AND_DROP -> R.drawable.ic_check_white_24dp
if (sortingMode == LibrarySort.DATE_ADDED ||
sortingMode == LibrarySort.LATEST_CHAPTER ||
sortingMode == LibrarySort.LAST_READ) !isAscending else isAscending ->
R.drawable.ic_arrow_down_white_24dp
else -> R.drawable.ic_arrow_up_white_24dp
val sortingMode = category.sortingMode()
val sheet = MaterialMenuSheet(
activity,
items,
activity.getString(R.string.sort_by),
sortingMode
) { sheet, item ->
onCatSortClicked(category, item)
val nCategory =
(adapter.getItem(adapterPosition) as? LibraryHeaderItem)?.category
val isAscending = nCategory?.isAscending() ?: false
val drawableRes = getSortRes(item, isAscending)
sheet.setDrawable(item, drawableRes)
false
}
)
val s = SpannableString(currentItem?.title ?: "")
s.setSpan(ForegroundColorSpan(itemView.context.getResourceColor(android.R.attr.colorAccent)), 0, s.length, 0)
currentItem?.title = s
// Finally show the PopupMenu
popup.show()
val isAscending = category.isAscending()
val drawableRes = getSortRes(sortingMode, isAscending)
sheet.setDrawable(sortingMode ?: -1, drawableRes)
sheet.show()
}
}
private fun tintVector(resId: Int): Drawable? {
return ContextCompat.getDrawable(itemView.context, resId)?.mutate()?.apply {
setTint(itemView.context.getResourceColor(android.R.attr.colorAccent))
}
private fun getSortRes(sortingMode: Int?, isAscending: Boolean): Int = when {
sortingMode == LibrarySort.DRAG_AND_DROP -> R.drawable.ic_check_white_24dp
if (sortingMode == LibrarySort.DATE_ADDED ||
sortingMode == LibrarySort.LATEST_CHAPTER ||
sortingMode == LibrarySort.LAST_READ) !isAscending else isAscending ->
R.drawable.ic_arrow_down_white_24dp
else -> R.drawable.ic_arrow_up_white_24dp
}
private fun onCatSortClicked(category: Category, menuId: Int?) {
@ -273,15 +263,15 @@ class LibraryHeaderItem(
else t - 1
} else {
val order = when (menuId) {
R.id.action_drag_and_drop -> {
LibrarySort.DRAG_AND_DROP -> {
adapter.libraryListener.sortCategory(category.id!!, 'D' - 'a' + 1)
return
}
R.id.action_date_added -> 5
R.id.action_total_chaps -> 4
R.id.action_last_read -> 3
R.id.action_unread -> 2
R.id.action_update -> 1
LibrarySort.DATE_ADDED -> 5
LibrarySort.TOTAL -> 4
LibrarySort.LAST_READ -> 3
LibrarySort.UNREAD -> 2
LibrarySort.LATEST_CHAPTER -> 1
else -> 0
}
if (order == category.catSortingMode()) {

View File

@ -0,0 +1,7 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="24dp"
android:width="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path android:fillColor="#000" android:pathData="M23,12L20.56,14.78L20.9,18.46L17.29,19.28L15.4,22.46L12,21L8.6,22.47L6.71,19.29L3.1,18.47L3.44,14.78L1,12L3.44,9.21L3.1,5.53L6.71,4.72L8.6,1.54L12,3L15.4,1.54L17.29,4.72L20.9,5.54L20.56,9.22L23,12M20.33,12L18.5,9.89L18.74,7.1L16,6.5L14.58,4.07L12,5.18L9.42,4.07L8,6.5L5.26,7.09L5.5,9.88L3.67,12L5.5,14.1L5.26,16.9L8,17.5L9.42,19.93L12,18.81L14.58,19.92L16,17.5L18.74,16.89L18.5,14.1L20.33,12M11,15H13V17H11V15M11,7H13V13H11V7" />
</vector>

View File

@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M14.94,4.66h-4.72l2.36,-2.36zM10.25,19.37h4.66l-2.33,2.33zM6.1,6.27L1.6,17.73h1.84l0.92,-2.45h5.11l0.92,2.45h1.84L7.74,6.27L6.1,6.27zM4.97,13.64l1.94,-5.18 1.94,5.18L4.97,13.64zM15.73,16.14h6.12v1.59h-8.53v-1.29l5.92,-8.56h-5.88v-1.6h8.3v1.26l-5.93,8.6z" />
</vector>

View File

@ -0,0 +1,8 @@
<!-- drawable/sort_numeric_variant.xml -->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="24dp"
android:width="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path android:fillColor="#000" android:pathData="M7.78,7C9.08,7.04 10,7.53 10.57,8.46C11.13,9.4 11.41,10.56 11.39,11.95C11.4,13.5 11.09,14.73 10.5,15.62C9.88,16.5 8.95,16.97 7.71,17C6.45,16.96 5.54,16.5 4.96,15.56C4.38,14.63 4.09,13.45 4.09,12C4.09,10.55 4.39,9.36 5,8.44C5.59,7.5 6.5,7.04 7.78,7M7.75,8.63C7.31,8.63 6.96,8.9 6.7,9.46C6.44,10 6.32,10.87 6.32,12C6.31,13.15 6.44,14 6.69,14.54C6.95,15.1 7.31,15.37 7.77,15.37C8.69,15.37 9.16,14.24 9.17,12C9.17,9.77 8.7,8.65 7.75,8.63M13.33,17V15.22L13.76,15.24L14.3,15.22L15.34,15.03C15.68,14.92 16,14.78 16.26,14.58C16.59,14.35 16.86,14.08 17.07,13.76C17.29,13.45 17.44,13.12 17.53,12.78L17.5,12.77C17.05,13.19 16.38,13.4 15.47,13.41C14.62,13.4 13.91,13.15 13.34,12.65C12.77,12.15 12.5,11.43 12.46,10.5C12.47,9.5 12.81,8.69 13.47,8.03C14.14,7.37 15,7.03 16.12,7C17.37,7.04 18.29,7.45 18.88,8.24C19.47,9 19.76,10 19.76,11.19C19.75,12.15 19.61,13 19.32,13.76C19.03,14.5 18.64,15.13 18.12,15.64C17.66,16.06 17.11,16.38 16.47,16.61C15.83,16.83 15.12,16.96 14.34,17H13.33M16.06,8.63C15.65,8.64 15.32,8.8 15.06,9.11C14.81,9.42 14.68,9.84 14.68,10.36C14.68,10.8 14.8,11.16 15.03,11.46C15.27,11.77 15.63,11.92 16.11,11.93C16.43,11.93 16.7,11.86 16.92,11.74C17.14,11.61 17.3,11.46 17.41,11.28C17.5,11.17 17.53,10.97 17.53,10.71C17.54,10.16 17.43,9.69 17.2,9.28C16.97,8.87 16.59,8.65 16.06,8.63M9.25,5L12.5,1.75L15.75,5H9.25M15.75,19L12.5,22.25L9.25,19H15.75Z" />
</vector>

View File

@ -11,10 +11,10 @@
<Space
android:id="@+id/start_space"
android:layout_width="6dp"
android:layout_height="1dp"
app:layout_constraintBottom_toBottomOf="@+id/category_title"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/category_title"
android:layout_height="1dp"/>
app:layout_constraintTop_toTopOf="@+id/category_title" />
<ImageView
android:id="@+id/checkbox"
@ -35,13 +35,13 @@
<ImageView
android:id="@+id/collapse_arrow"
android:layout_marginStart="8dp"
android:layout_width="14dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:alpha="0.5"
android:contentDescription="@string/select_all"
android:src="@drawable/ic_expand_less_24dp"
android:tint="?android:textColorPrimary"
android:alpha="0.5"
app:layout_constraintBottom_toBottomOf="@+id/category_title"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/category_title" />
@ -51,8 +51,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:barrierDirection="right"
app:constraint_referenced_ids="start_space,collapse_arrow,checkbox"
/>
app:constraint_referenced_ids="start_space,collapse_arrow,checkbox" />
<Space
android:id="@+id/space"
@ -66,20 +65,20 @@
<TextView
android:id="@+id/category_title"
style="@style/TextAppearance.MaterialComponents.Headline6"
android:textColor="?android:textColorPrimary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingStart="0dp"
android:paddingEnd="4dp"
android:paddingTop="4dp"
android:layout_marginTop="28dp"
android:background="@drawable/square_ripple"
android:paddingBottom="4dp"
android:layout_marginBottom="6dp"
android:background="@drawable/square_ripple"
android:ellipsize="end"
android:gravity="center|start"
android:inputType="none"
android:maxLines="2"
android:paddingStart="0dp"
android:paddingTop="4dp"
android:paddingEnd="4dp"
android:paddingBottom="4dp"
android:textColor="?android:textColorPrimary"
android:textSize="18sp"
app:layout_constrainedWidth="true"
app:layout_constraintBottom_toBottomOf="parent"
@ -120,16 +119,16 @@
<ImageView
android:id="@+id/update_button"
style="@style/Theme.Widget.CustomImageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="6dp"
android:clickable="true"
android:tooltipText="@string/update"
android:focusable="true"
android:padding="4dp"
android:layout_marginEnd="6dp"
style="@style/Theme.Widget.CustomImageButton"
android:src="@drawable/ic_refresh_white_24dp"
android:tint="@color/gray_button"
android:tooltipText="@string/update"
app:layout_constraintBottom_toBottomOf="@id/category_title"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/category_sort"

View File

@ -1,34 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<item
android:id="@+id/action_alpha"
android:title="@string/title"
android:icon="@drawable/ic_blank_24dp"/>
<item
android:id="@+id/action_last_read"
android:title="@string/last_read"
android:icon="@drawable/ic_blank_24dp"/>
<item
android:id="@+id/action_update"
android:title="@string/latest_chapter"
android:icon="@drawable/ic_blank_24dp"/>
<item
android:id="@+id/action_unread"
android:title="@string/unread"
android:icon="@drawable/ic_blank_24dp"/>
<item
android:id="@+id/action_total_chaps"
android:title="@string/total_chapters"
android:icon="@drawable/ic_blank_24dp"/>
<item
android:id="@+id/action_date_added"
android:title="@string/date_added"
android:icon="@drawable/ic_blank_24dp"/>
</menu>

View File

@ -1,39 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<item
android:id="@+id/action_alpha"
android:title="@string/title"
android:icon="@drawable/ic_blank_24dp"/>
<item
android:id="@+id/action_last_read"
android:title="@string/last_read"
android:icon="@drawable/ic_blank_24dp"/>
<item
android:id="@+id/action_update"
android:title="@string/latest_chapter"
android:icon="@drawable/ic_blank_24dp"/>
<item
android:id="@+id/action_unread"
android:title="@string/unread"
android:icon="@drawable/ic_blank_24dp"/>
<item
android:id="@+id/action_total_chaps"
android:title="@string/total_chapters"
android:icon="@drawable/ic_blank_24dp"/>
<item
android:id="@+id/action_date_added"
android:title="@string/date_added"
android:icon="@drawable/ic_blank_24dp"/>
<item
android:id="@+id/action_drag_and_drop"
android:title="@string/drag_and_drop"
android:icon="@drawable/ic_blank_24dp"/>
</menu>

View File

@ -125,7 +125,7 @@
<string name="series_type">Series type</string>
<!-- Library Sort -->
<string name="sort_by_">Sort by: %1$s</string>
<string name="sort_by">Sort by</string>
<string name="total_chapters">Total chapters</string>
<string name="date_added">Date added</string>
<string name="last_read">Last read</string>