package net.yeputons.ofeed;

import android.content.Context;
import android.media.Image;
import android.net.Uri;
import android.os.AsyncTask;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.vk.sdk.api.model.VKApiCommunity;
import com.vk.sdk.api.model.VKApiPost;
import com.vk.sdk.api.model.VKApiUser;
import net.yeputons.ofeed.db.WebResourcesCache;
import net.yeputons.ofeed.web.DownloadCompleteListener;
import net.yeputons.ofeed.web.ResourceDownload;
import net.yeputons.ofeed.web.WebResource;

import java.net.URI;
import java.util.Date;

public class PostViewAdapter extends ArrayAdapter<VKApiPost> {
    private final VKApiPost[] values;

    public PostViewAdapter(Context context, VKApiPost[] values) {
        super(context, R.layout.post, values);
        this.values = values;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View postView = View.inflate(getContext(), R.layout.post, null);

        VKApiPost post = values[position];

        ((TextView) postView.findViewById(R.id.postText)).setText(post.text);
        ((TextView) postView.findViewById(R.id.postDate)).setText(new Date(post.date * 1000).toString());

        String imageUri, name;
        if (post.from_id >= 0) {
            VKApiUser user = MainActivity.users.get(post.from_id);
            imageUri = user.photo_100;
            name = user.first_name + " " + user.last_name;
        } else {
            VKApiCommunity group = MainActivity.groups.get(-post.from_id);
            imageUri = group.photo_100;
            name = group.name;
        }
        if (imageUri != null) {
            final ImageView imageView = (ImageView) postView.findViewById(R.id.postAuthorPhoto);
            final WebResource resource = WebResourcesCache.getDownloadingWebResource(URI.create(imageUri));
            resource.setOrRunDownloadCompleteListener(new DownloadCompleteListener() {
                @Override
                public void onDownloadComplete() {
                    final ResourceDownload d = resource.getDownloaded();
                    final Uri resultingUri = d != null ? Uri.fromFile(d.getLocalFile()) : null;
                    imageView.post(new Runnable() {
                        @Override
                        public void run() {
                            if (d == null) {
                                Toast.makeText(getContext(), "Unable to load image", Toast.LENGTH_SHORT).show();
                                return;
                            }
                            imageView.setImageURI(resultingUri);
                        }
                    });
                }
            });
        }
        ((TextView) postView.findViewById(R.id.postAuthorName)).setText(name);
        return postView;
    }
}

@@ -1050,16 +1050,132 @@
 stView =
+ convertView;%0A        if (postView == null %7C%7C postView.findViewById(R.id.postText) == null) %7B%0A            postView =
  View.in
@@ -1215,16 +1215,26 @@
 , null);
+%0A        %7D
 %0A%0A      
@@ -1987,16 +1987,67 @@
 Photo);%0A
+            final String imageUriFinal = imageUri;%0A
         
@@ -2142,20 +2142,111 @@
 imageUri
+Final
 ));%0A
+            imageView.setTag(imageUriFinal);%0A            imageView.setImageURI(null);%0A
         
@@ -2397,24 +2397,141 @@
 omplete() %7B%0A
+                    if (imageView.getTag() != imageUriFinal) %7B%0A                        return;%0A                    %7D%0A
             

make it recycle Views