0
0
mirror of https://github.com/ankidroid/Anki-Android.git synced 2024-09-19 19:42:17 +02:00

refactor: use context receiver in Note.fromNotetypeId

This commit is contained in:
Brayan Oliveira 2024-02-25 14:18:09 -03:00 committed by lukstbit
parent 98adf1f9c0
commit 1c01e4cb6f
8 changed files with 15 additions and 14 deletions

View File

@ -1297,7 +1297,7 @@ class ContentProviderTest : InstrumentedTest() {
fields: Array<String>,
tag: String
): Uri {
val newNote = Note.fromNotetypeId(col, mid)
val newNote = col.run { Note.fromNotetypeId(mid) }
for (idx in fields.indices) {
newNote.setField(idx, fields[idx])
}

View File

@ -637,7 +637,7 @@ open class CardTemplateEditor : AnkiActivity(), DeckSelectionListener {
val notetype = templateEditor.tempModel!!.notetype
val notetypeFile = NotetypeFile(requireContext(), notetype)
val ord = templateEditor.viewPager.currentItem
val note = withCol { getNote(this) ?: Note.fromNotetypeId(this, notetype.id) }
val note = withCol { getNote(this) ?: Note.fromNotetypeId(notetype.id) }
val args = TemplatePreviewerArguments(
notetypeFile = notetypeFile,
id = note.id,

View File

@ -1834,9 +1834,9 @@ class NoteEditor : AnkiActivity(), DeckSelectionListener, SubtitleListener, Tags
/** Handles setting the current note (non-null afterwards) and rebuilding the UI based on this note */
private fun setNote(note: Note?, changeType: FieldChangeType) {
editorNote = if (note == null || addNote) {
getColUnsafe.let { col ->
val notetype = col.notetypes.current()
Note.fromNotetypeId(col, notetype.id)
getColUnsafe.run {
val notetype = notetypes.current()
Note.fromNotetypeId(notetype.id)
}
} else {
note

View File

@ -54,7 +54,7 @@ class TemplatePreviewerViewModel(arguments: TemplatePreviewerArguments) : CardVi
if (arguments.id != 0L) {
Note(this, arguments.id)
} else {
Note.fromNotetypeId(this, arguments.notetype.id)
Note.fromNotetypeId(arguments.notetype.id)
}
}.apply {
fields = arguments.fields

View File

@ -710,7 +710,7 @@ class CardContentProvider : ContentProvider() {
}
val fldsArray = Utils.splitFields(flds)
// Create empty note
val newNote = Note.fromNotetypeId(col, thisModelId)
val newNote = col.run { Note.fromNotetypeId(thisModelId) }
// Set fields
// Check that correct number of flds specified
if (fldsArray.size != newNote.fields.size) {
@ -754,7 +754,7 @@ class CardContentProvider : ContentProvider() {
val tags = values.getAsString(FlashCardsContract.Note.TAGS)
// val allowEmpty = AllowEmpty.fromBoolean(values.getAsBoolean(FlashCardsContract.Note.ALLOW_EMPTY))
// Create empty note
val newNote = Note.fromNotetypeId(col, modelId) // ué
val newNote = col.run { Note.fromNotetypeId(modelId) }
// Set fields
val fldsArray = Utils.splitFields(flds)
// Check that correct number of flds specified

View File

@ -302,7 +302,7 @@ open class Collection(
* @return The new note
*/
fun newNote(m: NotetypeJson): Note {
return Note.fromNotetypeId(this, m.id)
return Note.fromNotetypeId(m.id)
}
/**

View File

@ -65,9 +65,10 @@ class Note : Cloneable {
}
companion object {
fun fromNotetypeId(col: Collection, ntid: NoteTypeId): Note {
val backendNote = col.backend.newNote(ntid)
return Note(col, backendNote)
context (Collection)
fun fromNotetypeId(ntid: NoteTypeId): Note {
val backendNote = backend.newNote(ntid)
return Note(this@Collection, backendNote)
}
}

View File

@ -58,7 +58,7 @@ class NoteServiceTest : RobolectricTest() {
multiMediaNote!!.getField(0)!!.text = "foo"
multiMediaNote.getField(1)!!.text = "bar"
val basicNote = Note.fromNotetypeId(col, testModel.id).apply {
val basicNote = col.run { Note.fromNotetypeId(testModel.id) }.apply {
setField(0, "this should be changed to foo")
setField(1, "this should be changed to bar")
}
@ -79,7 +79,7 @@ class NoteServiceTest : RobolectricTest() {
testNotetype = col.notetypes.newBasicNotetype()
testNotetype.id = 45
col.notetypes.add(testNotetype)
val noteWithID45 = Note.fromNotetypeId(col, testNotetype.id)
val noteWithID45 = col.run { Note.fromNotetypeId(testNotetype.id) }
val expectedException: Throwable = assertThrows(RuntimeException::class.java) { NoteService.updateJsonNoteFromMultimediaNote(multiMediaNoteWithID42, noteWithID45) }
assertEquals(expectedException.message, "Source and Destination Note ID do not match.")
}