package com.edinubuntu.petlove.activity;

import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.widget.GridView;
import android.widget.Toast;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.edinubuntu.petlove.PetLove;
import com.edinubuntu.petlove.R;
import com.edinubuntu.petlove.adapter.RecordsAdapter;
import com.edinubuntu.petlove.model.AdaptPetsModel;
import com.edinubuntu.petlove.object.Record;
import com.edinubuntu.petlove.util.converter.RecordsJsonConverter;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import org.json.JSONException;

import java.util.ArrayList;

public class MainActivity extends SherlockActivity {

    private AdaptPetsModel adaptPetsModel;

    private java.util.List<Record> recordList;

    protected GridView petsGridView;

    private RecordsAdapter recordsAdapter;

    private Menu menu;

    private static int GRID_VIEW_ONE_COLUMN_PER_PAGE_WIDTH;
    private static int GRID_VIEW_TWO_COLUMN_PER_PAGE_WIDTH;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        /**
         * Initialize part
         */
        DisplayImageOptions imageOptions = new DisplayImageOptions.Builder().cacheInMemory().cacheOnDisc().build();
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
                .defaultDisplayImageOptions(imageOptions).build();
        ImageLoader.getInstance().init(config);

        ActionBar actionBar = getSupportActionBar();
        actionBar.setLogo(R.drawable.ic_launcher);

        // Setup Grid view width
        DisplayMetrics metrics = getResources().getDisplayMetrics();
        GRID_VIEW_ONE_COLUMN_PER_PAGE_WIDTH = metrics.widthPixels;
        GRID_VIEW_TWO_COLUMN_PER_PAGE_WIDTH = metrics.widthPixels / 3;
    }

    @Override
    public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
        getSupportMenuInflater().inflate(R.menu.main, menu);
        this.menu = menu;
        updateRefreshItem();
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_refresh:
                loadModel();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);

        recordList = new ArrayList<Record>();

        petsGridView = (GridView)findViewById(R.id.records_grid_view);
        petsGridView.setColumnWidth(GRID_VIEW_TWO_COLUMN_PER_PAGE_WIDTH);
        petsGridView.invalidateViews();

        recordsAdapter = new RecordsAdapter(MainActivity.this, android.R.layout.simple_list_item_1, recordList);

        petsGridView.setAdapter(recordsAdapter);

        adaptPetsModel = new AdaptPetsModel();

        loadModel();
    }

    private void loadModel() {
        adaptPetsModel.load(new AsyncHttpResponseHandler() {

            @Override
            public void onStart() {
                super.onStart();
                adaptPetsModel.setLoading(true);
                updateRefreshItem();
            }

            @Override
            public void onSuccess(String s) {
                super.onSuccess(s);

                RecordsJsonConverter recordsJsonConverter = new RecordsJsonConverter();
                try {
                    recordsJsonConverter.convert(s);

                    recordList = recordsJsonConverter.getRecords();

                    for (Record record : recordList) {
                        recordsAdapter.insert(record, recordsAdapter.getCount());
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                    Log.e(PetLove.TAG, e.toString());
                    Toast.makeText(MainActivity.this, e.getLocalizedMessage(), Toast.LENGTH_LONG).show();
                }

                recordsAdapter.notifyDataSetChanged();
            }

            @Override
            public void onFailure(Throwable throwable, String s) {
                super.onFailure(throwable, s);
            }

            @Override
            public void onFinish() {
                super.onFinish();
                adaptPetsModel.setLoading(false);
                updateRefreshItem();
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    public void updateRefreshItem() {
        if (menu != null) {
            MenuItem refreshItem = menu.findItem(R.id.action_refresh);
            if (this.adaptPetsModel.isLoading()) {
                refreshItem.setActionView(R.layout.indeterminate_progress_action);
            } else {
                refreshItem.setActionView(null);
            }
        }
    }
}

@@ -2676,24 +2676,29 @@
   loadModel(
+false
 );%0A         
@@ -3365,16 +3365,21 @@
 adModel(
+false
 );%0A    %7D
@@ -3407,16 +3407,34 @@
 adModel(
+final boolean more
 ) %7B%0A    
@@ -3784,24 +3784,116 @@
 uccess(s);%0A%0A
+                if (!more) %7B%0A                    recordsAdapter.clear();%0A                %7D%0A%0A
             

Add load more