Search in sources :

Example 1 with CanvasWebView

use of com.instructure.pandautils.views.CanvasWebView in project instructure-android by instructure.

the class QuizMultiAnswerBinder method bind.

public static void bind(final QuizMultiChoiceViewHolder holder, final QuizSubmissionQuestion quizSubmissionQuestion, final int courseColor, final int position, final boolean shouldLetAnswer, final Context context, final QuizPostMultiAnswers callback, final QuizToggleFlagState flagStateCallback) {
    if (holder == null) {
        return;
    }
    holder.question.loadUrl("about:blank");
    holder.question.setBackgroundColor(Color.TRANSPARENT);
    holder.question.setWebViewClient(new WebViewClient() {

        @Override
        public void onPageFinished(WebView view, String url) {
            view.loadUrl("javascript:MyApp.resize(document.body.getBoundingClientRect().height)");
            super.onPageFinished(view, url);
        }
    });
    holder.question.formatHTML(quizSubmissionQuestion.getQuestionText(), "");
    holder.questionNumber.setText(quizSubmissionQuestion.getQuestionName() + " " + (position + 1));
    holder.questionId = quizSubmissionQuestion.getId();
    LayoutInflater inflater = LayoutInflater.from(context);
    // are any in there
    if (holder.answerContainer.getChildCount() > 0) {
        holder.answerContainer.removeAllViews();
    }
    // add answers to the answer container
    int index = 0;
    for (final QuizSubmissionAnswer answer : quizSubmissionQuestion.getAnswers()) {
        final LinearLayout answerContainer = (LinearLayout) inflater.inflate(R.layout.quiz_multi_choice_answer, null, false);
        final CanvasWebView webView = (CanvasWebView) answerContainer.findViewById(R.id.html_answer);
        webView.setClickable(false);
        webView.setFocusableInTouchMode(false);
        final TextView textView = (TextView) answerContainer.findViewById(R.id.text_answer);
        final CheckBox checkBox = (CheckBox) answerContainer.findViewById(R.id.answer_checkbox);
        if (!TextUtils.isEmpty(answer.getHtml())) {
            textView.setVisibility(View.GONE);
            final String html = StringUtilities.trimTrailingWhitespace(answer.getHtml()).toString();
            webView.formatHTML(html, "");
            webView.setBackgroundColor(Color.TRANSPARENT);
            // we only care about marking the answers if they can actually answer
            if (shouldLetAnswer) {
                webView.setOnTouchListener(new View.OnTouchListener() {

                    @Override
                    public boolean onTouch(View view, MotionEvent motionEvent) {
                        // this may or may not be how we do the selection of things when we get the final UI
                        if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
                            answerContainer.performClick();
                            return true;
                        }
                        return true;
                    }
                });
            }
        } else if (!TextUtils.isEmpty(answer.getText())) {
            webView.setVisibility(View.GONE);
            textView.setText(answer.getText());
        }
        if (shouldLetAnswer) {
            answerContainer.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {
                    if (checkBox.isChecked()) {
                        checkBox.setChecked(false);
                        answerContainer.setBackgroundColor(context.getResources().getColor(R.color.canvasBackgroundLight));
                        callback.answerUnselected(holder.questionId, answer.getId());
                    } else {
                        checkBox.setChecked(true);
                        answerContainer.setBackgroundColor(context.getResources().getColor(R.color.canvasBackgroundMedium));
                        callback.answerSelected(holder.questionId, answer.getId());
                    }
                }
            });
        }
        final Drawable courseColorFlag = ColorKeeper.getColoredDrawable(context, R.drawable.vd_bookmark_filled, courseColor);
        if (quizSubmissionQuestion.isFlagged()) {
            holder.flag.setImageDrawable(courseColorFlag);
        } else {
            holder.flag.setImageDrawable(ColorKeeper.getColoredDrawable(context, R.drawable.vd_navigation_bookmarks, context.getResources().getColor(R.color.defaultTextGray)));
        }
        holder.flag.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                if (quizSubmissionQuestion.isFlagged()) {
                    // unflag it
                    holder.flag.setImageDrawable(ColorKeeper.getColoredDrawable(context, R.drawable.vd_navigation_bookmarks, context.getResources().getColor(R.color.defaultTextGray)));
                    flagStateCallback.toggleFlagged(false, quizSubmissionQuestion.getId());
                    quizSubmissionQuestion.setFlagged(false);
                } else {
                    // flag it
                    holder.flag.setImageDrawable(courseColorFlag);
                    flagStateCallback.toggleFlagged(true, quizSubmissionQuestion.getId());
                    quizSubmissionQuestion.setFlagged(true);
                }
            }
        });
        holder.answerContainer.addView(answerContainer);
        if (index == quizSubmissionQuestion.getAnswers().length - 1) {
            // if we're on the last answer remove the bottom divider
            answerContainer.findViewById(R.id.divider).setVisibility(View.GONE);
        } else {
            answerContainer.findViewById(R.id.divider).setVisibility(View.VISIBLE);
            index++;
        }
    }
}
Also used : Drawable(android.graphics.drawable.Drawable) CanvasWebView(com.instructure.pandautils.views.CanvasWebView) TextView(android.widget.TextView) View(android.view.View) WebView(android.webkit.WebView) MotionEvent(android.view.MotionEvent) CanvasWebView(com.instructure.pandautils.views.CanvasWebView) CheckBox(android.widget.CheckBox) LayoutInflater(android.view.LayoutInflater) QuizSubmissionAnswer(com.instructure.canvasapi2.models.QuizSubmissionAnswer) TextView(android.widget.TextView) CanvasWebView(com.instructure.pandautils.views.CanvasWebView) WebView(android.webkit.WebView) LinearLayout(android.widget.LinearLayout) WebViewClient(android.webkit.WebViewClient)

