-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: new reviewer actions configuration
Compared to the legacy reviewer, it: - Allows to reorder actions - Has a preview in the settings screen of how the menu looks - It is easier to setup with drag and drop gestures - Allows to scroll the `Always show` actions instead of hiding them if the user set many of them
- Loading branch information
Showing
13 changed files
with
718 additions
and
267 deletions.
There are no files selected for viewing
115 changes: 115 additions & 0 deletions
115
AnkiDroid/src/main/java/com/ichi2/anki/preferences/reviewer/ReviewerMenuSettingsAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/* | ||
* Copyright (c) 2024 Brayan Oliveira <[email protected]> | ||
* | ||
* This program is free software; you can redistribute it and/or modify it under | ||
* the terms of the GNU General Public License as published by the Free Software | ||
* Foundation; either version 3 of the License, or (at your option) any later | ||
* version. | ||
* | ||
* This program is distributed in the hope that it will be useful, but WITHOUT ANY | ||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A | ||
* PARTICULAR PURPOSE. See the GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License along with | ||
* this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package com.ichi2.anki.preferences.reviewer | ||
|
||
import android.view.LayoutInflater | ||
import android.view.MotionEvent | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import android.widget.TextView | ||
import androidx.appcompat.widget.AppCompatImageView | ||
import androidx.recyclerview.widget.RecyclerView | ||
import com.google.android.material.textview.MaterialTextView | ||
import com.ichi2.anki.R | ||
|
||
class ReviewerMenuSettingsAdapter( | ||
private val items: List<ReviewerMenuSettingsRecyclerItem>, | ||
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() { | ||
override fun onCreateViewHolder( | ||
parent: ViewGroup, | ||
viewType: Int, | ||
): RecyclerView.ViewHolder { | ||
val inflater = LayoutInflater.from(parent.context) | ||
return when (viewType) { | ||
ReviewerMenuSettingsRecyclerItem.ACTION_VIEW_TYPE -> { | ||
val itemView = inflater.inflate(R.layout.reviewer_menu_item, parent, false) | ||
ActionViewHolder(itemView) | ||
} | ||
ReviewerMenuSettingsRecyclerItem.DISPLAY_TYPE_VIEW_TYPE -> { | ||
val itemView = inflater.inflate(R.layout.reviewer_menu_display_type, parent, false) | ||
DisplayTypeViewHolder(itemView) | ||
} | ||
else -> throw IllegalArgumentException("Unexpected viewType") | ||
} | ||
} | ||
|
||
override fun onBindViewHolder( | ||
holder: RecyclerView.ViewHolder, | ||
position: Int, | ||
) { | ||
val item = items[position] | ||
when (holder) { | ||
is ActionViewHolder -> holder.bind((item as ReviewerMenuSettingsRecyclerItem.Action).viewerAction) | ||
is DisplayTypeViewHolder -> holder.bind((item as ReviewerMenuSettingsRecyclerItem.DisplayType).menuDisplayType) | ||
} | ||
} | ||
|
||
override fun getItemCount(): Int = items.size | ||
|
||
override fun getItemViewType(position: Int): Int = items[position].viewType | ||
|
||
private var onDragListener: ((RecyclerView.ViewHolder) -> Unit)? = null | ||
|
||
fun setOnDragListener(onDragListener: (RecyclerView.ViewHolder) -> Unit) { | ||
this.onDragListener = onDragListener | ||
} | ||
|
||
private inner class ActionViewHolder( | ||
itemView: View, | ||
) : RecyclerView.ViewHolder(itemView) { | ||
/** @see [R.layout.reviewer_menu_item] */ | ||
fun bind(action: ViewerAction) { | ||
action.titleRes.let { itemView.findViewById<TextView>(R.id.title).setText(it) } | ||
action.drawableRes?.let { itemView.findViewById<AppCompatImageView>(R.id.icon).setBackgroundResource(it) } | ||
|
||
itemView.findViewById<AppCompatImageView>(R.id.drag_handle).setOnTouchListener { _, event -> | ||
if (event.action == MotionEvent.ACTION_DOWN) { | ||
onDragListener?.invoke(this) | ||
} | ||
return@setOnTouchListener false | ||
} | ||
} | ||
} | ||
|
||
private class DisplayTypeViewHolder( | ||
itemView: View, | ||
) : RecyclerView.ViewHolder(itemView) { | ||
/** @see [R.layout.reviewer_menu_display_type] */ | ||
fun bind(displayCategory: MenuDisplayType) { | ||
itemView.findViewById<MaterialTextView>(R.id.title).setText(displayCategory.title) | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @param viewType type to be returned at [RecyclerView.Adapter.getItemViewType] | ||
*/ | ||
sealed class ReviewerMenuSettingsRecyclerItem( | ||
val viewType: Int, | ||
) { | ||
data class Action( | ||
val viewerAction: ViewerAction, | ||
) : ReviewerMenuSettingsRecyclerItem(ACTION_VIEW_TYPE) | ||
|
||
data class DisplayType( | ||
val menuDisplayType: MenuDisplayType, | ||
) : ReviewerMenuSettingsRecyclerItem(DISPLAY_TYPE_VIEW_TYPE) | ||
|
||
companion object { | ||
const val ACTION_VIEW_TYPE = 0 | ||
const val DISPLAY_TYPE_VIEW_TYPE = 1 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
.../main/java/com/ichi2/anki/preferences/reviewer/ReviewerMenuSettingsTouchHelperCallback.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* Copyright (c) 2024 Brayan Oliveira <[email protected]> | ||
* | ||
* This program is free software; you can redistribute it and/or modify it under | ||
* the terms of the GNU General Public License as published by the Free Software | ||
* Foundation; either version 3 of the License, or (at your option) any later | ||
* version. | ||
* | ||
* This program is distributed in the hope that it will be useful, but WITHOUT ANY | ||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A | ||
* PARTICULAR PURPOSE. See the GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License along with | ||
* this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package com.ichi2.anki.preferences.reviewer | ||
|
||
import androidx.recyclerview.widget.ItemTouchHelper | ||
import androidx.recyclerview.widget.RecyclerView | ||
import java.util.Collections | ||
|
||
/** | ||
* A [ItemTouchHelper.Callback] for the [ReviewerMenuSettingsAdapter]. | ||
* | ||
* It allows drag and dropping of [ReviewerMenuSettingsAdapter.ActionViewHolder], but not of | ||
* [ReviewerMenuSettingsAdapter.DisplayTypeViewHolder], or any kind of swipe. | ||
* | ||
* [setOnClearViewListener] can be used to set an action to run after the user interaction has ended | ||
* (see [clearView]). | ||
*/ | ||
class ReviewerMenuSettingsTouchHelperCallback( | ||
private val items: List<ReviewerMenuSettingsRecyclerItem>, | ||
) : ItemTouchHelper.Callback() { | ||
private val movementFlags = makeMovementFlags(ItemTouchHelper.UP or ItemTouchHelper.DOWN, 0) | ||
|
||
override fun getMovementFlags( | ||
recyclerView: RecyclerView, | ||
viewHolder: RecyclerView.ViewHolder, | ||
): Int = | ||
if (viewHolder.itemViewType == ReviewerMenuSettingsRecyclerItem.DISPLAY_TYPE_VIEW_TYPE) { | ||
0 | ||
} else { | ||
movementFlags | ||
} | ||
|
||
override fun onMove( | ||
recyclerView: RecyclerView, | ||
viewHolder: RecyclerView.ViewHolder, | ||
target: RecyclerView.ViewHolder, | ||
): Boolean { | ||
val fromPosition = viewHolder.absoluteAdapterPosition | ||
val toPosition = target.absoluteAdapterPosition | ||
|
||
// `Always show` should always be the first element, so don't allow moving above it | ||
if (toPosition == 0) return false | ||
|
||
Collections.swap(items, fromPosition, toPosition) | ||
recyclerView.adapter?.notifyItemMoved(fromPosition, toPosition) | ||
return true | ||
} | ||
|
||
override fun clearView( | ||
recyclerView: RecyclerView, | ||
viewHolder: RecyclerView.ViewHolder, | ||
) { | ||
super.clearView(recyclerView, viewHolder) | ||
onClearViewListener?.onClearView(items) | ||
} | ||
|
||
override fun isLongPressDragEnabled(): Boolean = false | ||
|
||
private var onClearViewListener: OnClearViewListener<ReviewerMenuSettingsRecyclerItem>? = null | ||
|
||
/** Sets a listener to be called after [clearView] */ | ||
fun setOnClearViewListener(listener: OnClearViewListener<ReviewerMenuSettingsRecyclerItem>) { | ||
onClearViewListener = listener | ||
} | ||
|
||
override fun onSwiped( | ||
viewHolder: RecyclerView.ViewHolder, | ||
direction: Int, | ||
) { | ||
// do nothing | ||
} | ||
} | ||
|
||
fun interface OnClearViewListener<T> { | ||
fun onClearView(items: List<T>) | ||
} |
Oops, something went wrong.