Search in sources :

Example 1 with ConfirmationDialog

use of com.ichi2.anki.dialogs.ConfirmationDialog 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();
    }
}
Also used : JSONArray(com.ichi2.utils.JSONArray) Model(com.ichi2.libanki.Model) Resources(android.content.res.Resources) File(java.io.File) TaskData(com.ichi2.async.TaskData) ConfirmationDialog(com.ichi2.anki.dialogs.ConfirmationDialog) VisibleForTesting(androidx.annotation.VisibleForTesting)

Example 2 with ConfirmationDialog

use of com.ichi2.anki.dialogs.ConfirmationDialog in project AnkiChinaAndroid by ankichinateam.

the class ModelBrowser method deleteModelDialog.

/*
     * Displays a confirmation box asking if you want to delete the note type and then deletes it if confirmed
     */
private void deleteModelDialog() {
    if (mModelIds.size() > 1) {
        Runnable confirm = new Runnable() {

            @Override
            public void run() {
                col.modSchemaNoCheck();
                try {
                    deleteModel();
                } catch (ConfirmModSchemaException e) {
                // This should never be reached because modSchema() didn't throw an exception
                }
                dismissContextMenu();
            }
        };
        Runnable cancel = new Runnable() {

            @Override
            public void run() {
                dismissContextMenu();
            }
        };
        try {
            col.modSchema();
            ConfirmationDialog d = new ConfirmationDialog();
            d.setArgs(getResources().getString(R.string.model_delete_warning));
            d.setConfirm(confirm);
            d.setCancel(cancel);
            ModelBrowser.this.showDialogFragment(d);
        } catch (ConfirmModSchemaException e) {
            ConfirmationDialog c = new ConfirmationDialog();
            c.setArgs(getResources().getString(R.string.full_sync_confirmation));
            c.setConfirm(confirm);
            c.setCancel(cancel);
            showDialogFragment(c);
        }
    } else // Prevent users from deleting last model
    {
        showToast(getString(R.string.toast_last_model));
    }
}
Also used : ConfirmModSchemaException(com.ichi2.anki.exception.ConfirmModSchemaException) ConfirmationDialog(com.ichi2.anki.dialogs.ConfirmationDialog)

Example 3 with ConfirmationDialog

use of com.ichi2.anki.dialogs.ConfirmationDialog in project AnkiChinaAndroid by ankichinateam.

the class ModelFieldEditor method deleteFieldDialog.

/*
     * Creates a dialog to delete the currently selected field
     */
private void deleteFieldDialog() {
    Runnable confirm = () -> {
        mCol.modSchemaNoCheck();
        deleteField();
        dismissContextMenu();
    };
    if (mFieldLabels.size() < 2) {
        UIUtils.showThemedToast(this, getResources().getString(R.string.toast_last_field), true);
    } else {
        try {
            mCol.modSchema();
            ConfirmationDialog d = new ConfirmationDialog();
            d.setArgs(getResources().getString(R.string.field_delete_warning));
            d.setConfirm(confirm);
            d.setCancel(mConfirmDialogCancel);
            showDialogFragment(d);
        } catch (ConfirmModSchemaException e) {
            ConfirmationDialog c = new ConfirmationDialog();
            c.setConfirm(confirm);
            c.setCancel(mConfirmDialogCancel);
            c.setArgs(getResources().getString(R.string.full_sync_confirmation));
            showDialogFragment(c);
        }
    }
}
Also used : ConfirmModSchemaException(com.ichi2.anki.exception.ConfirmModSchemaException) ConfirmationDialog(com.ichi2.anki.dialogs.ConfirmationDialog)

Example 4 with ConfirmationDialog

use of com.ichi2.anki.dialogs.ConfirmationDialog in project AnkiChinaAndroid by ankichinateam.

the class ModelFieldEditor method addFieldDialog.

// ----------------------------------------------------------------------------
// CONTEXT MENU DIALOGUES
// ----------------------------------------------------------------------------
/*
    * Creates a dialog to create a field
    */
private void addFieldDialog() {
    mFieldNameInput = new EditText(this);
    mFieldNameInput.setSingleLine(true);
    new MaterialDialog.Builder(this).title(R.string.model_field_editor_add).positiveText(R.string.dialog_ok).customView(mFieldNameInput, true).onPositive((dialog, which) -> {
        String fieldName = mFieldNameInput.getText().toString().replaceAll("[\\n\\r]", "");
        if (fieldName.length() == 0) {
            UIUtils.showThemedToast(this, getResources().getString(R.string.toast_empty_name), true);
        } else if (containsField(fieldName)) {
            UIUtils.showThemedToast(this, getResources().getString(R.string.toast_duplicate_field), true);
        } else {
            // Name is valid, now field is added
            changeHandler listener = changeFieldHandler();
            try {
                mCol.modSchema();
                CollectionTask.launchCollectionTask(ADD_FIELD, listener, new TaskData(new Object[] { mMod, fieldName }));
            } catch (ConfirmModSchemaException e) {
                // Create dialogue to for schema change
                ConfirmationDialog c = new ConfirmationDialog();
                c.setArgs(getResources().getString(R.string.full_sync_confirmation));
                Runnable confirm = () -> {
                    mCol.modSchemaNoCheck();
                    String fieldName1 = mFieldNameInput.getText().toString().replaceAll("[\\n\\r]", "");
                    CollectionTask.launchCollectionTask(ADD_FIELD, listener, new TaskData(new Object[] { mMod, fieldName1 }));
                    dismissContextMenu();
                };
                c.setConfirm(confirm);
                c.setCancel(mConfirmDialogCancel);
                ModelFieldEditor.this.showDialogFragment(c);
            }
            mCol.getModels().update(mMod);
            fullRefreshList();
        }
    }).negativeText(R.string.dialog_cancel).show();
}
Also used : EditText(android.widget.EditText) ConfirmModSchemaException(com.ichi2.anki.exception.ConfirmModSchemaException) TaskData(com.ichi2.async.TaskData) ConfirmationDialog(com.ichi2.anki.dialogs.ConfirmationDialog)

