Search in sources :

Example 26 with CourseComponent

use of org.edx.mobile.model.course.CourseComponent in project edx-app-android by edx.

the class CourseBaseActivityTest method getIntent.

/**
 * {@inheritDoc}
 */
@Override
protected Intent getIntent() {
    EnrolledCoursesResponse courseData;
    try {
        courseData = executeStrict(courseAPI.getEnrolledCourses()).get(0);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    Intent intent = super.getIntent();
    Bundle extras = new Bundle();
    extras.putSerializable(Router.EXTRA_COURSE_DATA, courseData);
    if (provideCourseId) {
        String courseId = courseData.getCourse().getId();
        CourseStructureV1Model model;
        CourseComponent courseComponent;
        try {
            model = executeStrict(courseAPI.getCourseStructure(courseId));
            courseComponent = (CourseComponent) CourseAPI.normalizeCourseStructure(model, courseId);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        extras.putString(Router.EXTRA_COURSE_COMPONENT_ID, courseComponent.getId());
    }
    intent.putExtra(Router.EXTRA_BUNDLE, extras);
    return intent;
}
Also used : Bundle(android.os.Bundle) EnrolledCoursesResponse(org.edx.mobile.model.api.EnrolledCoursesResponse) Intent(android.content.Intent) CourseStructureV1Model(org.edx.mobile.model.course.CourseStructureV1Model) CourseComponent(org.edx.mobile.model.course.CourseComponent)

Example 27 with CourseComponent

use of org.edx.mobile.model.course.CourseComponent in project edx-app-android by edx.

the class CourseOutlineActivityTest method sectionNavigationTest.

// Since Robolectric doesn't simulate actual Activity navigation, we
// can only test forward navigation, and only up to one level. This
// blocks us from testing the back stack restructuring upon switching
// to a different section from CourseUnitNavigationActivityTest.
/**
 * Testing navigation to a section
 */
@Test
public void sectionNavigationTest() {
    Intent intent = getIntent();
    Bundle extras = intent.getBundleExtra(Router.EXTRA_BUNDLE);
    EnrolledCoursesResponse courseData = (EnrolledCoursesResponse) extras.getSerializable(Router.EXTRA_COURSE_DATA);
    assertNotNull(courseData);
    String courseId = courseData.getCourse().getId();
    CourseStructureV1Model model;
    CourseComponent courseComponent;
    try {
        model = executeStrict(courseAPI.getCourseStructure(courseId));
        courseComponent = (CourseComponent) CourseAPI.normalizeCourseStructure(model, courseId);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    int subsectionRowIndex = -1;
    String subsectionId = null;
    CourseComponent subsectionUnit = null;
    List<IBlock> sections = courseComponent.getChildren();
    sectionIteration: for (@SuppressWarnings("unused") IBlock section : sections) {
        subsectionRowIndex++;
        for (IBlock subsection : section.getChildren()) {
            subsectionRowIndex++;
            if (((CourseComponent) subsection).isContainer()) {
                subsectionId = subsection.getId();
                List<CourseComponent> leafComponents = new ArrayList<>();
                courseComponent.fetchAllLeafComponents(leafComponents, EnumSet.allOf(BlockType.class));
                subsectionUnit = leafComponents.get(0);
                break sectionIteration;
            }
        }
    }
    assertNotNull(subsectionId);
    extras.putString(Router.EXTRA_COURSE_COMPONENT_ID, courseComponent.getId());
    ActivityController<? extends CourseOutlineActivity> controller = initialize(intent);
    CourseOutlineActivity activity = controller.get();
    Fragment fragment = activity.getSupportFragmentManager().findFragmentByTag(CourseOutlineFragment.TAG);
    assertThat(fragment).isInstanceOf(CourseOutlineFragment.class);
    CourseOutlineFragment courseOutlineFragment = (CourseOutlineFragment) fragment;
    clickRow(controller, courseOutlineFragment, subsectionRowIndex);
    Intent newIntent = assertNextStartedActivity(activity, CourseOutlineActivity.class);
    Bundle newData = newIntent.getBundleExtra(Router.EXTRA_BUNDLE);
    assertNotNull(newData);
    assertEquals(courseData, newData.getSerializable(Router.EXTRA_COURSE_DATA));
    assertEquals(subsectionId, newData.getString(Router.EXTRA_COURSE_COMPONENT_ID));
    // Back stack reconstruction upon receiving a specific path
    Intent resultData = new Intent();
    resultData.putExtra(Router.EXTRA_COURSE_COMPONENT_ID, subsectionUnit.getId());
    courseOutlineFragment.onActivityResult(CourseOutlineFragment.REQUEST_SHOW_COURSE_UNIT_DETAIL, Activity.RESULT_OK, resultData);
    ShadowActivity shadowActivity = Shadows.shadowOf(activity);
    BlockPath outlinePath = courseComponent.getPath();
    BlockPath leafPath = subsectionUnit.getPath();
    int outlinePathSize = outlinePath.getPath().size();
    for (int i = outlinePathSize + 1; ; i += 2) {
        newIntent = shadowActivity.getNextStartedActivity();
        CourseComponent nextComp = leafPath.get(i);
        if (nextComp == null || !nextComp.isContainer()) {
            assertNull(newIntent);
            break;
        }
        assertNotNull(newIntent);
        assertThat(newIntent).hasComponent(activity, CourseOutlineActivity.class);
        newData = newIntent.getBundleExtra(Router.EXTRA_BUNDLE);
        assertNotNull(newData);
        assertEquals(courseData, newData.getSerializable(Router.EXTRA_COURSE_DATA));
        assertEquals(nextComp.getId(), newData.getString(Router.EXTRA_COURSE_COMPONENT_ID));
    }
}
Also used : IBlock(org.edx.mobile.model.course.IBlock) BlockPath(org.edx.mobile.model.course.BlockPath) Bundle(android.os.Bundle) Intent(android.content.Intent) Fragment(android.support.v4.app.Fragment) CourseComponent(org.edx.mobile.model.course.CourseComponent) EnrolledCoursesResponse(org.edx.mobile.model.api.EnrolledCoursesResponse) ShadowActivity(org.robolectric.shadows.ShadowActivity) ArrayList(java.util.ArrayList) List(java.util.List) CourseStructureV1Model(org.edx.mobile.model.course.CourseStructureV1Model) Test(org.junit.Test)

Example 28 with CourseComponent

use of org.edx.mobile.model.course.CourseComponent in project edx-app-android by edx.

the class CourseOutlineActivityTest method unitNavigationTest.

/**
 * Testing navigation to a unit
 */
@Test
public void unitNavigationTest() {
    Intent intent = getIntent();
    Bundle extras = intent.getBundleExtra(Router.EXTRA_BUNDLE);
    EnrolledCoursesResponse courseData = (EnrolledCoursesResponse) extras.getSerializable(Router.EXTRA_COURSE_DATA);
    assertNotNull(courseData);
    String courseId = courseData.getCourse().getId();
    CourseStructureV1Model model;
    CourseComponent courseComponent;
    try {
        model = executeStrict(courseAPI.getCourseStructure(courseId));
        courseComponent = (CourseComponent) CourseAPI.normalizeCourseStructure(model, courseId);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    List<CourseComponent> leafComponents = new ArrayList<>();
    courseComponent.fetchAllLeafComponents(leafComponents, EnumSet.allOf(BlockType.class));
    CourseComponent courseUnit = leafComponents.get(0);
    CourseComponent lastUnit = leafComponents.get(leafComponents.size() - 1);
    assertNotEquals(lastUnit, courseUnit);
    courseComponent = courseUnit.getParent();
    if (courseUnit.getPath().getPath().size() % 2 > 0) {
        courseComponent = courseComponent.getParent();
    }
    int subsectionRowIndex = -1;
    List<IBlock> sections = courseComponent.getChildren();
    sectionIteration: for (@SuppressWarnings("unused") IBlock section : sections) {
        subsectionRowIndex++;
        if (courseUnit.equals(section)) {
            break;
        }
        for (@SuppressWarnings("unused") IBlock subsection : section.getChildren()) {
            subsectionRowIndex++;
            if (courseUnit.equals(subsection)) {
                break sectionIteration;
            }
        }
    }
    extras.putString(Router.EXTRA_COURSE_COMPONENT_ID, courseComponent.getId());
    ActivityController<? extends CourseOutlineActivity> controller = initialize(intent);
    CourseOutlineActivity activity = controller.get();
    Fragment fragment = activity.getSupportFragmentManager().findFragmentByTag(CourseOutlineFragment.TAG);
    assertThat(fragment).isInstanceOf(CourseOutlineFragment.class);
    CourseOutlineFragment courseOutlineFragment = (CourseOutlineFragment) fragment;
    clickRow(controller, courseOutlineFragment, subsectionRowIndex);
    Intent newIntent = assertNextStartedActivity(activity, CourseUnitNavigationActivity.class);
    Bundle newData = newIntent.getBundleExtra(Router.EXTRA_BUNDLE);
    assertNotNull(newData);
    assertEquals(courseData, newData.getSerializable(Router.EXTRA_COURSE_DATA));
    assertEquals(courseUnit.getId(), newData.getSerializable(Router.EXTRA_COURSE_COMPONENT_ID));
    // Test the back stack reconstruction upon receiving a specific path
    // Should not perform any action if it receives a unit selection from itself
    Intent resultData = new Intent();
    resultData.putExtra(Router.EXTRA_COURSE_COMPONENT_ID, courseUnit.getId());
    courseOutlineFragment.onActivityResult(CourseOutlineFragment.REQUEST_SHOW_COURSE_UNIT_DETAIL, Activity.RESULT_OK, resultData);
    ShadowActivity shadowActivity = Shadows.shadowOf(activity);
    assertNull(shadowActivity.getNextStartedActivity());
    assertFalse(shadowActivity.isFinishing());
    // Should finish itself to start the new navigation back stack if it receives
    // a unit selection from another section
    resultData.putExtra(Router.EXTRA_COURSE_COMPONENT_ID, lastUnit.getId());
    courseOutlineFragment.onActivityResult(CourseOutlineFragment.REQUEST_SHOW_COURSE_UNIT_DETAIL, Activity.RESULT_OK, resultData);
    assertNull(shadowActivity.getNextStartedActivity());
    assertTrue(shadowActivity.isFinishing());
}
Also used : IBlock(org.edx.mobile.model.course.IBlock) Bundle(android.os.Bundle) ArrayList(java.util.ArrayList) Intent(android.content.Intent) Fragment(android.support.v4.app.Fragment) CourseComponent(org.edx.mobile.model.course.CourseComponent) BlockType(org.edx.mobile.model.course.BlockType) EnrolledCoursesResponse(org.edx.mobile.model.api.EnrolledCoursesResponse) ShadowActivity(org.robolectric.shadows.ShadowActivity) CourseStructureV1Model(org.edx.mobile.model.course.CourseStructureV1Model) Test(org.junit.Test)

Example 29 with CourseComponent

use of org.edx.mobile.model.course.CourseComponent in project edx-app-android by edx.

the class CourseAPI method getCourseHierarchy.

@NonNull
public Map<String, SectionEntry> getCourseHierarchy(@NonNull final String courseId) throws Exception {
    CourseComponent course = this.getCourseStructureFromCache(courseId);
    if (course != null) {
        return mappingCourseHierarchyFrom(course);
    }
    List<VideoResponseModel> list = executeStrict(courseService.getVideosByCourseId(courseId));
    // create hierarchy with chapters, sections and subsections
    // HashMap<String, SectionEntry> chapterMap = new HashMap<String, SectionEntry>();
    Map<String, SectionEntry> chapterMap = new LinkedHashMap<String, SectionEntry>();
    for (VideoResponseModel m : list) {
        // add each video to its corresponding chapter and section
        // add this as a chapter
        String cname = m.getChapter().getDisplayName();
        // carry this courseId with video model
        m.setCourseId(courseId);
        SectionEntry s = null;
        if (chapterMap.containsKey(cname)) {
            s = chapterMap.get(cname);
        } else {
            s = new SectionEntry();
            s.chapter = cname;
            s.isChapter = true;
            s.section_url = m.getSectionUrl();
            chapterMap.put(cname, s);
        }
        // add this video to section inside in this chapter
        ArrayList<VideoResponseModel> videos = s.sections.get(m.getSection().getDisplayName());
        if (videos == null) {
            s.sections.put(m.getSection().getDisplayName(), new ArrayList<VideoResponseModel>());
            videos = s.sections.get(m.getSection().getDisplayName());
        }
        videos.add(m);
    }
    return chapterMap;
}
Also used : VideoResponseModel(org.edx.mobile.model.api.VideoResponseModel) SectionEntry(org.edx.mobile.model.api.SectionEntry) CourseComponent(org.edx.mobile.model.course.CourseComponent) LinkedHashMap(java.util.LinkedHashMap) NonNull(android.support.annotation.NonNull)

Example 30 with CourseComponent

use of org.edx.mobile.model.course.CourseComponent 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

CourseComponent (org.edx.mobile.model.course.CourseComponent)43 VideoBlockModel (org.edx.mobile.model.course.VideoBlockModel)10 ArrayList (java.util.ArrayList)9 EnrolledCoursesResponse (org.edx.mobile.model.api.EnrolledCoursesResponse)9 Bundle (android.os.Bundle)8 BlockPath (org.edx.mobile.model.course.BlockPath)8 CourseStructureV1Model (org.edx.mobile.model.course.CourseStructureV1Model)8 Test (org.junit.Test)7 View (android.view.View)6 IBlock (org.edx.mobile.model.course.IBlock)6 Intent (android.content.Intent)5 NonNull (android.support.annotation.NonNull)5 DiscussionBlockModel (org.edx.mobile.model.course.DiscussionBlockModel)5 HtmlBlockModel (org.edx.mobile.model.course.HtmlBlockModel)5 TextView (android.widget.TextView)4 IconImageView (com.joanzapata.iconify.widget.IconImageView)4 LinkedHashMap (java.util.LinkedHashMap)4 Fragment (android.support.v4.app.Fragment)3 HashMap (java.util.HashMap)3 CourseContentNotValidException (org.edx.mobile.exception.CourseContentNotValidException)3