package net.rdyonline.catwalk;

import java.util.List;

import net.rdyonline.catwalk.data.Image;
import net.rdyonline.catwalk.data.api.cat.CatApi;
import net.rdyonline.catwalk.tasks.SafeASyncTask;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.squareup.picasso.Picasso;

public class DisplayCatActivity extends Activity {

	private int mCurrentPage = 1;
	private int mPositionInPage = 0;
	
	private ImageView mCatImage;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_display_cat);
		bindViews();
		
		loadNextCat();
	}
	
	private void bindViews() {
		mCatImage = (ImageView) findViewById(R.id.cat);
	}

	private void loadNextCat() {
		new SafeASyncTask<List<Image>>(this) {

			@Override
			protected List<Image> onRun() {
				return new CatApi("http://thecatapi.com").getPage(mCurrentPage);
			}

			@Override
			protected void onSuccess(List<Image> result) {
				Image image = result.get(mPositionInPage);
				Picasso.with(this.getContext()).load(image.url).into(mCatImage);
				
				mCatImage.setVisibility(View.VISIBLE);
				
				mPositionInPage++;
			}
		}.execute();
	}
	
}

@@ -271,16 +271,58 @@
 w.View;%0A
+import android.view.View.OnClickListener;%0A
 import a
@@ -346,16 +346,16 @@
 geView;%0A
-
 %0Aimport 
@@ -502,18 +502,17 @@
 ge = 0;%0A
-%09
 %0A
+
 %09private
@@ -533,18 +533,17 @@
 tImage;%0A
-%09
 %0A
+
 %09@Overri
@@ -704,16 +704,32 @@
 ws();%0A%09%09
+setListeners();%0A
 %0A%09%09loadN
@@ -741,18 +741,17 @@
 t();%0A%09%7D%0A
-%09
 %0A
+
 %09private
@@ -820,24 +820,276 @@
 d.cat);%0A%09%7D%0A%0A
+%09private void setListeners() %7B%0A%09%09OnClickListener listener = new OnClickListener() %7B%0A%0A%09%09%09@Override%0A%09%09%09public void onClick(View v) %7B%0A%09%09%09%09mCatImage.setVisibility(View.INVISIBLE);%0A%09%09%09%09loadNextCat();%0A%09%09%09%7D%0A%09%09%7D;%0A%0A%09%09mCatImage.setOnClickListener(listener);%0A%09%7D%0A%0A
 %09private voi
@@ -1450,21 +1450,17 @@
 Image);%0A
-%09%09%09%09
 %0A
+
 %09%09%09%09mCat
@@ -1498,13 +1498,107 @@
 E);%0A
+%0A
 %09%09%09%09
-%0A
+if (mPositionInPage == 20 - 1) %7B%0A%09%09%09%09%09mCurrentPage++;%0A%09%09%09%09%09mPositionInPage = 0;%0A%09%09%09%09%7D else %7B%0A%09
 %09%09%09%09
@@ -1616,16 +1616,22 @@
 Page++;%0A
+%09%09%09%09%7D%0A
 %09%09%09%7D%0A%09%09%7D
@@ -1645,12 +1645,11 @@
 e();%0A%09%7D%0A
-%09
 %0A%7D%0A

load next cat image on click of the view