Search in sources :

Example 1 with CourseComparator

use of ca.etsmtl.applets.etsmobile.util.CourseComparator 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();
    }
}
Also used : ArrayList(java.util.ArrayList) Intent(android.content.Intent) SecurePreferences(ca.etsmtl.applets.etsmobile.util.SecurePreferences) MoodleCourse(ca.etsmtl.applets.etsmobile.model.Moodle.MoodleCourse) View(android.view.View) AdapterView(android.widget.AdapterView) ListView(android.widget.ListView) SpiceException(com.octo.android.robospice.persistence.exception.SpiceException) MoodleCourseActivity(ca.etsmtl.applets.etsmobile.ui.activity.MoodleCourseActivity) MoodleCoursesAdapter(ca.etsmtl.applets.etsmobile.ui.adapter.MoodleCoursesAdapter) CourseComparator(ca.etsmtl.applets.etsmobile.util.CourseComparator) MoodleToken(ca.etsmtl.applets.etsmobile.model.Moodle.MoodleToken) MoodleCourses(ca.etsmtl.applets.etsmobile.model.Moodle.MoodleCourses) AdapterView(android.widget.AdapterView) MoodleProfile(ca.etsmtl.applets.etsmobile.model.Moodle.MoodleProfile)

Aggregations

Intent (android.content.Intent)1 View (android.view.View)1 AdapterView (android.widget.AdapterView)1 ListView (android.widget.ListView)1 MoodleCourse (ca.etsmtl.applets.etsmobile.model.Moodle.MoodleCourse)1 MoodleCourses (ca.etsmtl.applets.etsmobile.model.Moodle.MoodleCourses)1 MoodleProfile (ca.etsmtl.applets.etsmobile.model.Moodle.MoodleProfile)1 MoodleToken (ca.etsmtl.applets.etsmobile.model.Moodle.MoodleToken)1 MoodleCourseActivity (ca.etsmtl.applets.etsmobile.ui.activity.MoodleCourseActivity)1 MoodleCoursesAdapter (ca.etsmtl.applets.etsmobile.ui.adapter.MoodleCoursesAdapter)1 CourseComparator (ca.etsmtl.applets.etsmobile.util.CourseComparator)1 SecurePreferences (ca.etsmtl.applets.etsmobile.util.SecurePreferences)1 SpiceException (com.octo.android.robospice.persistence.exception.SpiceException)1 ArrayList (java.util.ArrayList)1