Skip to content
Open
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
46 changes: 46 additions & 0 deletions AnkiDroid/src/main/java/com/ichi2/anki/dialogs/tags/TagsDialog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import androidx.core.content.ContextCompat
import androidx.core.os.BundleCompat
import androidx.core.os.bundleOf
import androidx.core.view.isVisible
import androidx.core.widget.doAfterTextChanged
import androidx.fragment.app.viewModels
import androidx.lifecycle.flowWithLifecycle
import androidx.lifecycle.lifecycleScope
Expand Down Expand Up @@ -389,13 +390,58 @@ class TagsDialog : AnalyticsDialogFragment {
addTag(input.toString())
d?.dismiss()
}

val inputET = addTagDialog.getInputField()

inputET.filters = arrayOf(addTagFilter)

if (!prefixTag.isNullOrEmpty()) {
// utilize the addTagFilter to append '::' properly by appending a space to prefixTag
Copy link
Contributor

Choose a reason for hiding this comment

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

Why removing the comment?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, that wasn’t intentional. I’ll add it back.

inputET.setText("$prefixTag ")
}

inputET.moveCursorToEnd()

val positiveButton =
addTagDialog.getButton(AlertDialog.BUTTON_POSITIVE)

positiveButton.isEnabled = false

val textInputLayout =
inputET.parent?.parent
as? com.google.android.material.textfield.TextInputLayout

inputET.doAfterTextChanged { text ->

val rawTag = text?.toString()?.trim()

if (rawTag.isNullOrEmpty()) {
textInputLayout?.error = null
positiveButton.isEnabled = false
return@doAfterTextChanged
}

lifecycleScope.launch {
val tags = viewModel.tags.await()

val normalized =
TagsUtil.getUniformedTag(rawTag)

val exists =
tags.contains(normalized)

if (exists) {
textInputLayout?.error =
getString(R.string.tag_already_exists)

positiveButton.isEnabled = false
} else {
textInputLayout?.error = null

positiveButton.isEnabled = true
}
}
}
addTagDialog.show()
}

Expand Down
1 change: 1 addition & 0 deletions AnkiDroid/src/main/res/values/02-strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@
<string name="dyn_deck_desc">This is a special deck for studying outside of the normal schedule. Cards will be automatically returned to their original decks after you review them. Deleting this deck from the deck list will return all remaining cards to their original deck.</string>
<string name="tag_editor_add_feedback">Touch “%2$s” to confirm adding “%1$s”</string>
<string name="tag_editor_add_feedback_existing">Existing tag “%1$s” selected</string>
<string name="tag_already_exists">Tag already exists</string>
<!-- Time spans -->

<!-- Currently only used if exporting APKG fails -->
Expand Down
Loading