Example 5 with ConfirmationDialog

use of com.ichi2.anki.dialogs.ConfirmationDialog in project AnkiChinaAndroid by ankichinateam.

the class ModelFieldEditor method repositionFieldDialog.

/*
     * Allows the user to select a number less than the number of fields in the current model to
     * reposition the current field to
     * Processing time is scales with number of items
     */
private void repositionFieldDialog() {
    mFieldNameInput = new EditText(this);
    mFieldNameInput.setRawInputType(InputType.TYPE_CLASS_NUMBER);
    new MaterialDialog.Builder(this).title(String.format(getResources().getString(R.string.model_field_editor_reposition), 1, mFieldLabels.size())).positiveText(R.string.dialog_ok).customView(mFieldNameInput, true).onPositive((dialog, which) -> {
        String newPosition = mFieldNameInput.getText().toString();
        int pos;
        try {
            pos = Integer.parseInt(newPosition);
        } catch (NumberFormatException n) {
            UIUtils.showThemedToast(this, getResources().getString(R.string.toast_out_of_range), true);
            return;
        }
        if (pos < 1 || pos > mFieldLabels.size()) {
            UIUtils.showThemedToast(this, getResources().getString(R.string.toast_out_of_range), true);
        } else {
            changeHandler listener = changeFieldHandler();
            // Input is valid, now attempt to modify
            try {
                mCol.modSchema();
                CollectionTask.launchCollectionTask(REPOSITION_FIELD, listener, new TaskData(new Object[] { mMod, mNoteFields.getJSONObject(mCurrentPos), pos - 1 }));
            } catch (ConfirmModSchemaException e) {
                // Handle mod schema confirmation
                ConfirmationDialog c = new ConfirmationDialog();
                c.setArgs(getResources().getString(R.string.full_sync_confirmation));
                Runnable confirm = () -> {
                    try {
                        mCol.modSchemaNoCheck();
                        String newPosition1 = mFieldNameInput.getText().toString();
                        int pos1 = Integer.parseInt(newPosition1);
                        CollectionTask.launchCollectionTask(REPOSITION_FIELD, listener, new TaskData(new Object[] { mMod, mNoteFields.getJSONObject(mCurrentPos), pos1 - 1 }));
                        dismissContextMenu();
                    } catch (JSONException e1) {
                        throw new RuntimeException(e1);
                    }
                };
                c.setConfirm(confirm);
                c.setCancel(mConfirmDialogCancel);
                ModelFieldEditor.this.showDialogFragment(c);
            }
        }
    }).negativeText(R.string.dialog_cancel).show();
}
Also used : EditText(android.widget.EditText) JSONException(com.ichi2.utils.JSONException) TaskData(com.ichi2.async.TaskData) ConfirmationDialog(com.ichi2.anki.dialogs.ConfirmationDialog) ConfirmModSchemaException(com.ichi2.anki.exception.ConfirmModSchemaException) JSONObject(com.ichi2.utils.JSONObject)

Aggregations

ConfirmationDialog (com.ichi2.anki.dialogs.ConfirmationDialog)19 ConfirmModSchemaException (com.ichi2.anki.exception.ConfirmModSchemaException)12 TaskData (com.ichi2.async.TaskData)7 CollectionTask (com.ichi2.async.CollectionTask)5 VisibleForTesting (androidx.annotation.VisibleForTesting)4 JSONException (com.ichi2.utils.JSONException)4 JSONObject (com.ichi2.utils.JSONObject)4 List (java.util.List)4 SharedPreferences (android.content.SharedPreferences)3 Resources (android.content.res.Resources)3 Bundle (android.os.Bundle)3 Collection (com.ichi2.libanki.Collection)3 ArrayList (java.util.ArrayList)3 AlertDialog (android.app.AlertDialog)2 BroadcastReceiver (android.content.BroadcastReceiver)2 Context (android.content.Context)2 Intent (android.content.Intent)2 IntentFilter (android.content.IntentFilter)2 Handler (android.os.Handler)2 SystemClock (android.os.SystemClock)2