Search in sources :

Example 6 with Course

use of com.instructure.canvasapi.model.Course in project instructure-android by instructure.

the class CourseAPI method getAllCoursesSynchronous.

// ///////////////////////////////////////////////////////////////////////////
// Synchronous
// 
// If Retrofit is unable to parse (no network for example) Synchronous calls
// will throw a nullPointer exception. All synchronous calls need to be in a
// try catch block.
// ///////////////////////////////////////////////////////////////////////////
public static Course[] getAllCoursesSynchronous(Context context) {
    RestAdapter restAdapter = CanvasRestAdapter.buildAdapter(context);
    // If not able to parse (no network for example), this will crash. Handle that case.
    try {
        ArrayList<Course> allCourses = new ArrayList<>();
        int page = 1;
        long firstItemId = -1;
        // for(ever) loop. break once we've run outta stuff;
        for (; ; ) {
            Course[] courses = restAdapter.create(CoursesInterface.class).getAllCoursesSynchronous(page);
            page++;
            // This is all or nothing. We don't want partial data.
            if (courses == null) {
                return null;
            } else if (courses.length == 0) {
                break;
            } else if (courses[0].getId() == firstItemId) {
                break;
            } else {
                firstItemId = courses[0].getId();
                Collections.addAll(allCourses, courses);
            }
        }
        return allCourses.toArray(new Course[allCourses.size()]);
    } catch (Exception E) {
        return null;
    }
}
Also used : ArrayList(java.util.ArrayList) Course(com.instructure.canvasapi.model.Course) RestAdapter(retrofit.RestAdapter) CanvasRestAdapter(com.instructure.canvasapi.utilities.CanvasRestAdapter)

Example 7 with Course

use of com.instructure.canvasapi.model.Course in project instructure-android by instructure.

the class CourseListFragment method setupCallbacks.

@Override
public void setupCallbacks() {
    favoriteCoursesCallback = new CanvasCallback<Course[]>(this) {

        @Override
        public void cache(Course[] courses, LinkHeaders linkHeaders, Response response) {
            // keep track of the courses that the user is a teacher in
            for (Course course : courses) {
                if (course.isTeacher() || course.isTA()) {
                    courseGroupMap.put(course.getId(), course);
                    // don't want to make an api call if we already have
                    if (!courseSectionMap.containsKey(course.getId())) {
                        SectionAPI.getCourseSectionsWithStudents(course, sectionCallback);
                    } else {
                        ArrayList<Section> sections = courseSectionMap.get(course.getId());
                        for (Section section : sections) {
                            addItem(course.getName(), section);
                        }
                        expandAllGroups();
                    }
                }
            }
            amendCourseGroupList();
        }

        @Override
        public void firstPage(Course[] courses, LinkHeaders linkHeaders, Response response) {
            // We use get resources, so check for null.
            if (getActivity() == null)
                return;
            cache(courses, linkHeaders, response);
            nextURL = linkHeaders.nextURL;
        }
    };
    sectionCallback = new CanvasCallback<Section[]>(this) {

        @Override
        public void cache(Section[] sections, LinkHeaders linkHeaders, Response response) {
            // add the sections to the course/section map
            if (sections != null && sections.length > 0) {
                long courseId = sections[0].getCourse_id();
                Course c = (Course) courseGroupMap.get(courseId);
                Section allSections = new Section();
                allSections.setId(Long.MIN_VALUE);
                allSections.setName(getString(R.string.allSections));
                allSections.setCourseId(courseId);
                // use a hash set so we don't allow duplicate students
                HashSet<User> allStudents = new HashSet<>();
                for (Section section : sections) {
                    allStudents.addAll(section.getStudents());
                }
                allSections.setStudents(new ArrayList<>(allStudents));
                // if the course section map already has the course in it, we'll want to add to the section list
                if (courseSectionMap.containsKey(courseId)) {
                    ArrayList<Section> currentSections = courseSectionMap.get(courseId);
                    // we want "all sections" to appear at the top, so we'll sort by id (all sections has a small id)
                    TreeSet<Section> currentSectionStudents = new TreeSet<>(new Comparator<Section>() {

                        @Override
                        public int compare(Section section1, Section section2) {
                            if (section1.getId() == Long.MIN_VALUE) {
                                return -1;
                            } else {
                                return section1.getName().compareToIgnoreCase(section2.getName());
                            }
                        }
                    });
                    currentSectionStudents.addAll(currentSections);
                    currentSectionStudents.addAll(Arrays.asList(sections));
                    // add the all sections first if there is more than one section.
                    if (currentSections.size() > 1) {
                        currentSectionStudents.add(allSections);
                    }
                    courseSectionMap.put(courseId, new ArrayList<>(currentSectionStudents));
                } else {
                    courseSectionMap.put(courseId, new ArrayList<>(Arrays.asList(sections)));
                }
                // add the items to the expandable list
                if (c.isTeacher() || c.isTA()) {
                    for (Section section : courseSectionMap.get(courseId)) {
                        addItem(c.getName(), section);
                    }
                }
            }
            expandAllGroups();
        }

        @Override
        public void firstPage(Section[] sections, LinkHeaders linkHeaders, Response response) {
            cache(sections, linkHeaders, response);
            if (linkHeaders.nextURL != null) {
                SectionAPI.getNextPageSectionsList(linkHeaders.nextURL, sectionCallback);
            }
        }
    };
}
Also used : LinkHeaders(com.instructure.canvasapi.utilities.LinkHeaders) ArrayList(java.util.ArrayList) Section(com.instructure.canvasapi.model.Section) Comparator(java.util.Comparator) Response(retrofit.client.Response) TreeSet(java.util.TreeSet) Course(com.instructure.canvasapi.model.Course) HashSet(java.util.HashSet)

