Skip to content

Commit 36052ba

Browse files
committed
refactor: add CardTemplateEditor.getIntent
1 parent fee0a2d commit 36052ba

File tree

3 files changed

+30
-20
lines changed

3 files changed

+30
-20
lines changed

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ open class CardTemplateEditor :
194194
// get id for currently edited note (optional)
195195
noteId = intent.getLongExtra(EDITOR_NOTE_ID, -1L)
196196
// get id for currently edited template (optional)
197-
startingOrdId = intent.getIntExtra("ordId", -1)
197+
startingOrdId = intent.getIntExtra(EDITOR_START_ORD_ID, -1)
198198
tabToCursorPositions[0] = hashMapOf()
199199
tabToViewId[0] = R.id.front_edit
200200
} else {
@@ -1532,5 +1532,25 @@ open class CardTemplateEditor :
15321532

15331533
@Suppress("unused")
15341534
private const val REQUEST_CARD_BROWSER_APPEARANCE = 1
1535+
1536+
@CheckResult
1537+
fun getIntent(
1538+
context: Context,
1539+
noteTypeId: NoteTypeId,
1540+
noteId: NoteId? = null,
1541+
ord: CardOrdinal? = null,
1542+
) = Intent(context, CardTemplateEditor::class.java)
1543+
.apply {
1544+
putExtra(EDITOR_NOTE_TYPE_ID, noteTypeId)
1545+
noteId?.let { putExtra(EDITOR_NOTE_ID, it) }
1546+
ord?.let { putExtra(EDITOR_START_ORD_ID, it) }
1547+
1548+
Timber.d(
1549+
"Built intent for CardTemplateEditor; ntid: %s; nid: %s; ord: %s",
1550+
noteTypeId,
1551+
noteId,
1552+
ord,
1553+
)
1554+
}
15351555
}
15361556
}

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

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,6 @@ import com.ichi2.anki.libanki.Note.ClozeUtils
112112
import com.ichi2.anki.libanki.NoteTypeId
113113
import com.ichi2.anki.libanki.NotetypeJson
114114
import com.ichi2.anki.libanki.Notetypes
115-
import com.ichi2.anki.libanki.Notetypes.Companion.NOT_FOUND_NOTE_TYPE
116115
import com.ichi2.anki.libanki.Utils
117116
import com.ichi2.anki.libanki.clozeNumbersInNote
118117
import com.ichi2.anki.model.CardStateFilter
@@ -1801,20 +1800,14 @@ class NoteEditorFragment :
18011800
}
18021801

18031802
private fun showCardTemplateEditor() {
1804-
val intent = Intent(requireContext(), CardTemplateEditor::class.java)
1805-
// Pass the note type ID
1806-
intent.putExtra("noteTypeId", currentlySelectedNotetype!!.id)
1807-
Timber.d(
1808-
"showCardTemplateEditor() for model %s",
1809-
intent.getLongExtra("noteTypeId", NOT_FOUND_NOTE_TYPE),
1810-
)
1811-
// Also pass the note id and ord if not adding new note
1812-
if (!addNote && currentEditedCard != null) {
1813-
intent.putExtra("noteId", currentEditedCard!!.nid)
1814-
Timber.d("showCardTemplateEditor() with note %s", currentEditedCard!!.nid)
1815-
intent.putExtra("ordId", currentEditedCard!!.ord)
1816-
Timber.d("showCardTemplateEditor() with ord %s", currentEditedCard!!.ord)
1817-
}
1803+
val intent =
1804+
CardTemplateEditor.getIntent(
1805+
requireContext(),
1806+
noteTypeId = currentlySelectedNotetype!!.id,
1807+
// Also pass the note id and ord if not adding new note
1808+
noteId = if (addNote) null else currentEditedCard?.nid,
1809+
ord = if (addNote) null else currentEditedCard?.ord,
1810+
)
18181811
requestTemplateEditLauncher.launch(intent)
18191812
}
18201813

AnkiDroid/src/main/java/com/ichi2/anki/notetype/ManageNoteTypesState.kt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,7 @@ data class ManageNoteTypesState(
7070
data class CardEditor(
7171
val ntid: NoteTypeId,
7272
) : Destination {
73-
override fun toIntent(context: Context): Intent =
74-
Intent(context, CardTemplateEditor::class.java).apply {
75-
putExtra(CardTemplateEditor.EDITOR_NOTE_TYPE_ID, ntid)
76-
}
73+
override fun toIntent(context: Context) = CardTemplateEditor.getIntent(context, noteTypeId = ntid)
7774
}
7875

7976
data class FieldsEditor(

0 commit comments

Comments
 (0)