Search in sources :

Example 6 with LinkHeaders

use of com.instructure.canvasapi.utilities.LinkHeaders 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 7 with LinkHeaders

use of com.instructure.canvasapi.utilities.LinkHeaders in project instructure-android by instructure.

the class HomeActivity method setupCallbacks.

private void setupCallbacks() {
    avatarTarget = new Target() {

        @Override
        public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom loadedFrom) {
            avatar.setImageBitmap(bitmap);
        }

        @Override
        public void onBitmapFailed(Drawable drawable) {
            avatar.setImageResource(R.drawable.ic_cv_student);
        }

        @Override
        public void onPrepareLoad(Drawable drawable) {
        }
    };
    userCallback = new UserCallback(this) {

        @Override
        public void cachedUser(User user) {
            if (user != null && user.getAvatarURL() != null) {
                Picasso.with(getContext()).load(user.getAvatarURL()).into(avatarTarget);
                userName.setText(user.getName());
            }
        }

        @Override
        public void user(User user, Response response) {
            Picasso.with(getContext()).load(user.getAvatarURL()).into(avatarTarget);
            userName.setText(user.getName());
        }
    };
    courseColorsCallback = new CanvasCallback<CanvasColor>(this) {

        @Override
        public void cache(CanvasColor canvasColor) {
        }

        @Override
        public void firstPage(CanvasColor canvasColor, LinkHeaders linkHeaders, Response response) {
            if (response.getStatus() == 200) {
                // Replaces the current cache with the updated fresh one from the api.
                CanvasContextColor.addToCache(canvasColor);
                // Sends a broadcast so the course grid can refresh it's colors if needed.
                // When first logging in this will probably get called/return after the courses.
                Intent intent = new Intent(com.instructure.pandautils.utils.Const.COURSE_THING_CHANGED);
                Bundle extras = new Bundle();
                extras.putBoolean(com.instructure.pandautils.utils.Const.COURSE_COLOR, true);
                intent.putExtras(extras);
                LocalBroadcastManager.getInstance(getContext()).sendBroadcast(intent);
            }
        }
    };
}
Also used : User(com.instructure.canvasapi.model.User) LinkHeaders(com.instructure.canvasapi.utilities.LinkHeaders) Bundle(android.os.Bundle) Drawable(android.graphics.drawable.Drawable) Intent(android.content.Intent) Response(retrofit.client.Response) UserCallback(com.instructure.canvasapi.utilities.UserCallback) Target(com.squareup.picasso.Target) Bitmap(android.graphics.Bitmap) CanvasColor(com.instructure.canvasapi.model.CanvasColor) Picasso(com.squareup.picasso.Picasso)

Example 8 with LinkHeaders

use of com.instructure.canvasapi.utilities.LinkHeaders in project instructure-android by instructure.

the class HomeActivity method initMasquerading.

