Search in sources :

Example 1 with CardBrowser.sCardBrowserCard

use of com.ichi2.anki.CardBrowser.sCardBrowserCard in project AnkiChinaAndroid by ankichinateam.

the class NoteEditor method onCollectionLoaded.

// Finish initializing the activity after the collection has been correctly loaded
@Override
protected void onCollectionLoaded(Collection col) {
    super.onCollectionLoaded(col);
    Intent intent = getIntent();
    Timber.d("NoteEditor() onCollectionLoaded: caller: %d", mCaller);
    registerExternalStorageListener();
    View mainView = findViewById(android.R.id.content);
    mToolbar = findViewById(R.id.editor_toolbar);
    mToolbar.setFormatListener(formatter -> {
        View currentFocus = getCurrentFocus();
        if (!(currentFocus instanceof FieldEditText)) {
            return;
        }
        modifyCurrentSelection(formatter, (FieldEditText) currentFocus);
    });
    enableToolbar(mainView);
    mFieldsLayoutContainer = findViewById(R.id.CardEditorEditFieldsLayout);
    mTagsButton = findViewById(R.id.CardEditorTagText);
    mCardsButton = findViewById(R.id.CardEditorCardsText);
    mCardsButton.setOnClickListener(v -> {
        Timber.i("NoteEditor:: Cards button pressed. Opening template editor");
        showCardTemplateEditor();
    });
    mAedictIntent = false;
    mCurrentEditedCard = null;
    switch(mCaller) {
        case CALLER_NOCALLER:
            Timber.e("no caller could be identified, closing");
            finishWithoutAnimation();
            return;
        case CALLER_REVIEWER:
            mCurrentEditedCard = AbstractFlashcardViewer.getEditorCard();
            if (mCurrentEditedCard == null) {
                finishWithoutAnimation();
                return;
            }
            mEditorNote = mCurrentEditedCard.note();
            mAddNote = false;
            break;
        case CALLER_STUDYOPTIONS:
        case CALLER_DECKPICKER:
        case CALLER_REVIEWER_ADD:
        case CALLER_CARDBROWSER_ADD:
        case CALLER_CARDEDITOR:
            mAddNote = true;
            break;
        case CALLER_CARDBROWSER_EDIT:
            mCurrentEditedCard = CardBrowser.sCardBrowserCard;
            if (mCurrentEditedCard == null) {
                finishWithoutAnimation();
                return;
            }
            mEditorNote = mCurrentEditedCard.note();
            mAddNote = false;
            break;
        case CALLER_CARDEDITOR_INTENT_ADD:
            {
                fetchIntentInformation(intent);
                if (mSourceText == null) {
                    finishWithoutAnimation();
                    return;
                }
                if ("Aedict Notepad".equals(mSourceText[0]) && addFromAedict(mSourceText[1])) {
                    finishWithoutAnimation();
                    return;
                }
                mAddNote = true;
                break;
            }
        default:
            break;
    }
    // Note type Selector
    mNoteTypeSpinner = findViewById(R.id.note_type_spinner);
    ArrayList<Model> models = getCol().getModels().all();
    Collections.sort(models, NamedJSONComparator.instance);
    final ArrayList<String> modelNames = new ArrayList<>(models.size());
    mAllModelIds = new ArrayList<>(models.size());
    for (JSONObject m : models) {
        modelNames.add(m.getString("name"));
        mAllModelIds.add(m.getLong("id"));
    }
    ArrayAdapter<String> noteTypeAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, modelNames);
    mNoteTypeSpinner.setAdapter(noteTypeAdapter);
    noteTypeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    // Deck Selector
    TextView deckTextView = findViewById(R.id.CardEditorDeckText);
    // If edit mode and more than one card template distinguish between "Deck" and "Card deck"
    if (!mAddNote && mEditorNote.model().getJSONArray("tmpls").length() > 1) {
        deckTextView.setText(R.string.CardEditorCardDeck);
    }
    mNoteDeckSpinner = findViewById(R.id.note_deck_spinner);
    ArrayList<Deck> decks = getCol().getDecks().all();
    Collections.sort(decks, DeckComparator.instance);
    final ArrayList<String> deckNames = new ArrayList<>(decks.size());
    mAllDeckIds = new ArrayList<>(decks.size());
    for (Deck d : decks) {
        // add current deck and all other non-filtered decks to deck list
        long thisDid = d.getLong("id");
        if (d.getInt("dyn") == 0 || (!mAddNote && mCurrentEditedCard != null && mCurrentEditedCard.getDid() == thisDid)) {
            deckNames.add(d.getString("name"));
            mAllDeckIds.add(thisDid);
        }
    }
    ArrayAdapter<String> noteDeckAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, deckNames);
    mNoteDeckSpinner.setAdapter(noteDeckAdapter);
    noteDeckAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    mNoteDeckSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
            // Timber.i("NoteEditor:: onItemSelected() fired on mNoteDeckSpinner with pos = %d", pos);
            mCurrentDid = mAllDeckIds.get(pos);
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
        // Do Nothing
        }
    });
    mCurrentDid = intent.getLongExtra(EXTRA_DID, mCurrentDid);
    setDid(mEditorNote);
    setNote(mEditorNote, FieldChangeType.onActivityCreation(shouldReplaceNewlines()));
    if (mAddNote) {
        mNoteTypeSpinner.setOnItemSelectedListener(new SetNoteTypeListener());
        setTitle(R.string.menu_add_note);
        // set information transferred by intent
        String contents = null;
        String[] tags = intent.getStringArrayExtra(EXTRA_TAGS);
        if (mSourceText != null) {
            if (mAedictIntent && (mEditFields.size() == 3) && mSourceText[1].contains("[")) {
                contents = mSourceText[1].replaceFirst("\\[", "\u001f" + mSourceText[0] + "\u001f");
                contents = contents.substring(0, contents.length() - 1);
            } else if (mEditFields.size() > 0) {
                mEditFields.get(0).setText(mSourceText[0]);
                if (mEditFields.size() > 1) {
                    mEditFields.get(1).setText(mSourceText[1]);
                }
            }
        } else {
            contents = intent.getStringExtra(EXTRA_CONTENTS);
        }
        if (contents != null) {
            setEditFieldTexts(contents);
        }
        if (tags != null) {
            setTags(tags);
        }
    } else {
        mNoteTypeSpinner.setOnItemSelectedListener(new EditNoteTypeListener());
        setTitle(R.string.cardeditor_title_edit_card);
    }
    findViewById(R.id.CardEditorTagButton).setOnClickListener(v -> {
        Timber.i("NoteEditor:: Tags button pressed... opening tags editor");
        showTagsDialog();
    });
    if (!mAddNote && mCurrentEditedCard != null) {
        Timber.i("onCollectionLoaded() Edit note activity successfully started with card id %d", mCurrentEditedCard.getId());
    }
    if (mAddNote) {
        Timber.i("onCollectionLoaded() Edit note activity successfully started in add card mode with node id %d", mEditorNote.getId());
    }
    // don't open keyboard if not adding note
    if (!mAddNote) {
        this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
    }
    // set focus to FieldEditText 'first' on startup like Anki desktop
    if (mEditFields != null && !mEditFields.isEmpty()) {
        mEditFields.getFirst().requestFocus();
    }
}
Also used : ArrayList(java.util.ArrayList) Deck(com.ichi2.libanki.Deck) Intent(android.content.Intent) View(android.view.View) AdapterView(android.widget.AdapterView) TextView(android.widget.TextView) SuppressLint(android.annotation.SuppressLint) JSONObject(com.ichi2.utils.JSONObject) Model(com.ichi2.libanki.Model) OnItemSelectedListener(android.widget.AdapterView.OnItemSelectedListener) TextView(android.widget.TextView) ArrayAdapter(android.widget.ArrayAdapter)

Aggregations

SuppressLint (android.annotation.SuppressLint)1 Intent (android.content.Intent)1 View (android.view.View)1 AdapterView (android.widget.AdapterView)1 OnItemSelectedListener (android.widget.AdapterView.OnItemSelectedListener)1 ArrayAdapter (android.widget.ArrayAdapter)1 TextView (android.widget.TextView)1 Deck (com.ichi2.libanki.Deck)1 Model (com.ichi2.libanki.Model)1 JSONObject (com.ichi2.utils.JSONObject)1 ArrayList (java.util.ArrayList)1