Search in sources :

Example 16 with ApiType

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

the class AddSubmissionFragment method setUpCallback.

// /////////////////////////////////////////////////////////////////////////
// CallBack
// /////////////////////////////////////////////////////////////////////////
public void setUpCallback() {
    canvasCallbackSubmission = new StatusCallback<Submission>() {

        @Override
        public void onResponse(@NonNull Response<Submission> response, @NonNull LinkHeaders linkHeaders, @NonNull ApiType type) {
            if (!apiCheck()) {
                return;
            }
            Submission result = response.body();
            if (result.getBody() != null || result.getUrl() != null) {
                Toast.makeText(getActivity(), R.string.successPostingSubmission, Toast.LENGTH_LONG).show();
                // clear text fields because they are saved
                textSubmission.setText("");
                urlSubmission.setText("");
                // Send broadcast so list is updated.
                EventBus.getDefault().post(new FileUploadEvent(new FileUploadNotification(null, new ArrayList<Attachment>())));
                Navigation navigation = getNavigation();
                if (navigation != null)
                    navigation.popCurrentFragment();
            } else {
                Toast.makeText(getActivity(), R.string.errorPostingSubmission, Toast.LENGTH_LONG).show();
            }
        }
    };
    mLTIToolCallback = new StatusCallback<List<LTITool>>() {

        @Override
        public void onResponse(@NonNull Response<List<LTITool>> response, @NonNull LinkHeaders linkHeaders, @NonNull ApiType type) {
            for (LTITool ltiTool : response.body()) {
                final String url = ltiTool.getUrl();
                if (url != null && url.contains("instructuremedia.com/lti/launch")) {
                    mArcUpload.setVisibility(View.VISIBLE);
                    mArcLTITool = ltiTool;
                    break;
                }
            }
            // check to see if we should automatically show the file upload dialog
            showFileUploadDialog();
        }

        @Override
        public void onFail(@Nullable Call<List<LTITool>> call, @NonNull Throwable error, @Nullable Response response) {
            // we don't want to show it if this failed due to there being no cache
            if (response != null && response.code() != 504) {
                showFileUploadDialog();
            }
        }
    };
}
Also used : Navigation(com.instructure.interactions.Navigation) Submission(com.instructure.canvasapi2.models.Submission) LTITool(com.instructure.canvasapi2.models.LTITool) LinkHeaders(com.instructure.canvasapi2.utils.LinkHeaders) ArrayList(java.util.ArrayList) FileUploadNotification(com.instructure.pandautils.utils.FileUploadNotification) Response(retrofit2.Response) ApiType(com.instructure.canvasapi2.utils.ApiType) FileUploadEvent(com.instructure.pandautils.utils.FileUploadEvent) List(java.util.List) ArrayList(java.util.ArrayList)

Example 17 with ApiType

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

the class ModuleListRecyclerAdapter method getModuleItemsCallback.