public void initMasquerading() {
    btnMasquerade = (ImageButton) findViewById(R.id.btn_masquerade);
    masquerade = (RelativeLayout) findViewById(R.id.masquerade);
    masqueradeId = (EditText) findViewById(R.id.masqueradeId);
    logoutButton = (RelativeLayout) findViewById(R.id.navigationFooter);
    subHeader = (LinearLayout) findViewById(R.id.subHeader);
    int red = getResources().getColor(R.color.masqueradeRed);
    int blue = getResources().getColor(R.color.speedgrader_aqua);
    // Setup masquerading views
    if (Masquerading.isMasquerading(this)) {
        masquerade.setVisibility(View.VISIBLE);
        masqueradeId.setText(String.valueOf(Masquerading.getMasqueradingId(this)));
        btnMasquerade.setImageDrawable(CanvasContextColor.getColoredDrawable(this, R.drawable.ic_cv_login_x, red));
        subHeader.setBackgroundColor(getResources().getColor(R.color.masqueradeRed));
        navigationHeader.setBackgroundColor(red);
        logoutButton.setBackgroundColor(red);
        setActionBarColor(getResources().getColor(R.color.masqueradeRed));
    } else {
        btnMasquerade.setImageDrawable(CanvasContextColor.getColoredDrawable(this, R.drawable.ic_cv_arrow_right, blue));
        logoutButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.logout_bg));
        navigationHeader.setBackgroundColor(blue);
        setActionBarColor(getResources().getColor(R.color.sg_defaultPrimary));
        subHeader.setBackgroundColor(blue);
    }
    // Set up gesture for the two finger double tap to show the masquerading option
    gesture = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener() {

        public boolean onDown(MotionEvent event) {
            return true;
        }
    });
    gestureListener = new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            return gesture.onTouchEvent(event);
        }
    };
    findViewById(R.id.aboutContent).setOnTouchListener(gestureListener);
    masqueradeId.setOnEditorActionListener(new TextView.OnEditorActionListener() {

        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_GO) {
                btnMasquerade.performClick();
                return true;
            }
            return false;
        }
    });
    btnMasquerade.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            if (Masquerading.isMasquerading(HomeActivity.this)) {
                masquerade.setVisibility(View.GONE);
                masqueradeId.setText("");
                Masquerading.stopMasquerading(HomeActivity.this);
                // delete the cache for the masqueraded user
                File cacheDir = new File(getFilesDir(), "cache_masquerade");
                // need to delete the contents of the internal cache folder so previous user's results don't show up on incorrect user
                com.instructure.canvasapi.utilities.FileUtilities.deleteAllFilesInDirectory(cacheDir);
                // clear any shared preferences for the masqueraded user
                SharedPreferences masq_settings = getSharedPreferences(App.MASQ_PREF_NAME, 0);
                SharedPreferences.Editor masq_editor = masq_settings.edit();
                masq_editor.clear();
                masq_editor.commit();
                reinitViews();
            } else {
                if (masqueradeId.getText().toString().trim().length() == 0) {
                    Toast.makeText(HomeActivity.this, R.string.emptyId, Toast.LENGTH_SHORT).show();
                    return;
                }
                // Actually try to masquerade.
                long id = 0;
                if (masqueradeId.getText().toString().trim().length() > 0) {
                    try {
                        id = Long.parseLong(masqueradeId.getText().toString());
                    } catch (NumberFormatException e) {
                        id = 0;
                    }
                }
                Masquerading.startMasquerading(id, HomeActivity.this, new CanvasCallback<User>(APIHelpers.statusDelegateWithContext(HomeActivity.this)) {

                    @Override
                    public void cache(User user) {
                    }

                    @Override
                    public void firstPage(User user, LinkHeaders linkHeaders, Response response) {
                        // Make sure we got a valid user back.
                        if (user != null && user.getId() > 0) {
                            APIHelpers.setCacheUser(HomeActivity.this, user);
                            reinitViews();
                        } else {
                            onFailure(null);
                        }
                    }

                    // 
                    @Override
                    public boolean onFailure(RetrofitError retrofitError) {
                        Masquerading.stopMasquerading(HomeActivity.this);
                        Toast.makeText(HomeActivity.this, R.string.masqueradeFail, Toast.LENGTH_SHORT).show();
                        return true;
                    }
                }, APIHelpers.getDomain(getContext()));
            }
        }
    });
}
Also used : User(com.instructure.canvasapi.model.User) SharedPreferences(android.content.SharedPreferences) CanvasCallback(com.instructure.canvasapi.utilities.CanvasCallback) LinkHeaders(com.instructure.canvasapi.utilities.LinkHeaders) GestureDetector(android.view.GestureDetector) HelveticaTextView(com.instructure.speedgrader.views.HelveticaTextView) ImageView(android.widget.ImageView) CircleImageView(de.hdodenhof.circleimageview.CircleImageView) View(android.view.View) TextView(android.widget.TextView) ScrollView(android.widget.ScrollView) MotionEvent(android.view.MotionEvent) KeyEvent(android.view.KeyEvent) Response(retrofit.client.Response) HelveticaTextView(com.instructure.speedgrader.views.HelveticaTextView) TextView(android.widget.TextView) File(java.io.File) RetrofitError(retrofit.RetrofitError)

Aggregations

LinkHeaders (com.instructure.canvasapi.utilities.LinkHeaders)8 Response (retrofit.client.Response)8 SharedPreferences (android.content.SharedPreferences)4 User (com.instructure.canvasapi.model.User)4 CanvasCallback (com.instructure.canvasapi.utilities.CanvasCallback)3 RetrofitError (retrofit.RetrofitError)3 Intent (android.content.Intent)2 WebResourceResponse (android.webkit.WebResourceResponse)2 APIStatusDelegate (com.instructure.canvasapi.utilities.APIStatusDelegate)2 UserCallback (com.instructure.canvasapi.utilities.UserCallback)2 PackageInfo (android.content.pm.PackageInfo)1 PackageManager (android.content.pm.PackageManager)1 Bitmap (android.graphics.Bitmap)1 Drawable (android.graphics.drawable.Drawable)1 Bundle (android.os.Bundle)1 GestureDetector (android.view.GestureDetector)1 KeyEvent (android.view.KeyEvent)1 MotionEvent (android.view.MotionEvent)1 View (android.view.View)1 CookieManager (android.webkit.CookieManager)1