Example 8 with Course

use of com.instructure.canvasapi.model.Course in project instructure-android by instructure.

the class StudentChooserFragment method onActivityCreated.

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    if (getArguments() != null) {
        Course course = getArguments().getParcelable(Const.COURSE);
        courseName.setText(course.getName());
        sectionPeople = getArguments().getParcelableArrayList(Const.SECTION_PEOPLE);
        for (User user : sectionPeople) {
            adapter.addItem(user);
        }
        if (adapter.getCount() == 0) {
            showEmptyView();
            emptyView.setText(getString(R.string.noStudents));
        }
        getParentActivity().setActionBarTitle(getString(R.string.studentChooser));
    }
    getCheckedState();
    // initialize the text to speech engine
    tts = new TextToSpeech(getActivity(), new TextToSpeech.OnInitListener() {

        @Override
        public void onInit(int status) {
            if (status == TextToSpeech.SUCCESS) {
                int result = tts.setLanguage(Locale.US);
                if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                    Log.e("error", "This Language is not supported");
                } else {
                    tts.setLanguage(Locale.getDefault());
                }
            } else {
                Log.e("error", "Initilization Failed!");
            }
        }
    });
}
Also used : User(com.instructure.canvasapi.model.User) Course(com.instructure.canvasapi.model.Course) TextToSpeech(android.speech.tts.TextToSpeech)

Example 9 with Course

use of com.instructure.canvasapi.model.Course in project instructure-android by instructure.

the class DocumentActivity method onOptionsItemSelected.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    Submission currentSubmission = getCurrentSubmissionInViewpager();
    int id = item.getItemId();
    switch(id) {
        case android.R.id.home:
            onBackPressed();
            return true;
        case R.id.edit_assignments:
            genericDialog = new EditAssignmentDialog();
            genericDialog.setArguments(EditAssignmentDialog.createBundle(mAssignment, (Course) mCanvasContext));
            genericDialog.show(getSupportFragmentManager(), EditAssignmentDialog.TAG);
            return true;
        case R.id.download:
            if (currentAttachment != null) {
                downloadFile(currentAttachment.getUrl(), currentAttachment.getFilename());
            } else if (currentSubmission.getSubmissionType() != null && currentSubmission.getSubmissionType().equals(mediaUpload)) {
                downloadFile(currentSubmission.getMediaComment().getUrl(), currentSubmission.getMediaComment().getFileName());
            } else {
                Toast.makeText(DocumentActivity.this, getString(R.string.nothingToDownload), Toast.LENGTH_SHORT).show();
            }
            return true;
        case R.id.open_browser:
            if (currentAttachment != null) {
                startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(currentAttachment.getUrl())));
            } else {
                Toast.makeText(DocumentActivity.this, getString(R.string.nothingToShowInBrowser), Toast.LENGTH_SHORT).show();
            }
            return true;
    }
    return super.onOptionsItemSelected(item);
}
Also used : Submission(com.instructure.canvasapi.model.Submission) EditAssignmentDialog(com.instructure.speedgrader.dialogs.EditAssignmentDialog) Intent(android.content.Intent) Course(com.instructure.canvasapi.model.Course)

Aggregations

Course (com.instructure.canvasapi.model.Course)9 Gson (com.google.gson.Gson)3 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)3 User (com.instructure.canvasapi.model.User)2 CanvasRestAdapter (com.instructure.canvasapi.utilities.CanvasRestAdapter)2 RestAdapter (retrofit.RestAdapter)2 Intent (android.content.Intent)1 TextToSpeech (android.speech.tts.TextToSpeech)1 Section (com.instructure.canvasapi.model.Section)1 Submission (com.instructure.canvasapi.model.Submission)1 LinkHeaders (com.instructure.canvasapi.utilities.LinkHeaders)1 EditAssignmentDialog (com.instructure.speedgrader.dialogs.EditAssignmentDialog)1 Comparator (java.util.Comparator)1 HashSet (java.util.HashSet)1 TreeSet (java.util.TreeSet)1 Response (retrofit.client.Response)1