Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 33 additions & 43 deletions AnkiDroid/src/main/java/com/ichi2/anki/widgets/DeckAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,16 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageButton
import android.widget.LinearLayout
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.core.content.res.getDrawableOrThrow
import androidx.core.content.withStyledAttributes
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.ichi2.anki.R
import com.ichi2.anki.common.annotations.NeedsTest
import com.ichi2.anki.databinding.DeckItemBinding
import com.ichi2.anki.deckpicker.DisplayDeckNode
import com.ichi2.anki.libanki.DeckId
import com.ichi2.anki.utils.ext.findViewById
import kotlinx.coroutines.runBlocking
import net.ankiweb.rsdroid.RustCleanup

Expand Down Expand Up @@ -92,17 +89,8 @@ class DeckAdapter(
}

class ViewHolder(
v: View,
) : RecyclerView.ViewHolder(v) {
val deckLayout: RelativeLayout = findViewById(R.id.DeckPickerHoriz)
val countsLayout: LinearLayout = findViewById(R.id.counts_layout)
val deckExpander: ImageButton = findViewById(R.id.deckpicker_expander)
val indentView: ImageButton = findViewById(R.id.deckpicker_indent)
val deckName: TextView = findViewById(R.id.deckpicker_name)
val deckNew: TextView = findViewById(R.id.deckpicker_new)
val deckLearn: TextView = findViewById(R.id.deckpicker_lrn)
val deckRev: TextView = findViewById(R.id.deckpicker_rev)
}
val binding: DeckItemBinding,
) : RecyclerView.ViewHolder(binding.root)

