Search in sources :

Example 1 with PollSessionResponse

use of com.instructure.canvasapi2.models.PollSessionResponse 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 2 with PollSessionResponse

use of com.instructure.canvasapi2.models.PollSessionResponse 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 3 with PollSessionResponse

use of com.instructure.canvasapi2.models.PollSessionResponse in project instructure-android by instructure.

the class PollsManager method getFirstPagePollSessions.

public static void getFirstPagePollSessions(long pollId, StatusCallback<PollSessionResponse> callback, boolean forceNetwork) {
    if (isTesting() || mTesting) {
    // TODO:
    } else {
        final RestBuilder adapter = new RestBuilder(callback);
        final RestParams params = new RestParams.Builder().withPerPageQueryParam(true).withShouldIgnoreToken(false).withForceReadFromNetwork(forceNetwork).build();
        PollsSessionAPI.getFirstPagePollSessions(pollId, adapter, params, callback);
    }
}
Also used : RestParams(com.instructure.canvasapi2.builders.RestParams) RestBuilder(com.instructure.canvasapi2.builders.RestBuilder) RestBuilder(com.instructure.canvasapi2.builders.RestBuilder)

Example 4 with PollSessionResponse

use of com.instructure.canvasapi2.models.PollSessionResponse in project instructure-android by instructure.

the class PollsManager method getNextPagePollSessions.

public static void getNextPagePollSessions(String nextUrl, StatusCallback<PollSessionResponse> callback, boolean forceNetwork) {
    if (isTesting() || mTesting) {
    // TODO:
    } else {
        final RestBuilder adapter = new RestBuilder(callback);
        final RestParams params = new RestParams.Builder().withPerPageQueryParam(true).withShouldIgnoreToken(false).withForceReadFromNetwork(forceNetwork).build();
        PollsSessionAPI.getNextPagePollSessions(nextUrl, adapter, params, callback);
    }
}
Also used : RestParams(com.instructure.canvasapi2.builders.RestParams) RestBuilder(com.instructure.canvasapi2.builders.RestBuilder) RestBuilder(com.instructure.canvasapi2.builders.RestBuilder)

Example 5 with PollSessionResponse

use of com.instructure.canvasapi2.models.PollSessionResponse in project instructure-android by instructure.

the class PollsManager method updatePollSession.

public static void updatePollSession(long pollId, long pollSessionId, long courseId, long sectionId, boolean hasPublicResults, StatusCallback<PollSessionResponse> callback, boolean forceNetwork) {
    if (isTesting() || mTesting) {
    // TODO:
    } else {
        final RestBuilder adapter = new RestBuilder(callback);
        final RestParams params = new RestParams.Builder().withPerPageQueryParam(true).withShouldIgnoreToken(false).withForceReadFromNetwork(forceNetwork).build();
        PollsSessionAPI.updatePollSession(pollId, pollSessionId, courseId, sectionId, hasPublicResults, adapter, params, callback);
    }
}
Also used : RestParams(com.instructure.canvasapi2.builders.RestParams) RestBuilder(com.instructure.canvasapi2.builders.RestBuilder) RestBuilder(com.instructure.canvasapi2.builders.RestBuilder)

Aggregations

RestBuilder (com.instructure.canvasapi2.builders.RestBuilder)7 RestParams (com.instructure.canvasapi2.builders.RestParams)7 Bundle (android.os.Bundle)2 PollChoiceResponse (com.instructure.canvasapi2.models.PollChoiceResponse)2 PollSession (com.instructure.canvasapi2.models.PollSession)2 PollSessionResponse (com.instructure.canvasapi2.models.PollSessionResponse)2 ApiType (com.instructure.canvasapi2.utils.ApiType)2 LinkHeaders (com.instructure.canvasapi2.utils.LinkHeaders)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 ResponseBody (okhttp3.ResponseBody)2 Response (retrofit2.Response)2 Poll (com.instructure.canvasapi2.models.Poll)1 PollChoice (com.instructure.canvasapi2.models.PollChoice)1 PollResponse (com.instructure.canvasapi2.models.PollResponse)1 Section (com.instructure.canvasapi2.models.Section)1