Search in sources :

Example 1 with JSONNameComparator

use of com.ichi2.utils.JSONNameComparator in project Anki-Android by Ramblurr.

the class MultimediaCardEditorActivity method showDeckSelectDialog.

private Dialog showDeckSelectDialog() {
    StyledDialog dialog = null;
    StyledDialog.Builder builder = new StyledDialog.Builder(this);
    ArrayList<CharSequence> dialogDeckItems = new ArrayList<CharSequence>();
    // Use this array to know which ID is associated with each
    // Item(name)
    final ArrayList<Long> dialogDeckIds = new ArrayList<Long>();
    ArrayList<JSONObject> decks = mCol.getDecks().all();
    Collections.sort(decks, new JSONNameComparator());
    builder.setTitle(R.string.deck);
    for (JSONObject d : decks) {
        try {
            if (d.getInt("dyn") == 0) {
                dialogDeckItems.add(d.getString("name"));
                dialogDeckIds.add(d.getLong("id"));
            }
        } catch (JSONException e) {
            Log.e("Multimedia Editor", e.getMessage());
        }
    }
    // Convert to Array
    String[] items = new String[dialogDeckItems.size()];
    dialogDeckItems.toArray(items);
    builder.setItems(items, new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int item) {
            long newId = dialogDeckIds.get(item);
            if (mCurrentDid != newId) {
                if (mAddNote) {
                    try {
                        // TODO: remove from dynamic deck (see CardEditor.java in v2.1)
                        // TODO: mEditorNote.setDid(newId);
                        mEditorNote.model().put("did", newId);
                        mCol.getModels().setChanged();
                    } catch (JSONException e) {
                        Log.e("Multimedia Editor", e.getMessage());
                    }
                }
                mCurrentDid = newId;
                createEditorUI(mNote);
            }
        }
    });
    dialog = builder.create();
    return dialog;
}
Also used : JSONNameComparator(com.ichi2.utils.JSONNameComparator) DialogInterface(android.content.DialogInterface) StyledDialog(com.ichi2.themes.StyledDialog) ArrayList(java.util.ArrayList) JSONException(org.json.JSONException) SuppressLint(android.annotation.SuppressLint) JSONObject(org.json.JSONObject)

Example 2 with JSONNameComparator

use of com.ichi2.utils.JSONNameComparator in project Anki-Android by Ramblurr.

the class CardEditor method onCreateDialog.