Example 2 with CanvasWebView

use of com.instructure.pandautils.views.CanvasWebView in project instructure-android by instructure.

the class QuizMultiChoiceBinder method bind.

public static void bind(final QuizMultiChoiceViewHolder holder, final QuizSubmissionQuestion quizSubmissionQuestion, final int position, final Context context, final CanvasWebView.CanvasEmbeddedWebViewCallback embeddedWebViewCallback, final CanvasWebView.CanvasWebViewClientCallback webViewClientCallback) {
    if (holder == null) {
        return;
    }
    holder.question.loadUrl("about:blank");
    holder.question.setBackgroundColor(Color.TRANSPARENT);
    holder.question.setCanvasWebViewClientCallback(webViewClientCallback);
    if (context instanceof Activity) {
        holder.question.addJavascriptInterface(new WebAppInterface(((Activity) context), holder.question), "MyApp");
    } else if (context instanceof ContextThemeWrapper) {
        holder.question.addJavascriptInterface(new WebAppInterface((Activity) (((ContextThemeWrapper) context).getBaseContext()), holder.question), "MyApp");
    }
    holder.question.formatHTML(quizSubmissionQuestion.getQuestionText(), "");
    holder.question.setCanvasEmbeddedWebViewCallback(embeddedWebViewCallback);
    holder.questionNumber.setText(context.getString(R.string.question) + " " + (position + 1));
    holder.questionId = quizSubmissionQuestion.getId();
    LayoutInflater inflater = LayoutInflater.from(context);
    // are any in there
    if (holder.answerContainer.getChildCount() > 0) {
        holder.answerContainer.removeAllViews();
    }
    // add answers to the answer container
    int index = 0;
    for (final QuizSubmissionAnswer answer : quizSubmissionQuestion.getAnswers()) {
        final LinearLayout answerWrapper = (LinearLayout) inflater.inflate(R.layout.quiz_multi_choice_answer, null, false);
        final CanvasWebView webView = (CanvasWebView) answerWrapper.findViewById(R.id.html_answer);
        webView.setClickable(false);
        webView.setFocusableInTouchMode(false);
        final TextView textView = (TextView) answerWrapper.findViewById(R.id.text_answer);
        final CheckBox checkBox = (CheckBox) answerWrapper.findViewById(R.id.answer_checkbox);
        if (!TextUtils.isEmpty(answer.getHtml())) {
            textView.setVisibility(View.GONE);
            final String html = answer.getHtml();
            webView.loadHtml(html, "");
            webView.setBackgroundColor(Color.TRANSPARENT);
            if (quizSubmissionQuestion.getAnswer() != null) {
                if (Long.parseLong((String) quizSubmissionQuestion.getAnswer()) == answer.getId()) {
                    answerWrapper.setBackgroundColor(context.getResources().getColor(R.color.canvasBackgroundMedium));
                    // mark this one as selected
                    checkBox.setChecked(true);
                }
            }
        } else if (!TextUtils.isEmpty(answer.getText())) {
            webView.setVisibility(View.GONE);
            textView.setText(answer.getText());
            if (quizSubmissionQuestion.getAnswer() != null) {
                if (!TextUtils.isEmpty((String) quizSubmissionQuestion.getAnswer()) && Long.parseLong((String) quizSubmissionQuestion.getAnswer()) == answer.getId()) {
                    // mark this one as selected
                    checkBox.setChecked(true);
                    answerWrapper.setBackgroundColor(context.getResources().getColor(R.color.canvasBackgroundMedium));
                }
            }
        }
        holder.answerContainer.addView(answerWrapper);
        if (index == quizSubmissionQuestion.getAnswers().length - 1) {
            // if we're on the last answer remove the bottom divider
            answerWrapper.findViewById(R.id.divider).setVisibility(View.GONE);
        } else {
            answerWrapper.findViewById(R.id.divider).setVisibility(View.VISIBLE);
            index++;
        }
    }
}
Also used : CanvasWebView(com.instructure.pandautils.views.CanvasWebView) ContextThemeWrapper(android.view.ContextThemeWrapper) CheckBox(android.widget.CheckBox) LayoutInflater(android.view.LayoutInflater) QuizSubmissionAnswer(com.instructure.canvasapi2.models.QuizSubmissionAnswer) Activity(android.app.Activity) TextView(android.widget.TextView) LinearLayout(android.widget.LinearLayout)

