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;
}
}
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);
}
}
};
}
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!");
}
}
});
}
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);
}
Aggregations