Search in sources :

Example 6 with LinkHeaders

use of com.instructure.canvasapi2.utils.LinkHeaders in project instructure-android by instructure.

the class QuizSubmissionQuestionListRecyclerAdapter method bindTheHolder.

public void bindTheHolder(final QuizSubmissionQuestion baseItem, RecyclerView.ViewHolder holder, int position) {
    int courseColor = ColorKeeper.getOrGenerateColor(canvasContext);
    if (position == super.getItemCount()) {
        // submit button
        SubmitButtonBinder.bind((SubmitButtonViewHolder) holder, getContext(), canvasContext, QuizSubmissionQuestionListRecyclerAdapter.this, new QuizSubmit() {

            @Override
            public void submitQuiz() {
                QuizManager.postQuizSubmit(canvasContext, quizSubmission, true, new StatusCallback<QuizSubmissionResponse>() {

                    @Override
                    public void onResponse(@NonNull Response<QuizSubmissionResponse> response, @NonNull LinkHeaders linkHeaders, @NonNull ApiType type) {
                        if (type == ApiType.CACHE)
                            return;
                        // Submitted!
                        Toast.makeText(getContext(), R.string.quizSubmittedSuccessfully, Toast.LENGTH_SHORT).show();
                        // Go back to the startQuizFragment
                        ((Activity) getContext()).onBackPressed();
                        Fragment fragment = ((NavigationActivity) getContext()).getTopFragment();
                        if (fragment instanceof QuizStartFragment) {
                            ((QuizStartFragment) fragment).updateQuizInfo();
                        }
                    }

                    @Override
                    public void onFail(@Nullable Call<QuizSubmissionResponse> call, @NonNull Throwable error, @Nullable Response response) {
                        Toast.makeText(getContext(), R.string.quizSubmittedFailure, Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
        return;
    }
    switch(baseItem.getQuestionType()) {
        case ESSAY:
        case SHORT_ANSWER:
            addEssayQuestion(baseItem, (QuizEssayViewHolder) holder, position, courseColor);
            break;
        case MUTIPLE_CHOICE:
        case TRUE_FALSE:
            addMultipleChoiceQuestion(baseItem, (QuizMultiChoiceViewHolder) holder, position, courseColor);
            break;
        case TEXT_ONLY:
            QuizTextOnlyBinder.bind((QuizTextOnlyViewHolder) holder, baseItem);
            break;
        case MULTIPLE_ANSWERS:
            addMultipleAnswerQuestion((QuizMultiChoiceViewHolder) holder, position, courseColor);
            break;
        case MATCHING:
            addMatchingQuestion(baseItem, (QuizMatchingViewHolder) holder, position, courseColor);
            break;
        case FILE_UPLOAD:
            addFileUploadQuestion(baseItem, (QuizFileUploadViewHolder) holder, position, courseColor);
            break;
        case NUMERICAL:
            addNumericalQuestion(baseItem, (QuizNumericalViewHolder) holder, position, courseColor);
            break;
        case MULTIPLE_DROPDOWNS:
            addMultipleDropdown(baseItem, (QuizMultipleDropdownViewHolder) holder, position, courseColor);
            break;
    }
}
Also used : Call(retrofit2.Call) LinkHeaders(com.instructure.canvasapi2.utils.LinkHeaders) FragmentActivity(android.support.v4.app.FragmentActivity) NavigationActivity(com.instructure.candroid.activity.NavigationActivity) Activity(android.app.Activity) StatusCallback(com.instructure.canvasapi2.StatusCallback) Fragment(android.support.v4.app.Fragment) InternalWebviewFragment(com.instructure.candroid.fragment.InternalWebviewFragment) QuizStartFragment(com.instructure.candroid.fragment.QuizStartFragment) QuizStartFragment(com.instructure.candroid.fragment.QuizStartFragment) QuizSubmissionResponse(com.instructure.canvasapi2.models.QuizSubmissionResponse) Response(retrofit2.Response) QuizSubmissionQuestionResponse(com.instructure.canvasapi2.models.QuizSubmissionQuestionResponse) QuizSubmit(com.instructure.candroid.interfaces.QuizSubmit) NonNull(android.support.annotation.NonNull) ApiType(com.instructure.canvasapi2.utils.ApiType) NavigationActivity(com.instructure.candroid.activity.NavigationActivity) Nullable(android.support.annotation.Nullable)

Example 7 with LinkHeaders

use of com.instructure.canvasapi2.utils.LinkHeaders in project instructure-android by instructure.

the class PollSessionListFragment method setupCallbacks.

@Override
public void setupCallbacks() {
    pollSessionCallback = new StatusCallback<PollSessionResponse>() {

        @Override
        public void onResponse(@NonNull Response<PollSessionResponse> response, @NonNull LinkHeaders linkHeaders, @NonNull ApiType type) {
            if (getActivity() == null || type.isCache())
                return;
            List<PollSession> pollSessions = response.body().getPollSessions();
            sessionNextUrl = linkHeaders.nextUrl;
            if (pollSessions != null) {
                for (PollSession pollSession : pollSessions) {
                    addItem(pollSession);
                    SectionManager.getSection(pollSession.getCourse_id(), pollSession.getCourse_section_id(), sectionCallback, true);
                }
            }
        }

        @Override
        public void onFinished(ApiType type) {
            if (swipeRefreshLayout != null) {
                swipeRefreshLayout.setRefreshing(false);
            }
        }
    };
    sectionCallback = new StatusCallback<Section>() {

        @Override
        public void onResponse(@NonNull Response<Section> response, @NonNull LinkHeaders linkHeaders, @NonNull ApiType type) {
            if (getActivity() == null || type.isCache())
                return;
            sectionMap.put(response.body().getId(), response.body());
            notifyDataSetChanged();
        }
    };
    pollChoiceCallback = new StatusCallback<PollChoiceResponse>() {

        @Override
        public void onResponse(@NonNull Response<PollChoiceResponse> response, @NonNull LinkHeaders linkHeaders, @NonNull ApiType type) {
            if (getActivity() == null || type.isCache())
                return;
            List<PollChoice> pollChoices = response.body().getPollChoices();
            if (pollChoices != null) {
                pollChoiceArrayList.addAll(pollChoices);
            }
            // or generate the CSV, depending on which action they selected
            if (!StatusCallback.moreCallsExist(linkHeaders)) {
                if (fromGenerateCSV) {
                    // generate a map from the array list of poll choices
                    for (PollChoice choice : pollChoiceArrayList) {
                        pollChoiceMap.put(choice.getId(), choice);
                    }
                    generateCSV();
                } else {
                    AddQuestionFragment addQuestionFragment = new AddQuestionFragment();
                    // populate the current data with the bundle
                    Bundle bundle = new Bundle();
                    bundle.putParcelable(Constants.POLL_BUNDLE, poll);
                    bundle.putParcelableArrayList(Constants.POLL_CHOICES, pollChoiceArrayList);
                    addQuestionFragment.setArguments(bundle);
                    ((FragmentManagerActivity) getActivity()).swapFragments(addQuestionFragment, AddQuestionFragment.class.getSimpleName(), R.anim.slide_in_from_bottom, 0, 0, R.anim.slide_out_to_bottom);
                }
            } else {
                // otherwise, get the next group of poll choices.
                PollsManager.getNextPagePollChoices(linkHeaders.nextUrl, pollChoiceCallback, false);
            }
        }

        @Override
        public void onFinished(ApiType type) {
            if (swipeRefreshLayout != null) {
                swipeRefreshLayout.setRefreshing(false);
            }
        }
    };
    responseCanvasCallback = new StatusCallback<ResponseBody>() {

        @Override
        public void onFail(@Nullable Call<ResponseBody> call, @NonNull Throwable error, @Nullable Response response) {
            AppMsg.makeText(getActivity(), getString(R.string.errorDeletingPollSession), AppMsg.STYLE_ERROR).show();
            // we didn't actually delete anything, but we removed the item from the list to make the animation smoother, so now
            // lets get the poll sessions again
            reloadData();
        }
    };
}
Also used : PollSession(com.instructure.canvasapi2.models.PollSession) PollChoice(com.instructure.canvasapi2.models.PollChoice) LinkHeaders(com.instructure.canvasapi2.utils.LinkHeaders) PollSessionResponse(com.instructure.canvasapi2.models.PollSessionResponse) Bundle(android.os.Bundle) PollChoiceResponse(com.instructure.canvasapi2.models.PollChoiceResponse) Section(com.instructure.canvasapi2.models.Section) ResponseBody(okhttp3.ResponseBody) PollSessionResponse(com.instructure.canvasapi2.models.PollSessionResponse) Response(retrofit2.Response) PollChoiceResponse(com.instructure.canvasapi2.models.PollChoiceResponse) ApiType(com.instructure.canvasapi2.utils.ApiType) ArrayList(java.util.ArrayList) List(java.util.List)

Example 8 with LinkHeaders

use of com.instructure.canvasapi2.utils.LinkHeaders in project instructure-android by instructure.

the class QuestionListFragment method setupCallbacks.

@Override
public void setupCallbacks() {
    pollCallback = new StatusCallback<PollResponse>() {

        @Override
        public void onResponse(@NonNull Response<PollResponse> response, @NonNull LinkHeaders linkHeaders, @NonNull ApiType type) {
            if (getActivity() == null || type.isCache())
                return;
            nextUrl = linkHeaders.nextUrl;
            if (response.body().getPolls().size() == 0) {
                displayEmptyState();
            } else {
                List<Poll> polls = response.body().getPolls();
                for (Poll poll : polls) {
                    // add all the polls to a list. we'll use the list later to populate the
                    // different groups after we get some session information about each poll
                    pollList.add(poll);
                    PollsManager.getFirstPagePollSessions(poll.getId(), pollSessionCallback, true);
                }
            }
        }

        @Override
        public void onFinished(ApiType type) {
            if (swipeRefreshLayout != null) {
                swipeRefreshLayout.setRefreshing(false);
            }
        }
    };
    responseCanvasCallback = new StatusCallback<ResponseBody>() {

        @Override
        public void onResponse(@NonNull Response<ResponseBody> response, @NonNull LinkHeaders linkHeaders, @NonNull ApiType type) {
            if (response.code() == 204) {
                if (pollToDelete != null) {
                    // reset it so we don't try to remove it from the list again
                    pollToDelete = null;
                }
            }
        }

        @Override
        public void onFail(@Nullable Call<ResponseBody> call, @NonNull Throwable error, @Nullable Response response) {
            AppMsg.makeText(getActivity(), getString(R.string.errorDeletingPoll), AppMsg.STYLE_ERROR).show();
            // we didn't actually delete anything, but we removed the item from the list to make the animation smoother, so now
            // lets get the polls again
            reloadData();
        }
    };
    pollSessionCallback = new StatusCallback<PollSessionResponse>() {

        @Override
        public void onResponse(@NonNull Response<PollSessionResponse> response, @NonNull LinkHeaders linkHeaders, @NonNull ApiType type) {
            if (getActivity() == null || type.isCache())
                return;
            List<PollSession> pollSessions = response.body().getPollSessions();
            for (PollSession session : pollSessions) {
                if (session.is_published()) {
                    openSessions.put(session.getPoll_id(), session);
                    // we only care about there being one active poll session
                    break;
                } else {
                    closedSessions.put(session.getPoll_id(), session);
                }
            }
            // and add it to the "active" group
            for (Poll poll : pollList) {
                if (openSessions.containsKey(poll.getId())) {
                    removeItem(poll);
                    addItem(getString(R.string.active), poll);
                } else // if the poll doesn't have an open session or any closed sessions, it is still in the draft state
                if (!closedSessions.containsKey(poll.getId())) {
                    removeItem(poll);
                    addItem(getString(R.string.draft), poll);
                } else {
                    removeItem(poll);
                    addItem(getString(R.string.inactive), poll);
                }
            }
            expandAllGroups();
            if (linkHeaders.nextUrl != null) {
                PollsManager.getNextPagePollSessions(linkHeaders.nextUrl, pollSessionCallback, true);
            }
            notifyDataSetChanged();
        }

        @Override
        public void onFinished(ApiType type) {
            if (swipeRefreshLayout != null) {
                swipeRefreshLayout.setRefreshing(false);
            }
        }
    };
    pollChoiceCallback = new StatusCallback<PollChoiceResponse>() {

        @Override
        public void onResponse(@NonNull Response<PollChoiceResponse> response, @NonNull LinkHeaders linkHeaders, @NonNull ApiType type) {
            if (getActivity() == null || type.isCache())
                return;
            List<PollChoice> pollChoices = response.body().getPollChoices();
            if (pollChoices != null) {
                pollChoiceArrayList.addAll(pollChoices);
            }
            // or generate the CSV, depending on which action they selected
            if (!StatusCallback.moreCallsExist(linkHeaders)) {
                AddQuestionFragment addQuestionFragment = new AddQuestionFragment();
                // populate the current data with the bundle
                Bundle bundle = new Bundle();
                bundle.putParcelable(Constants.POLL_BUNDLE, selectedPoll);
                bundle.putParcelableArrayList(Constants.POLL_CHOICES, pollChoiceArrayList);
                addQuestionFragment.setArguments(bundle);
                ((FragmentManagerActivity) getActivity()).swapFragments(addQuestionFragment, AddQuestionFragment.class.getSimpleName());
            } else {
                // otherwise, get the next group of poll choices.
                PollsManager.getNextPagePollChoices(linkHeaders.nextUrl, pollChoiceCallback, true);
            }
        }

        @Override
        public void onFinished(ApiType type) {
            if (swipeRefreshLayout != null) {
                swipeRefreshLayout.setRefreshing(false);
            }
        }
    };
}
Also used : PollSession(com.instructure.canvasapi2.models.PollSession) LinkHeaders(com.instructure.canvasapi2.utils.LinkHeaders) PollSessionResponse(com.instructure.canvasapi2.models.PollSessionResponse) Bundle(android.os.Bundle) PollChoiceResponse(com.instructure.canvasapi2.models.PollChoiceResponse) ResponseBody(okhttp3.ResponseBody) PollSessionResponse(com.instructure.canvasapi2.models.PollSessionResponse) PollResponse(com.instructure.canvasapi2.models.PollResponse) Response(retrofit2.Response) PollChoiceResponse(com.instructure.canvasapi2.models.PollChoiceResponse) ApiType(com.instructure.canvasapi2.utils.ApiType) PollResponse(com.instructure.canvasapi2.models.PollResponse) Poll(com.instructure.canvasapi2.models.Poll) ArrayList(java.util.ArrayList) List(java.util.List)

Example 9 with LinkHeaders

use of com.instructure.canvasapi2.utils.LinkHeaders in project instructure-android by instructure.

the class QuizQuestionsFragment method setupCallbacks.

private void setupCallbacks() {
    quizFileUploadListener = new QuizFileUploadListener() {

        @Override
        public void onFileUploadClicked(long quizQuestionId, int position) {
            Bundle bundle = UploadFilesDialog.createQuizFileBundle(quizQuestionId, course.getId(), quiz.getId(), position);
            UploadFilesDialog.show(getFragmentManager(), bundle, new Function1<Integer, Unit>() {

                @Override
                public Unit invoke(Integer integer) {
                    return null;
                }
            });
        }
    };
    quizSubmissionQuestionResponseCanvasCallback = new StatusCallback<QuizSubmissionQuestionResponse>() {

        @Override
        public void onResponse(@NonNull Response<QuizSubmissionQuestionResponse> response, @NonNull LinkHeaders linkHeaders, @NonNull ApiType type) {
            if (type == ApiType.CACHE)
                return;
            List<QuizSubmissionQuestion> questions = response.body().getQuizSubmissionQuestions();
            // sort the questions based on position that is part of the model object
            if (questions != null) {
                Collections.sort(questions, new Comparator<QuizSubmissionQuestion>() {

                    @Override
                    public int compare(QuizSubmissionQuestion lh, QuizSubmissionQuestion rh) {
                        if (lh != null && rh != null) {
                            if (lh.getPosition() < rh.getPosition()) {
                                return -1;
                            } else if (lh.getPosition() > rh.getPosition()) {
                                return 1;
                            }
                        }
                        return 0;
                    }
                });
            }
            if (quizQuestionAdapter == null) {
                quizQuestionAdapter = new QuizSubmissionQuestionListRecyclerAdapter(getActivity(), questions, course, shouldLetAnswer, quizSubmission, quizFileUploadListener);
            } else {
                quizQuestionAdapter.addAll(questions);
            }
            // cache the views after we view them
            recyclerView.setItemViewCacheSize(response.body().getQuizSubmissionQuestions().size());
            recyclerView.setAdapter(quizQuestionAdapter);
            if (linkHeaders.nextUrl != null) {
                QuizManager.getNextPageSubmissionQuestions(linkHeaders.nextUrl, false, quizSubmissionQuestionResponseCanvasCallback);
            }
        }
    };
    submitQuizCallback = new StatusCallback<QuizSubmissionResponse>() {

        @Override
        public void onResponse(@NonNull Response<QuizSubmissionResponse> response, @NonNull LinkHeaders linkHeaders, @NonNull ApiType type) {
            if (type == ApiType.CACHE)
                return;
            switch(auto_submit_reason) {
                case TIMED_QUIZ:
                    showToast(R.string.submitReasonTimedQuiz);
                    break;
                case DUE_DATE:
                    showToast(R.string.quizSubmittedSuccessfully);
                    break;
                case LOCK_DATE:
                    showToast(R.string.submitReasonLockAt);
                    break;
                default:
                    showToast(R.string.quizSubmittedSuccessfully);
            }
            if (chronometer != null) {
                chronometer.stop();
            }
            if (countDownTimer != null) {
                countDownTimer.cancel();
            }
            getActivity().onBackPressed();
        }
    };
    quizSubmissionTimeCanvasCallback = new StatusCallback<QuizSubmissionTime>() {

        @Override
        public void onResponse(@NonNull Response<QuizSubmissionTime> response, @NonNull LinkHeaders linkHeaders, @NonNull ApiType type) {
            if (type == ApiType.CACHE)
                return;
            mQuizSubmissionTime = response.body();
            if (shouldLetAnswer && isAdded()) {
                if (quiz.getTimeLimit() == 0 && quiz.getDueAt() == null && quiz.getLockAt() == null) {
                    normalTimer();
                } else if (quiz.getTimeLimit() > 0) {
                    timeLimitCountDown(mQuizSubmissionTime.getTimeLeft());
                } else if (quiz.getDueAt() != null && (quiz.getLockAt() == null || quiz.getDueAt().before(quiz.getLockAt()))) {
                    // if we have a due date, we want to give them an option to submit it when it gets to that point, but only if the due date is before the lock date
                    auto_submit_reason = AUTO_SUBMIT_REASON.DUE_DATE;
                    countDownForSubmit(mQuizSubmissionTime.getTimeLeft() * MILLISECOND);
                } else if (quiz.getLockAt() != null) {
                    auto_submit_reason = AUTO_SUBMIT_REASON.LOCK_DATE;
                    countDownForSubmit(mQuizSubmissionTime.getTimeLeft() * MILLISECOND);
                }
            } else {
                int minutes = (int) Math.ceil((double) (quizSubmission.getTimeSpent()) / 60);
                chronometer.setVisibility(View.GONE);
                timer.setText(String.format(Locale.getDefault(), getString(R.string.timeSpentFormat), minutes));
            }
        }
    };
}
Also used : QuizSubmissionTime(com.instructure.canvasapi2.models.QuizSubmissionTime) LinkHeaders(com.instructure.canvasapi2.utils.LinkHeaders) Bundle(android.os.Bundle) QuizFileUploadListener(com.instructure.candroid.interfaces.QuizFileUploadListener) Function1(kotlin.jvm.functions.Function1) Comparator(java.util.Comparator) QuizSubmissionQuestion(com.instructure.canvasapi2.models.QuizSubmissionQuestion) ApiType(com.instructure.canvasapi2.utils.ApiType) QuizSubmissionQuestionResponse(com.instructure.canvasapi2.models.QuizSubmissionQuestionResponse) List(java.util.List) ArrayList(java.util.ArrayList) QuizSubmissionQuestionListRecyclerAdapter(com.instructure.candroid.adapter.QuizSubmissionQuestionListRecyclerAdapter) QuizSubmissionResponse(com.instructure.canvasapi2.models.QuizSubmissionResponse)

Example 10 with LinkHeaders

use of com.instructure.canvasapi2.utils.LinkHeaders in project instructure-android by instructure.

the class QuizManager_Test method getQuizQuestions.

public static void getQuizQuestions(long courseId, long quizId, StatusCallback<List<QuizQuestion>> callback) {
    // TODO:
    Response response = new Response.Builder().code(200).message("todo").protocol(Protocol.HTTP_1_0).body(ResponseBody.create(MediaType.parse("application/json"), "todo".getBytes())).addHeader("content-type", "application/json").build();
    List<QuizQuestion> quizQuestions = new ArrayList<>();
    retrofit2.Response<List<QuizQuestion>> response1 = retrofit2.Response.success(quizQuestions, response);
    callback.onResponse(response1, new LinkHeaders(), ApiType.CACHE);
}
Also used : QuizSubmissionResponse(com.instructure.canvasapi2.models.QuizSubmissionResponse) Response(okhttp3.Response) QuizSubmissionQuestionResponse(com.instructure.canvasapi2.models.QuizSubmissionQuestionResponse) LinkHeaders(com.instructure.canvasapi2.utils.LinkHeaders) QuizQuestion(com.instructure.canvasapi2.models.QuizQuestion) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

LinkHeaders (com.instructure.canvasapi2.utils.LinkHeaders)87 Response (okhttp3.Response)57 ArrayList (java.util.ArrayList)31 Request (okhttp3.Request)27 List (java.util.List)25 RestBuilder (com.instructure.canvasapi2.builders.RestBuilder)24 ApiType (com.instructure.canvasapi2.utils.ApiType)23 Response (retrofit2.Response)15 QuizSubmissionResponse (com.instructure.canvasapi2.models.QuizSubmissionResponse)13 QuizSubmissionQuestionResponse (com.instructure.canvasapi2.models.QuizSubmissionQuestionResponse)12 ParentResponse (com.instructure.canvasapi2.models.ParentResponse)10 Conversation (com.instructure.canvasapi2.models.Conversation)9 NonNull (android.support.annotation.NonNull)8 GradingPeriodResponse (com.instructure.canvasapi2.models.GradingPeriodResponse)8 StatusCallback (com.instructure.canvasapi2.StatusCallback)7 FileFolder (com.instructure.canvasapi2.models.FileFolder)6 Submission (com.instructure.canvasapi2.models.Submission)6 Assignment (com.instructure.canvasapi2.models.Assignment)5 CourseNickname (com.instructure.canvasapi2.models.CourseNickname)5 ResponseBody (okhttp3.ResponseBody)5