private ModuleItemCallback getModuleItemsCallback(final ModuleObject moduleObject, final boolean isNotifyGroupChange) {
    if (mModuleItemCallbacks.containsKey(moduleObject.getId())) {
        return mModuleItemCallbacks.get(moduleObject.getId());
    } else {
        ModuleItemCallback moduleItemCallback = new ModuleItemCallback(moduleObject) {

            private int checkMasteryPaths(int position, ModuleItem item) {
                if (item.getMasteryPaths() != null && item.getMasteryPaths().isLocked()) {
                    // add another module item that says it's locked
                    ModuleItem masteryPathsLocked = new ModuleItem();
                    // set an id so that if there is more than one path we'll display all of them. otherwise addOrUpdateItem will overwrite it
                    masteryPathsLocked.setId(UUID.randomUUID().getLeastSignificantBits());
                    masteryPathsLocked.setTitle(String.format(Locale.getDefault(), getContext().getString(R.string.locked_mastery_paths), item.getTitle()));
                    masteryPathsLocked.setType(ModuleItem.TYPE.Locked.toString());
                    masteryPathsLocked.setCompletionRequirement(null);
                    masteryPathsLocked.setPosition(position++);
                    addOrUpdateItem(this.getModuleObject(), masteryPathsLocked);
                } else if (item.getMasteryPaths() != null && !item.getMasteryPaths().isLocked() && item.getMasteryPaths().getSelectedSetId() == 0) {
                    // add another module item that says select to choose assignment group
                    // We only want to do this when we have a mastery paths object, it's unlocked, and the user hasn't already selected a set
                    ModuleItem masteryPathsSelect = new ModuleItem();
                    // set an id so that if there is more than one path we'll display all of them. otherwise addOrUpdateItem will overwrite it
                    masteryPathsSelect.setId(UUID.randomUUID().getLeastSignificantBits());
                    masteryPathsSelect.setTitle(getContext().getString(R.string.choose_assignment_group));
                    masteryPathsSelect.setType(ModuleItem.TYPE.ChooseAssignmentGroup.toString());
                    masteryPathsSelect.setCompletionRequirement(null);
                    masteryPathsSelect.setPosition(position++);
                    // sort the mastery paths by position
                    ArrayList<AssignmentSet> assignmentSets = new ArrayList<>();
                    assignmentSets.addAll(Arrays.asList(item.getMasteryPaths().getAssignmentSets()));
                    Collections.sort(assignmentSets, new Comparator<AssignmentSet>() {

                        @Override
                        public int compare(AssignmentSet lh, AssignmentSet rh) {
                            if (lh != null && rh != null) {
                                if (lh.getPosition() < rh.getPosition()) {
                                    return -1;
                                } else if (lh.getPosition() > rh.getPosition()) {
                                    return 1;
                                }
                            }
                            return 0;
                        }
                    });
                    AssignmentSet[] set = new AssignmentSet[assignmentSets.size()];
                    assignmentSets.toArray(set);
                    item.getMasteryPaths().setAssignmentSets(set);
                    masteryPathsSelect.setMasteryPathsItemId(item.getId());
                    masteryPathsSelect.setMasteryPaths(item.getMasteryPaths());
                    addOrUpdateItem(this.getModuleObject(), masteryPathsSelect);
                }
                return position;
            }

            @Override
            public void onResponse(@NonNull Response<List<ModuleItem>> response, @NonNull LinkHeaders linkHeaders, @NonNull ApiType type) {
                List<ModuleItem> moduleItems = response.body();
                if (type == ApiType.API) {
                    int position = (moduleItems.size() > 0 && moduleItems.get(0) != null) ? moduleItems.get(0).getPosition() - 1 : 0;
                    for (ModuleItem item : moduleItems) {
                        item.setPosition(position++);
                        addOrUpdateItem(this.getModuleObject(), item);
                        position = checkMasteryPaths(position, item);
                    }
                    String nextItemsURL = linkHeaders.nextUrl;
                    if (nextItemsURL != null) {
                        ModuleManager.getNextPageModuleItems(nextItemsURL, this, true);
                    }
                    this.setIsFromNetwork(true);
                    expandGroup(this.getModuleObject(), isNotifyGroupChange);
                } else if (type == ApiType.CACHE) {
                    int position = (moduleItems.size() > 0 && moduleItems.get(0) != null) ? moduleItems.get(0).getPosition() - 1 : 0;
                    for (ModuleItem item : moduleItems) {
                        item.setPosition(position++);
                        addOrUpdateItem(this.getModuleObject(), item);
                    }
                    String nextItemsURL = linkHeaders.nextUrl;
                    if (nextItemsURL != null) {
                        ModuleManager.getNextPageModuleItems(nextItemsURL, this, true);
                    }
                    // Wait for the network to expand when there are no items
                    if (moduleItems.size() > 0) {
                        expandGroup(this.getModuleObject(), isNotifyGroupChange);
                    }
                }
            }

            @Override
            public void onFail(@Nullable Call<List<ModuleItem>> call, @NonNull Throwable error, @Nullable Response response) {
                // Only expand if there was no cache result and no network. No connection empty cell will be displayed
                if (response != null && response.code() == 504 && APIHelper.isCachedResponse(response) && getContext() != null && !Utils.isNetworkAvailable(getContext())) {
                    expandGroup(this.getModuleObject(), isNotifyGroupChange);
                }
            }
        };
        mModuleItemCallbacks.put(moduleObject.getId(), moduleItemCallback);
        return moduleItemCallback;
    }
}
Also used : Call(retrofit2.Call) LinkHeaders(com.instructure.canvasapi2.utils.LinkHeaders) ArrayList(java.util.ArrayList) AssignmentSet(com.instructure.canvasapi2.models.AssignmentSet) Comparator(java.util.Comparator) ModuleItem(com.instructure.canvasapi2.models.ModuleItem) Response(retrofit2.Response) NonNull(android.support.annotation.NonNull) ApiType(com.instructure.canvasapi2.utils.ApiType) Nullable(android.support.annotation.Nullable)

