Search in sources :

Example 16 with ConfirmationDialog

use of com.ichi2.anki.dialogs.ConfirmationDialog in project Anki-Android by ankidroid.

the class NoteEditor method saveNote.

@VisibleForTesting
void saveNote() {
    final Resources res = getResources();
    if (mSelectedTags == null) {
        mSelectedTags = new ArrayList<>(0);
    }
    saveToggleStickyMap();
    // 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;
        TaskManager.launchCollectionTask(new CollectionTask.AddNote(mEditorNote), saveNoteHandler());
        updateFieldsFromStickyText();
    } else {
        // Check whether note type has been changed
        final Model newModel = getCurrentlySelectedModel();
        final Model oldModel = (mCurrentEditedCard == null) ? null : mCurrentEditedCard.model();
        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) CollectionTask(com.ichi2.async.CollectionTask) ConfirmationDialog(com.ichi2.anki.dialogs.ConfirmationDialog) VisibleForTesting(androidx.annotation.VisibleForTesting)

Example 17 with ConfirmationDialog

use of com.ichi2.anki.dialogs.ConfirmationDialog in project Anki-Android by ankidroid.

the class Reviewer method showResetCardDialog.

private void showResetCardDialog() {
    // Show confirmation dialog before resetting card progress
    Timber.i("showResetCardDialog() Reset progress button pressed");
    // Show confirmation dialog before resetting card progress
    ConfirmationDialog dialog = new ConfirmationDialog();
    String title = getResources().getString(R.string.reset_card_dialog_title);
    String message = getResources().getString(R.string.reset_card_dialog_message);
    dialog.setArgs(title, message);
    Runnable confirm = () -> {
        Timber.i("NoteEditor:: ResetProgress button pressed");
        List<Long> cardIds = Collections.singletonList(mCurrentCard.getId());
        new SchedulerService.ResetCards(cardIds).runWithHandler(scheduleCollectionTaskHandler(R.plurals.reset_cards_dialog_acknowledge));
    };
    dialog.setConfirm(confirm);
    showDialogFragment(dialog);
}
Also used : ColorStateList(android.content.res.ColorStateList) List(java.util.List) SpannableString(android.text.SpannableString) ConfirmationDialog(com.ichi2.anki.dialogs.ConfirmationDialog)

Example 18 with ConfirmationDialog

use of com.ichi2.anki.dialogs.ConfirmationDialog in project Anki-Android by ankidroid.

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 = () -> {
            mCol.modSchemaNoCheck();
            deleteModel();
            dismissContextMenu();
        };
        Runnable cancel = this::dismissContextMenu;
        try {
            mCol.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) {
            e.log();
            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 19 with ConfirmationDialog

use of com.ichi2.anki.dialogs.ConfirmationDialog in project Anki-Android by ankidroid.

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 FixedEditText(this);
    mFieldNameInput.setRawInputType(InputType.TYPE_CLASS_NUMBER);
    new MaterialEditTextDialog.Builder(this, mFieldNameInput).title(String.format(getResources().getString(R.string.model_field_editor_reposition), 1, mFieldLabels.size())).positiveText(R.string.dialog_ok).onPositive((dialog, which) -> {
        String newPosition = mFieldNameInput.getText().toString();
        int pos;
        try {
            pos = Integer.parseInt(newPosition);
        } catch (NumberFormatException n) {
            Timber.w(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();
                TaskManager.launchCollectionTask(new CollectionTask.RepositionField(mMod, mNoteFields.getJSONObject(mCurrentPos), pos - 1), listener);
            } catch (ConfirmModSchemaException e) {
                e.log();
                // Handle mod schema confirmation
                ConfirmationDialog c = new ConfirmationDialog();
                c.setArgs(getResources().getString(R.string.full_sync_confirmation));
                Runnable confirm = () -> {
                    try {
                        mCol.modSchemaNoCheck();
                        TaskManager.launchCollectionTask(new CollectionTask.RepositionField(mMod, mNoteFields.getJSONObject(mCurrentPos), pos - 1), listener);
                        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 : JSONException(com.ichi2.utils.JSONException) CollectionTask(com.ichi2.async.CollectionTask) ConfirmationDialog(com.ichi2.anki.dialogs.ConfirmationDialog) ConfirmModSchemaException(com.ichi2.anki.exception.ConfirmModSchemaException) FixedEditText(com.ichi2.ui.FixedEditText)

Example 20 with ConfirmationDialog

use of com.ichi2.anki.dialogs.ConfirmationDialog in project Anki-Android by ankidroid.

the class ModelFieldEditor method sortByField.

/*
     * Changes the sort field (that displays in card browser) to the current field
     */
private void sortByField() {
    changeHandler listener = changeFieldHandler();
    try {
        mCol.modSchema();
        TaskManager.launchCollectionTask(new CollectionTask.ChangeSortField(mMod, mCurrentPos), listener);
    } catch (ConfirmModSchemaException e) {
        e.log();
        // Handler mMod schema confirmation
        ConfirmationDialog c = new ConfirmationDialog();
        c.setArgs(getResources().getString(R.string.full_sync_confirmation));
        Runnable confirm = () -> {
            mCol.modSchemaNoCheck();
            TaskManager.launchCollectionTask(new CollectionTask.ChangeSortField(mMod, mCurrentPos), listener);
            dismissContextMenu();
        };
        c.setConfirm(confirm);
        c.setCancel(mConfirmDialogCancel);
        ModelFieldEditor.this.showDialogFragment(c);
    }
}
Also used : ConfirmModSchemaException(com.ichi2.anki.exception.ConfirmModSchemaException) CollectionTask(com.ichi2.async.CollectionTask) ConfirmationDialog(com.ichi2.anki.dialogs.ConfirmationDialog)

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