Skip to content

Commit

Permalink
caching avatars using urls, not userId
Browse files Browse the repository at this point in the history
  • Loading branch information
Mateusz Herych authored and atermenji committed Jul 23, 2013
1 parent b2a8782 commit 47042f0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
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
26 changes: 16 additions & 10 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 @@ -140,15 +141,19 @@ private Bitmap decode(final File file) {
return BitmapFactory.decodeFile(file.getAbsolutePath(), options);
}

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

/**
* 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 @@ -168,7 +173,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 @@ -232,11 +237,12 @@ public AvatarLoader bind(final ActionBar actionBar,

@Override
public BitmapDrawable call() throws Exception {
final BitmapDrawable image = getImageBy(userId);
final String avatarFilename = getAvatarFilenameForUrl(getAvatarUrl(user));
final BitmapDrawable image = getImageBy(avatarFilename);
if (image != null)
return image;
else
return fetchAvatar(avatarUrl, userId);
return fetchAvatar(avatarUrl, avatarFilename);
}

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

final BitmapDrawable image = getImageBy(userId);
final String avatarFilename = getAvatarFilenameForUrl(avatarUrl);
final BitmapDrawable image = getImageBy(avatarFilename);
if (image != null)
return image;
else
return fetchAvatar(avatarUrl, userId);
return fetchAvatar(avatarUrl, avatarFilename);
}

@Override
protected void onSuccess(final BitmapDrawable image)
throws Exception {
protected void onSuccess(final BitmapDrawable image) throws Exception {
if (image == null)
return;
loaded.put(userId, image);
Expand Down

0 comments on commit 47042f0

Please sign in to comment.