Skip to content

Commit

Permalink
feat: propose renaming decks to '01' to reorder
Browse files Browse the repository at this point in the history
A regular complaint is that '10' is between '1' and '2'
in the deck list:

['1', '10', '2']

If a user renames a deck, the ordering is correct:

['01', '02', '10']

We should inform the user that this is the solution

Fixes 16036
  • Loading branch information
david-allison committed Apr 2, 2024
1 parent bcfdac5 commit 7bbf72c
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
12 changes: 12 additions & 0 deletions AnkiDroid/src/main/java/com/ichi2/anki/dialogs/CreateDeckDialog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package com.ichi2.anki.dialogs

import android.app.Activity
import android.content.Context
import androidx.annotation.VisibleForTesting
import androidx.appcompat.app.AlertDialog
import com.google.android.material.snackbar.Snackbar
import com.ichi2.anki.CollectionHelper
Expand Down Expand Up @@ -103,6 +104,14 @@ class CreateDeckDialog(
}
dialog.getInputTextLayout().error = null
dialog.positiveButton.isEnabled = true

// Users expect the ordering [1, 2, 10], but get [1, 10, 2]
// To fix: they need [01, 02, 10]. Show a hint to help them
dialog.getInputTextLayout().helperText = if (text.containsNumberLargerThanNine()) {
context.getString(R.string.create_deck_numeric_hint)
} else {
null
}
}
shownDialog = dialog
return dialog
Expand Down Expand Up @@ -221,3 +230,6 @@ class CreateDeckDialog(
}
}
}

@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
fun CharSequence.containsNumberLargerThanNine(): Boolean = Regex("""[1-9]\d+""").find(this) != null
1 change: 1 addition & 0 deletions AnkiDroid/src/main/res/values/03-dialogs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -261,4 +261,5 @@

<string name="deck_already_exists">Deck already exists</string>

<string name="create_deck_numeric_hint">If you have deck ordering issues (e.g. ‘10’ appears before ‘2’), replace ‘2’ with ‘02’</string>
</resources>
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/****************************************************************************************
* Copyright (c) 2021 Akshay Jadhav <[email protected]> *
* Copyright (c) 2024 David Allison <[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 *
Expand Down Expand Up @@ -33,7 +34,8 @@ import com.ichi2.utils.positiveButton
import okhttp3.internal.closeQuietly
import org.hamcrest.CoreMatchers.equalTo
import org.hamcrest.CoreMatchers.nullValue
import org.hamcrest.MatcherAssert.*
import org.hamcrest.Matcher
import org.hamcrest.MatcherAssert.assertThat
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
Expand Down Expand Up @@ -116,6 +118,28 @@ class CreateDeckDialogTest : RobolectricTest() {
}
}

@Test
fun `deck ordering hint`() {
// The correct way to order a deck is ['01', '02', '10']
val expectedText = "If you have deck ordering issues (e.g. ‘10’ appears before ‘2’), replace ‘2’ with ‘02’"
testDialog(DeckDialogType.DECK) {
fun assertHelperText(reason: String?, matcher: Matcher<in CharSequence?>) =
assertThat(reason, getInputTextLayout().helperText, matcher)

input = "test"
assertHelperText("no number suggestion if text-only", nullValue())
input = "1. Cheese"
assertHelperText("no number suggestion if number is less than 10", nullValue())
input = "10. Cheese"
assertHelperText(
"Number suggestion if number is greater than or equal to 10",
equalTo(expectedText)
)
input = "1. Cheese"
assertHelperText("hint is removed if the number is removed", nullValue())
}
}

@Test
fun nameMayNotBeZeroLength() {
testDialog(DeckDialogType.DECK) {
Expand Down Expand Up @@ -234,3 +258,19 @@ class CreateDeckDialogTest : RobolectricTest() {
.joinToString("::")
}
}

/** Test of [CreateDeckDialog] */
class CreateDeckDialogNonAndroidTest {
@Test
fun `number larger than nine detection`() {
fun assertLargerThanNine(reason: String?, input: String, result: Boolean) =
assertThat(reason, input.containsNumberLargerThanNine(), equalTo(result))

assertLargerThanNine("empty string", "", false)
assertLargerThanNine("text", "deck name", false)
assertLargerThanNine("less than ten", "9. - Chemicals", false)
assertLargerThanNine("Ten or greater", "10. - Chemicals", true)
assertLargerThanNine("Ten or greater", "99. - Chemicals", true)
assertLargerThanNine("zero prefix", "09. - Chemicals", false)
}
}

0 comments on commit 7bbf72c

Please sign in to comment.