Skip to content

Commit 0dc4d6d

Browse files
committed
Move bitmap cache to App and update callsites
1 parent 818f78a commit 0dc4d6d

File tree

5 files changed

+30
-13
lines changed

5 files changed

+30
-13
lines changed

src/AndroidClient/androidchat/src/main/java/servicestack/net/androidchat/App.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22

33
import android.app.Application;
44
import android.content.Context;
5+
import android.graphics.Bitmap;
6+
import android.support.v4.util.LruCache;
57

68
import net.servicestack.android.AndroidServerEventsClient;
79
import net.servicestack.android.AndroidServiceClient;
10+
import net.servicestack.android.AsyncUtils;
11+
import net.servicestack.client.AsyncSuccess;
812

913
/**
1014
* Created by mythz on 2/19/2017.
@@ -16,6 +20,11 @@ public class App extends Application {
1620
private Context context;
1721
private AndroidServerEventsClient serverEventsClient;
1822
private static App instance;
23+
private LruCache bitmapCache = new LruCache(4 * 1024 * 1024) {// 4MiB
24+
protected int sizeOf(String key, Bitmap value) {
25+
return value.getByteCount();
26+
}
27+
};
1928

2029
public App() {}
2130

@@ -44,4 +53,21 @@ public AndroidServerEventsClient getServerEventsClient(){
4453
public AndroidServiceClient getServiceClient(){
4554
return serverEventsClient.getAndroidClient();
4655
}
56+
57+
public void readBitmap(final String url, final AsyncSuccess<Bitmap> success){
58+
Bitmap cachedBitmap = (Bitmap)bitmapCache.get(url);
59+
if (cachedBitmap != null){
60+
success.success(cachedBitmap);
61+
return;
62+
}
63+
64+
AsyncUtils.readBitmap(url, imageBitmap -> {
65+
bitmapCache.put(url, imageBitmap);
66+
success.success(imageBitmap);
67+
},
68+
error -> {
69+
error.printStackTrace();
70+
});
71+
}
72+
4773
}

src/AndroidClient/androidchat/src/main/java/servicestack/net/androidchat/ChatCommandHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public void changeBackground(String message)
126126
? message.substring(4, message.length() - 5)
127127
: message;
128128

129-
AsyncUtils.readBitmap(url, bitmap -> {
129+
App.get().readBitmap(url, bitmap -> {
130130
ImageView chatBackground = (ImageView)parentActivity.findViewById(R.id.chat_background);
131131
parentActivity.runOnUiThread(() -> chatBackground.setImageBitmap(bitmap));
132132
});

src/AndroidClient/androidchat/src/main/java/servicestack/net/androidchat/Extensions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public static void updateUserProfile(ServerEventConnect connectMsg, MainActivity
6565
TextView txtUser = (TextView)activity.findViewById(R.id.txtUserName);
6666
txtUser.setText(connectMsg.getDisplayName());
6767

68-
AsyncUtils.readBitmap(connectMsg.getProfileUrl(), bitmap -> {
68+
App.get().readBitmap(connectMsg.getProfileUrl(), bitmap -> {
6969
ImageView imgProfile = (ImageView)activity.findViewById(R.id.imgProfile);
7070
imgProfile.setImageBitmap(bitmap);
7171
});

src/AndroidClient/androidchat/src/main/java/servicestack/net/androidchat/MainActivity.java

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -141,18 +141,9 @@ protected void onCreate(Bundle savedInstanceState) {
141141
sendButton.setOnClickListener(this::onSendClick);
142142
}
143143

144-
static LruCache bitmapCache = new LruCache(4 * 1024 * 1024) {// 4MiB
145-
protected int sizeOf(String key, Bitmap value) {
146-
return value.getByteCount();
147-
}
148-
};
149-
150144
private void InitDefaultBackground(final ImageView chatBackground){
151145
final String url = "https://servicestack.net/img/slide/image01.jpg";
152-
AsyncUtils.readBitmap(url, imageBitmap -> {
153-
bitmapCache.put(url, imageBitmap);
154-
chatBackground.setImageBitmap(imageBitmap);
155-
});
146+
App.get().readBitmap(url, chatBackground::setImageBitmap);
156147
}
157148

158149
public boolean onChannelClick(MenuItem menuItem)

src/AndroidClient/androidchat/src/main/java/servicestack/net/androidchat/MessageListViewAdapter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public View getView(int position, View convertView, ViewGroup parent) {
6464
label.setText(displayMessage);
6565

6666
ImageView image = (ImageView)row.findViewById(R.id.imgUser);
67-
AsyncUtils.readBitmap(profileUrl, image::setImageBitmap);
67+
App.get().readBitmap(profileUrl, image::setImageBitmap);
6868
return row;
6969
}
7070

0 commit comments

Comments
 (0)