Example 18 with ApiType

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

the class QuizSubmissionQuestionListRecyclerAdapter method addMultipleDropdown.

private void addMultipleDropdown(final QuizSubmissionQuestion baseItem, QuizMultipleDropdownViewHolder holder, int position, int courseColor) {
    addAnsweredQuestion(baseItem);
    QuizMultipleDropdownBinder.bind(holder, baseItem, courseColor, position, shouldLetAnswer, getContext(), embeddedWebViewCallback, webViewClientCallback, new QuizPostMultipleDropdown() {

        @Override
        public void postMultipleDropdown(final long questionId, HashMap<String, Long> answers) {
            QuizManager.postQuizQuestionMultipleDropdown(quizSubmission, questionId, answers, true, new StatusCallback<QuizSubmissionQuestionResponse>() {

                @Override
                public void onResponse(@NonNull Response<QuizSubmissionQuestionResponse> response, @NonNull LinkHeaders linkHeaders, @NonNull ApiType type) {
                    if (type == ApiType.CACHE)
                        return;
                    QuizSubmissionQuestionResponse quizSubmissionQuestionResponse = response.body();
                    if (quizSubmissionQuestionResponse.getQuizSubmissionQuestions() != null) {
                        for (QuizSubmissionQuestion question : quizSubmissionQuestionResponse.getQuizSubmissionQuestions()) {
                            if (baseItem.getId() == question.getId()) {
                                baseItem.setAnswer(question.getAnswer());
                            }
                        }
                        // make sure each answer has a match
                        int numAnswers = 0;
                        // API returns a LinkedTreeMap
                        for (String map : ((LinkedTreeMap<String, String>) baseItem.getAnswer()).values()) {
                            if (map != null && !map.equals("null")) {
                                numAnswers++;
                            }
                        }
                        if (numAnswers == ((LinkedTreeMap<String, String>) baseItem.getAnswer()).size()) {
                            addAnsweredQuestion(questionId);
                        } else {
                            removeAnsweredQuestion(questionId);
                        }
                    }
                }
            });
        }
    }, flagStateCallback);
}
Also used : LinkedTreeMap(com.google.gson.internal.LinkedTreeMap) QuizPostMultipleDropdown(com.instructure.candroid.interfaces.QuizPostMultipleDropdown) LinkHeaders(com.instructure.canvasapi2.utils.LinkHeaders) StatusCallback(com.instructure.canvasapi2.StatusCallback) QuizSubmissionQuestion(com.instructure.canvasapi2.models.QuizSubmissionQuestion) QuizSubmissionResponse(com.instructure.canvasapi2.models.QuizSubmissionResponse) Response(retrofit2.Response) QuizSubmissionQuestionResponse(com.instructure.canvasapi2.models.QuizSubmissionQuestionResponse) NonNull(android.support.annotation.NonNull) ApiType(com.instructure.canvasapi2.utils.ApiType) QuizSubmissionQuestionResponse(com.instructure.canvasapi2.models.QuizSubmissionQuestionResponse)

Example 19 with ApiType

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

the class QuizSubmissionQuestionListRecyclerAdapter method addMatchingQuestion.

