Skip to content

Commit

Permalink
"Deck Search" display now same as the Deck Viewer (#10389)
Browse files Browse the repository at this point in the history
* "Deck Search" display updated

Instead of displaying the entire deck path, now only the subdeck name is displayed along with appropriate indentation. Deck and subdeck creation works just like before.
  • Loading branch information
viciousAegis authored Feb 27, 2022
1 parent babb9ff commit 5959c1e
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -219,17 +219,19 @@ open class DeckSelectionDialog : AnalyticsDialogFragment() {

open inner class DecksArrayAdapter(deckNames: List<SelectableDeck>) : RecyclerView.Adapter<DecksArrayAdapter.ViewHolder>(), Filterable {
inner class ViewHolder(val deckTextView: TextView) : RecyclerView.ViewHolder(deckTextView) {
var deckName: String = ""

fun setDeck(deck: SelectableDeck) {
deckTextView.text = deck.name
deckName = deck.name
deckTextView.text = deck.displayName
}

init {
deckTextView.setOnClickListener {
val deckName = deckTextView.text.toString()
selectDeckByNameAndClose(deckName)
}
deckTextView.setOnLongClickListener { // creating sub deck with parent deck path
showSubDeckDialog(deckTextView.text.toString())
showSubDeckDialog(deckName)
true
}
}
Expand Down Expand Up @@ -312,6 +314,15 @@ open class DeckSelectionDialog : AnalyticsDialogFragment() {
*/
val name: String

/**
* The name to be displayed to the user. Contains
* only the sub-deck name with proper indentation
* rather than the entire deck name.
* Eg: foo::bar -> \t\tbar
*/
val displayName: String // TODO should be a lazy value
get() = getDisplayName(name)

constructor(deckId: Long, name: String) {
this.deckId = deckId
this.name = name
Expand All @@ -323,6 +334,15 @@ open class DeckSelectionDialog : AnalyticsDialogFragment() {
name = `in`.readString()!!
}

/**
* @param name the entire name(path) of the deck
* @return the deck/subdeck name to be displayed to the user
*/
private fun getDisplayName(name: String): String {
var nameArr = name.split("::")
return "\t\t".repeat(nameArr.size - 1) + nameArr[nameArr.size - 1]
}

/** "All decks" comes first. Then usual deck name order. */
override fun compareTo(other: SelectableDeck): Int {
if (deckId == Stats.ALL_DECKS_ID) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (c) 2022 Akshit Sinha <[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.dialogs

import com.ichi2.anki.dialogs.DeckSelectionDialog.SelectableDeck
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers
import org.junit.Test

class DeckSelectionDialogTest {

@Test
fun verifyDeckDisplayName() {
val input = "deck::sub-deck::sub-deck2::sub-deck3"
val expected = "\t\t\t\t\t\tsub-deck3"

val deck = SelectableDeck(1234, input)
val actual: String = deck.displayName

assertThat(actual, Matchers.equalTo(expected))
}
}

0 comments on commit 5959c1e

Please sign in to comment.