package com.newsblur.network;

import java.lang.ref.WeakReference;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.widget.ImageView;
import android.widget.Toast;

import com.newsblur.R;
import com.newsblur.domain.Comment;
import com.newsblur.domain.UserDetails;
import com.newsblur.util.PrefsUtils;
import com.newsblur.util.UIUtils;
import com.newsblur.view.FlowLayout;

public class LikeCommentTask extends AsyncTask<Void, Void, Boolean>{
	
	final WeakReference<ImageView> favouriteIconViewHolder;
	
	private final APIManager apiManager;
	private final String storyId;
	private final Comment comment;
	private final String feedId;
	private final Context context;
	private final String userId;
	private Bitmap userImage;
	private WeakReference<FlowLayout> favouriteAvatarHolder;
	private UserDetails user;

	public LikeCommentTask(final Context context, final APIManager apiManager, final ImageView favouriteIcon, final FlowLayout favouriteAvatarLayout, final String storyId, final Comment comment, final String feedId, final String userId) {
		this.apiManager = apiManager;
		this.storyId = storyId;
		this.comment = comment;
		this.feedId = feedId;
		this.context = context;
		this.userId = userId;
		
		favouriteAvatarHolder = new WeakReference<FlowLayout>(favouriteAvatarLayout);
		favouriteIconViewHolder = new WeakReference<ImageView>(favouriteIcon);
		
		userImage = PrefsUtils.getUserImage(context);
		user = PrefsUtils.getUserDetails(context);
	}
	
	@Override
	protected Boolean doInBackground(Void... params) {
		return apiManager.favouriteComment(storyId, comment.userId, feedId);
	}
	
	@Override
	protected void onPostExecute(Boolean result) {
		if (favouriteIconViewHolder.get() != null) {
			if (result.booleanValue()) {
				favouriteIconViewHolder.get().setImageResource(R.drawable.have_favourite);
				
				ImageView favouriteImage = new ImageView(context);
				favouriteImage.setTag(user.id);
				userImage = UIUtils.roundCorners(userImage, 10f);
				favouriteImage.setImageBitmap(userImage);
				favouriteAvatarHolder.get().addView(favouriteImage);
				
				String[] newArray = new String[comment.likingUsers.length + 1];
				System.arraycopy(comment.likingUsers, 0, newArray, 0, comment.likingUsers.length);
				newArray[newArray.length - 1] = userId;
				comment.likingUsers = newArray;
				
				Toast.makeText(context, R.string.comment_favourited, Toast.LENGTH_SHORT).show();
			} else {
				Toast.makeText(context, R.string.error_liking_comment, Toast.LENGTH_SHORT).show();
			}
		}
	}
}

@@ -1873,20 +1873,77 @@
 );%0A%09%09%09%09%0A
-%09%09%09%09
+                if (userImage != null) %7B%0A                    
 ImageVie
@@ -1985,20 +1985,36 @@
 ntext);%0A
-%09%09%09%09
+                    
 favourit
@@ -2037,20 +2037,36 @@
 er.id);%0A
-%09%09%09%09
+                    
 userImag
@@ -2107,20 +2107,36 @@
 , 10f);%0A
-%09%09%09%09
+                    
 favourit
@@ -2169,20 +2169,36 @@
 Image);%0A
-%09%09%09%09
+                    
 favourit
@@ -2238,24 +2238,42 @@
 riteImage);%0A
+                %7D%0A
 %09%09%09%09%0A%09%09%09%09Str

Fix crash on comments by users with no picture.