From 5959c1e0e0c4714b72abc70e91abefbc70cd8385 Mon Sep 17 00:00:00 2001 From: Akshit Sinha <86671025+viciousAegis@users.noreply.github.com> Date: Sun, 27 Feb 2022 20:03:28 +0530 Subject: [PATCH] "Deck Search" display now same as the Deck Viewer (#10389) * "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. --- .../ichi2/anki/dialogs/DeckSelectionDialog.kt | 26 ++++++++++++-- .../anki/dialogs/DeckSelectionDialogTest.kt | 36 +++++++++++++++++++ 2 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 AnkiDroid/src/test/java/com/ichi2/anki/dialogs/DeckSelectionDialogTest.kt diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/dialogs/DeckSelectionDialog.kt b/AnkiDroid/src/main/java/com/ichi2/anki/dialogs/DeckSelectionDialog.kt index 7d6a888ab601..cd3700b1e645 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/dialogs/DeckSelectionDialog.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/dialogs/DeckSelectionDialog.kt @@ -219,17 +219,19 @@ open class DeckSelectionDialog : AnalyticsDialogFragment() { open inner class DecksArrayAdapter(deckNames: List) : RecyclerView.Adapter(), 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 } } @@ -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 @@ -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) { diff --git a/AnkiDroid/src/test/java/com/ichi2/anki/dialogs/DeckSelectionDialogTest.kt b/AnkiDroid/src/test/java/com/ichi2/anki/dialogs/DeckSelectionDialogTest.kt new file mode 100644 index 000000000000..69ff21e9be4a --- /dev/null +++ b/AnkiDroid/src/test/java/com/ichi2/anki/dialogs/DeckSelectionDialogTest.kt @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2022 Akshit Sinha + * + * 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 . + */ + +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)) + } +}