Skip to content

Commit 19e92c1

Browse files
mikehardytimrae
authored andcommitted
Reload Note.model after template editing, Fixes ankidroid#5011
The template editor edits the model template live in the database which bleeds into Note.model, if you discard the template edits and return to the NoteEditor the Note.model.templates state was still the edited + reverted state. This hook and call allow for the Note.model object to be loaded from the database where the template editor left the correct state as it was closing down
1 parent 7065293 commit 19e92c1

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

AnkiDroid/src/main/java/com/ichi2/anki/NoteEditor.java

+10-2
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ protected void onCollectionLoaded(Collection col) {
332332
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
333333

334334
Intent intent = getIntent();
335-
Timber.d("onCollectionLoaded: caller: %d", mCaller);
335+
Timber.d("NoteEditor() onCollectionLoaded: caller: %d", mCaller);
336336

337337
registerExternalStorageListener();
338338

@@ -532,7 +532,7 @@ public void onClick(View v) {
532532
});
533533

534534
if (!mAddNote && mCurrentEditedCard != null) {
535-
Timber.i("NoteEditor:: Edit note activity successfully started with card id %d", mCurrentEditedCard.getId());
535+
Timber.i("onCollectionLoaded() Edit note activity successfully started with card id %d", mCurrentEditedCard.getId());
536536
}
537537

538538
//set focus to FieldEditText 'first' on startup like Anki desktop
@@ -1067,19 +1067,22 @@ private void showCardTemplateEditor() {
10671067
// Pass the model ID
10681068
try {
10691069
intent.putExtra("modelId", getCurrentlySelectedModel().getLong("id"));
1070+
Timber.d("showCardTemplateEditor() for model %s", intent.getLongExtra("modelId", -1L));
10701071
} catch (JSONException e) {
10711072
throw new RuntimeException(e);
10721073
}
10731074
// Also pass the card ID if not adding new note
10741075
if (!mAddNote) {
10751076
intent.putExtra("noteId", mCurrentEditedCard.note().getId());
1077+
Timber.d("showCardTemplateEditor() with note %s", mCurrentEditedCard.note().getId());
10761078
}
10771079
startActivityForResultWithAnimation(intent, REQUEST_TEMPLATE_EDIT, ActivityTransitionAnimation.LEFT);
10781080
}
10791081

10801082

10811083
@Override
10821084
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
1085+
Timber.d("onActivityResult() with request/result: %s/%s", requestCode, resultCode);
10831086
super.onActivityResult(requestCode, resultCode, data);
10841087

10851088
if (resultCode == DeckPicker.RESULT_DB_ERROR) {
@@ -1126,6 +1129,9 @@ else if (fieldEditText.hasFocus()) {
11261129
if (resultCode == RESULT_OK) {
11271130
mReloadRequired = true;
11281131
}
1132+
// rebuild the model post-template-edit so we get the correct number of cards
1133+
Timber.d("onActivityResult() template edit - reloading model");
1134+
mEditorNote.reloadModel();
11291135
updateCards(mEditorNote.model());
11301136
break;
11311137
}
@@ -1480,10 +1486,12 @@ private void updateTags() {
14801486

14811487
/** Update the list of card templates for current note type */
14821488
private void updateCards(JSONObject model) {
1489+
Timber.d("updateCards()");
14831490
try {
14841491
JSONArray tmpls = model.getJSONArray("tmpls");
14851492
String cardsList = "";
14861493
// Build comma separated list of card names
1494+
Timber.d("updateCards() template count is %s", tmpls.length());
14871495
for (int i = 0; i < tmpls.length(); i++) {
14881496
String name = tmpls.getJSONObject(i).optString("name");
14891497
// If more than one card then make currently selected card underlined

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

+7
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
import java.util.Locale;
3232
import java.util.Map;
3333

34+
import timber.log.Timber;
35+
3436

3537
public class Note implements Cloneable {
3638

@@ -88,6 +90,7 @@ public Note(Collection col, JSONObject model, Long id) {
8890

8991

9092
public void load() {
93+
Timber.d("load()");
9194
Cursor cursor = null;
9295
try {
9396
cursor = mCol.getDb().getDatabase()
@@ -113,6 +116,10 @@ public void load() {
113116
}
114117
}
115118

119+
public void reloadModel() {
120+
mModel = mCol.getModels().get(mMid);
121+
}
122+
116123

117124
/*
118125
* If fields or tags have changed, write changes to disk.

0 commit comments

Comments
 (0)