Search in sources :

Example 1 with AssignmentSet

use of com.instructure.canvasapi2.models.AssignmentSet 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)

Aggregations

NonNull (android.support.annotation.NonNull)1 Nullable (android.support.annotation.Nullable)1 AssignmentSet (com.instructure.canvasapi2.models.AssignmentSet)1 ModuleItem (com.instructure.canvasapi2.models.ModuleItem)1 ApiType (com.instructure.canvasapi2.utils.ApiType)1 LinkHeaders (com.instructure.canvasapi2.utils.LinkHeaders)1 ArrayList (java.util.ArrayList)1 Comparator (java.util.Comparator)1 Call (retrofit2.Call)1 Response (retrofit2.Response)1