Cleaned up code

Signed-off-by: Ricky Barrette <rickbarrette@gmail.com>
This commit is contained in:
2012-10-07 09:01:16 -04:00
parent b96e16f9ab
commit 012ed61d7b
3 changed files with 72 additions and 66 deletions

View File

@@ -68,20 +68,44 @@ public class ForumListFragment extends ListFragment implements DatabaseListener
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.v(TAG, "onCreate()");
mDb = new ForumDatabase(this.getActivity(), this);
mDb = new ForumDatabase(getActivity(), this);
new Thread(new Runnable() {
@Override
public void run() {
mDb.saveForums(true);
}
@Override
public void run() {
mDb.saveForums(true);
}
}).start();
if (ForumContent.ITEMS.size() != 0)
if (ForumContent.ITEMS.size() != 0)
setListAdapter(new ForumAdapter(getActivity()));
}
@Override
public void onDatabaseCreate() {
// TODO Auto-generated method stub
}
@Override
public void onDatabaseUpgrade() {
// TODO Auto-generated method stub
}
@Override
public void onDatabaseUpgradeComplete() {
// TODO Auto-generated method stub
}
@Override
public void onDeletionComplete() {
// TODO Auto-generated method stub
}
@Override
public void onDetach() {
super.onDetach();
@@ -94,6 +118,12 @@ public class ForumListFragment extends ListFragment implements DatabaseListener
mCallbacks.onItemSelected(this, ForumContent.ITEMS.get(position).id);
}
@Override
public void onRestoreComplete() {
// TODO Auto-generated method stub
}
@Override
public void onSaveInstanceState(final Bundle outState) {
super.onSaveInstanceState(outState);
@@ -101,6 +131,17 @@ public class ForumListFragment extends ListFragment implements DatabaseListener
outState.putInt(STATE_ACTIVATED_POSITION, mActivatedPosition);
}
@Override
public void onSyncComplete() {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
ForumContent.getForum(mDb);
setListAdapter(new ForumAdapter(getActivity()));
}
});
}
@Override
public void onViewCreated(final View view, final Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
@@ -120,45 +161,4 @@ public class ForumListFragment extends ListFragment implements DatabaseListener
public void setActivateOnItemClick(final boolean activateOnItemClick) {
getListView().setChoiceMode(activateOnItemClick ? AbsListView.CHOICE_MODE_SINGLE : AbsListView.CHOICE_MODE_NONE);
}
@Override
public void onDatabaseCreate() {
// TODO Auto-generated method stub
}
@Override
public void onDatabaseUpgrade() {
// TODO Auto-generated method stub
}
@Override
public void onDatabaseUpgradeComplete() {
// TODO Auto-generated method stub
}
@Override
public void onDeletionComplete() {
// TODO Auto-generated method stub
}
@Override
public void onRestoreComplete() {
// TODO Auto-generated method stub
}
@Override
public void onSyncComplete() {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
ForumContent.getForum(mDb);
setListAdapter(new ForumAdapter(getActivity()));
}
});
}
}

View File

@@ -36,7 +36,7 @@ public interface DatabaseListener {
public void onDeletionComplete();
public void onRestoreComplete();
public void onSyncComplete();
}

View File