@Override
protected Dialog onCreateDialog(int id) {
    StyledDialog dialog = null;
    Resources res = getResources();
    StyledDialog.Builder builder = new StyledDialog.Builder(this);
    switch(id) {
        case DIALOG_TAGS_SELECT:
            builder.setTitle(R.string.card_details_tags);
            builder.setPositiveButton(res.getString(R.string.select), new OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if (mAddNote) {
                        try {
                            JSONArray ja = new JSONArray();
                            for (String t : selectedTags) {
                                ja.put(t);
                            }
                            mCol.getModels().current().put("tags", ja);
                            mCol.getModels().setChanged();
                        } catch (JSONException e) {
                            throw new RuntimeException(e);
                        }
                        mEditorNote.setTags(selectedTags);
                    }
                    mCurrentTags = selectedTags;
                    updateTags();
                }
            });
            builder.setNegativeButton(res.getString(R.string.cancel), null);
            mNewTagEditText = (EditText) new EditText(this);
            mNewTagEditText.setHint(R.string.add_new_tag);
            InputFilter filter = new InputFilter() {

                public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                    for (int i = start; i < end; i++) {
                        if (source.charAt(i) == ' ' || source.charAt(i) == ',') {
                            return "";
                        }
                    }
                    return null;
                }
            };
            mNewTagEditText.setFilters(new InputFilter[] { filter });
            ImageView mAddTextButton = new ImageView(this);
            mAddTextButton.setImageResource(R.drawable.ic_addtag);
            mAddTextButton.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    String tag = mNewTagEditText.getText().toString();
                    if (tag.length() != 0) {
                        if (mEditorNote.hasTag(tag)) {
                            mNewTagEditText.setText("");
                            return;
                        }
                        selectedTags.add(tag);
                        actualizeTagDialog(mTagsDialog);
                        mNewTagEditText.setText("");
                    }
                }
            });
            FrameLayout frame = new FrameLayout(this);
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.RIGHT | Gravity.CENTER_VERTICAL);
            params.rightMargin = 10;
            mAddTextButton.setLayoutParams(params);
            frame.addView(mNewTagEditText);
            frame.addView(mAddTextButton);
            builder.setView(frame, false, true);
            dialog = builder.create();
            mTagsDialog = dialog;
            break;
        case DIALOG_DECK_SELECT:
            ArrayList<CharSequence> dialogDeckItems = new ArrayList<CharSequence>();
            // Use this array to know which ID is associated with each
            // Item(name)
            final ArrayList<Long> dialogDeckIds = new ArrayList<Long>();
            ArrayList<JSONObject> decks = mCol.getDecks().all();
            Collections.sort(decks, new JSONNameComparator());
            builder.setTitle(R.string.deck);
            for (JSONObject d : decks) {
                try {
                    if (d.getInt("dyn") == 0) {
                        dialogDeckItems.add(d.getString("name"));
                        dialogDeckIds.add(d.getLong("id"));
                    }
                } catch (JSONException e) {
                    throw new RuntimeException(e);
                }
            }
            // Convert to Array
            String[] items = new String[dialogDeckItems.size()];
            dialogDeckItems.toArray(items);
            builder.setItems(items, new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int item) {
                    long newId = dialogDeckIds.get(item);
                    if (mCurrentDid != newId) {
                        if (mAddNote) {
                            try {
                                // TODO: mEditorNote.setDid(newId);
                                mEditorNote.model().put("did", newId);
                                mCol.getModels().setChanged();
                            } catch (JSONException e) {
                                throw new RuntimeException(e);
                            }
                        }
                        mCurrentDid = newId;
                        updateDeck();
                    }
                }
            });
            dialog = builder.create();
            mDeckSelectDialog = dialog;
            break;
        case DIALOG_MODEL_SELECT:
            ArrayList<CharSequence> dialogItems = new ArrayList<CharSequence>();
            // Use this array to know which ID is associated with each
            // Item(name)
            final ArrayList<Long> dialogIds = new ArrayList<Long>();
            ArrayList<JSONObject> models = mCol.getModels().all();
            Collections.sort(models, new JSONNameComparator());
            builder.setTitle(R.string.note_type);
            for (JSONObject m : models) {
                try {
                    dialogItems.add(m.getString("name"));
                    dialogIds.add(m.getLong("id"));
                } catch (JSONException e) {
                    throw new RuntimeException(e);
                }
            }
            // Convert to Array
            String[] items2 = new String[dialogItems.size()];
            dialogItems.toArray(items2);
            builder.setItems(items2, new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int item) {
                    long oldModelId;
                    try {
                        oldModelId = mCol.getModels().current().getLong("id");
                    } catch (JSONException e) {
                        throw new RuntimeException(e);
                    }
                    long newId = dialogIds.get(item);
                    if (oldModelId != newId) {
                        mCol.getModels().setCurrent(mCol.getModels().get(newId));
                        JSONObject cdeck = mCol.getDecks().current();
                        try {
                            cdeck.put("mid", newId);
                        } catch (JSONException e) {
                            throw new RuntimeException(e);
                        }
                        mCol.getDecks().save(cdeck);
                        int size = mEditFields.size();
                        String[] oldValues = new String[size];
                        for (int i = 0; i < size; i++) {
                            oldValues[i] = mEditFields.get(i).getText().toString();
                        }
                        setNote();
                        resetEditFields(oldValues);
                        mTimerHandler.removeCallbacks(checkDuplicatesRunnable);
                        duplicateCheck(false);
                    }
                }
            });
            dialog = builder.create();
            break;
        case DIALOG_RESET_CARD:
            builder.setTitle(res.getString(R.string.reset_card_dialog_title));
            builder.setMessage(res.getString(R.string.reset_card_dialog_message));
            builder.setPositiveButton(res.getString(R.string.yes), new OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                // for (long cardId :
                // mDeck.getCardsFromFactId(mEditorNote.getId())) {
                // mDeck.cardFromId(cardId).resetCard();
                // }
                // mDeck.reset();
                // setResult(Reviewer.RESULT_EDIT_CARD_RESET);
                // mCardReset = true;
                // Themes.showThemedToast(CardEditor.this,
                // getResources().getString(
                // R.string.reset_card_dialog_confirmation), true);
                }
            });
            builder.setNegativeButton(res.getString(R.string.no), null);
            builder.setCancelable(true);
            dialog = builder.create();
            break;
        case DIALOG_INTENT_INFORMATION:
            dialog = createDialogIntentInformation(builder, res);
    }
    return dialog;
}
Also used : DialogInterface(android.content.DialogInterface) Builder(com.ichi2.themes.StyledDialog.Builder) StyledDialog(com.ichi2.themes.StyledDialog) ArrayList(java.util.ArrayList) ImageView(android.widget.ImageView) EditText(android.widget.EditText) InputFilter(android.text.InputFilter) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) Spanned(android.text.Spanned) ImageView(android.widget.ImageView) View(android.view.View) AdapterView(android.widget.AdapterView) TextView(android.widget.TextView) ListView(android.widget.ListView) OnClickListener(android.content.DialogInterface.OnClickListener) JSONObject(org.json.JSONObject) FrameLayout(android.widget.FrameLayout) Builder(com.ichi2.themes.StyledDialog.Builder) OnClickListener(android.content.DialogInterface.OnClickListener) Resources(android.content.res.Resources)

