Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refreshing avatars #349

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ gen-external-apklibs
*.iml
.DS_Store
*.swp
out
37 changes: 25 additions & 12 deletions app/src/main/java/com/github/mobile/util/AvatarLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.Base64;
import android.util.Log;
import android.widget.ImageView;

Expand Down Expand Up @@ -124,11 +125,11 @@ public AvatarLoader(final Context context) {
/**
* Get image for user
*
* @param userId
* @param user
* @return image
*/
protected BitmapDrawable getImage(final String userId) {
File avatarFile = new File(avatarDir, userId);
protected BitmapDrawable getImage(final User user) {
File avatarFile = new File(avatarDir, getAvatarFilename(user));

if (!avatarFile.exists() || avatarFile.length() == 0)
return null;
Expand All @@ -149,7 +150,7 @@ protected BitmapDrawable getImage(final String userId) {
* @return image
*/
protected BitmapDrawable getImage(final CommitUser user) {
File avatarFile = new File(avatarDir, user.getEmail());
File avatarFile = new File(avatarDir, getAvatarFilename(user));

if (!avatarFile.exists() || avatarFile.length() == 0)
return null;
Expand All @@ -163,6 +164,18 @@ protected BitmapDrawable getImage(final CommitUser user) {
}
}

private String getAvatarFilename(CommitUser user) {
return getAvatarFilenameForUrl(getAvatarUrl(user));
}

private String getAvatarFilename(User user) {
return getAvatarFilenameForUrl(getAvatarUrl(user));
}

private String getAvatarFilenameForUrl(String avatarUrl) {
return Base64.encodeToString(avatarUrl.getBytes(), Base64.DEFAULT);
}

/**
* Decode file to bitmap
*
Expand All @@ -177,11 +190,11 @@ protected Bitmap decode(final File file) {
* Fetch avatar from URL
*
* @param url
* @param userId
* @param cachedAvatarFilename
* @return bitmap
*/
protected BitmapDrawable fetchAvatar(final String url, final String userId) {
File rawAvatar = new File(avatarDir, userId + "-raw");
protected BitmapDrawable fetchAvatar(final String url, final String cachedAvatarFilename) {
File rawAvatar = new File(avatarDir, cachedAvatarFilename + "-raw");
HttpRequest request = HttpRequest.get(url);
if (request.ok())
request.receive(rawAvatar);
Expand All @@ -201,7 +214,7 @@ protected BitmapDrawable fetchAvatar(final String url, final String userId) {
return null;
}

File roundedAvatar = new File(avatarDir, userId);
File roundedAvatar = new File(avatarDir, cachedAvatarFilename);
FileOutputStream output = null;
try {
output = new FileOutputStream(roundedAvatar);
Expand Down Expand Up @@ -265,11 +278,11 @@ public AvatarLoader bind(final ActionBar actionBar,

@Override
public BitmapDrawable call() throws Exception {
final BitmapDrawable image = getImage(userId);
final BitmapDrawable image = getImage(user);
if (image != null)
return image;
else
return fetchAvatar(avatarUrl, userId);
return fetchAvatar(avatarUrl, getAvatarFilename(user));
}

@Override
Expand Down Expand Up @@ -361,11 +374,11 @@ public BitmapDrawable call() throws Exception {
if (!userId.equals(view.getTag(id.iv_avatar)))
return null;

final BitmapDrawable image = getImage(userId);
final BitmapDrawable image = getImage(user);
if (image != null)
return image;
else
return fetchAvatar(loadUrl, userId);
return fetchAvatar(loadUrl, getAvatarFilename(user));
}

@Override
Expand Down