@@ -85,8 +85,8 @@ public class ForumDatabase {
* These tables will be used for storing forum, topic, and post id's
*/
db.execSQL("CREATE TABLE " + TABLE_FORUMS + "( id TEXT PRIMARY KEY)");
db.execSQL("CREATE TABLE " + TABLE_TOPICS + "( id TEXT PRIMARY KEY, " + KEY_FORUM_ID + " TEXT, "+ KEY_TIME_STAMP + " DATE)");
db.execSQL("CREATE TABLE " + TABLE_THREADS + "( id TEXT PRIMARY KEY, " + KEY_FORUM_ID + " TEXT, " + KEY_TOPIC_ID + " TEXT, "+ KEY_TIME_STAMP + " DATE)");
db.execSQL("CREATE TABLE " + TABLE_TOPICS + "( id TEXT PRIMARY KEY, " + KEY_FORUM_ID + " TEXT, " + KEY_TIME_STAMP + " DATE)");
db.execSQL("CREATE TABLE " + TABLE_THREADS + "( id TEXT PRIMARY KEY, " + KEY_FORUM_ID + " TEXT, " + KEY_TOPIC_ID + " TEXT, " + KEY_TIME_STAMP + " DATE)");
/*
* These tables will be used for storing forum, topic, and post
@@ -432,13 +432,14 @@ public class ForumDatabase {
/**
* Retrieves a list of thread ids
*
* @param id of topic
* @param id
* of topic
* @return
* @author ricky barrette
*/
public List<String> getThreads(String id) {
public List<String> getThreads(final String id) {
final ArrayList<String> list = new ArrayList<String>();
final Cursor c = mDb.query(TABLE_THREADS, new String[] { KEY_ID }, KEY_TOPIC_ID +" = "+ DatabaseUtils.sqlEscapeString(id), null, null, null, KEY_TIME_STAMP);
final Cursor c = mDb.query(TABLE_THREADS, new String[] { KEY_ID }, KEY_TOPIC_ID + " = " + DatabaseUtils.sqlEscapeString(id), null, null, null, KEY_TIME_STAMP);
if (c.moveToFirst())
do
list.add(c.getString(0));
@@ -461,13 +462,15 @@ public class ForumDatabase {
/**
* Retrieves a list of topic ids
*
* @param id of forum
* @param id
* of forum
* @return
* @author ricky barrette
*/
public List<String> getTopics(String id) {
public List<String> getTopics(final String id) {
final ArrayList<String> list = new ArrayList<String>();
final Cursor c = mDb.query(TABLE_TOPICS, new String[] { KEY_ID }, KEY_FORUM_ID +" = "+ DatabaseUtils.sqlEscapeString(id) , null, null, null, KEY_TIME_STAMP + " DESC");
final Cursor c = mDb.query(TABLE_TOPICS, new String[] { KEY_ID }, KEY_FORUM_ID + " = " + DatabaseUtils.sqlEscapeString(id), null, null, null, KEY_TIME_STAMP
+ " DESC");
if (c.moveToFirst())
do
list.add(c.getString(0));
@@ -539,7 +542,7 @@ public class ForumDatabase {
}
}
if(mListener != null)
if (mListener != null)
mListener.onSyncComplete();
}
@@ -553,7 +556,7 @@ public class ForumDatabase {
public void saveThreads(final XMLRPCClient client, final Object forumId, final Object topicId) {
saveThreads(client, forumId, topicId, false);
}
/**
* Downloads and save threads into the database
*
@@ -574,10 +577,11 @@ public class ForumDatabase {
if (result != null)
for (final Object element : result) {
final HashMap<?, ?> contentHash = (HashMap<?, ?>) element;
upsertThreadTable((String) forumId, (String) topicId, (String) contentHash.get("post_id"), (Date) contentHash.get("post_time"), convertHashMapToContentValues(contentHash));
upsertThreadTable((String) forumId, (String) topicId, (String) contentHash.get("post_id"), (Date) contentHash.get("post_time"),
convertHashMapToContentValues(contentHash));
}
if(!isRecursive && mListener != null)
if (!isRecursive && mListener != null)
mListener.onSyncComplete();
}
@@ -600,12 +604,13 @@ public class ForumDatabase {
if (result != null)
for (final Object element : result) {
final HashMap<?, ?> contentHash = (HashMap<?, ?>) element;
upsertTopicTable((String) forumId, (String) contentHash.get("topic_id"), (Date) contentHash.get("last_reply_time"), convertHashMapToContentValues(contentHash));
upsertTopicTable((String) forumId, (String) contentHash.get("topic_id"), (Date) contentHash.get("last_reply_time"),
convertHashMapToContentValues(contentHash));
if (isRecursive)
saveThreads(client, forumId, contentHash.get("topic_id"), isRecursive);
}
if(!isRecursive && mListener != null)
if (!isRecursive && mListener != null)
mListener.onSyncComplete();
}
@@ -641,7 +646,7 @@ public class ForumDatabase {
* @author ricky barrette
*/
private void upsertTable(final String table, final String where, final ContentValues item) {
Log.v(TAG, "upserting: " + item.getAsString(KEY) + " : "+ item.getAsString(KEY_VALUE));
Log.v(TAG, "upserting: " + item.getAsString(KEY) + " : " + item.getAsString(KEY_VALUE));
if (!(mDb.update(table, item, where, null) > 0))
mDb.insert(table, null, item);
}
@@ -656,7 +661,8 @@ public class ForumDatabase {
* @throws NullPointerException
* @author ricky barrette
*/
private void upsertThreadTable(final String forumId, final String topicId, final String postId, final Date time, final List<ContentValues> values) throws NullPointerException {
private void upsertThreadTable(final String forumId, final String topicId, final String postId, final Date time, final List<ContentValues> values)
throws NullPointerException {
if (values == null)
throw new NullPointerException("values was null");