Example 3 with CanvasWebView

use of com.instructure.pandautils.views.CanvasWebView in project instructure-android by instructure.

the class QuizMultiChoiceBinder method bind.

public static void bind(final QuizMultiChoiceViewHolder holder, final QuizSubmissionQuestion quizSubmissionQuestion, final int courseColor, final int position, final boolean shouldLetAnswer, final Context context, final CanvasWebView.CanvasEmbeddedWebViewCallback embeddedWebViewCallback, final CanvasWebView.CanvasWebViewClientCallback webViewClientCallback, final QuizPostMultiChoice callback, final QuizToggleFlagState flagStateCallback) {
    if (holder == null) {
        return;
    }
    holder.question.loadUrl("about:blank");
    holder.question.setBackgroundColor(Color.TRANSPARENT);
    holder.question.setCanvasWebViewClientCallback(webViewClientCallback);
    holder.question.formatHTML(quizSubmissionQuestion.getQuestionText(), "");
    holder.question.setCanvasEmbeddedWebViewCallback(embeddedWebViewCallback);
    holder.questionNumber.setText(context.getString(R.string.question) + " " + (position + 1));
    holder.questionId = quizSubmissionQuestion.getId();
    LayoutInflater inflater = LayoutInflater.from(context);
    // are any in there
    if (holder.answerContainer.getChildCount() > 0) {
        holder.answerContainer.removeAllViews();
    }
    // Add answers to the answer container
    int index = 0;
    for (final QuizSubmissionAnswer answer : quizSubmissionQuestion.getAnswers()) {
        final LinearLayout answerWrapper = (LinearLayout) inflater.inflate(R.layout.quiz_multi_choice_answer, null, false);
        final CanvasWebView webView = answerWrapper.findViewById(R.id.html_answer);
        webView.setClickable(false);
        webView.setFocusableInTouchMode(false);
        final TextView textView = answerWrapper.findViewById(R.id.text_answer);
        final CheckBox checkBox = answerWrapper.findViewById(R.id.answer_checkbox);
        if (!TextUtils.isEmpty(answer.getHtml())) {
            textView.setVisibility(View.GONE);
            final String html = StringUtilities.trimTrailingWhitespace(answer.getHtml()).toString();
            webView.formatHTML(html, "");
            webView.setBackgroundColor(Color.TRANSPARENT);
            // We only care about marking the answers if they can actually answer
            if (shouldLetAnswer) {
                webView.setOnTouchListener(new View.OnTouchListener() {

                    @Override
                    public boolean onTouch(View view, MotionEvent motionEvent) {
                        // this may or may not be how we do the selection of things when we get the final UI
                        if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
                            answerWrapper.performClick();
                            return true;
                        }
                        return true;
                    }
                });
            }
            if (quizSubmissionQuestion.getAnswer() != null) {
                if (Long.parseLong((String) quizSubmissionQuestion.getAnswer()) == answer.getId()) {
                    answerWrapper.setBackgroundColor(context.getResources().getColor(R.color.canvasBackgroundMedium));
                    // Mark this one as selected
                    checkBox.setChecked(true);
                }
            }
        } else if (!TextUtils.isEmpty(answer.getText())) {
            webView.setVisibility(View.GONE);
            textView.setText(answer.getText());
            if (quizSubmissionQuestion.getAnswer() != null) {
                if (!TextUtils.isEmpty((String) quizSubmissionQuestion.getAnswer()) && Long.parseLong((String) quizSubmissionQuestion.getAnswer()) == answer.getId()) {
                    // mark this one as selected
                    checkBox.setChecked(true);
                    answerWrapper.setBackgroundColor(context.getResources().getColor(R.color.canvasBackgroundMedium));
                }
            }
        }
        if (shouldLetAnswer) {
            answerWrapper.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {
                    for (int i = 0; i < holder.answerContainer.getChildCount(); i++) {
                        if (holder.answerContainer.getId() != view.getId()) {
                            resetViews(i, holder, context);
                        }
                    }
                    checkBox.setChecked(true);
                    answerWrapper.setBackgroundColor(context.getResources().getColor(R.color.canvasBackgroundMedium));
                    // Post the answer to the api
                    callback.postAnswer(holder.questionId, answer.getId());
                    // Set the answer on the quizSubmissionQuestion so we'll remember which question was answered during row recycling
                    quizSubmissionQuestion.setAnswer(Long.toString(answer.getId()));
                }
            });
        }
        final Drawable courseColorFlag = ColorKeeper.getColoredDrawable(context, R.drawable.vd_bookmark_filled, courseColor);
        if (quizSubmissionQuestion.isFlagged()) {
            holder.flag.setImageDrawable(courseColorFlag);
        } else {
            holder.flag.setImageDrawable(ColorKeeper.getColoredDrawable(context, R.drawable.vd_navigation_bookmarks, context.getResources().getColor(R.color.defaultTextGray)));
        }
        if (shouldLetAnswer) {
            holder.flag.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {
                    if (quizSubmissionQuestion.isFlagged()) {
                        // Unflag it
                        holder.flag.setImageDrawable(ColorKeeper.getColoredDrawable(context, R.drawable.vd_navigation_bookmarks, context.getResources().getColor(R.color.defaultTextGray)));
                        flagStateCallback.toggleFlagged(false, quizSubmissionQuestion.getId());
                        quizSubmissionQuestion.setFlagged(false);
                    } else {
                        // Flag it
                        holder.flag.setImageDrawable(courseColorFlag);
                        flagStateCallback.toggleFlagged(true, quizSubmissionQuestion.getId());
                        quizSubmissionQuestion.setFlagged(true);
                    }
                }
            });
        } else {
            holder.flag.setEnabled(false);
        }
        holder.answerContainer.addView(answerWrapper);
        if (index == quizSubmissionQuestion.getAnswers().length - 1) {
            // If we're on the last answer remove the bottom divider
            answerWrapper.findViewById(R.id.divider).setVisibility(View.GONE);
        } else {
            answerWrapper.findViewById(R.id.divider).setVisibility(View.VISIBLE);
            index++;
        }
    }
}
Also used : Drawable(android.graphics.drawable.Drawable) CanvasWebView(com.instructure.pandautils.views.CanvasWebView) TextView(android.widget.TextView) View(android.view.View) MotionEvent(android.view.MotionEvent) CanvasWebView(com.instructure.pandautils.views.CanvasWebView) CheckBox(android.widget.CheckBox) LayoutInflater(android.view.LayoutInflater) QuizSubmissionAnswer(com.instructure.canvasapi2.models.QuizSubmissionAnswer) TextView(android.widget.TextView) LinearLayout(android.widget.LinearLayout)

