Search in sources :

Example 1 with ChapterModel

use of org.edx.mobile.model.api.ChapterModel in project edx-app-android by edx.

the class CourseAPI method getLiveOrganizedVideosByChapter.

@NonNull
public List<SectionItemInterface> getLiveOrganizedVideosByChapter(@NonNull final String courseId, @NonNull final String chapter) throws Exception {
    CourseComponent course = this.getCourseStructureFromCache(courseId);
    if (course != null) {
        return mappingAllVideoResponseModelFrom(course, new Filter<VideoResponseModel>() {

            @Override
            public boolean apply(VideoResponseModel videoResponseModel) {
                return videoResponseModel != null && videoResponseModel.getChapterName().equals(chapter);
            }
        });
    }
    List<VideoResponseModel> videos = executeStrict(courseService.getVideosByCourseId(courseId));
    ArrayList<SectionItemInterface> list = new ArrayList<SectionItemInterface>();
    // add chapter to the result
    ChapterModel c = new ChapterModel();
    c.name = chapter;
    list.add(c);
    HashMap<String, ArrayList<VideoResponseModel>> sections = new LinkedHashMap<String, ArrayList<VideoResponseModel>>();
    for (VideoResponseModel v : videos) {
        // filter videos by chapter
        if (v.getChapter().getDisplayName().equals(chapter)) {
            // sort out the section of this video
            if (sections.containsKey(v.getSection().getDisplayName())) {
                ArrayList<VideoResponseModel> sv = sections.get(v.getSection().getDisplayName());
                if (sv == null) {
                    sv = new ArrayList<VideoResponseModel>();
                }
                sv.add(v);
            } else {
                ArrayList<VideoResponseModel> vlist = new ArrayList<VideoResponseModel>();
                vlist.add(v);
                sections.put(v.getSection().getDisplayName(), vlist);
            }
        }
    }
    // now add sectioned videos to the result
    for (Map.Entry<String, ArrayList<VideoResponseModel>> entry : sections.entrySet()) {
        // add section to the result
        SectionItemModel s = new SectionItemModel();
        s.name = entry.getKey();
        list.add(s);
        // add videos to the result
        if (entry.getValue() != null) {
            for (VideoResponseModel v : entry.getValue()) {
                list.add(v);
            }
        }
    }
    return list;
}
Also used : ArrayList(java.util.ArrayList) CourseComponent(org.edx.mobile.model.course.CourseComponent) LinkedHashMap(java.util.LinkedHashMap) SectionItemModel(org.edx.mobile.model.api.SectionItemModel) VideoResponseModel(org.edx.mobile.model.api.VideoResponseModel) SectionItemInterface(org.edx.mobile.interfaces.SectionItemInterface) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ChapterModel(org.edx.mobile.model.api.ChapterModel) NonNull(android.support.annotation.NonNull)

Aggregations

NonNull (android.support.annotation.NonNull)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 SectionItemInterface (org.edx.mobile.interfaces.SectionItemInterface)1 ChapterModel (org.edx.mobile.model.api.ChapterModel)1 SectionItemModel (org.edx.mobile.model.api.SectionItemModel)1 VideoResponseModel (org.edx.mobile.model.api.VideoResponseModel)1 CourseComponent (org.edx.mobile.model.course.CourseComponent)1