Example 3 with JSONNameComparator

use of com.ichi2.utils.JSONNameComparator in project Anki-Android by Ramblurr.

the class MultimediaCardEditorActivity method showModelSelectDialog.

private Dialog showModelSelectDialog() {
    StyledDialog dialog = null;
    StyledDialog.Builder builder = new StyledDialog.Builder(this);
    ArrayList<CharSequence> dialogItems = new ArrayList<CharSequence>();
    // Use this array to know which ID is associated with each
    // Item(name)
    final ArrayList<Long> dialogIds = new ArrayList<Long>();
    ArrayList<JSONObject> models = mCol.getModels().all();
    Collections.sort(models, new JSONNameComparator());
    builder.setTitle(R.string.note_type);
    for (JSONObject m : models) {
        try {
            dialogItems.add(m.getString("name"));
            dialogIds.add(m.getLong("id"));
        } catch (JSONException e) {
            Log.e("Multimedia Editor", e.getMessage());
        }
    }
    // Convert to Array
    String[] items2 = new String[dialogItems.size()];
    dialogItems.toArray(items2);
    builder.setItems(items2, new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int item) {
            long oldModelId;
            try {
                oldModelId = mCol.getModels().current().getLong("id");
            } catch (JSONException e) {
                Log.e("Multimedia Editor", e.getMessage());
                return;
            }
            long newId = dialogIds.get(item);
            if (oldModelId != newId) {
                changeCurrentModel(newId);
                createEditorUI(mNote);
            }
        }
    });
    dialog = builder.create();
    return dialog;
}
Also used : JSONNameComparator(com.ichi2.utils.JSONNameComparator) DialogInterface(android.content.DialogInterface) StyledDialog(com.ichi2.themes.StyledDialog) ArrayList(java.util.ArrayList) JSONException(org.json.JSONException) SuppressLint(android.annotation.SuppressLint) JSONObject(org.json.JSONObject)

Aggregations

DialogInterface (android.content.DialogInterface)3 StyledDialog (com.ichi2.themes.StyledDialog)3 ArrayList (java.util.ArrayList)3 JSONException (org.json.JSONException)3 JSONObject (org.json.JSONObject)3 SuppressLint (android.annotation.SuppressLint)2 JSONNameComparator (com.ichi2.utils.JSONNameComparator)2 OnClickListener (android.content.DialogInterface.OnClickListener)1 Resources (android.content.res.Resources)1 InputFilter (android.text.InputFilter)1 Spanned (android.text.Spanned)1 View (android.view.View)1 AdapterView (android.widget.AdapterView)1 EditText (android.widget.EditText)1 FrameLayout (android.widget.FrameLayout)1 ImageView (android.widget.ImageView)1 ListView (android.widget.ListView)1 TextView (android.widget.TextView)1 Builder (com.ichi2.themes.StyledDialog.Builder)1 JSONArray (org.json.JSONArray)1