Search in sources :

Example 1 with MoodleCourse

use of ca.etsmtl.applets.etsmobile.model.Moodle.MoodleCourse in project ETSMobile-Android2 by ApplETS.

the class MoodleCoursesAdapter method getView.

@SuppressLint("DefaultLocale")
@Override
public View getView(final int position, View view, ViewGroup parent) {
    ViewHolder holder;
    if (view != null) {
        holder = (ViewHolder) view.getTag();
    } else {
        holder = new ViewHolder();
    }
    switch(getItemViewType(position)) {
        case TYPE_COURSE:
            view = inflater.inflate(R.layout.row_moodle_course, parent, false);
            final MoodleCourse item = getItem(position);
            holder.tvCourseName = (TextView) view.findViewById(R.id.tv_moodle_course_name);
            holder.tvCourseSigle = (TextView) view.findViewById(R.id.tv_moodle_course_sigle);
            // Extracts course and group
            Pattern pattern = Pattern.compile("([A-Z]{3,3}\\d{3,3}(-.[^ ]*)?)");
            Matcher matcher = pattern.matcher(item.getFullname());
            if (matcher.find())
                holder.tvCourseSigle.setText(matcher.group(1));
            else {
                holder.tvCourseSigle.setText(item.getFullname());
                view.setTag(holder);
                break;
            }
            //Extracts course's full name and session
            pattern = Pattern.compile("(?:[^ ]* )(.*)");
            matcher = pattern.matcher(item.getFullname());
            if (matcher.find())
                holder.tvCourseName.setText(matcher.group(1).replace("(", "{").split("\\{")[0]);
            else {
                holder.tvCourseName.setText(item.getFullname());
            }
            view.setTag(holder);
            break;
        case TYPE_SEPARATOR:
            view = inflater.inflate(R.layout.list_separator_moodle, parent, false);
            holder.tvCourseSeperator = (TextView) view.findViewById(R.id.textViewSeparator);
            holder.tvCourseSeperator.setText(getItem(position).getFullname());
            view.setTag(holder);
            break;
    }
    return view;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) MoodleCourse(ca.etsmtl.applets.etsmobile.model.Moodle.MoodleCourse) SuppressLint(android.annotation.SuppressLint)

Example 2 with MoodleCourse

use of ca.etsmtl.applets.etsmobile.model.Moodle.MoodleCourse 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

MoodleCourse (ca.etsmtl.applets.etsmobile.model.Moodle.MoodleCourse)2 SuppressLint (android.annotation.SuppressLint)1 Intent (android.content.Intent)1 View (android.view.View)1 AdapterView (android.widget.AdapterView)1 ListView (android.widget.ListView)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 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1