package com.geel9.facepunch.activities; import java.util.ArrayList; import com.geel9.facepunch.APISession.Category; import com.geel9.facepunch.APISession.FPThread; import com.geel9.facepunch.APISession.Forum; import com.geel9.facepunch.APISession.QuoteCallback; import com.geel9.facepunch.APISession.ThreadCallback; import com.geel9.facepunch.APISession.unReadCallback; import com.geel9.facepunch.R.id; import com.geel9.facepunch.R.layout; import com.geel9.facepunch.R.string; import com.geel9.facepunch.fragments.ThreadListFragment; import com.geel9.facepunch.fragments.ThreadListFragment.OnThreadSelectedListener; import com.geel9.facepunch.APISession; import com.geel9.facepunch.R; import android.app.AlertDialog; import android.app.Dialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.graphics.Color; import android.os.Bundle; import android.text.Html; import android.view.Gravity; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.view.View.OnLongClickListener; import android.view.ViewGroup; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ScrollView; import android.widget.TextView; import android.widget.Toast; public class PopularThreadsActivity extends FPActivity implements OnThreadSelectedListener { // Used for restoring list private class State { FPThread[] threads; int scrollY; } private FPThread[] displayedThreads; private ThreadListFragment threadFragment; @Override public void OnLogin(){ loadPage(); super.OnLogin(); } public void OnLogout(){ loadPage(); super.OnLogout(); } @Override public void RemoveMenuItems(com.actionbarsherlock.view.Menu menu){ menu.removeItem(ID_POPULARTHREADS); super.RemoveMenuItems(menu); } @Override public void OnRefreshPressed(){ loadPage(); super.OnRefreshPressed(); } @Override public void onCreate( Bundle savedInstanceState ) { // Load layout super.onCreate( savedInstanceState ); setContentView( R.layout.forumview ); threadFragment = (ThreadListFragment)getSupportFragmentManager().findFragmentById(R.id.forumThreadListFragment); threadFragment.setRetainInstance(true); //Restore forum list final State data = (State)getLastCustomNonConfigurationInstance(); if ( data != null ) { ActionText("Popular Threads"); if(data.threads == null){ loadPage(); return; } threadFragment.PopulateList( data.threads ); displayedThreads = data.threads; final ScrollView scroller = (ScrollView)findViewById( R.id.scrollViewThreads ); scroller.post( new Runnable() { public void run() { scroller.scrollTo( 0, data.scrollY ); } } ); return; } CheckLoginState(); //Set header title Intent intent = getIntent(); //We use the ForumView layout. Hide the subforums. LinearLayout subs = (LinearLayout)findViewById(R.id.LinearLayoutSub); subs.setVisibility(View.GONE); ActionText("Popular Threads"); loadPage(); } // Save activity state @Override public Object onRetainCustomNonConfigurationInstance() { State data = new State(); data.threads = displayedThreads; data.scrollY = ( (ScrollView)findViewById( R.id.scrollViewThreads ) ).getScrollY(); return data; } private void loadPage(){ threadFragment.ShowLoadingIcon(); api.listPopularThreads(this, new APISession.ThreadCallback() { public void onResult(boolean success, FPThread[] threads) { threadFragment.RemoveLoadingIcon(); if(success && threads != null && threads.length > 0){ threadFragment.PopulateList(threads); } else{ Toast.makeText(PopularThreadsActivity.this, getString(R.string.popularThreadsLoadingFailed), Toast.LENGTH_SHORT).show(); } } }); } public void OnThreadSelected(FPThread thread) { Intent i = new Intent().setClass(PopularThreadsActivity.this, ThreadActivity.class); i.putExtra("threadId", thread.getId()); i.putExtra("threadName", thread.getTitle()); i.putExtra("numPages", thread.pageCount()); i.putExtra("locked", thread.isLocked()); int page = api.loggedIn() ? -1 : thread.pageCount(); i.putExtra("threadPage", page); startActivity(i); } public void OnThreadLongClicked(final FPThread thread) { final CharSequence[] items; if(api.loggedIn()){ items = new CharSequence[]{ "First page", "First unread post", "Last page" }; } else{ items = new CharSequence[]{ "First page", "Last page" }; } new AlertDialog.Builder( PopularThreadsActivity.this ) .setTitle( "Thread actions" ) .setItems( items, new DialogInterface.OnClickListener() { public void onClick( DialogInterface dialog, int item ) { String option = (String) items[item]; // First page if ( option.equals("First page") ) { Intent i = new Intent().setClass(PopularThreadsActivity.this, ThreadActivity.class); i.putExtra("threadId", thread.getId()); i.putExtra("threadName", thread.getTitle()); i.putExtra("threadPage", 1); i.putExtra("numPages", thread.pageCount()); i.putExtra("locked", thread.isLocked()); startActivity(i); } //First unread post else if(option.equals("First unread post")){ Intent i = new Intent().setClass(PopularThreadsActivity.this, ThreadActivity.class); i.putExtra("threadId", thread.getId()); i.putExtra("threadName", thread.getTitle()); i.putExtra("threadPage", -1); i.putExtra("numPages", thread.pageCount()); i.putExtra("locked", thread.isLocked()); startActivity(i); } //Last page else if ( option.equals("Last page") ) { Intent i = new Intent().setClass(PopularThreadsActivity.this, ThreadActivity.class); i.putExtra("threadId", thread.getId()); i.putExtra("threadName", thread.getTitle()); i.putExtra("threadPage", thread.pageCount()); i.putExtra("numPages", thread.pageCount()); i.putExtra("locked", thread.isLocked()); startActivity(i); } } } ) .create().show(); } }