Skip to content

Commit e25af62

Browse files
david-allisonlukstbit
authored andcommitted
improvement(note): libAnki compat
* Add LibAnkiAlias annotations * Reorder methods * Remove deleted methods upstream
1 parent 27f082f commit e25af62

File tree

1 file changed

+37
-27
lines changed
  • AnkiDroid/src/main/java/com/ichi2/libanki

1 file changed

+37
-27
lines changed

AnkiDroid/src/main/java/com/ichi2/libanki/Note.kt

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ import androidx.annotation.VisibleForTesting
2222
import anki.notes.NoteFieldsCheckResponse
2323
import com.ichi2.libanki.Consts.DEFAULT_DECK_ID
2424
import com.ichi2.libanki.backend.model.toBackendNote
25+
import com.ichi2.libanki.utils.LibAnkiAlias
2526
import com.ichi2.libanki.utils.NotInLibAnki
2627
import com.ichi2.utils.KotlinCleanup
2728
import com.ichi2.utils.emptyStringArray
28-
import java.util.AbstractSet
2929
import java.util.regex.Pattern
3030

3131
@KotlinCleanup("lots to do")
@@ -77,6 +77,7 @@ class Note : Cloneable {
7777
loadFromBackendNote(col, note)
7878
}
7979

80+
@LibAnkiAlias("_load_from_backend_note")
8081
private fun loadFromBackendNote(
8182
col: Collection,
8283
note: anki.notes.Note,
@@ -100,6 +101,7 @@ class Note : Cloneable {
100101

101102
fun cards(col: Collection): List<Card> = cardIds(col).map { col.getCard(it) }
102103

104+
@LibAnkiAlias("ephemeral_card")
103105
fun ephemeralCard(
104106
col: Collection,
105107
ord: Int = 0,
@@ -139,6 +141,7 @@ class Note : Cloneable {
139141

140142
/** The first card, assuming it exists. */
141143
@CheckResult
144+
@NotInLibAnki
142145
fun firstCard(col: Collection): Card =
143146
col.getCard(
144147
col.db.queryLongScalar(
@@ -171,6 +174,7 @@ class Note : Cloneable {
171174
return result
172175
}
173176

177+
@LibAnkiAlias("_field_index")
174178
private fun fieldIndex(key: String): Int {
175179
val fieldPair =
176180
fMap!![key]
@@ -183,35 +187,48 @@ class Note : Cloneable {
183187
return fieldPair.first
184188
}
185189

190+
@LibAnkiAlias("__getitem__")
186191
fun getItem(key: String): String = fields[fieldIndex(key)]
187192

193+
@LibAnkiAlias("__setitem__")
188194
fun setItem(
189195
key: String,
190196
value: String,
191197
) {
192198
fields[fieldIndex(key)] = value
193199
}
194200

201+
@LibAnkiAlias("__contains__")
195202
operator fun contains(key: String): Boolean = fMap!!.containsKey(key)
196203

204+
@NotInLibAnki
205+
fun setField(
206+
index: Int,
207+
value: String,
208+
) {
209+
fields[index] = value
210+
}
211+
197212
/**
198213
* Tags
199214
* ***********************************************************
200215
*/
216+
217+
@LibAnkiAlias("has_tag")
201218
fun hasTag(
202219
col: Collection,
203220
tag: String,
204221
): Boolean = col.tags.inList(tag, tags)
205222

206-
fun stringTags(col: Collection): String = col.tags.join(col.tags.canonify(tags))
207-
208-
fun setTagsFromStr(
209-
col: Collection,
210-
str: String,
211-
) {
212-
tags = col.tags.split(str)
223+
/**
224+
* Add tag. Duplicates will be stripped on save.
225+
*/
226+
@LibAnkiAlias("add_tag")
227+
fun addTag(tag: String) {
228+
tags.add(tag)
213229
}
214230

231+
@LibAnkiAlias("remove_tag")
215232
fun removeTag(tag: String) {
216233
val rem: MutableList<String> =
217234
ArrayList(
@@ -227,31 +244,24 @@ class Note : Cloneable {
227244
}
228245
}
229246

230-
/*
231-
* duplicates will be stripped on save
232-
*/
233-
fun addTag(tag: String) {
234-
tags.add(tag)
235-
}
247+
@LibAnkiAlias("string_tags")
248+
fun stringTags(col: Collection): String = col.tags.join(col.tags.canonify(tags))
236249

237-
fun addTags(tags: AbstractSet<String>) {
238-
tags.addAll(tags)
250+
@LibAnkiAlias("set_tags_from_str")
251+
fun setTagsFromStr(
252+
col: Collection,
253+
str: String,
254+
) {
255+
tags = col.tags.split(str)
239256
}
240257

241258
/**
242259
* Unique/duplicate check
243260
* ***********************************************************
244261
*/
245-
fun fieldsCheck(col: Collection): NoteFieldsCheckResponse.State = col.backend.noteFieldsCheck(toBackendNote()).state
246-
247-
fun sFld(col: Collection): String = col.db.queryString("SELECT sfld FROM notes WHERE id = ?", id)
248262

249-
fun setField(
250-
index: Int,
251-
value: String,
252-
) {
253-
fields[index] = value
254-
}
263+
@LibAnkiAlias("fields_check")
264+
fun fieldsCheck(col: Collection): NoteFieldsCheckResponse.State = col.backend.noteFieldsCheck(toBackendNote()).state
255265

256266
public override fun clone(): Note =
257267
try {
@@ -270,7 +280,7 @@ class Note : Cloneable {
270280
override fun hashCode(): Int = (id xor (id ushr 32)).toInt()
271281

272282
object ClozeUtils {
273-
private val mClozeRegexPattern = Pattern.compile("\\{\\{c(\\d+)::")
283+
private val clozeRegexPattern = Pattern.compile("\\{\\{c(\\d+)::")
274284

275285
/**
276286
* Calculate the next number that should be used if inserting a new cloze deletion.
@@ -286,7 +296,7 @@ class Note : Cloneable {
286296
// Begin looping through the fields
287297
for (fieldLiteral in fieldValues) {
288298
// Begin searching in the current field for cloze references
289-
val matcher = mClozeRegexPattern.matcher(fieldLiteral)
299+
val matcher = clozeRegexPattern.matcher(fieldLiteral)
290300
while (matcher.find()) {
291301
val detectedClozeId = matcher.group(1)!!.toInt()
292302
if (detectedClozeId > highestClozeId) {

0 commit comments

Comments
 (0)