Search in sources :

Example 16 with ModuleItem

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

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

the class ModuleListRecyclerAdapter method isSequentiallyEnabled.

// endregion
// region Module binder Helpers
private boolean isSequentiallyEnabled(ModuleObject moduleObject, ModuleItem moduleItem) {
    // 0 based) or the previous item is unlocked
    if (mCanvasContext instanceof Course && (((Course) mCanvasContext).isTeacher() || ((Course) mCanvasContext).isTA())) {
        return true;
    }
    if (moduleObject.isSequential_progress() && (moduleObject.getState() != null && (moduleObject.getState().equals(ModuleObject.STATE.unlocked.toString()) || moduleObject.getState().equals(ModuleObject.STATE.started.toString())))) {
        // group is sequential, need to figure out which ones to grey out
        int indexOfCurrentModuleItem = storedIndexOfItem(moduleObject, moduleItem);
        if (indexOfCurrentModuleItem != -1) {
            // getItem performs invalid index checks
            ModuleItem previousModuleItem = getItem(moduleObject, indexOfCurrentModuleItem - 1);
            ModuleItem nextModuleItem = getItem(moduleObject, indexOfCurrentModuleItem + 1);
            if (isComplete(moduleItem)) {
                return true;
            } else if (previousModuleItem == null) {
                // Its the first one in the sequence
                return true;
            } else if (!isComplete(previousModuleItem)) {
                // previous item is not complete
                return false;
            } else if (isComplete(previousModuleItem) && !isComplete(moduleItem)) {
                // previous complete, so show current as next in sequence
                return true;
            } else {
                return false;
            }
        }
    }
    return true;
}
Also used : ModuleItem(com.instructure.canvasapi2.models.ModuleItem) Course(com.instructure.canvasapi2.models.Course)

Aggregations

ModuleItem (com.instructure.canvasapi2.models.ModuleItem)16 Test (org.junit.Test)9 Course (com.instructure.canvasapi2.models.Course)8 Bundle (android.os.Bundle)7 ParentFragment (com.instructure.candroid.fragment.ParentFragment)7 NonNull (android.support.annotation.NonNull)3 View (android.view.View)2 ModuleObject (com.instructure.canvasapi2.models.ModuleObject)2 ArrayList (java.util.ArrayList)2 Drawable (android.graphics.drawable.Drawable)1 Uri (android.net.Uri)1 Nullable (android.support.annotation.Nullable)1 Fragment (android.support.v4.app.Fragment)1 CardView (android.support.v7.widget.CardView)1 ModuleListRecyclerAdapter (com.instructure.candroid.adapter.ModuleListRecyclerAdapter)1 ModuleAdapterToFragmentCallback (com.instructure.candroid.interfaces.ModuleAdapterToFragmentCallback)1 ModuleProgressionUtility (com.instructure.candroid.util.ModuleProgressionUtility)1 AssignmentSet (com.instructure.canvasapi2.models.AssignmentSet)1 ModuleContentDetails (com.instructure.canvasapi2.models.ModuleContentDetails)1 ApiType (com.instructure.canvasapi2.utils.ApiType)1