private void addMatchingQuestion(final QuizSubmissionQuestion baseItem, QuizMatchingViewHolder holder, int position, int courseColor) {
    addAnsweredQuestion(baseItem);
    QuizMatchingBinder.bind(holder, baseItem, courseColor, position, shouldLetAnswer, getContext(), embeddedWebViewCallback, webViewClientCallback, new QuizPostMatching() {

        @Override
        public void postMatching(final long questionId, HashMap<Long, Integer> answers) {
            QuizManager.postQuizQuestionMatching(quizSubmission, questionId, answers, true, new StatusCallback<QuizSubmissionQuestionResponse>() {

                @Override
                public void onResponse(@NonNull Response<QuizSubmissionQuestionResponse> response, @NonNull LinkHeaders linkHeaders, @NonNull ApiType type) {
                    if (type == ApiType.CACHE)
                        return;
                    final QuizSubmissionQuestionResponse quizSubmissionQuestionResponse = response.body();
                    if (quizSubmissionQuestionResponse.getQuizSubmissionQuestions() != null) {
                        for (QuizSubmissionQuestion question : quizSubmissionQuestionResponse.getQuizSubmissionQuestions()) {
                            if (baseItem.getId() == question.getId()) {
                                baseItem.setAnswer(question.getAnswer());
                            }
                        }
                        // make sure each answer has a match
                        int numAnswers = 0;
                        // API returns an ArrayList of LinkedTreeMaps
                        for (LinkedTreeMap<String, String> map : ((ArrayList<LinkedTreeMap<String, String>>) baseItem.getAnswer())) {
                            if (map.get(Const.QUIZ_MATCH_ID) != null && !map.get(Const.QUIZ_MATCH_ID).equals("null")) {
                                numAnswers++;
                            }
                        }
                        if (numAnswers == baseItem.getAnswers().length) {
                            addAnsweredQuestion(questionId);
                        } else {
                            removeAnsweredQuestion(questionId);
                        }
                    }
                }
            });
        }
    }, flagStateCallback);
}
Also used : QuizPostMatching(com.instructure.candroid.interfaces.QuizPostMatching) LinkedTreeMap(com.google.gson.internal.LinkedTreeMap) LinkHeaders(com.instructure.canvasapi2.utils.LinkHeaders) StatusCallback(com.instructure.canvasapi2.StatusCallback) QuizSubmissionQuestion(com.instructure.canvasapi2.models.QuizSubmissionQuestion) QuizSubmissionResponse(com.instructure.canvasapi2.models.QuizSubmissionResponse) Response(retrofit2.Response) QuizSubmissionQuestionResponse(com.instructure.canvasapi2.models.QuizSubmissionQuestionResponse) NonNull(android.support.annotation.NonNull) ApiType(com.instructure.canvasapi2.utils.ApiType) QuizSubmissionQuestionResponse(com.instructure.canvasapi2.models.QuizSubmissionQuestionResponse)

Example 20 with ApiType

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

the class FileFolderManager method getFirstPageFoldersRoot.

public static void getFirstPageFoldersRoot(CanvasContext canvasContext, boolean forceNetwork, final StatusCallback<List<FileFolder>> callback) {
    if (isTesting() || mTesting) {
    // TODO
    } else {
        final RestBuilder adapter = new RestBuilder(callback);
        RestParams params = new RestParams.Builder().withCanvasContext(canvasContext).withForceReadFromNetwork(forceNetwork).withPerPageQueryParam(true).build();
        final RestParams folderParams = new RestParams.Builder().withForceReadFromNetwork(forceNetwork).build();
        FileFolderAPI.getRootFolderForContext(adapter, canvasContext, new StatusCallback<FileFolder>() {

            @Override
            public void onResponse(@NonNull Response<FileFolder> response, @NonNull LinkHeaders linkHeaders, @NonNull ApiType type) {
                FileFolderAPI.getFirstPageFolders(adapter, response.body().getId(), callback, folderParams);
            }
        }, params);
    }
}
Also used : RestParams(com.instructure.canvasapi2.builders.RestParams) LinkHeaders(com.instructure.canvasapi2.utils.LinkHeaders) FileFolder(com.instructure.canvasapi2.models.FileFolder) UpdateFileFolder(com.instructure.canvasapi2.models.UpdateFileFolder) RestBuilder(com.instructure.canvasapi2.builders.RestBuilder) ApiType(com.instructure.canvasapi2.utils.ApiType) RestBuilder(com.instructure.canvasapi2.builders.RestBuilder)

Aggregations

ApiType (com.instructure.canvasapi2.utils.ApiType)23 LinkHeaders (com.instructure.canvasapi2.utils.LinkHeaders)23 Response (retrofit2.Response)15 NonNull (android.support.annotation.NonNull)8 StatusCallback (com.instructure.canvasapi2.StatusCallback)7 ArrayList (java.util.ArrayList)7 FileFolder (com.instructure.canvasapi2.models.FileFolder)6 QuizSubmissionResponse (com.instructure.canvasapi2.models.QuizSubmissionResponse)5 List (java.util.List)5 ResponseBody (okhttp3.ResponseBody)5 RestBuilder (com.instructure.canvasapi2.builders.RestBuilder)4 RestParams (com.instructure.canvasapi2.builders.RestParams)4 QuizSubmissionQuestionResponse (com.instructure.canvasapi2.models.QuizSubmissionQuestionResponse)4 UpdateFileFolder (com.instructure.canvasapi2.models.UpdateFileFolder)4 Bundle (android.os.Bundle)3 PollSessionResponse (com.instructure.canvasapi2.models.PollSessionResponse)3 Nullable (android.support.annotation.Nullable)2 View (android.view.View)2 TextView (android.widget.TextView)2 LinkedTreeMap (com.google.gson.internal.LinkedTreeMap)2