Started working on displaying user avatars
Currently the downloading and displaying of avatar images is working, however we now need to fetch the author avatar url from the forum. Signed-off-by: Ricky Barrette <rickbarrette@gmail.com>
This commit is contained in:
@@ -0,0 +1,111 @@
|
||||
/***
|
||||
Copyright (c) 2008-2009 CommonsWare, LLC
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
not use this file except in compliance with the License. You may obtain
|
||||
a copy of the License at
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package com.commonsware.cwac.thumbnail;
|
||||
|
||||
import org.RickBarrette.osj.forum.Log;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.ListAdapter;
|
||||
|
||||
import com.commonsware.cwac.adapter.AdapterWrapper;
|
||||
import com.commonsware.cwac.cache.SimpleWebImageCache;
|
||||
|
||||
public class ThumbnailAdapter extends AdapterWrapper {
|
||||
private static final String TAG = "ThumbnailAdapter";
|
||||
private final int[] imageIds;
|
||||
private SimpleWebImageCache<ThumbnailBus, ThumbnailMessage> cache = null;
|
||||
private Activity host = null;
|
||||
|
||||
private final ThumbnailBus.Receiver<ThumbnailMessage> onCache = new ThumbnailBus.Receiver<ThumbnailMessage>() {
|
||||
@Override
|
||||
public void onReceive(final ThumbnailMessage message) {
|
||||
final ImageView image = message.getImageView();
|
||||
|
||||
host.runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (image.getTag() != null && image.getTag().toString().equals(message.getUrl()))
|
||||
image.setImageDrawable(cache.get(message.getUrl()));
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Constructor wrapping a supplied ListAdapter
|
||||
*/
|
||||
public ThumbnailAdapter(final Activity host, final ListAdapter wrapped, final SimpleWebImageCache<ThumbnailBus, ThumbnailMessage> cache, final int[] imageIds) {
|
||||
super(wrapped);
|
||||
|
||||
this.host = host;
|
||||
this.imageIds = imageIds;
|
||||
this.cache = cache;
|
||||
|
||||
cache.getBus().register(getBusKey(), onCache);
|
||||
}
|
||||
|
||||
public void close() {
|
||||
cache.getBus().unregister(onCache);
|
||||
}
|
||||
|
||||
private String getBusKey() {
|
||||
return toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a View that displays the data at the specified position in the data
|
||||
* set. In this case, if we are at the end of the list and we are still in
|
||||
* append mode, we ask for a pending view and return it, plus kick off the
|
||||
* background task to append more data to the wrapped adapter.
|
||||
*
|
||||
* @param position
|
||||
* Position of the item whose data we want
|
||||
* @param convertView
|
||||
* View to recycle, if not null
|
||||
* @param parent
|
||||
* ViewGroup containing the returned View
|
||||
*/
|
||||
@Override
|
||||
public View getView(final int position, final View convertView, final ViewGroup parent) {
|
||||
final View result = super.getView(position, convertView, parent);
|
||||
|
||||
processView(result);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public void processView(final View row) {
|
||||
Log.v(TAG, "Processing View");
|
||||
for (final int imageId : imageIds) {
|
||||
final ImageView image = (ImageView) row.findViewById(imageId);
|
||||
|
||||
if (image != null && image.getTag() != null) {
|
||||
final ThumbnailMessage msg = cache.getBus().createMessage(getBusKey());
|
||||
|
||||
msg.setImageView(image);
|
||||
msg.setUrl(image.getTag().toString());
|
||||
|
||||
try {
|
||||
cache.notify(msg.getUrl(), msg);
|
||||
} catch (final Throwable t) {
|
||||
Log.e(TAG, "Exception trying to fetch image", t);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
/***
|
||||
Copyright (c) 2008-2009 CommonsWare, LLC
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
not use this file except in compliance with the License. You may obtain
|
||||
a copy of the License at
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package com.commonsware.cwac.thumbnail;
|
||||
|
||||
import com.commonsware.cwac.bus.AbstractBus;
|
||||
|
||||
public class ThumbnailBus extends AbstractBus<ThumbnailMessage, String, ThumbnailBus.MatchStrategy> {
|
||||
class MatchStrategy implements AbstractBus.Strategy<ThumbnailMessage, String> {
|
||||
@Override
|
||||
public boolean isMatch(final ThumbnailMessage message, final String filter) {
|
||||
return filter != null && message != null && filter.equals(message.getKey());
|
||||
}
|
||||
}
|
||||
|
||||
public ThumbnailBus() {
|
||||
super();
|
||||
|
||||
setStrategy(new MatchStrategy());
|
||||
}
|
||||
|
||||
public ThumbnailMessage createMessage(final String key) {
|
||||
return new ThumbnailMessage(key);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
/***
|
||||
Copyright (c) 2008-2009 CommonsWare, LLC
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
not use this file except in compliance with the License. You may obtain
|
||||
a copy of the License at
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package com.commonsware.cwac.thumbnail;
|
||||
|
||||
import android.widget.ImageView;
|
||||
|
||||
public class ThumbnailMessage {
|
||||
private final String key;
|
||||
private ImageView image;
|
||||
private String url;
|
||||
|
||||
public ThumbnailMessage(final String key) {
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
public ImageView getImageView() {
|
||||
return image;
|
||||
}
|
||||
|
||||
public String getKey() {
|
||||
return key;
|
||||
}
|
||||
|
||||
public String getUrl() {
|
||||
return url;
|
||||
}
|
||||
|
||||
public void setImageView(final ImageView image) {
|
||||
this.image = image;
|
||||
}
|
||||
|
||||
public void setUrl(final String url) {
|
||||
this.url = url;
|
||||
}
|
||||
}
|
||||
@@ -35,6 +35,10 @@ import android.widget.AbsListView;
|
||||
import android.widget.AdapterView;
|
||||
import android.widget.ListView;
|
||||
|
||||
import com.commonsware.cwac.cache.SimpleWebImageCache;
|
||||
import com.commonsware.cwac.thumbnail.ThumbnailAdapter;
|
||||
import com.commonsware.cwac.thumbnail.ThumbnailBus;
|
||||
import com.commonsware.cwac.thumbnail.ThumbnailMessage;
|
||||
import com.handmark.pulltorefresh.extras.listfragment.PullToRefreshListFragment;
|
||||
import com.handmark.pulltorefresh.library.PullToRefreshBase;
|
||||
|
||||
@@ -173,7 +177,8 @@ public class ForumDetailFragment extends PullToRefreshListFragment implements Da
|
||||
@Override
|
||||
public void run() {
|
||||
TopicContent.getTopics((String) mItem.content.get("forum_id"), mDb);
|
||||
setListAdapter(new TopicAdapter(getActivity()));
|
||||
setListAdapter(new ThumbnailAdapter(getActivity(), new TopicAdapter(getActivity()), new SimpleWebImageCache<ThumbnailBus, ThumbnailMessage>(null, null,
|
||||
101, new ThumbnailBus()), new int[] { R.id.quickContactBadge1 }));
|
||||
getPullToRefreshListView().onRefreshComplete();
|
||||
}
|
||||
});
|
||||
|
||||
@@ -35,6 +35,10 @@ import android.widget.AbsListView;
|
||||
import android.widget.AdapterView;
|
||||
import android.widget.ListView;
|
||||
|
||||
import com.commonsware.cwac.cache.SimpleWebImageCache;
|
||||
import com.commonsware.cwac.thumbnail.ThumbnailAdapter;
|
||||
import com.commonsware.cwac.thumbnail.ThumbnailBus;
|
||||
import com.commonsware.cwac.thumbnail.ThumbnailMessage;
|
||||
import com.handmark.pulltorefresh.extras.listfragment.PullToRefreshListFragment;
|
||||
import com.handmark.pulltorefresh.library.PullToRefreshBase;
|
||||
|
||||
@@ -171,7 +175,8 @@ public class TopicDetailFragment extends PullToRefreshListFragment implements Da
|
||||
@Override
|
||||
public void run() {
|
||||
ThreadContent.getThread((String) mItem.content.get("topic_id"), mDb);
|
||||
setListAdapter(new ThreadAdapter(getActivity()));
|
||||
setListAdapter(new ThumbnailAdapter(getActivity(), new ThreadAdapter(getActivity()), new SimpleWebImageCache<ThumbnailBus, ThumbnailMessage>(null, null,
|
||||
101, new ThumbnailBus()), new int[] { R.id.quickContactBadge1 }));
|
||||
getPullToRefreshListView().onRefreshComplete();
|
||||
}
|
||||
});
|
||||
|
||||
@@ -32,6 +32,11 @@ import android.widget.AbsListView;
|
||||
import android.widget.AdapterView;
|
||||
import android.widget.ListView;
|
||||
|
||||
import com.commonsware.cwac.cache.SimpleWebImageCache;
|
||||
import com.commonsware.cwac.thumbnail.ThumbnailAdapter;
|
||||
import com.commonsware.cwac.thumbnail.ThumbnailBus;
|
||||
import com.commonsware.cwac.thumbnail.ThumbnailMessage;
|
||||
|
||||
public class TopicListFragment extends ListFragment {
|
||||
|
||||
private static final String STATE_ACTIVATED_POSITION = "activated_position";
|
||||
@@ -61,7 +66,8 @@ public class TopicListFragment extends ListFragment {
|
||||
@Override
|
||||
public void onCreate(final Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setListAdapter(new TopicAdapter(getActivity()));
|
||||
setListAdapter(new ThumbnailAdapter(getActivity(), new TopicAdapter(getActivity()), new SimpleWebImageCache<ThumbnailBus, ThumbnailMessage>(null, null, 101,
|
||||
new ThumbnailBus()), new int[] { R.id.quickContactBadge1 }));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -129,6 +129,8 @@ public class ThreadAdapter extends BaseAdapter {
|
||||
holder.title.setText((String) getItem(position).get("post_title"));
|
||||
holder.user.setText((String) getItem(position).get("post_author_name"));
|
||||
holder.post.setText((String) getItem(position).get("post_content"));
|
||||
holder.badage.setImageResource(R.drawable.ic_launcher);
|
||||
holder.badage.setTag("http://www.gravatar.com/avatar/9b4f3d34ca3ce31198efac149c3a1ca2?s=300&d=identicon&r=PG");
|
||||
|
||||
return convertView;
|
||||
}
|
||||
|
||||
@@ -131,6 +131,8 @@ public class TopicAdapter extends BaseAdapter {
|
||||
holder.user.setText((String) getItem(position).get("topic_author_name"));
|
||||
holder.lastestPost.setText((String) getItem(position).get("short_content"));
|
||||
holder.newPosts.setVisibility(Boolean.parseBoolean((String) getItem(position).get("new_post")) ? View.VISIBLE : View.GONE);
|
||||
holder.badage.setImageResource(R.drawable.ic_launcher);
|
||||
holder.badage.setTag("http://www.gravatar.com/avatar/9b4f3d34ca3ce31198efac149c3a1ca2?s=300&d=identicon&r=PG");
|
||||
|
||||
return convertView;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user