-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Fix missing minus symbol when updating tags in card browser #19499
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
thedroiddiv
wants to merge
2
commits into
ankidroid:main
Choose a base branch
from
thedroiddiv:fix/incorrect-tags
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+188
−11
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,9 +26,12 @@ import kotlinx.coroutines.flow.asStateFlow | |
| /** | ||
| * @param noteIds IDs of notes whose tags should bfe retrieved and marked as "checked" | ||
| * @param checkedTags additional list of checked tags. | ||
| * These tags coming from EXTRAS are treated as absolute checked and cannot be indeterminate. | ||
| * @param isCustomStudying true if all inputs are to be handled as unchecked tags, false otherwise( | ||
| * this is a temporary parameter until custom study by tags is modified) | ||
| * They are joined with the tags retrieved from noteIds | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems to be in the wrong place, as does the line below |
||
| * | ||
| * @see <a href="https://github.com/ankidroid/Anki-Android/pull/19499#discussion_r2532184695">Extra checked tags</> | ||
| */ | ||
| class TagsDialogViewModel( | ||
| noteIds: Collection<NoteId> = emptyList(), | ||
|
|
@@ -43,17 +46,26 @@ class TagsDialogViewModel( | |
| init { | ||
| tags = | ||
| asyncIO { | ||
| val allTags = withCol { tags.all() }.toSet() | ||
| val allCheckedTags = | ||
| noteIds | ||
| .flatMapIndexedTo(mutableSetOf()) { index, nid -> | ||
| _initProgress.emit(InitProgress.FetchingNoteTags(index + 1, noteIds.size)) | ||
| withCol { getNote(nid) }.tags | ||
| }.apply { | ||
| addAll(checkedTags) | ||
| } | ||
| val allTags = withCol { tags.all() } | ||
| val allCheckedTags = mutableSetOf<String>() | ||
| val uncheckedTags = mutableSetOf<String>() | ||
| // For each note, put the checked tag in checked list and unchecked tags in unchecked list. | ||
| // This will result in few tags being present in both lists, checked and | ||
| // unchecked as they might be present in one note but absent in any other. | ||
| // Such tags are referred as `indeterminateTags` in [TagsList] | ||
| noteIds.forEachIndexed { index, nid -> | ||
| // TODO: Lift up withCol{ } call out of loop. Performs `N` expensive db queries. | ||
| val noteTags = withCol { getNote(nid) }.tags | ||
| _initProgress.emit(InitProgress.FetchingNoteTags(index + 1, noteIds.size)) | ||
| val (checked, unchecked) = allTags.partition { noteTags.contains(it) } | ||
| allCheckedTags.addAll(checked) | ||
| uncheckedTags.addAll(unchecked) | ||
| } | ||
| // add the extra checked tags, these are to be shown as `checked` and cannot be indeterminate | ||
| val extraCheckedTags = checkedTags.toSet() | ||
| allCheckedTags.addAll(extraCheckedTags) | ||
| uncheckedTags.removeAll(extraCheckedTags) | ||
| _initProgress.emit(InitProgress.Processing) | ||
| val uncheckedTags = allTags - allCheckedTags | ||
| if (isCustomStudying) { | ||
| TagsList( | ||
| allTags = allCheckedTags, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
164 changes: 164 additions & 0 deletions
164
AnkiDroid/src/test/java/com/ichi2/anki/dialogs/tags/TagsDialogViewModelTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,164 @@ | ||
| /* | ||
| * Copyright (c) 2025 Divyansh Kushwaha <[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.tags | ||
|
|
||
| import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
| import com.ichi2.anki.RobolectricTest | ||
| import com.ichi2.anki.libanki.NoteId | ||
| import com.ichi2.anki.libanki.testutils.ext.newNote | ||
| import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
| import kotlinx.coroutines.test.StandardTestDispatcher | ||
| import org.junit.Assert.assertEquals | ||
| import org.junit.Assert.assertFalse | ||
| import org.junit.Assert.assertTrue | ||
| import org.junit.Before | ||
| import org.junit.Test | ||
| import org.junit.runner.RunWith | ||
| import kotlin.properties.Delegates | ||
|
|
||
| @OptIn(ExperimentalCoroutinesApi::class) | ||
| @RunWith(AndroidJUnit4::class) | ||
| class TagsDialogViewModelTest : RobolectricTest() { | ||
| private var note1 by Delegates.notNull<NoteId>() | ||
| private var note2 by Delegates.notNull<NoteId>() | ||
david-allison marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| private val testDispatcher = StandardTestDispatcher() | ||
|
|
||
| /** | ||
| * Initialize the collections with three notes with each having some tags | ||
| * ``` | ||
| * note1 a,b,c | ||
| * note2 b,d | ||
| * note3 e,f | ||
| * ``` | ||
| */ | ||
| @Before | ||
| fun setupNotes() { | ||
| setupTestDispatcher(testDispatcher) | ||
|
|
||
| val n1 = col.newNote() | ||
| col.addNote(n1, 0) | ||
| col.tags.bulkAdd(listOf(n1.id), "a b c") | ||
| note1 = n1.id | ||
|
|
||
| val n2 = col.newNote() | ||
| col.addNote(n2, 0) | ||
| col.tags.bulkAdd(listOf(n2.id), "b d") | ||
| note2 = n2.id | ||
|
|
||
| val n3 = col.newNote() | ||
| col.addNote(n3, 0) | ||
| col.tags.bulkAdd(listOf(n3.id), "e f") | ||
| } | ||
|
|
||
| @Test | ||
| fun `single note produces correct checked, indeterminate and all sets`() = | ||
| runTest(testDispatcher) { | ||
| val vm = | ||
| TagsDialogViewModel( | ||
| noteIds = listOf(note1), | ||
| checkedTags = listOf("a"), | ||
| isCustomStudying = false, | ||
| ) | ||
| val tags = vm.tags.await() | ||
| // only one note, tags can either be checked or unchecked only | ||
| // all : {a,b,c,d,e,f} | ||
| // indeterminate: { } | ||
| // checked: {a,b,c} | ||
| assertEquals(listOf("a", "b", "c"), tags.copyOfCheckedTagList().sorted()) | ||
| assertTrue(tags.copyOfIndeterminateTagList().isEmpty()) | ||
| assertEquals(listOf("a", "b", "c", "d", "e", "f"), tags.copyOfAllTagList().sorted()) | ||
| } | ||
|
|
||
| @Test | ||
| fun `multiple notes create indeterminate tags correctly`() = | ||
| runTest(testDispatcher) { | ||
| // note 1 a, b, c | ||
| // note 2 b, d | ||
| val vm = | ||
| TagsDialogViewModel( | ||
| noteIds = listOf(note1, note2), | ||
| checkedTags = emptyList(), | ||
| isCustomStudying = false, | ||
| ) | ||
|
|
||
| val tags = vm.tags.await() | ||
| val checked = tags.copyOfCheckedTagList() | ||
| val ind = tags.copyOfIndeterminateTagList() | ||
|
|
||
| // All tags in the collection: {a,b,c,d,e,f} | ||
| // Assuming indeterminate logic in [TagsList] is correct | ||
| // a [-] - indeterminate | ||
| // b [x] - checked | ||
| // c [-] - indeterminate | ||
| // d [-] - indeterminate | ||
| // e [ ] - unchecked | ||
| // f [ ] - unchecked | ||
| assertEquals(listOf("b"), checked.sorted()) | ||
| assertEquals(listOf("a", "c", "d"), ind.sorted()) | ||
| assertFalse(tags.isChecked("e")) | ||
| assertFalse(tags.isChecked("f")) | ||
| } | ||
|
|
||
| @Test | ||
| fun `extra checkedTags is absolute checked and not indeterminate`() = | ||
| runTest(testDispatcher) { | ||
| val vm = | ||
| TagsDialogViewModel( | ||
| noteIds = listOf(note1), | ||
| checkedTags = listOf("d", "x"), | ||
| isCustomStudying = false, | ||
| ) | ||
|
|
||
| // note1.tags {a,b} | ||
| // extraCheckedTags {d, x} | ||
| // allTags {a,b,c,d,e} | ||
| // implies inside dialog | ||
| // checkedTags {a, b, d, x} | ||
| // uncheckedTags { c, e } | ||
| // indeterminate { } | ||
| val tags = vm.tags.await() | ||
| assertTrue(tags.isChecked("d")) | ||
| assertFalse(tags.isIndeterminate("d")) | ||
| assertTrue(tags.isChecked("x")) | ||
| assertFalse(tags.isIndeterminate("d")) | ||
| assertTrue(tags.copyOfAllTagList().contains("x")) | ||
| } | ||
|
|
||
| @Test | ||
| fun `custom study mode makes all tags unchecked`() = | ||
| runTest(testDispatcher) { | ||
| val vm = | ||
| TagsDialogViewModel( | ||
| noteIds = listOf(note1, note2), | ||
| checkedTags = listOf("a"), | ||
| isCustomStudying = true, | ||
| ) | ||
| val tags = vm.tags.await() | ||
| // tags from both notes = {a,b,c,d} | ||
| val all = tags.copyOfAllTagList().sorted() | ||
| assertEquals(listOf("a", "b", "c", "d"), all) | ||
| // custom study: checked = empty | ||
| assertTrue(tags.copyOfCheckedTagList().isEmpty()) | ||
| // custom study: indeterminate = empty | ||
| assertTrue(tags.copyOfIndeterminateTagList().isEmpty()) | ||
| // custom study: | ||
| // checked - empty, unchecked - empty, all - not empty | ||
| // implies unchecked = allTags | ||
| assertTrue(tags.copyOfAllTagList().isNotEmpty()) | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel this documentation is a little too far away from the caller to understand what
EXTRASisFully optional, or moving it to another issue