-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Copy pathCollectionOperations.kt
108 lines (103 loc) · 4.02 KB
/
CollectionOperations.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
/****************************************************************************************
* Copyright (c) 2022 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.async
import com.ichi2.libanki.Card
import com.ichi2.libanki.Collection
import com.ichi2.libanki.Note
import net.ankiweb.rsdroid.BackendFactory
import timber.log.Timber
/**
* This file contains functions that have been migrated from [CollectionTask]
* Remove this comment when migration has been completed
* TODO: All functions associated to Collection can be converted to extension function to avoid redundant parameter [col] in each.
*/
/**
* Saves the newly updated card [editCard] to disk
* @return updated card
*/
fun updateCard(
col: Collection,
editCard: Card,
isFromReviewer: Boolean,
canAccessScheduler: Boolean,
): Card {
Timber.d("doInBackgroundUpdateNote")
// Save the note
val editNote = editCard.note()
if (BackendFactory.defaultLegacySchema) {
col.db.executeInTransaction {
// TODO: undo integration
editNote.flush()
// flush card too, in case, did has been changed
editCard.flush()
}
} else {
// TODO: the proper way to do this would be to call this in undoableOp() in a coroutine
col.newBackend.updateNote(editNote)
// no need to flush card in new path
}
return if (isFromReviewer) {
if (col.decks.active().contains(editCard.did) || !canAccessScheduler) {
editCard.apply {
load()
q(true) // reload qa-cache
}
} else {
col.sched.card!! // check: are there deleted too?
}
} else {
editCard
}
}
// TODO: Move the operation where it is actually used, no need for a separate function since it is fairly simple
/**
* Takes a list of edited notes and saves the change permanently to disk
* @param col Collection
* @param notesToUpdate a list of edited notes that is to be saved
* @return list of updated (in disk) notes
*/
fun updateMultipleNotes(
col: Collection,
notesToUpdate: List<Note>,
): List<Note> {
Timber.d("CollectionOperations: updateMultipleNotes")
return col.db.executeInTransaction {
for (note in notesToUpdate) {
note.flush()
}
notesToUpdate
}
}
/**
* Takes a list of media file names and removes them from the Collection
* @param col Collection from which media is to be deleted
* @param unused List of media names to be deleted
*/
fun deleteMedia(
col: Collection,
unused: List<String>
): Int {
val m = col.media
if (!BackendFactory.defaultLegacySchema) {
// FIXME: this provides progress info that is not currently used
col.newMedia.removeFiles(unused)
} else {
for (fname in unused) {
m.removeFile(fname)
}
}
return unused.size
}