forked from ankidroid/Anki-Android
-
Notifications
You must be signed in to change notification settings - Fork 0
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
11 changed files
with
729 additions
and
105 deletions.
There are no files selected for viewing
116 changes: 116 additions & 0 deletions
116
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,116 @@ | ||
/* | ||
* 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 androidx.appcompat.widget.AppCompatImageView | ||
import androidx.recyclerview.widget.RecyclerView | ||
import com.google.android.material.textview.MaterialTextView | ||
import com.ichi2.anki.R | ||
import com.ichi2.ui.FixedTextView | ||
|
||
class ReviewerMenuSettingsAdapter( | ||
private val items: List<ReviewerMenuSettingsRecyclerItem>, | ||
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() { | ||
override fun onCreateViewHolder( | ||
parent: ViewGroup, | ||
viewType: Int, | ||
): RecyclerView.ViewHolder = | ||
when (viewType) { | ||
ReviewerMenuSettingsRecyclerItem.ACTION_VIEW_TYPE -> { | ||
val itemView = | ||
LayoutInflater | ||
.from(parent.context) | ||
.inflate(R.layout.reviewer_menu_item, parent, false) | ||
ActionViewHolder(itemView) | ||
} | ||
ReviewerMenuSettingsRecyclerItem.DISPLAY_TYPE_VIEW_TYPE -> { | ||
val itemView = | ||
LayoutInflater | ||
.from(parent.context) | ||
.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<FixedTextView>(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) | ||
} | ||
} | ||
} | ||
|
||
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
79 changes: 79 additions & 0 deletions
79
.../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,79 @@ | ||
/* | ||
* 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 | ||
|
||
class ReviewerMenuSettingsTouchHelperCallback( | ||
private val items: List<ReviewerMenuSettingsRecyclerItem>, | ||
) : ItemTouchHelper.Callback() { | ||
private val movementFlags = makeMovementFlags(ItemTouchHelper.UP or ItemTouchHelper.DOWN, 0) | ||
private var onClearViewListener: OnClearViewListener<ReviewerMenuSettingsRecyclerItem>? = null | ||
|
||
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` is always 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 onSwiped( | ||
viewHolder: RecyclerView.ViewHolder, | ||
direction: Int, | ||
) { | ||
// do nothing | ||
} | ||
|
||
override fun clearView( | ||
recyclerView: RecyclerView, | ||
viewHolder: RecyclerView.ViewHolder, | ||
) { | ||
super.clearView(recyclerView, viewHolder) | ||
onClearViewListener?.onClearView(items) | ||
} | ||
|
||
override fun isLongPressDragEnabled(): Boolean = false | ||
|
||
/** Sets a listener to be called after [clearView] */ | ||
fun setOnClearViewListener(listener: OnClearViewListener<ReviewerMenuSettingsRecyclerItem>) { | ||
onClearViewListener = listener | ||
} | ||
} | ||
|
||
fun interface OnClearViewListener<T> { | ||
fun onClearView(items: List<T>) | ||
} |
Oops, something went wrong.