Skip to content

Commit 9eeef01

Browse files
BrayanDSOmikehardy
authored andcommitted
perf: improve tags dialog initialization
1 parent 14b7351 commit 9eeef01

File tree

10 files changed

+314
-341
lines changed

10 files changed

+314
-341
lines changed

AnkiDroid/src/main/java/com/ichi2/anki/AbstractFlashcardViewer.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2619,12 +2619,11 @@ abstract class AbstractFlashcardViewer :
26192619
get() = displayAnswer
26202620

26212621
internal fun showTagsDialog() {
2622-
val tags = ArrayList(getColUnsafe.tags.all())
2623-
val selTags = ArrayList(currentCard!!.note(getColUnsafe).tags)
2622+
val noteId = currentCard!!.note(getColUnsafe).id
26242623
val dialog =
26252624
tagsDialogFactory!!
26262625
.newTagsDialog()
2627-
.withArguments(this, TagsDialog.DialogType.EDIT_TAGS, selTags, tags)
2626+
.withArguments(this, TagsDialog.DialogType.EDIT_TAGS, noteIds = listOf(noteId))
26282627
showDialogFragment(dialog)
26292628
}
26302629

AnkiDroid/src/main/java/com/ichi2/anki/CardBrowser.kt

Lines changed: 11 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ import com.ichi2.libanki.ChangeManager
121121
import com.ichi2.libanki.Collection
122122
import com.ichi2.libanki.DeckId
123123
import com.ichi2.libanki.DeckNameId
124-
import com.ichi2.libanki.NoteId
125124
import com.ichi2.libanki.SortOrder
126125
import com.ichi2.libanki.undoableOp
127126
import com.ichi2.ui.CardBrowserSearchView
@@ -131,10 +130,8 @@ import com.ichi2.utils.dp
131130
import com.ichi2.utils.increaseHorizontalPaddingOfOverflowMenuIcons
132131
import com.ichi2.utils.updatePaddingRelative
133132
import com.ichi2.widget.WidgetStatus.updateInBackground
134-
import kotlinx.coroutines.Dispatchers
135133
import kotlinx.coroutines.Job
136134
import kotlinx.coroutines.launch
137-
import kotlinx.coroutines.withContext
138135
import net.ankiweb.rsdroid.RustCleanup
139136
import net.ankiweb.rsdroid.Translations
140137
import timber.log.Timber
@@ -1547,84 +1544,16 @@ open class CardBrowser :
15471544
if (!viewModel.hasSelectedAnyRows()) {
15481545
Timber.d("showEditTagsDialog: called with empty selection")
15491546
}
1550-
1551-
var progressMax: Int? = null // this can be made null to blank the dialog
1552-
var progress = 0
1553-
1554-
fun onProgress(progressContext: ProgressContext) {
1555-
val max = progressMax
1556-
if (max == null) {
1557-
progressContext.amount = null
1558-
progressContext.text = getString(R.string.dialog_processing)
1559-
} else {
1560-
progressContext.amount = Pair(progress, max)
1561-
}
1562-
}
1563-
launchCatchingTask {
1564-
withProgress(extractProgress = ::onProgress) {
1565-
val allTags = withCol { tags.all() }
1566-
val selectedNoteIds = viewModel.queryAllSelectedNoteIds()
1567-
1568-
progressMax = selectedNoteIds.size * 2
1569-
// TODO!! This is terribly slow on AnKing
1570-
val checkedTags =
1571-
withCol {
1572-
selectedNoteIds
1573-
.asSequence() // reduce memory pressure
1574-
.flatMap { nid ->
1575-
progress++
1576-
getNote(nid).tags // requires withCol
1577-
}.distinct()
1578-
.toList()
1579-
}
1580-
1581-
if (selectedNoteIds.size == 1) {
1582-
Timber.d("showEditTagsDialog: edit tags for one note")
1583-
tagsDialogListenerAction = TagsDialogListenerAction.EDIT_TAGS
1584-
val dialog =
1585-
tagsDialogFactory.newTagsDialog().withArguments(
1586-
this@CardBrowser,
1587-
type = TagsDialog.DialogType.EDIT_TAGS,
1588-
checkedTags = checkedTags,
1589-
allTags = allTags,
1590-
)
1591-
showDialogFragment(dialog)
1592-
return@withProgress
1593-
}
1594-
// TODO!! This is terribly slow on AnKing
1595-
// PERF: This MUST be combined with the above sequence - this becomes O(2n) on a
1596-
// database operation performed over 30k times
1597-
val uncheckedTags =
1598-
withCol {
1599-
selectedNoteIds
1600-
.asSequence() // reduce memory pressure
1601-
.flatMap { nid: NoteId ->
1602-
progress++
1603-
val note = getNote(nid) // requires withCol
1604-
val noteTags = note.tags.toSet()
1605-
allTags.filter { t: String? -> !noteTags.contains(t) }
1606-
}.distinct()
1607-
.toList()
1608-
}
1609-
1610-
progressMax = null
1611-
1612-
Timber.d("showEditTagsDialog: edit tags for multiple note")
1613-
tagsDialogListenerAction = TagsDialogListenerAction.EDIT_TAGS
1614-
1615-
// withArguments performs IO, can be 18 seconds
1616-
val dialog =
1617-
withContext(Dispatchers.IO) {
1618-
tagsDialogFactory.newTagsDialog().withArguments(
1619-
context = this@CardBrowser,
1620-
type = TagsDialog.DialogType.EDIT_TAGS,
1621-
checkedTags = checkedTags,
1622-
uncheckedTags = uncheckedTags,
1623-
allTags = allTags,
1624-
)
1625-
}
1626-
showDialogFragment(dialog)
1627-
}
1547+
tagsDialogListenerAction = TagsDialogListenerAction.EDIT_TAGS
1548+
lifecycleScope.launch {
1549+
val noteIds = viewModel.queryAllSelectedNoteIds()
1550+
val dialog =
1551+
tagsDialogFactory.newTagsDialog().withArguments(
1552+
this@CardBrowser,
1553+
type = TagsDialog.DialogType.EDIT_TAGS,
1554+
noteIds = noteIds,
1555+
)
1556+
showDialogFragment(dialog)
16281557
}
16291558
}
16301559

@@ -1635,8 +1564,7 @@ open class CardBrowser :
16351564
tagsDialogFactory.newTagsDialog().withArguments(
16361565
context = this@CardBrowser,
16371566
type = TagsDialog.DialogType.FILTER_BY_TAG,
1638-
checkedTags = ArrayList(0),
1639-
allTags = withCol { tags.all() },
1567+
noteIds = emptyList(),
16401568
)
16411569
showDialogFragment(dialog)
16421570
}

AnkiDroid/src/main/java/com/ichi2/anki/NoteEditor.kt

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1605,18 +1605,13 @@ class NoteEditor :
16051605
}
16061606

16071607
private fun showTagsDialog() {
1608-
if (selectedTags == null) {
1609-
selectedTags = ArrayList(0)
1610-
}
1611-
val tags = ArrayList(getColUnsafe.tags.all())
1612-
val selTags = ArrayList(selectedTags!!)
1608+
val selTags = selectedTags?.let { ArrayList(it) } ?: arrayListOf()
16131609
val dialog =
16141610
with(requireContext()) {
16151611
tagsDialogFactory!!.newTagsDialog().withArguments(
16161612
context = this,
16171613
type = TagsDialog.DialogType.EDIT_TAGS,
16181614
checkedTags = selTags,
1619-
allTags = tags,
16201615
)
16211616
}
16221617
showDialogFragment(dialog)

0 commit comments

Comments
 (0)