Search in sources :

Example 21 with User

use of com.instructure.canvasapi.model.User 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 22 with User

use of com.instructure.canvasapi.model.User in project instructure-android by instructure.

the class DocumentActivity method groupSubmissionsByGroupId.

public List<Submission> groupSubmissionsByGroupId(Submission[] submissions, User[] courseStudents) {
    LongSparseArray<Group> groupMap = new LongSparseArray<>();
    LongSparseArray<Group> studentGroupMap = new LongSparseArray<>();
    // Map students to groups
    for (Group group : mGroups) {
        groupMap.put(group.getId(), group);
        for (User user : group.getUsers()) {
            studentGroupMap.put(user.getId(), group);
        }
    }
    // Get list of unassigned students
    LongSparseArray<User> unassignedStudents = new LongSparseArray<>();
    for (User student : courseStudents) {
        if (studentGroupMap.indexOfKey(student.getId()) < 0) {
            unassignedStudents.put(student.getId(), student);
        }
    }
    LongSparseArray<Submission> groupSubmissionMap = new LongSparseArray<>();
    for (Group group : mGroups) groupSubmissionMap.put(group.getId(), null);
    List<Submission> individualSubmissions = new ArrayList<>();
    List<Submission> emptyGroupSubmissions = new ArrayList<>();
    List<Submission> emptyIndividualSubmissions = new ArrayList<>();
    for (Submission submission : submissions) {
        // Group submissions by group ID
        long groupId = submission.getGroup().getId();
        // Check for and fix missing group info
        if (groupId == 0) {
            long userId = submission.getUser_id();
            if (studentGroupMap.get(userId) != null) {
                Group group = studentGroupMap.get(userId);
                submission.setGroup(group);
                groupId = group.getId();
            }
        }
        if (groupSubmissionMap.get(groupId) == null) {
            // check if we need to increment the graded count
            if (UserSubmissionsListAdapter.isGraded(submission)) {
                gradedCount++;
            }
            if (groupId != 0) {
                groupSubmissionMap.put(groupId, submission);
            } else {
                // It's possible the submission belongs to a user who doesn't belong to a group
                // in that case, we still want to add that submission to our results and display the username
                individualSubmissions.add(submission);
                unassignedStudents.remove(submission.getUser_id());
            }
        } else if (!UserSubmissionsListAdapter.isGraded(groupSubmissionMap.get(groupId)) && UserSubmissionsListAdapter.isGraded(submission)) {
            groupSubmissionMap.put(groupId, submission);
        }
    }
    List<Submission> result = new ArrayList<>();
    // Add group submissions to result, create empty submissions as necessary
    for (int i = 0; i < groupSubmissionMap.size(); i++) {
        if (groupSubmissionMap.valueAt(i) != null) {
            result.add(groupSubmissionMap.valueAt(i));
        } else {
            Group group = groupMap.get(groupSubmissionMap.keyAt(i));
            emptyGroupSubmissions.add(createEmptySubmissionForGroup(group));
        }
    }
    // Create empty submissions for students not assigned to a group
    for (int i = 0; i < unassignedStudents.size(); i++) {
        emptyIndividualSubmissions.add(createEmptySubmissionForUser(unassignedStudents.valueAt(i)));
    }
    result.addAll(individualSubmissions);
    result.addAll(emptyGroupSubmissions);
    result.addAll(emptyIndividualSubmissions);
    return result;
}
Also used : Group(com.instructure.canvasapi.model.Group) LongSparseArray(android.support.v4.util.LongSparseArray) User(com.instructure.canvasapi.model.User) Submission(com.instructure.canvasapi.model.Submission) ArrayList(java.util.ArrayList)

Example 23 with User

use of com.instructure.canvasapi.model.User 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

User (com.instructure.canvasapi.model.User)23 TextView (android.widget.TextView)4 Gson (com.google.gson.Gson)4 LinkHeaders (com.instructure.canvasapi.utilities.LinkHeaders)4 Response (retrofit.client.Response)4 Intent (android.content.Intent)3 View (android.view.View)3 ImageView (android.widget.ImageView)3 Test (org.junit.Test)3 RetrofitError (retrofit.RetrofitError)3 PackageInfo (android.content.pm.PackageInfo)2 PackageManager (android.content.pm.PackageManager)2 Bundle (android.os.Bundle)2 Course (com.instructure.canvasapi.model.Course)2 Section (com.instructure.canvasapi.model.Section)2 Submission (com.instructure.canvasapi.model.Submission)2 CanvasRestAdapter (com.instructure.canvasapi.utilities.CanvasRestAdapter)2 UserCallback (com.instructure.canvasapi.utilities.UserCallback)2 CircleImageView (de.hdodenhof.circleimageview.CircleImageView)2 RestAdapter (retrofit.RestAdapter)2