use of com.ichi2.async.CollectionTask.TASK_TYPE.ADD_NOTE in project AnkiChinaAndroid by ankichinateam.
the class NoteEditor method saveNote.
@VisibleForTesting
void saveNote() {
final Resources res = getResources();
if (mSelectedTags == null) {
mSelectedTags = new ArrayList<>(0);
}
// treat add new note and edit existing note independently
if (mAddNote) {
// DEFECT: This does not block addition if cloze transpositions are in non-cloze fields.
if (isClozeType() && !hasClozeDeletions()) {
displayErrorSavingNote();
return;
}
// load all of the fields into the note
for (FieldEditText f : mEditFields) {
updateField(f);
}
// Save deck to model
mEditorNote.model().put("did", mCurrentDid);
// Save tags to model
mEditorNote.setTagsFromStr(tagsAsString(mSelectedTags));
JSONArray tags = new JSONArray();
for (String t : mSelectedTags) {
tags.put(t);
}
getCol().getModels().current().put("tags", tags);
getCol().getModels().setChanged();
mReloadRequired = true;
CollectionTask.launchCollectionTask(ADD_NOTE, saveNoteHandler(), new TaskData(mEditorNote));
} else {
// Check whether note type has been changed
final Model newModel = getCurrentlySelectedModel();
final Model oldModel = (mCurrentEditedCard == null) ? null : mCurrentEditedCard.model();
File target = new File(FileUtil.createTmpDir(this), mCurrentEditedCard.getId() + ".wav");
if (target.exists()) {
Timber.i("editing card audio is exists,delete it");
target.delete();
}
if (!newModel.equals(oldModel)) {
mReloadRequired = true;
if (mModelChangeCardMap.size() < mEditorNote.numberOfCards() || mModelChangeCardMap.containsValue(null)) {
// If cards will be lost via the new mapping then show a confirmation dialog before proceeding with the change
ConfirmationDialog dialog = new ConfirmationDialog();
dialog.setArgs(res.getString(R.string.confirm_map_cards_to_nothing));
Runnable confirm = () -> {
// Bypass the check once the user confirms
changeNoteTypeWithErrorHandling(oldModel, newModel);
};
dialog.setConfirm(confirm);
showDialogFragment(dialog);
} else {
// Otherwise go straight to changing note type
changeNoteTypeWithErrorHandling(oldModel, newModel);
}
return;
}
// Regular changes in note content
boolean modified = false;
// changed did? this has to be done first as remFromDyn() involves a direct write to the database
if (mCurrentEditedCard != null && mCurrentEditedCard.getDid() != mCurrentDid) {
mReloadRequired = true;
// remove card from filtered deck first (if relevant)
getCol().getSched().remFromDyn(new long[] { mCurrentEditedCard.getId() });
// refresh the card object to reflect the database changes in remFromDyn()
mCurrentEditedCard.load();
// also reload the note object
mEditorNote = mCurrentEditedCard.note();
// then set the card ID to the new deck
mCurrentEditedCard.setDid(mCurrentDid);
modified = true;
}
// now load any changes to the fields from the form
for (FieldEditText f : mEditFields) {
modified = modified | updateField(f);
}
// added tag?
for (String t : mSelectedTags) {
modified = modified || !mEditorNote.hasTag(t);
}
// removed tag?
modified = modified || mEditorNote.getTags().size() > mSelectedTags.size();
if (modified) {
mEditorNote.setTagsFromStr(tagsAsString(mSelectedTags));
mChanged = true;
}
closeNoteEditor();
}
}
use of com.ichi2.async.CollectionTask.TASK_TYPE.ADD_NOTE in project AnkiChinaAndroid by ankichinateam.
the class CardBrowser method onActivityResult.
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// FIXME:
Timber.d("onActivityResult(requestCode=%d, resultCode=%d)", requestCode, resultCode);
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == DeckPicker.RESULT_DB_ERROR) {
closeCardBrowser(DeckPicker.RESULT_DB_ERROR);
}
if (requestCode == EDIT_CARD && resultCode != RESULT_CANCELED) {
Timber.i("CardBrowser:: CardBrowser: Saving card...");
CollectionTask.launchCollectionTask(UPDATE_NOTE, updateCardHandler(), new TaskData(sCardBrowserCard, false));
} else if (requestCode == ADD_NOTE && resultCode == RESULT_OK) {
if (mSearchView != null) {
mSearchTerms = mSearchView.getQuery().toString();
searchCards();
} else {
Timber.w("Note was added from browser and on return mSearchView == null");
}
}
// Previewing can now perform an "edit", so it can pass on a reloadRequired
if (requestCode == PREVIEW_CARDS && data != null && (data.getBooleanExtra("reloadRequired", false) || data.getBooleanExtra("noteChanged", false))) {
searchCards();
if (getReviewerCardId() == mCurrentCardId) {
mReloadRequired = true;
}
}
if (requestCode == EDIT_CARD && data != null && (data.getBooleanExtra("reloadRequired", false) || data.getBooleanExtra("noteChanged", false))) {
// if reloadRequired or noteChanged flag was sent from note editor then reload card list
searchCards();
// in use by reviewer?
if (getReviewerCardId() == mCurrentCardId) {
mReloadRequired = true;
}
}
// maybe the availability of undo changed
invalidateOptionsMenu();
}
use of com.ichi2.async.CollectionTask.TASK_TYPE.ADD_NOTE in project AnkiChinaAndroid by ankichinateam.
the class SelfStudyActivity method onActivityResult.
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// FIXME:
Timber.d("onActivityResult(requestCode=%d, resultCode=%d)", requestCode, resultCode);
if (data != null) {
Timber.d("onActivityResult data (reloadRequired=%s, noteChanged=%s)", data.getBooleanExtra("reloadRequired", false), data.getBooleanExtra("noteChanged", false));
}
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == DeckPicker.RESULT_DB_ERROR) {
closeCardBrowser(DeckPicker.RESULT_DB_ERROR);
}
if (requestCode == EDIT_CARD && resultCode != RESULT_CANCELED) {
Timber.i("CardBrowser:: CardBrowser: Saving card...");
CollectionTask.launchCollectionTask(UPDATE_NOTE, updateCardHandler(), new TaskData(sCardBrowserCard, false));
} else if (requestCode == ADD_NOTE && resultCode == RESULT_OK) {
if (mSearchView != null) {
mSearchTerms = mSearchView.getQuery().toString();
searchCards();
} else {
Timber.w("Note was added from browser and on return mSearchView == null");
}
}
// }
if (requestCode == PREVIEW_CARDS && data != null && (data.getBooleanExtra("reloadRequired", false) || data.getBooleanExtra("noteChanged", false))) {
searchCards();
if (getReviewerCardId() == mCurrentCardId) {
mReloadRequired = true;
}
}
if (requestCode == EDIT_CARD && data != null && (data.getBooleanExtra("reloadRequired", false) || data.getBooleanExtra("noteChanged", false))) {
// if reloadRequired or noteChanged flag was sent from note editor then reload card list
searchCards();
// in use by reviewer?
if (getReviewerCardId() == mCurrentCardId) {
mReloadRequired = true;
}
}
// maybe the availability of undo changed
invalidateOptionsMenu();
}
use of com.ichi2.async.CollectionTask.TASK_TYPE.ADD_NOTE in project Anki-Android by Ramblurr.
the class DeckPicker method onActivityResult.
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
mDontSaveOnStop = false;
if (resultCode == RESULT_MEDIA_EJECTED) {
showDialog(DIALOG_SD_CARD_NOT_MOUNTED);
return;
} else if (resultCode == RESULT_DB_ERROR) {
handleDbError();
return;
}
if (requestCode == SHOW_STUDYOPTIONS && resultCode == RESULT_OK) {
loadCounts();
} else if (requestCode == ADD_NOTE && resultCode != RESULT_CANCELED) {
loadCounts();
} else if (requestCode == BROWSE_CARDS && (resultCode == Activity.RESULT_OK || resultCode == Activity.RESULT_CANCELED)) {
loadCounts();
} else if (requestCode == ADD_CRAM_DECK) {
// TODO: check, if ok has been clicked
loadCounts();
} else if (requestCode == REPORT_ERROR) {
showStartupScreensAndDialogs(AnkiDroidApp.getSharedPrefs(getBaseContext()), 4);
} else if (requestCode == SHOW_INFO_UPGRADE_DECKS) {
if (intent != null && intent.hasExtra(Info.TYPE_UPGRADE_STAGE)) {
int type = intent.getIntExtra(Info.TYPE_UPGRADE_STAGE, Info.UPGRADE_SCREEN_BASIC1);
if (type == Info.UPGRADE_CONTINUE) {
showStartupScreensAndDialogs(AnkiDroidApp.getSharedPrefs(getBaseContext()), 3);
} else {
showUpgradeScreen(true, type, !intent.hasExtra(Info.TYPE_ANIMATION_RIGHT));
}
} else {
if (resultCode == RESULT_OK) {
if (mOpenCollectionDialog != null && mOpenCollectionDialog.isShowing()) {
mOpenCollectionDialog.dismiss();
}
if (AnkiDroidApp.colIsOpen()) {
AnkiDroidApp.closeCollection(true);
}
AnkiDroidApp.openCollection(AnkiDroidApp.getCollectionPath());
loadCounts();
} else {
finishWithAnimation();
}
}
} else if (requestCode == SHOW_INFO_WELCOME || requestCode == SHOW_INFO_NEW_VERSION) {
if (resultCode == RESULT_OK) {
showStartupScreensAndDialogs(AnkiDroidApp.getSharedPrefs(getBaseContext()), requestCode == SHOW_INFO_WELCOME ? 1 : 2);
} else {
finishWithAnimation();
}
} else if (requestCode == PREFERENCES_UPDATE) {
String oldPath = mPrefDeckPath;
SharedPreferences pref = restorePreferences();
String newLanguage = pref.getString("language", "");
if (AnkiDroidApp.setLanguage(newLanguage)) {
mInvalidateMenu = true;
}
if (mNotMountedDialog != null && mNotMountedDialog.isShowing() && pref.getBoolean("internalMemory", false)) {
showStartupScreensAndDialogs(pref, 0);
} else if (!mPrefDeckPath.equals(oldPath)) {
loadCollection();
}
// if (resultCode == StudyOptions.RESULT_RESTART) {
// setResult(StudyOptions.RESULT_RESTART);
// finishWithAnimation();
// } else {
// SharedPreferences preferences = PrefSettings.getSharedPrefs(getBaseContext());
// BackupManager.initBackup();
// if (!mPrefDeckPath.equals(preferences.getString("deckPath", AnkiDroidApp.getStorageDirectory())) ||
// mPrefDeckOrder != Integer.parseInt(preferences.getString("deckOrder", "0"))) {
// // populateDeckList(preferences.getString("deckPath", AnkiDroidApp.getStorageDirectory()));
// }
// }
} else if (requestCode == REPORT_FEEDBACK && resultCode == RESULT_OK) {
} else if (requestCode == LOG_IN_FOR_SYNC && resultCode == RESULT_OK) {
sync();
} else if (requestCode == LOG_IN_FOR_SHARED_DECK && resultCode == RESULT_OK) {
addSharedDeck();
} else if (requestCode == ADD_SHARED_DECKS) {
if (intent != null) {
mImportPath = intent.getStringExtra("importPath");
}
if (AnkiDroidApp.colIsOpen() && mImportPath != null) {
DeckTask.launchDeckTask(DeckTask.TASK_TYPE_IMPORT, mImportAddListener, new TaskData(AnkiDroidApp.getCol(), mImportPath, true));
mImportPath = null;
}
} else if (requestCode == REQUEST_REVIEW) {
// Log.i(AnkiDroidApp.TAG, "Result code = " + resultCode);
switch(resultCode) {
default:
// do not reload counts, if activity is created anew because it has been before destroyed by android
loadCounts();
break;
case Reviewer.RESULT_NO_MORE_CARDS:
mDontSaveOnStop = true;
Intent i = new Intent();
i.setClass(this, StudyOptionsActivity.class);
i.putExtra("onlyFnsMsg", true);
startActivityForResult(i, SHOW_STUDYOPTIONS);
if (AnkiDroidApp.SDK_VERSION > 4) {
ActivityTransitionAnimation.slide(this, ActivityTransitionAnimation.RIGHT);
}
break;
}
}
// workaround for hidden dialog on return
BroadcastMessages.showDialog();
}
use of com.ichi2.async.CollectionTask.TASK_TYPE.ADD_NOTE in project Anki-Android by Ramblurr.
the class CardBrowser method onActivityResult.
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// FIXME:
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == DeckPicker.RESULT_DB_ERROR) {
closeCardBrowser(DeckPicker.RESULT_DB_ERROR);
}
// switching back to the multimedia card editor.
if (requestCode == EDIT_CARD && resultCode == MultimediaCardEditorActivity.RESULT_DELETED) {
deleteNote(sCardBrowserCard);
DeckTask.launchDeckTask(DeckTask.TASK_TYPE_DISMISS_NOTE, mDeleteNoteHandler, new DeckTask.TaskData(mCol.getSched(), sCardBrowserCard, 3));
} else if (requestCode == EDIT_CARD && resultCode != RESULT_CANCELED) {
// Log.i(AnkiDroidApp.TAG, "CardBrowser: Saving card...");
DeckTask.launchDeckTask(DeckTask.TASK_TYPE_UPDATE_FACT, mUpdateCardHandler, new DeckTask.TaskData(mCol.getSched(), sCardBrowserCard, false));
} else if (requestCode == ADD_NOTE && resultCode == RESULT_OK) {
mSearchTerms = mSearchEditText.getText().toString().toLowerCase();
searchCards();
}
}
Aggregations