package com.geel9.facepunch.fragments;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.text.Html;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import com.actionbarsherlock.app.SherlockFragment;
import com.geel9.facepunch.APISession;
import com.geel9.facepunch.R;
import com.geel9.facepunch.APISession.FPThread;
import com.geel9.facepunch.R.anim;
import com.geel9.facepunch.R.id;
import com.geel9.facepunch.R.layout;
public class ThreadListFragment extends SherlockFragment {
public interface OnThreadSelectedListener{
public void OnThreadSelected(FPThread thread);
public void OnThreadLongClicked(FPThread thread);
}
private OnThreadSelectedListener tListener;
public Activity myActivity;
public boolean hasPopulated = false;
private Activity GetActivity(){
if(myActivity == null)
return getActivity();
return myActivity;
}
protected void applyLoadingIcon( final ImageView iv )
{
iv.setImageResource( R.anim.loadingspinner );
final AnimationDrawable loadingAnimation = (AnimationDrawable)iv.getDrawable();
iv.post( new Runnable()
{
public void run()
{
loadingAnimation.start();
}
} );
}
public void ShowLoadingIcon(){
// Show loading spinner
LayoutInflater inflater = (LayoutInflater)GetActivity().getSystemService( Context.LAYOUT_INFLATER_SERVICE );
final LinearLayout threadList = (LinearLayout)getView().findViewById( R.id.threadList );
threadList.removeAllViews();
final ImageView loaderImage = (ImageView)inflater.inflate( R.layout.loadingspinner, threadList, false );
threadList.removeAllViews();
applyLoadingIcon( loaderImage );
threadList.addView( loaderImage );
threadList.setGravity( Gravity.CENTER_VERTICAL );
}
public void RemoveLoadingIcon(){
final LinearLayout threadList = (LinearLayout)getView().findViewById( R.id.threadList );
threadList.removeAllViews();
threadList.setGravity(Gravity.NO_GRAVITY);
}
public void PopulateList(FPThread[] threads){
hasPopulated = true;
LayoutInflater inflater = (LayoutInflater)GetActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout threadList = (LinearLayout)getView().findViewById(R.id.threadList);
//Populate list with results
boolean switchColor = false;
try{
for(final FPThread thread: threads){
switchColor = !switchColor;
//Create the thread view
final LinearLayout threadItem = (LinearLayout)inflater.inflate(R.layout.listthread, threadList, false);
((TextView)threadItem.findViewById(R.id.threadTitle)).setText(Html.fromHtml(thread.getTitle()));
String reading = "";
String unread = "";
if(thread.readerCount() > 0)
reading = " • " + thread.readerCount() + " reading";
if(thread.unreadPostCount() > 0){
unread = " • <font color='red'><b>" + thread.unreadPostCount() + " new posts.</b></font>";
}
((TextView)threadItem.findViewById(R.id.threadPages)).setText(Html.fromHtml(thread.getAuthor().getName() + reading + unread));
((TextView)threadItem.findViewById(R.id.threadAuthor)).setText(Html.fromHtml(thread.getLastAuthor()));
((TextView)threadItem.findViewById(R.id.threadAuthorTime)).setText(Html.fromHtml(thread.getLastAuthorTime()));
//Android color format: (alpha << 24) | (r << 16) | (g << 8) | b
int backColor = Color.WHITE;
if(switchColor) backColor = (255 << 24) | (250 << 16) | (250 << 8) | 250;
if(thread.isLocked())
backColor = (255 << 24) | (200 << 16) | (200 << 8) | 200;
if(thread.isSticky())
backColor = (255 << 24) | (255 << 16) | (255 << 8) | 170;
threadItem.setBackgroundColor(backColor);
//Load thread
threadItem.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
tListener.OnThreadSelected(thread);
}
});
threadItem.setOnLongClickListener(new OnLongClickListener(){
public boolean onLongClick(View v) {
tListener.OnThreadLongClicked(thread);
return true;
}
});
threadList.addView(threadItem);
}
}catch(Exception e){
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
hasPopulated = false;
return inflater.inflate(R.layout.threadlistfragment, container, false);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
this.setRetainInstance(true);
hasPopulated = false;
try {
tListener = (OnThreadSelectedListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement OnThreadSelectedListener");
}
}
}