-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Copy pathCardBrowserViewModel.kt
1122 lines (968 loc) · 38.7 KB
/
CardBrowserViewModel.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2023 David Allison <davidallisongithub@gmail.com>
*
* 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.browser
import android.os.Parcel
import android.os.Parcelable
import androidx.annotation.CheckResult
import androidx.core.content.edit
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import androidx.lifecycle.viewmodel.initializer
import androidx.lifecycle.viewmodel.viewModelFactory
import anki.collection.OpChanges
import anki.collection.OpChangesWithCount
import anki.search.BrowserColumns
import anki.search.BrowserRow
import com.ichi2.anki.AnkiDroidApp
import com.ichi2.anki.CollectionManager
import com.ichi2.anki.CollectionManager.TR
import com.ichi2.anki.CollectionManager.withCol
import com.ichi2.anki.CrashReportService
import com.ichi2.anki.DeckSpinnerSelection.Companion.ALL_DECKS_ID
import com.ichi2.anki.Flag
import com.ichi2.anki.PreviewerDestination
import com.ichi2.anki.browser.RepositionCardsRequest.RepositionData
import com.ichi2.anki.export.ExportDialogFragment.ExportType
import com.ichi2.anki.launchCatchingIO
import com.ichi2.anki.model.CardStateFilter
import com.ichi2.anki.model.CardsOrNotes
import com.ichi2.anki.model.CardsOrNotes.CARDS
import com.ichi2.anki.model.CardsOrNotes.NOTES
import com.ichi2.anki.model.SortType
import com.ichi2.anki.pages.CardInfoDestination
import com.ichi2.anki.preferences.SharedPreferencesProvider
import com.ichi2.annotations.NeedsTest
import com.ichi2.libanki.Card
import com.ichi2.libanki.CardId
import com.ichi2.libanki.CardType
import com.ichi2.libanki.ChangeManager
import com.ichi2.libanki.DeckId
import com.ichi2.libanki.QueueType
import com.ichi2.libanki.QueueType.ManuallyBuried
import com.ichi2.libanki.QueueType.SiblingBuried
import com.ichi2.libanki.undoableOp
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.Job
import kotlinx.coroutines.async
import kotlinx.coroutines.ensureActive
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.combineTransform
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.flattenMerge
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import net.ankiweb.rsdroid.BackendException
import org.jetbrains.annotations.VisibleForTesting
import timber.log.Timber
import java.io.DataInputStream
import java.io.DataOutputStream
import java.io.File
import java.io.FileInputStream
import java.io.FileOutputStream
import java.util.Collections
import kotlin.math.max
import kotlin.math.min
// TODO: move the tag computation to ViewModel
@NeedsTest("reverseDirectionFlow/sortTypeFlow are not updated on .launch { }")
@NeedsTest("columIndex1/2 config is not not updated on init")
@NeedsTest("13442: selected deck is not changed, as this affects the reviewer")
@NeedsTest("search is called after launch()")
class CardBrowserViewModel(
private val lastDeckIdRepository: LastDeckIdRepository,
private val cacheDir: File,
options: CardBrowserLaunchOptions?,
preferences: SharedPreferencesProvider,
private val manualInit: Boolean = false,
) : ViewModel(),
SharedPreferencesProvider by preferences {
// TODO: abstract so we can use a `Context` and `pref_display_filenames_in_browser_key`
val showMediaFilenames = sharedPrefs().getBoolean("card_browser_show_media_filenames", false)
/** A job which ensures that parallel searches do not occur */
var searchJob: Job? = null
private set
// temporary flow for refactoring - called when cards are cleared
val flowOfCardsUpdated = MutableSharedFlow<Unit>()
val cards = BrowserRowCollection(CARDS, mutableListOf())
val flowOfSearchState = MutableSharedFlow<SearchState>()
var searchTerms = ""
private set
private var restrictOnDeck: String = ""
/** text in the search box (potentially unsubmitted) */
// this does not currently bind to the value in the UI and is only used for posting
val flowOfFilterQuery = MutableSharedFlow<String>()
/**
* Whether the browser is working in Cards mode or Notes mode.
* default: [CARDS]
* */
private val flowOfCardsOrNotes = MutableStateFlow(CARDS)
val cardsOrNotes get() = flowOfCardsOrNotes.value
// card that was clicked (not marked)
var currentCardId: CardId = 0
private val sortTypeFlow = MutableStateFlow(SortType.NO_SORTING)
val order get() = sortTypeFlow.value
private val reverseDirectionFlow = MutableStateFlow(ReverseDirection(orderAsc = false))
val orderAsc get() = reverseDirectionFlow.value.orderAsc
/**
* A map from column backend key to backend column definition
*
* @see [flowOfColumnHeadings]
*/
private val flowOfAllColumns = MutableSharedFlow<Map<String, BrowserColumns.Column>>()
val flowOfActiveColumns =
MutableStateFlow(
BrowserColumnCollection(
listOf(
CardBrowserColumn.QUESTION,
CardBrowserColumn.ANSWER,
),
),
)
@get:VisibleForTesting
val activeColumns
get() = flowOfActiveColumns.value.columns
val flowOfSearchQueryExpanded = MutableStateFlow(false)
private val searchQueryInputFlow = MutableStateFlow<String?>(null)
/** The query which is currently in the search box, potentially null. Only set when search box was open */
val tempSearchQuery get() = searchQueryInputFlow.value
/** Whether the current element in the search bar can be saved */
val flowOfCanSearch =
searchQueryInputFlow
.map { it?.isNotEmpty() == true }
.stateIn(viewModelScope, SharingStarted.Eagerly, initialValue = false)
val flowOfIsTruncated: MutableStateFlow<Boolean> =
MutableStateFlow(sharedPrefs().getBoolean("isTruncated", false))
val isTruncated get() = flowOfIsTruncated.value
private val _selectedRows: MutableSet<CardOrNoteId> = Collections.synchronizedSet(LinkedHashSet())
// immutable accessor for _selectedRows
val selectedRows: Set<CardOrNoteId> get() = _selectedRows
private val refreshSelectedRowsFlow = MutableSharedFlow<Unit>()
val flowOfSelectedRows: Flow<Set<CardOrNoteId>> =
flowOf(selectedRows).combine(refreshSelectedRowsFlow) { row, _ -> row }
suspend fun queryAllSelectedCardIds() = selectedRows.queryCardIds(this.cardsOrNotes)
suspend fun queryAllSelectedNoteIds() = selectedRows.queryNoteIds(this.cardsOrNotes)
@VisibleForTesting
internal suspend fun queryAllCardIds() = cards.queryCardIds()
var lastSelectedId: CardOrNoteId? = null
val flowOfIsInMultiSelectMode =
flowOfSelectedRows
.map { it.isNotEmpty() }
.stateIn(viewModelScope, SharingStarted.Eagerly, initialValue = false)
val isInMultiSelectMode
get() = flowOfIsInMultiSelectMode.value
val lastDeckId: DeckId?
get() = lastDeckIdRepository.lastDeckId
suspend fun setDeckId(deckId: DeckId) {
Timber.i("setting deck: %d", deckId)
lastDeckIdRepository.lastDeckId = deckId
restrictOnDeck =
if (deckId == ALL_DECKS_ID) {
""
} else {
val deckName = withCol { decks.name(deckId) }
"deck:\"$deckName\" "
}
flowOfDeckId.update { deckId }
}
val flowOfDeckId = MutableStateFlow(lastDeckId)
val deckId get() = flowOfDeckId.value
suspend fun queryCardInfoDestination(): CardInfoDestination? {
val firstSelectedCard = selectedRows.firstOrNull()?.toCardId(cardsOrNotes) ?: return null
return CardInfoDestination(firstSelectedCard)
}
suspend fun queryDataForCardEdit(id: CardOrNoteId): CardId = id.toCardId(cardsOrNotes)
private suspend fun getInitialDeck(): DeckId {
// TODO: Handle the launch intent
val lastDeckId = lastDeckId
if (lastDeckId == ALL_DECKS_ID) {
return ALL_DECKS_ID
}
// If a valid value for last deck exists then use it, otherwise use libanki selected deck
return if (lastDeckId != null && withCol { decks.get(lastDeckId) != null }) {
lastDeckId
} else {
withCol { decks.selected() }
}
}
val flowOfInitCompleted = MutableStateFlow(false)
val flowOfColumnHeadings: StateFlow<List<ColumnHeading>> =
combine(flowOfActiveColumns, flowOfCardsOrNotes, flowOfAllColumns) { activeColumns, cardsOrNotes, allColumns ->
Timber.d("updated headings for %d columns", activeColumns.count)
activeColumns.columns.map {
ColumnHeading(
label = allColumns[it.ankiColumnKey]!!.getLabel(cardsOrNotes),
)
}
// stateIn is required for tests
}.stateIn(viewModelScope, SharingStarted.Eagerly, initialValue = emptyList())
/**
* Whether the task launched from CardBrowserViewModel.init has completed.
*
* If `false`, we don't have the initial values to perform the first search
*/
@get:VisibleForTesting
val initCompleted get() = flowOfInitCompleted.value
/**
* A search should be triggered if these properties change
*/
private val searchRequested =
flowOf(flowOfCardsOrNotes, flowOfDeckId)
.flattenMerge()
/**
* Emits an item when:
* * [initCompleted] is true
* * A property which defines the search has been changed ([searchRequested])
*
* @see launchSearchForCards
*/
private val performSearchFlow =
flowOfInitCompleted.combineTransform(searchRequested) { init, _ ->
if (!init) return@combineTransform
emit(Unit)
}
init {
Timber.d("CardBrowserViewModel::init")
var selectAllDecks = false
when (options) {
is CardBrowserLaunchOptions.SystemContextMenu -> {
searchTerms = options.search.toString()
}
is CardBrowserLaunchOptions.SearchQueryJs -> {
searchTerms = options.search
selectAllDecks = options.allDecks
}
is CardBrowserLaunchOptions.DeepLink -> {
searchTerms = options.search
}
null -> {}
}
performSearchFlow
.onEach {
launchSearchForCards()
}.launchIn(viewModelScope)
reverseDirectionFlow
.ignoreValuesFromViewModelLaunch()
.onEach { newValue -> withCol { newValue.updateConfig(config) } }
.launchIn(viewModelScope)
sortTypeFlow
.ignoreValuesFromViewModelLaunch()
.onEach { sortType -> withCol { sortType.save(config, sharedPrefs()) } }
.launchIn(viewModelScope)
flowOfCardsOrNotes
.onEach { cardsOrNotes ->
Timber.d("loading columns for %s mode", cardsOrNotes)
updateActiveColumns(BrowserColumnCollection.load(sharedPrefs(), cardsOrNotes))
}.launchIn(viewModelScope)
viewModelScope.launch {
val initialDeckId = if (selectAllDecks) ALL_DECKS_ID else getInitialDeck()
// PERF: slightly inefficient if the source was lastDeckId
setDeckId(initialDeckId)
refreshBackendColumns()
val cardsOrNotes = withCol { CardsOrNotes.fromCollection(this@withCol) }
flowOfCardsOrNotes.update { cardsOrNotes }
withCol {
sortTypeFlow.update { SortType.fromCol(config, cardsOrNotes, sharedPrefs()) }
reverseDirectionFlow.update { ReverseDirection.fromConfig(config) }
}
Timber.i("initCompleted")
if (!manualInit) {
flowOfInitCompleted.update { true }
launchSearchForCards()
}
}
}
/**
* Called if `onCreate` is called again, which may be due to the collection being reopened
*
* If this is the case, the backend has lost the active columns state, which is required for
* [transformBrowserRow]
*/
fun onReinit() {
// this can occur after process death, if so, the ViewModel starts normally
if (!initCompleted) return
Timber.d("onReinit: executing")
// we currently have no way to test whether setActiveBrowserColumns was called
// so set it again. This needs to be done immediately to ensure that the RecyclerView
// gets correct values when initialized
CollectionManager
.getBackend()
.setActiveBrowserColumns(flowOfActiveColumns.value.backendKeys)
// if the language has changed, the backend column labels may have changed
viewModelScope.launch {
refreshBackendColumns()
}
}
/** Handles an update to the list of backend columns */
private suspend fun refreshBackendColumns() {
flowOfAllColumns.emit(withCol { allBrowserColumns() }.associateBy { it.key })
}
/** Handles an update of the visible columns */
private suspend fun updateActiveColumns(columns: BrowserColumnCollection) {
Timber.d("updating active columns")
withCol { backend.setActiveBrowserColumns(columns.backendKeys) }
flowOfActiveColumns.update { columns }
}
@VisibleForTesting
fun manualInit() {
require(manualInit) { "'manualInit' should be true" }
flowOfInitCompleted.update { true }
Timber.d("manualInit")
}
/** Whether any rows are selected */
fun hasSelectedAnyRows(): Boolean = selectedRows.isNotEmpty()
/**
* All the notes of the selected cards will be marked
* If one or more card is unmarked, all will be marked,
* otherwise, they will be unmarked
*/
suspend fun toggleMark() {
val cardIds = queryAllSelectedCardIds()
if (cardIds.isEmpty()) {
Timber.i("Not marking cards - nothing selected")
return
}
undoableOp(this) {
val noteIds = notesOfCards(cardIds)
// if all notes are marked, remove the mark
// if no notes are marked, add the mark
// if there is a mix, enable the mark on all
val wantMark = !noteIds.all { getNote(it).hasTag(this@undoableOp, "marked") }
Timber.i("setting mark = %b for %d notes", wantMark, noteIds.size)
if (wantMark) {
tags.bulkAdd(noteIds, "marked")
} else {
tags.bulkRemove(noteIds, "marked")
}
}
}
/**
* Deletes the selected notes,
* @return the number of deleted notes
*/
suspend fun deleteSelectedNotes(): Int {
// PERF: use `undoableOp(this)` & notify CardBrowser of changes
// this does a double search
val cardIds = queryAllSelectedCardIds()
return undoableOp { removeNotes(cids = cardIds) }
.count
.also {
endMultiSelectMode()
refreshSearch()
}
}
fun setCardsOrNotes(newValue: CardsOrNotes) =
viewModelScope.launch {
Timber.i("setting mode to %s", newValue)
withCol {
// Change this to only change the preference on a state change
newValue.saveToCollection(this@withCol)
}
flowOfCardsOrNotes.update { newValue }
}
fun setTruncated(value: Boolean) {
viewModelScope.launch {
flowOfIsTruncated.emit(value)
}
sharedPrefs().edit {
putBoolean("isTruncated", value)
}
}
fun selectAll() {
if (_selectedRows.addAll(cards)) {
Timber.d("selecting all: %d item(s)", cards.size)
refreshSelectedRowsFlow()
}
}
fun selectNone() {
if (_selectedRows.isEmpty()) return
Timber.d("selecting none")
_selectedRows.clear()
refreshSelectedRowsFlow()
}
@VisibleForTesting
fun toggleRowSelectionAtPosition(position: Int) = toggleRowSelection(cards[position])
fun toggleRowSelection(id: CardOrNoteId) {
if (_selectedRows.contains(id)) {
_selectedRows.remove(id)
} else {
_selectedRows.add(id)
}
Timber.d("toggled selecting id '%s'; %d selected", id, selectedRowCount())
lastSelectedId = id
refreshSelectedRowsFlow()
}
@VisibleForTesting
fun selectRowAtPosition(pos: Int) {
if (_selectedRows.add(cards[pos])) {
refreshSelectedRowsFlow()
}
}
fun selectRowsBetween(
start: CardOrNoteId,
end: CardOrNoteId,
) {
val startPos = cards.indexOf(start)
val endPos = cards.indexOf(end)
selectRowsBetweenPositions(startPos, endPos)
}
/**
* @throws BackendException if the row is deleted
*/
fun transformBrowserRow(id: CardOrNoteId): Pair<BrowserRow, Boolean> {
val row = CollectionManager.getBackend().browserRowForId(id.cardOrNoteId)
val isSelected = selectedRows.contains(id)
return Pair(row, isSelected)
}
/**
* Selects the cards between [startPos] and [endPos]
*/
fun selectRowsBetweenPositions(
startPos: Int,
endPos: Int,
) {
val begin = min(startPos, endPos)
val end = max(startPos, endPos)
Timber.d("selecting indices between %d and %d", begin, end)
val cards = (begin..end).map { cards[it] }
if (_selectedRows.addAll(cards)) {
refreshSelectedRowsFlow()
}
}
/** emits a new value in [flowOfSelectedRows] */
private fun refreshSelectedRowsFlow() =
viewModelScope.launch {
refreshSelectedRowsFlow.emit(Unit)
Timber.d("refreshed selected rows")
}
fun selectedRowCount(): Int = selectedRows.size
suspend fun selectedNoteCount() = selectedRows.queryNoteIds(cardsOrNotes).distinct().size
fun hasSelectedAllDecks(): Boolean = lastDeckId == ALL_DECKS_ID
fun changeCardOrder(which: SortType) {
val changeType =
when {
which != order -> ChangeCardOrder.OrderChange(which)
// if the same element is selected again, reverse the order
which != SortType.NO_SORTING -> ChangeCardOrder.DirectionChange
else -> null
} ?: return
Timber.i("updating order: %s", changeType)
when (changeType) {
is ChangeCardOrder.OrderChange -> {
sortTypeFlow.update { which }
reverseDirectionFlow.update { ReverseDirection(orderAsc = false) }
launchSearchForCards()
}
ChangeCardOrder.DirectionChange -> {
reverseDirectionFlow.update { ReverseDirection(orderAsc = !orderAsc) }
cards.reverse()
viewModelScope.launch { flowOfSearchState.emit(SearchState.Completed) }
}
}
}
/**
* Updates the backend with a new collection of columns
*
* @param columns the new columns to use
* @param cardsOrNotes the mode to update columns for. If this is the active mode, then flows
* will be updated with the new columns
*
* @return Whether the operation was successful (a valid list was provided, and it was a change)
*/
@CheckResult
fun updateActiveColumns(
columns: List<CardBrowserColumn>,
cardsOrNotes: CardsOrNotes,
): Boolean {
if (columns.isEmpty()) {
Timber.d("updateColumns: no columns")
return false
}
if (activeColumns == columns) {
Timber.d("updateColumns: no changes")
return false
}
// update the backend with the new columns
val columnCollection =
BrowserColumnCollection.replace(sharedPrefs(), cardsOrNotes, columns).newColumns
// A user can edit the non-active columns if they:
// * Edit the cards/notes setting in the browser options
// * Edit the visible columns
// * Save the columns and discard the options changes
val isEditingCurrentHeadings = cardsOrNotes == this.cardsOrNotes
Timber.d("editing columns for current headings: %b", isEditingCurrentHeadings)
if (isEditingCurrentHeadings) {
viewModelScope.launch {
updateActiveColumns(columnCollection)
}
}
return true
}
/**
* Toggles the 'suspend' state of the selected cards
*
* If all cards are suspended, unsuspend all
* If no cards are suspended, suspend all
* If there is a mix, suspend all
*
* Changes are handled by [ChangeManager]
*/
fun toggleSuspendCards() =
viewModelScope.launch {
if (!hasSelectedAnyRows()) {
return@launch
}
Timber.d("toggling selected cards suspend status")
val cardIds = queryAllSelectedCardIds()
undoableOp<OpChanges> {
val wantUnsuspend = cardIds.all { getCard(it).queue == QueueType.Suspended }
if (wantUnsuspend) {
sched.unsuspendCards(cardIds)
} else {
sched.suspendCards(cardIds).changes
}
}
Timber.d("finished 'toggleSuspendCards'")
}
/**
* if all cards are buried, unbury all
* if no cards are buried, bury all
* if there is a mix, bury all
*
* if no cards are checked, do nothing
*
* @return Whether the operation was bury/unbury, and the number of affected cards.
* `null` if nothing happened
*/
suspend fun toggleBury(): BuryResult? {
if (!hasSelectedAnyRows()) {
Timber.w("no cards to bury")
return null
}
// https://github.com/ankitects/anki/blob/074becc0cee1e9ae59be701ad6c26787f74b4594/qt/aqt/browser/browser.py#L896-L902
fun Card.isBuried(): Boolean = queue == ManuallyBuried || queue == SiblingBuried
val cardIds = queryAllSelectedCardIds()
// this variable exists as `undoableOp` needs an OpChanges as return value
var wasBuried: Boolean? = null
undoableOp {
// this differs from Anki Desktop which uses the first selected card to determine the
// 'checked' status
val wantUnbury = cardIds.all { getCard(it).isBuried() }
wasBuried = !wantUnbury
if (wantUnbury) {
Timber.i("unburying %d cards", cardIds.size)
sched.unburyCards(cardIds)
} else {
Timber.i("burying %d cards", cardIds.size)
sched.buryCards(cardIds).changes
}
}
return BuryResult(wasBuried = wasBuried!!, count = cardIds.size)
}
fun querySelectionExportData(): Pair<ExportType, List<Long>>? {
if (!hasSelectedAnyRows()) return null
return when (this.cardsOrNotes) {
CARDS -> Pair(ExportType.Cards, selectedRows.map { it.cardOrNoteId })
NOTES -> Pair(ExportType.Notes, selectedRows.map { it.cardOrNoteId })
}
}
/**
* Obtains data to be displayed to the user then sent to [repositionSelectedRows]
*/
suspend fun prepareToRepositionCards(): RepositionCardsRequest {
val selectedCardIds = queryAllSelectedCardIds()
// Only new cards may be repositioned (If any non-new found show error dialog and return false)
if (selectedCardIds.any { withCol { getCard(it).queue != QueueType.New } }) {
return RepositionCardsRequest.ContainsNonNewCardsError
}
// query obtained from Anki Desktop
// https://github.com/ankitects/anki/blob/1fb1cbbf85c48a54c05cb4442b1b424a529cac60/qt/aqt/operations/scheduling.py#L117
try {
val (min, max) =
withCol {
db.query("select min(due), max(due) from cards where type=? and odid=0", CardType.New.code).use {
it.moveToNext()
return@withCol Pair(max(0, it.getInt(0)), it.getInt(1))
}
}
return RepositionData(
min = min,
max = max,
)
} catch (e: Exception) {
// TODO: Remove this once we've verified no production errors
Timber.w(e, "getting min/max position")
CrashReportService.sendExceptionReport(e, "prepareToRepositionCards")
return RepositionData(
min = null,
max = null,
)
}
}
/**
* @see [com.ichi2.libanki.sched.Scheduler.sortCards]
* @return the number of cards which were repositioned
*/
suspend fun repositionSelectedRows(position: Int): Int {
val ids = queryAllSelectedCardIds()
Timber.d("repositioning %d cards to %d", ids.size, position)
return undoableOp {
sched.sortCards(cids = ids, position, step = 1, shuffle = false, shift = true)
}.count
}
/** Returns the number of rows of the current result set */
val rowCount: Int
get() = cards.size
fun getRowAtPosition(position: Int) = cards[position]
private suspend fun updateSavedSearches(func: MutableMap<String, String>.() -> Unit): Map<String, String> {
val filters = savedSearches().toMutableMap()
func(filters)
withCol { config.set("savedFilters", filters) }
return filters
}
suspend fun savedSearches(): Map<String, String> = withCol { config.get("savedFilters") } ?: hashMapOf()
fun savedSearchesUnsafe(col: com.ichi2.libanki.Collection): Map<String, String> = col.config.get("savedFilters") ?: hashMapOf()
suspend fun removeSavedSearch(searchName: String): Map<String, String> {
Timber.d("removing user search")
return updateSavedSearches {
remove(searchName)
}
}
@CheckResult
suspend fun saveSearch(
searchName: String,
searchTerms: String,
): SaveSearchResult {
Timber.d("saving user search")
var alreadyExists = false
updateSavedSearches {
if (get(searchName) != null) {
alreadyExists = true
} else {
set(searchName, searchTerms)
}
}
return if (alreadyExists) SaveSearchResult.ALREADY_EXISTS else SaveSearchResult.SUCCESS
}
/** Ignores any values before [initCompleted] is set */
private fun <T> Flow<T>.ignoreValuesFromViewModelLaunch(): Flow<T> = this.filter { initCompleted }
private suspend fun setFilterQuery(filterQuery: String) {
this.flowOfFilterQuery.emit(filterQuery)
launchSearchForCards(filterQuery)
}
/**
* Searches for all marked notes and replaces the current search results with these marked notes.
*/
suspend fun searchForMarkedNotes() {
// only intended to be used if the user has no selection
if (hasSelectedAnyRows()) return
setFilterQuery("tag:marked")
}
/**
* Searches for all suspended cards and replaces the current search results with these suspended cards.
*/
suspend fun searchForSuspendedCards() {
// only intended to be used if the user has no selection
if (hasSelectedAnyRows()) return
setFilterQuery("is:suspended")
}
suspend fun setFlagFilter(flag: Flag) {
Timber.i("filtering to flag: %s", flag)
val flagSearchTerm = "flag:${flag.code}"
val searchTerms =
when {
searchTerms.contains("flag:") -> searchTerms.replaceFirst("flag:.".toRegex(), flagSearchTerm)
searchTerms.isNotEmpty() -> "$flagSearchTerm $searchTerms"
else -> flagSearchTerm
}
setFilterQuery(searchTerms)
}
suspend fun filterByTags(
selectedTags: List<String>,
cardState: CardStateFilter,
) {
val sb = StringBuilder(cardState.toSearch)
// join selectedTags as "tag:$tag" with " or " between them
val tagsConcat = selectedTags.joinToString(" or ") { tag -> "\"tag:$tag\"" }
if (selectedTags.isNotEmpty()) {
sb.append("($tagsConcat)") // Only if we added anything to the tag list
}
setFilterQuery(sb.toString())
}
/** Previewing */
suspend fun queryPreviewIntentData(): PreviewerDestination {
// If in NOTES mode, we show one Card per Note, as this matches Anki Desktop
return if (selectedRowCount() > 1) {
PreviewerDestination(currentIndex = 0, PreviewerIdsFile(cacheDir, queryAllSelectedCardIds()))
} else {
// Preview all cards, starting from the one that is currently selected
val startIndex = indexOfFirstCheckedCard() ?: 0
PreviewerDestination(startIndex, PreviewerIdsFile(cacheDir, queryOneCardIdPerNote()))
}
}
private suspend fun queryOneCardIdPerNote(): List<CardId> = cards.queryOneCardIdPerRow()
/** @return the index of the first checked card in [cards], or `null` if no cards are checked */
private fun indexOfFirstCheckedCard(): Int? {
val idToFind = selectedRows.firstOrNull() ?: return null
return cards.indexOf(idToFind)
}
fun setSearchQueryExpanded(expand: Boolean) {
if (expand) {
expandSearchQuery()
} else {
collapseSearchQuery()
}
}
private fun collapseSearchQuery() {
searchQueryInputFlow.update { null }
flowOfSearchQueryExpanded.update { false }
}
private fun expandSearchQuery() {
flowOfSearchQueryExpanded.update { true }
}
fun updateQueryText(newText: String) {
searchQueryInputFlow.update { newText }
}
fun removeUnsubmittedInput() {
searchQueryInputFlow.update { null }
}
fun moveSelectedCardsToDeck(deckId: DeckId): Deferred<OpChangesWithCount> =
viewModelScope.async {
val selectedCardIds = queryAllSelectedCardIds()
return@async undoableOp {
setDeck(selectedCardIds, deckId)
}
}
suspend fun updateSelectedCardsFlag(flag: Flag): List<CardId> {
val idsToChange = queryAllSelectedCardIds()
withCol { setUserFlag(flag, cids = idsToChange) }
return idsToChange
}
/**
* Turn off [Multi-Select Mode][isInMultiSelectMode] and return to normal state
*/
fun endMultiSelectMode() = selectNone()
/**
* @param forceRefresh if `true`, perform a search even if the search query is unchanged
*/
fun launchSearchForCards(
searchQuery: String,
forceRefresh: Boolean = true,
) {
if (!forceRefresh && searchTerms == searchQuery) {
Timber.d("skipping duplicate search: forceRefresh is false")
return
}
searchTerms = searchQuery
launchSearchForCards()
}
/**
* @see com.ichi2.anki.searchForRows
*/
@NeedsTest("Invalid searches are handled. For instance: 'and'")
fun launchSearchForCards() {
if (!initCompleted) return
viewModelScope.launch {
// update the UI while we're searching
clearCardsList()
val query: String =
if (searchTerms.contains("deck:")) {
"($searchTerms)"
} else {
if ("" != searchTerms) "$restrictOnDeck($searchTerms)" else restrictOnDeck
}
searchJob?.cancel()
searchJob =
launchCatchingIO(
errorMessageHandler = { error -> flowOfSearchState.emit(SearchState.Error(error)) },
) {
flowOfSearchState.emit(SearchState.Searching)
Timber.d("performing search: '%s'", query)
val cards = com.ichi2.anki.searchForRows(query, order.toSortOrder(), cardsOrNotes)
Timber.d("Search returned %d card(s)", cards.size)
ensureActive()
this@CardBrowserViewModel.cards.replaceWith(cardsOrNotes, cards)
flowOfSearchState.emit(SearchState.Completed)
}
}
}
private fun refreshSearch() = launchSearchForCards()
private suspend fun clearCardsList() {
cards.reset()
flowOfCardsUpdated.emit(Unit)
}
suspend fun queryCardIdAtPosition(index: Int): CardId = cards.queryCardIdsAt(index).first()
suspend fun querySelectedCardIdAtPosition(index: Int): CardId = selectedRows.toList()[index].toCardId(cardsOrNotes)
/**
* Obtains two lists of column headings with preview data
* (preview uses the first row of data, if it exists)
*
* The two lists are:
* (1): An ordered list of columns which is displayed to the user
* (2): A list of columns which are available to display to the user
*/
suspend fun previewColumnHeadings(cardsOrNotes: CardsOrNotes): Pair<List<ColumnWithSample>, List<ColumnWithSample>> {
val currentColumns =
when {
// if we match, use the loaded the columns
cardsOrNotes == this.cardsOrNotes -> activeColumns
else -> BrowserColumnCollection.load(sharedPrefs(), cardsOrNotes).columns
}
val columnsWithSample = ColumnWithSample.loadSample(cards.firstOrNull(), cardsOrNotes)
// we return this as two lists as 'currentColumns' uses the collection ordering
return Pair(
columnsWithSample
.filter { currentColumns.contains(it.columnType) }
.sortedBy { currentColumns.indexOf(it.columnType) },
columnsWithSample.filter { !currentColumns.contains(it.columnType) },
)
}
companion object {
fun factory(
lastDeckIdRepository: LastDeckIdRepository,
cacheDir: File,
preferencesProvider: SharedPreferencesProvider? = null,
options: CardBrowserLaunchOptions?,
) = viewModelFactory {
initializer {
CardBrowserViewModel(
lastDeckIdRepository,
cacheDir,
options,
preferencesProvider ?: AnkiDroidApp.sharedPreferencesProvider,
)
}
}
}
/**
* @param wasBuried `true` if all cards were buried, `false` if unburied
* @param count the number of affected cards
*/
data class BuryResult(
val wasBuried: Boolean,
val count: Int,
)