package com.lukekorth.android_500px;

import android.app.ActionBar;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lukekorth.android_500px.helpers.Utils;
import com.lukekorth.android_500px.models.Photos;
import com.lukekorth.android_500px.models.WallpaperChangedEvent;
import com.squareup.otto.Subscribe;
import com.squareup.picasso.Picasso;

import java.util.List;

public class HistoryActivity extends ListActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActionBar actionBar = getActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        updateHistory(null);
    }

    @Subscribe
    public void updateHistory(WallpaperChangedEvent event) {
        setListAdapter(new PhotoAdapter(this, Photos.getSeenPhotos()));
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
        }

        return false;
    }

    private static class PhotoAdapter extends BaseAdapter {

        private Context mContext;
        private List<Photos> mPhotos;
        private LayoutInflater mLayoutInflater;
        private Picasso mPicasso;
        private int mSize;

        public PhotoAdapter(Context context, List<Photos> photos) {
            mContext = context;
            mPhotos = photos;
            mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            mPicasso = WallpaperApplication.getPicasso(context);
            mSize = Utils.dpToPx(context, 100);
        }

        @Override
        public int getCount() {
            return mPhotos.size();
        }

        @Override
        public Object getItem(int position) {
            return mPhotos.get(position);
        }

        @Override
        public long getItemId(int position) {
            return mPhotos.get(position).photo_id;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            final Photos photo = mPhotos.get(position);

            ViewHolderItem viewHolderItem;
            if(convertView == null){
                convertView = mLayoutInflater.inflate(R.layout.photo, parent, false);
                viewHolderItem = new ViewHolderItem();
                viewHolderItem.mPhoto = (ImageView) convertView.findViewById(R.id.thumbnail);
                viewHolderItem.mName = (TextView) convertView.findViewById(R.id.name);
                viewHolderItem.mPhotographer = (TextView) convertView.findViewById(R.id.photographer);
                viewHolderItem.mSeenAt = (TextView) convertView.findViewById(R.id.seen);
                convertView.setTag(viewHolderItem);
            } else {
                viewHolderItem = (ViewHolderItem) convertView.getTag();
            }

            CharSequence timeSeen = DateUtils.getRelativeTimeSpanString(photo.seenAt, System.currentTimeMillis(), 0);
            viewHolderItem.mName.setText(photo.name);
            viewHolderItem.mPhotographer.setText("© " + photo.userName + " / 500px");
            viewHolderItem.mSeenAt.setText("Seen " + timeSeen);
            mPicasso.load(photo.imageUrl).resize(mSize, mSize).centerCrop().into(viewHolderItem.mPhoto);
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mContext, ViewPhotoActivity.class)
                            .putExtra(ViewPhotoActivity.PHOTO_POSITION_KEY, position);
                    mContext.startActivity(intent);
                }
            });

            return convertView;
        }

        static class ViewHolderItem {
            ImageView mPhoto;
            TextView mName;
            TextView mPhotographer;
            TextView mSeenAt;
        }
    }

}

@@ -869,24 +869,79 @@
 anceState);%0A
+        WallpaperApplication.getBus().register(this);%0A%0A
         Acti
@@ -1183,24 +1183,161 @@
 ll);%0A    %7D%0A%0A
+    @Override%0A    protected void onDestroy() %7B%0A        super.onDestroy();%0A        WallpaperApplication.getBus().unregister(this);%0A    %7D%0A%0A
     @Subscri

Add missing otto registration