Example 4 with CanvasWebView

use of com.instructure.pandautils.views.CanvasWebView in project instructure-android by instructure.

the class QuizMultiAnswerBinder method bind.

public static void bind(final QuizMultiChoiceViewHolder holder, final QuizSubmissionQuestion quizSubmissionQuestion, final int position, final Context context) {
    if (holder == null) {
        return;
    }
    holder.question.loadUrl("about:blank");
    holder.question.setBackgroundColor(Color.TRANSPARENT);
    holder.question.setWebViewClient(new WebViewClient() {

        @Override
        public void onPageFinished(WebView view, String url) {
            view.loadUrl("javascript:MyApp.resize(document.body.getBoundingClientRect().height)");
            super.onPageFinished(view, url);
        }
    });
    if (context instanceof Activity) {
        holder.question.addJavascriptInterface(new QuizMultiChoiceBinder.WebAppInterface(((Activity) context), holder.question), "MyApp");
    } else if (context instanceof ContextThemeWrapper) {
        holder.question.addJavascriptInterface(new QuizMultiChoiceBinder.WebAppInterface((Activity) (((ContextThemeWrapper) context).getBaseContext()), holder.question), "MyApp");
    }
    holder.question.loadHtml(quizSubmissionQuestion.getQuestionText(), "");
    holder.questionNumber.setText(quizSubmissionQuestion.getQuestionName() + " " + (position + 1));
    holder.questionId = quizSubmissionQuestion.getId();
    LayoutInflater inflater = LayoutInflater.from(context);
    // are any in there
    if (holder.answerContainer.getChildCount() > 0) {
        holder.answerContainer.removeAllViews();
    }
    // add answers to the answer container
    int index = 0;
    for (final QuizSubmissionAnswer answer : quizSubmissionQuestion.getAnswers()) {
        final LinearLayout answerContainer = (LinearLayout) inflater.inflate(R.layout.quiz_multi_choice_answer, null, false);
        final CanvasWebView webView = (CanvasWebView) answerContainer.findViewById(R.id.html_answer);
        webView.setClickable(false);
        webView.setFocusableInTouchMode(false);
        final TextView textView = (TextView) answerContainer.findViewById(R.id.text_answer);
        final CheckBox checkBox = (CheckBox) answerContainer.findViewById(R.id.answer_checkbox);
        if (!TextUtils.isEmpty(answer.getHtml())) {
            textView.setVisibility(View.GONE);
            final String html = answer.getHtml();
            webView.loadHtml(html, "");
            webView.setBackgroundColor(Color.TRANSPARENT);
        } else if (!TextUtils.isEmpty(answer.getText())) {
            webView.setVisibility(View.GONE);
            textView.setText(answer.getText());
        }
        holder.answerContainer.addView(answerContainer);
        if (index == quizSubmissionQuestion.getAnswers().length - 1) {
            // if we're on the last answer remove the bottom divider
            answerContainer.findViewById(R.id.divider).setVisibility(View.GONE);
        } else {
            answerContainer.findViewById(R.id.divider).setVisibility(View.VISIBLE);
            index++;
        }
    }
}
Also used : Activity(android.app.Activity) CanvasWebView(com.instructure.pandautils.views.CanvasWebView) ContextThemeWrapper(android.view.ContextThemeWrapper) CheckBox(android.widget.CheckBox) LayoutInflater(android.view.LayoutInflater) QuizSubmissionAnswer(com.instructure.canvasapi2.models.QuizSubmissionAnswer) TextView(android.widget.TextView) CanvasWebView(com.instructure.pandautils.views.CanvasWebView) WebView(android.webkit.WebView) LinearLayout(android.widget.LinearLayout) WebViewClient(android.webkit.WebViewClient)

Aggregations

LayoutInflater (android.view.LayoutInflater)4 CheckBox (android.widget.CheckBox)4 LinearLayout (android.widget.LinearLayout)4 TextView (android.widget.TextView)4 QuizSubmissionAnswer (com.instructure.canvasapi2.models.QuizSubmissionAnswer)4 CanvasWebView (com.instructure.pandautils.views.CanvasWebView)4 Activity (android.app.Activity)2 Drawable (android.graphics.drawable.Drawable)2 ContextThemeWrapper (android.view.ContextThemeWrapper)2 MotionEvent (android.view.MotionEvent)2 View (android.view.View)2 WebView (android.webkit.WebView)2 WebViewClient (android.webkit.WebViewClient)2