package com.bignerdranch.android.photogallery;

import android.graphics.drawable.Drawable;
import android.graphics.drawable.DrawableWrapper;
import android.media.Image;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class PhotoGalleryFragment extends Fragment {

    private static final String TAG = "PhotoGalleryFragment";

    private RecyclerView mPhotoRecyclerView;
    private List<GalleryItem> mItems = new ArrayList<>();
    private ThumbnailDownloader<PhotoHolder> mThumbnailDownloader;


    private class FetchItemsTasks extends AsyncTask<Void, Void, List<GalleryItem>> {
        @Override protected List<GalleryItem> doInBackground(Void... params) {
            return new FlickrFetcher().fetchItems();
        }

        @Override
        protected void onPostExecute(List<GalleryItem> galleryItems) {
            mItems = galleryItems;
            setupAdapter();
        }
    }

    public static PhotoGalleryFragment newInstance() {

        Bundle args = new Bundle();

        PhotoGalleryFragment fragment = new PhotoGalleryFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
        new FetchItemsTasks().execute();

        mThumbnailDownloader = new ThumbnailDownloader<>();
        mThumbnailDownloader.start();
        mThumbnailDownloader.getLooper();
        Log.i(TAG, "Background thread started");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mThumbnailDownloader.quit();
        Log.i(TAG, "Background thread destroyed");
    }

    @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_photo_gallery, container, false);

        mPhotoRecyclerView = (RecyclerView) view.findViewById(R.id.fragment_photo_gallery_recycler_view);
        mPhotoRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 3));

        setupAdapter();

        return view;
    }

    private void setupAdapter() {
        if (isAdded()) {
            mPhotoRecyclerView.setAdapter(new PhotoAdapter(mItems));
        }
    }

    private class PhotoHolder extends RecyclerView.ViewHolder {

        private ImageView mItemImageView;

        public PhotoHolder(View itemView) {
            super(itemView);
            mItemImageView = (ImageView) itemView.findViewById(R.id.fragment_photo_gallery_image_view);
        }

        public void bindDrawable(Drawable drawable) {
            mItemImageView.setImageDrawable(drawable);
        }
    }

    private class PhotoAdapter extends RecyclerView.Adapter<PhotoHolder> {

        private List<GalleryItem> mGalleryItems;

        public PhotoAdapter(List<GalleryItem> items) {
            mGalleryItems = items;
        }

        @Override
        public PhotoHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
            View view = layoutInflater.inflate(R.layout.gallery_item, parent, false);
            return new PhotoHolder(view);
        }

        @Override
        public void onBindViewHolder(PhotoHolder photoHolder, int position) {
            GalleryItem item = mGalleryItems.get(position);
            Drawable placeholder = getResources().getDrawable(R.drawable.the_hoff);
            photoHolder.bindDrawable(placeholder);
            mThumbnailDownloader.queueThumbnail(photoHolder, item.getUrl());
        }

        @Override
        public int getItemCount() {
            return mGalleryItems.size();
        }
    }
}

@@ -41,16 +41,97 @@
 llery;%0A%0A
+import android.graphics.Bitmap;%0Aimport android.graphics.drawable.BitmapDrawable;%0A
 import a
@@ -294,24 +294,51 @@
 .os.Bundle;%0A
+import android.os.Handler;%0A
 import andro
@@ -1865,57 +1865,577 @@
     
-mThumbnailDownloader = new ThumbnailDownloader%3C%3E(
+Handler responseHandler = new Handler();%0A        mThumbnailDownloader = new ThumbnailDownloader%3C%3E(responseHandler);%0A        mThumbnailDownloader.setThumbnailDownloadListener(%0A                new ThumbnailDownloader.ThumbnailDownloadListener%3CPhotoHolder%3E() %7B%0A                    @Override%0A                    public void onThumbnailDownload(PhotoHolder photoHolder, Bitmap bitmap) %7B%0A                        Drawable drawable = new BitmapDrawable(getResources(), bitmap);%0A                        photoHolder.bindDrawable(drawable);%0A                    %7D%0A                %7D
 );%0A 
@@ -2673,24 +2673,24 @@
 der.quit();%0A
-
         Log.
@@ -2731,24 +2731,153 @@
 d%22);%0A    %7D%0A%0A
+    @Override%0A    public void onDestroyView() %7B%0A        super.onDestroyView();%0A        mThumbnailDownloader.clearQueue();%0A    %7D%0A%0A
     @Nullabl

Complete de usage of downloader, I am sorry karma this is just to follow the course, but I know it is horrible