package com.kimuchi1203.starlight;

import twitter4j.Paging;
import twitter4j.ResponseList;
import twitter4j.Twitter;
import android.os.Bundle;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.Loader;
import android.util.Log;

public class TwitterHomeTimelineLoaderCallbacks implements
		LoaderCallbacks<ResponseList<twitter4j.Status>> {

	private MainActivity parent;
	private Twitter twitter;
	private Paging paging;
	private UserManager userManager;
	
	public TwitterHomeTimelineLoaderCallbacks(MainActivity mainActivity,
			Twitter twitter2, UserManager userManager2) {
		parent = mainActivity;
		twitter = twitter2;
		userManager = userManager2;
	}

	@Override
	public Loader<ResponseList<twitter4j.Status>> onCreateLoader(int arg0,
			Bundle arg1) {
		paging = (Paging) arg1.getSerializable("paging");
		TwitterHomeTimelineLoader loader = new TwitterHomeTimelineLoader(parent, twitter, paging);
		loader.forceLoad();
		return loader;
	}

	@Override
	public void onLoadFinished(Loader<ResponseList<twitter4j.Status>> arg0,
			ResponseList<twitter4j.Status> arg1) {
		ResponseList<twitter4j.Status> home = arg1; 
		if((null!=home)&&(home.size()>0)){
			userManager.loadIcon(home);
			if(null!=paging){
				long sinceId = paging.getSinceId();
				long maxId = paging.getMaxId();
				Log.v("twitter", "getHomeTimeline since "+sinceId+" max "+maxId);
				if((-1!=sinceId)&&(-1!=maxId)){
					// [.. sinceId] <tailId ..home.. headId> [maxId+1 .. lastId]
					int cnt;
					for(cnt=0;cnt<parent.adapter.getCount();++cnt){
						twitter4j.Status st = parent.adapter.getItem(cnt);
						if(st.getId()<maxId+1){
							break;
						}
					}
					for(int i=0;i<home.size();++i){
						parent.adapter.insert(home.get(i), cnt+i);
					}
					// from sinceId to tailId
					Paging p2 = new Paging();
					p2.setSinceId(sinceId);
					p2.setMaxId(home.get(home.size()-1).getId()-1);
					parent.getHomeTimeline(p2);
				}else if(-1!=sinceId){
					// [.. sinceId=lastId] <tailId ..home.. headId>
					for (int i = 0; i < home.size(); ++i) {
						parent.adapter.insert(home.get(i), i);
					}
					// update lastId <-- headId
					parent.setLastId(home.get(0).getId());
					// from sinceId to tailId
					Paging p2 = new Paging();
					p2.setSinceId(sinceId);
					p2.setMaxId(home.get(home.size()-1).getId()-1);
					parent.getHomeTimeline(p2);
				}else{
					// <tailId .. home.. headId> [maxId+1 .. lastId]
					for (int i = 0; i < home.size(); ++i) {
						//Log.v("twitter", home.get(i).getText());
						parent.adapter.add(home.get(i));
					}
				}
			}else{
				// <tailId .. home.. headId>
				for (int i = 0; i < home.size(); ++i) {
					parent.adapter.add(home.get(i));
				}
				// update lastId <-- headId
				parent.setLastId(home.get(0).getId());
			}
		}
		parent.adapter.notifyDataSetChanged();
		parent.hideHeader();
		parent.hideFooter();
		parent.setLoadingFlag(false);
	}

	@Override
	public void onLoaderReset(Loader<ResponseList<twitter4j.Status>> arg0) {
		// TODO Auto-generated method stub

	}

}

@@ -264,16 +264,45 @@
 til.Log;
+%0Aimport android.widget.Toast;
 %0A%0Apublic
@@ -3046,43 +3046,192 @@
 %7B%0A%09%09
-// TODO Auto-generated method stub%0A
+parent.adapter.notifyDataSetChanged();%0A%09%09parent.hideHeader();%0A%09%09parent.hideFooter();%0A%09%09parent.setLoadingFlag(false);%0A%09%09Toast.makeText(parent, %22stop loading%22, Toast.LENGTH_LONG).show();
 %0A%09%7D%0A

implement TwitterHomeTimelineLoaderCallbacks#onLoaderReset enable to re-load, show toast.