use of ca.etsmtl.applets.etsmobile.model.Moodle.MoodleProfile in project ETSMobile-Android2 by ApplETS.
the class MoodleFragment method onRequestSuccess.
@Override
public void onRequestSuccess(Object o) {
try {
if (o instanceof MoodleToken) {
MoodleToken moodleToken = (MoodleToken) o;
SecurePreferences securePreferences = new SecurePreferences(getActivity());
securePreferences.edit().putString(UserCredentials.MOODLE_TOKEN, moodleToken.getToken()).commit();
ApplicationManager.userCredentials.setMoodleToken(moodleToken.getToken());
if (moodleToken.getToken().equals("")) {
throw new Exception("Impossible de se connecter");
}
queryMoodleProfile(moodleToken);
}
if (o instanceof MoodleProfile) {
MoodleProfile moodleProfile = (MoodleProfile) o;
queryMoodleCourses(moodleProfile);
}
if (o instanceof MoodleCourses) {
MoodleCourses moodleCourses = (MoodleCourses) o;
moodleCoursesAdapter = new MoodleCoursesAdapter(getActivity(), R.layout.row_moodle_course, this);
Collections.sort(moodleCourses, new CourseComparator());
// To get the most current semester first
Collections.reverse(moodleCourses);
String semesterString;
List<String> semesterList = new ArrayList<>();
for (MoodleCourse moodleCourse : moodleCourses) {
if (moodleCourse.getFullname().matches("(.*)([AÉH](\\d){4})(.*)"))
semesterString = moodleCourse.getFullname().replace("(", "{").split("\\{")[1].replace(")", "");
else
semesterString = null;
semesterString = convertSemesterString(semesterString);
if (!semesterList.contains(semesterString)) {
semesterList.add(semesterString);
MoodleCourse courseSemesterSeparator = new MoodleCourse();
courseSemesterSeparator.setCourseSemester(semesterString);
moodleCoursesAdapter.addSectionHeader(courseSemesterSeparator);
moodleCoursesAdapter.addCourse(moodleCourse);
} else
moodleCoursesAdapter.addCourse(moodleCourse);
}
moodleCoursesListView.setAdapter(moodleCoursesAdapter);
moodleCoursesListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
MoodleCourse moodleCourse = (MoodleCourse) parent.getItemAtPosition(position);
if (moodleCourse.getId() != MoodleCourse.IS_SEMESTER) {
Intent i = new Intent(getActivity(), MoodleCourseActivity.class);
i.putExtra("idCours", moodleCourse.getId());
i.putExtra("nameCours", moodleCourse.getShortname());
getActivity().startActivity(i);
}
}
});
super.onRequestSuccess(null);
}
} catch (Exception e) {
Log.w("MoodleFragment", "Exception caught in onRequestSuccess: " + e);
if (getActivity() != null)
Toast.makeText(getActivity(), e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
Aggregations