/**
* Set new data in the adapter. This should be used instead of [submitList] (which is called
Expand Down Expand Up @@ -134,67 +122,69 @@ class DeckAdapter(
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int,
): ViewHolder = ViewHolder(layoutInflater.inflate(R.layout.deck_item, parent, false))
): ViewHolder = ViewHolder(DeckItemBinding.inflate(layoutInflater, parent, false))

override fun onBindViewHolder(
holder: ViewHolder,
position: Int,
) {
val binding = holder.binding
val node = getItem(position)
// Set the expander icon and padding according to whether or not there are any subdecks
val deckLayout = holder.deckLayout
if (hasSubdecks) {
deckLayout.setPaddingRelative(startPaddingSmall, 0, endPadding, 0)
holder.deckExpander.visibility = View.VISIBLE
binding.deckLayout.setPaddingRelative(startPaddingSmall, 0, endPadding, 0)
binding.deckExpander.visibility = View.VISIBLE
// Create the correct expander for this deck
runBlocking { setDeckExpander(holder.deckExpander, holder.indentView, node) }
runBlocking { setDeckExpander(binding.deckExpander, holder.binding.indentView, node) }
} else {
holder.deckExpander.visibility = View.GONE
holder.indentView.minimumWidth = 0
deckLayout.setPaddingRelative(startPadding, 0, endPadding, 0)
binding.deckExpander.visibility = View.GONE
binding.indentView.minimumWidth = 0
binding.deckLayout.setPaddingRelative(startPadding, 0, endPadding, 0)
}
if (node.canCollapse) {
holder.deckExpander.setOnClickListener {
binding.deckExpander.setOnClickListener {
onDeckChildrenToggled(node.did)
notifyItemChanged(position) // Ensure UI updates
}
} else {
holder.deckExpander.isClickable = false
holder.deckExpander.setOnClickListener(null)
binding.deckExpander.isClickable = false
binding.deckExpander.setOnClickListener(null)
}
holder.deckLayout.setBackgroundResource(rowCurrentDrawable)
holder.binding.deckLayout.setBackgroundResource(rowCurrentDrawable)
// set a different background color for the current selected deck
if (node.isSelected) {
holder.deckLayout.setBackgroundResource(rowCurrentDrawable)
holder.binding.deckLayout.setBackgroundResource(rowCurrentDrawable)
if (activityHasBackground) {
val background = holder.deckLayout.background.mutate()
val background =
holder.binding.deckLayout.background
.mutate()
background.alpha = (255 * SELECTED_DECK_ALPHA_AGAINST_BACKGROUND).toInt()
holder.deckLayout.background = background
holder.binding.deckLayout.background = background
}
} else {
holder.deckLayout.setBackgroundResource(selectableItemBackground)
holder.binding.deckLayout.setBackgroundResource(selectableItemBackground)
}
// Set deck name and colour. Filtered decks have their own colour
holder.deckName.text = node.lastDeckNameComponent
holder.deckName.setTextColor(if (node.filtered) deckNameDynColor else deckNameDefaultColor)
binding.deckName.text = node.lastDeckNameComponent
binding.deckName.setTextColor(if (node.filtered) deckNameDynColor else deckNameDefaultColor)

// Set the card counts and their colors
holder.deckNew.text = node.newCount.toString()
holder.deckNew.setTextColor(if (node.newCount == 0) zeroCountColor else newCountColor)
holder.deckLearn.text = node.lrnCount.toString()
holder.deckLearn.setTextColor(if (node.lrnCount == 0) zeroCountColor else learnCountColor)
holder.deckRev.text = node.revCount.toString()
holder.deckRev.setTextColor(if (node.revCount == 0) zeroCountColor else reviewCountColor)
binding.deckNew.text = node.newCount.toString()
binding.deckNew.setTextColor(if (node.newCount == 0) zeroCountColor else newCountColor)
binding.deckLearn.text = node.lrnCount.toString()
binding.deckLearn.setTextColor(if (node.lrnCount == 0) zeroCountColor else learnCountColor)
binding.deckReview.text = node.revCount.toString()
binding.deckReview.setTextColor(if (node.revCount == 0) zeroCountColor else reviewCountColor)

holder.deckLayout.setOnClickListener { onDeckSelected(node.did) }
holder.deckLayout.setOnLongClickListener {
holder.binding.deckLayout.setOnClickListener { onDeckSelected(node.did) }
holder.binding.deckLayout.setOnLongClickListener {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small nitpick: not consistent, there are several lines in this method were holder.binding is used while most places use binding directly.

onDeckContextRequested(node.did)
true
}
holder.countsLayout.setOnClickListener { onDeckCountsSelected(node.did) }
binding.countsLayout.setOnClickListener { onDeckCountsSelected(node.did) }

// Right click listener for right click context menus
holder.deckLayout.setOnGenericMotionListener { _, motionEvent ->
holder.binding.deckLayout.setOnGenericMotionListener { _, motionEvent ->
if (motionEvent.action == android.view.MotionEvent.ACTION_BUTTON_PRESS &&
motionEvent.buttonState and android.view.MotionEvent.BUTTON_SECONDARY != 0
) {
Expand Down
16 changes: 8 additions & 8 deletions AnkiDroid/src/main/res/layout/deck_item.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
-->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/DeckPickerHoriz"
android:id="@+id/deck_layout"
android:background="?attr/selectableItemBackground"
android:layout_width="match_parent"
android:layout_height="wrap_content"
Expand All @@ -27,7 +27,7 @@
tools:background="@android:color/holo_orange_light">

<ImageButton
android:id="@+id/deckpicker_indent"
android:id="@+id/indent_view"
android:minWidth="0dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
Expand All @@ -37,19 +37,19 @@
android:id="@+id/deck_name_linear_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="@+id/deckpicker_indent"
android:layout_toEndOf="@+id/indent_view"
android:layout_toStartOf="@+id/counts_layout"
android:orientation="horizontal"
android:gravity="center_vertical">
<ImageButton
android:id="@+id/deckpicker_expander"
android:id="@+id/deck_expander"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:minWidth="48dp"
android:padding="12dp"
android:background="?attr/selectableItemBackgroundBorderless" />
<com.ichi2.ui.FixedTextView
android:id="@+id/deckpicker_name"
android:id="@+id/deck_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="48dp"
Expand All @@ -75,7 +75,7 @@
android:layout_alignParentEnd="true"
android:layout_centerVertical="true" >
<com.ichi2.ui.FixedTextView
android:id="@+id/deckpicker_new"
android:id="@+id/deck_new"
android:contentDescription="@string/deck_picker_new"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Expand All @@ -87,7 +87,7 @@
tools:text="10" />

<com.ichi2.ui.FixedTextView
android:id="@+id/deckpicker_lrn"
android:id="@+id/deck_learn"
android:contentDescription="@string/deck_picker_lrn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Expand All @@ -99,7 +99,7 @@
tools:text="42" />

<com.ichi2.ui.FixedTextView
android:id="@+id/deckpicker_rev"
android:id="@+id/deck_review"
android:contentDescription="@string/deck_picker_rev"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Expand Down
2 changes: 1 addition & 1 deletion AnkiDroid/src/test/java/com/ichi2/anki/DeckPickerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ class DeckPickerTest : RobolectricTest() {
// ACT: open up the Deck Context Menu
val deckToClick =
deckPickerBinding.decks.children.single {
it.findViewById<TextView>(R.id.deckpicker_name).text == "With Cards"
it.findViewById<TextView>(R.id.deck_name).text == "With Cards"
}
deckToClick.performLongClick()

Expand Down