From 012ed61d7be35479d85aafb24ccb214d36fec812 Mon Sep 17 00:00:00 2001 From: Ricky Barrette Date: Sun, 7 Oct 2012 09:01:16 -0400 Subject: [PATCH] Cleaned up code Signed-off-by: Ricky Barrette --- .../osj/forum/ForumListFragment.java | 98 +++++++++---------- .../osj/forum/database/DatabaseListener.java | 2 +- .../osj/forum/database/ForumDatabase.java | 38 ++++--- 3 files changed, 72 insertions(+), 66 deletions(-) diff --git a/OSJ Forum/src/org/RickBarrette/osj/forum/ForumListFragment.java b/OSJ Forum/src/org/RickBarrette/osj/forum/ForumListFragment.java index fc732a3..8a3708c 100644 --- a/OSJ Forum/src/org/RickBarrette/osj/forum/ForumListFragment.java +++ b/OSJ Forum/src/org/RickBarrette/osj/forum/ForumListFragment.java @@ -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())); - } - }); - } } diff --git a/OSJ Forum/src/org/RickBarrette/osj/forum/database/DatabaseListener.java b/OSJ Forum/src/org/RickBarrette/osj/forum/database/DatabaseListener.java index 0c7b69b..06ba0a3 100644 --- a/OSJ Forum/src/org/RickBarrette/osj/forum/database/DatabaseListener.java +++ b/OSJ Forum/src/org/RickBarrette/osj/forum/database/DatabaseListener.java @@ -36,7 +36,7 @@ public interface DatabaseListener { public void onDeletionComplete(); public void onRestoreComplete(); - + public void onSyncComplete(); } \ No newline at end of file diff --git a/OSJ Forum/src/org/RickBarrette/osj/forum/database/ForumDatabase.java b/OSJ Forum/src/org/RickBarrette/osj/forum/database/ForumDatabase.java index 8d2dba2..bcaca0b 100644 --- a/OSJ Forum/src/org/RickBarrette/osj/forum/database/ForumDatabase.java +++ b/OSJ Forum/src/org/RickBarrette/osj/forum/database/ForumDatabase.java @@ -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 getThreads(String id) { + public List getThreads(final String id) { final ArrayList list = new ArrayList(); - 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 getTopics(String id) { + public List getTopics(final String id) { final ArrayList list = new ArrayList(); - 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 values) throws NullPointerException { + private void upsertThreadTable(final String forumId, final String topicId, final String postId, final Date time, final List values) + throws NullPointerException { if (values == null) throw new NullPointerException("values was null");