Search in sources :

Example 1 with TypeAnswer

use of com.ichi2.anki.cardviewer.TypeAnswer in project Anki-Android by Ramblurr.

the class PreviewClass method displayCardAnswer.

private void displayCardAnswer() {
    // prevent answering (by e.g. gestures) before card is loaded
    if (mCurrentCard == null) {
        return;
    }
    sDisplayAnswer = true;
    String answer = mCurrentCard.getAnswer(mCurrentSimpleInterface);
    answer = typeAnsAnswerFilter(answer);
    String displayString = "";
    if (mCurrentSimpleInterface) {
        mCardContent = convertToSimple(answer);
        if (mCardContent.length() == 0) {
            SpannableString hint = new SpannableString(getResources().getString(R.string.simple_interface_hint, R.string.card_details_answer));
            hint.setSpan(new StyleSpan(Typeface.ITALIC), 0, mCardContent.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            mCardContent = hint;
        }
    } else {
        Sound.stopSounds();
        if (mPrefFixArabic) {
            // reshape
            answer = ArabicUtilities.reshapeSentence(answer, true);
        }
        // If the user wrote an answer
        if (typeAnswer()) {
            mAnswerField.setVisibility(View.GONE);
            if (mCurrentCard != null) {
                if (mPrefFixArabic) {
                    // reshape
                    mTypeCorrect = ArabicUtilities.reshapeSentence(mTypeCorrect, true);
                }
                // Obtain the user answer and the correct answer
                String userAnswer = mAnswerField.getText().toString();
                Matcher matcher = sSpanPattern.matcher(Utils.stripHTMLMedia(mTypeCorrect));
                String correctAnswer = matcher.replaceAll("");
                matcher = sBrPattern.matcher(correctAnswer);
                correctAnswer = matcher.replaceAll("\n");
                matcher = Sound.sSoundPattern.matcher(correctAnswer);
                correctAnswer = matcher.replaceAll("");
                // Log.i(AnkiDroidApp.TAG, "correct answer = " + correctAnswer);
                // Obtain the diff and send it to updateCard
                DiffEngine diff = new DiffEngine();
                StringBuffer span = new StringBuffer();
                span.append("<span style=\"font-family: '").append(mTypeFont).append("'; font-size: ").append(12).append("px\">");
                span.append(diff.diff_prettyHtml(diff.diff_main(userAnswer, correctAnswer), mNightMode));
                span.append("</span>");
                span.append("<br/>").append(answer);
                displayString = enrichWithQADiv(span.toString(), true);
            }
            // Hide soft keyboard
            InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(mAnswerField.getWindowToken(), 0);
        } else {
            displayString = enrichWithQADiv(answer, true);
        }
    }
    updateCard(displayString);
}
Also used : SpannableString(android.text.SpannableString) DiffEngine(com.ichi2.utils.DiffEngine) Matcher(java.util.regex.Matcher) StyleSpan(android.text.style.StyleSpan) InputMethodManager(android.view.inputmethod.InputMethodManager) SpannedString(android.text.SpannedString) SpannableString(android.text.SpannableString)

Example 2 with TypeAnswer

use of com.ichi2.anki.cardviewer.TypeAnswer in project Anki-Android by Ramblurr.

the class Reviewer method displayCardAnswer.

private void displayCardAnswer() {
    // prevent answering (by e.g. gestures) before card is loaded
    if (mCurrentCard == null) {
        return;
    }
    sDisplayAnswer = true;
    setFlipCardAnimation();
    String answer = mCurrentCard.getAnswer(mCurrentSimpleInterface);
    answer = typeAnsAnswerFilter(answer);
    String displayString = "";
    if (mCurrentSimpleInterface) {
        mCardContent = convertToSimple(answer);
        if (mCardContent.length() == 0) {
            SpannableString hint = new SpannableString(getResources().getString(R.string.simple_interface_hint, R.string.card_details_answer));
            hint.setSpan(new StyleSpan(Typeface.ITALIC), 0, mCardContent.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            mCardContent = hint;
        }
    } else {
        Sound.stopSounds();
        if (mPrefFixArabic) {
            // reshape
            answer = ArabicUtilities.reshapeSentence(answer, true);
        }
        // If the user wrote an answer
        if (typeAnswer()) {
            mAnswerField.setVisibility(View.GONE);
            if (mCurrentCard != null) {
                if (mPrefFixArabic) {
                    // reshape
                    mTypeCorrect = ArabicUtilities.reshapeSentence(mTypeCorrect, true);
                }
                // Obtain the user answer and the correct answer
                String userAnswer = mAnswerField.getText().toString();
                Matcher matcher = sSpanPattern.matcher(Utils.stripHTMLMedia(mTypeCorrect));
                String correctAnswer = matcher.replaceAll("");
                matcher = sBrPattern.matcher(correctAnswer);
                correctAnswer = matcher.replaceAll("\n");
                matcher = Sound.sSoundPattern.matcher(correctAnswer);
                correctAnswer = matcher.replaceAll("");
                // Log.i(AnkiDroidApp.TAG, "correct answer = " + correctAnswer);
                // Obtain the diff and send it to updateCard
                DiffEngine diff = new DiffEngine();
                StringBuffer span = new StringBuffer();
                span.append("<span style=\"font-family: '").append(mTypeFont).append("'; font-size: ").append(mTypeSize).append("px\">");
                span.append(diff.diff_prettyHtml(diff.diff_main(userAnswer, correctAnswer), mNightMode));
                span.append("</span>");
                span.append("<br/>").append(answer);
                displayString = enrichWithQADiv(span.toString(), true);
            }
            // Hide soft keyboard
            InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(mAnswerField.getWindowToken(), 0);
        } else {
            displayString = enrichWithQADiv(answer, true);
        }
    }
    mIsSelecting = false;
    updateCard(displayString);
    showEaseButtons();
    // If the user want to show next question automatically
    if (mPrefUseTimer) {
        mTimeoutHandler.removeCallbacks(mShowQuestionTask);
        mTimeoutHandler.postDelayed(mShowQuestionTask, mWaitQuestionSecond * 1000);
    }
}
Also used : SpannableString(android.text.SpannableString) DiffEngine(com.ichi2.utils.DiffEngine) Matcher(java.util.regex.Matcher) StyleSpan(android.text.style.StyleSpan) InputMethodManager(android.view.inputmethod.InputMethodManager) SpannedString(android.text.SpannedString) SpannableString(android.text.SpannableString)

Aggregations

SpannableString (android.text.SpannableString)2 SpannedString (android.text.SpannedString)2 StyleSpan (android.text.style.StyleSpan)2 InputMethodManager (android.view.inputmethod.InputMethodManager)2 DiffEngine (com.ichi2.utils.DiffEngine)2 Matcher (java.util.regex.Matcher)2