From 7ec5e1fa35a7adaa1ec84375951101c204f99a48 Mon Sep 17 00:00:00 2001 From: Ricky Barrette Date: Sat, 6 Oct 2012 11:20:12 -0400 Subject: [PATCH] Cleaned up code Signed-off-by: Ricky Barrette --- .../osj/forum/ForumListActivity.java | 7 +- .../osj/forum/ForumListFragment.java | 29 ++- .../osj/forum/content/ForumContent.java | 6 +- .../osj/forum/content/ThreadContent.java | 6 +- .../osj/forum/content/TopicAdapter.java | 2 +- .../osj/forum/content/TopicContent.java | 6 +- .../osj/forum/database/DatabaseListener.java | 9 +- .../osj/forum/database/ForumDatabase.java | 183 ++++++++++-------- 8 files changed, 131 insertions(+), 117 deletions(-) diff --git a/OSJ Forum/src/org/RickBarrette/osj/forum/ForumListActivity.java b/OSJ Forum/src/org/RickBarrette/osj/forum/ForumListActivity.java index 4507c42..fb452b2 100644 --- a/OSJ Forum/src/org/RickBarrette/osj/forum/ForumListActivity.java +++ b/OSJ Forum/src/org/RickBarrette/osj/forum/ForumListActivity.java @@ -47,9 +47,10 @@ public class ForumListActivity extends FragmentActivity implements OnItemSelecte Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(this)); setContentView(R.layout.activity_forum_list); - - new Thread(new Runnable(){ - public void run(){ + + new Thread(new Runnable() { + @Override + public void run() { Looper.prepare(); new ForumDatabase(ForumListActivity.this).saveForums(true); } diff --git a/OSJ Forum/src/org/RickBarrette/osj/forum/ForumListFragment.java b/OSJ Forum/src/org/RickBarrette/osj/forum/ForumListFragment.java index 2cbc857..7caf748 100644 --- a/OSJ Forum/src/org/RickBarrette/osj/forum/ForumListFragment.java +++ b/OSJ Forum/src/org/RickBarrette/osj/forum/ForumListFragment.java @@ -65,21 +65,20 @@ public class ForumListFragment extends ListFragment { public void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); - if (ForumContent.ITEMS.size() == 0){ -// new Thread(new Runnable() { -// @Override -// public void run() { - ForumContent.getForum(getActivity()); - getActivity().runOnUiThread(new Runnable() { - @Override - public void run() { - setListAdapter(new ForumAdapter(getActivity())); - } - }); -// } -// }).start(); - } - else + if (ForumContent.ITEMS.size() == 0) { + // new Thread(new Runnable() { + // @Override + // public void run() { + ForumContent.getForum(getActivity()); + getActivity().runOnUiThread(new Runnable() { + @Override + public void run() { + setListAdapter(new ForumAdapter(getActivity())); + } + }); + // } + // }).start(); + } else setListAdapter(new ForumAdapter(getActivity())); } diff --git a/OSJ Forum/src/org/RickBarrette/osj/forum/content/ForumContent.java b/OSJ Forum/src/org/RickBarrette/osj/forum/content/ForumContent.java index 9661bb0..1bb8ff6 100644 --- a/OSJ Forum/src/org/RickBarrette/osj/forum/content/ForumContent.java +++ b/OSJ Forum/src/org/RickBarrette/osj/forum/content/ForumContent.java @@ -105,12 +105,12 @@ public class ForumContent { public static void getForum(final Context context) { ITEMS.clear(); ITEM_MAP.clear(); - + final ForumDatabase db = new ForumDatabase(context); - List list = db.getForums(); + final List list = db.getForums(); int index = 0; - for(String item : list) + for (final String item : list) addItem(new ForumItem(Integer.valueOf(index++).toString(), db.getForum(item))); } } \ No newline at end of file diff --git a/OSJ Forum/src/org/RickBarrette/osj/forum/content/ThreadContent.java b/OSJ Forum/src/org/RickBarrette/osj/forum/content/ThreadContent.java index efe6bb5..2d2d0b1 100644 --- a/OSJ Forum/src/org/RickBarrette/osj/forum/content/ThreadContent.java +++ b/OSJ Forum/src/org/RickBarrette/osj/forum/content/ThreadContent.java @@ -88,12 +88,12 @@ public class ThreadContent { public static void getThread(final String topicId, final int startNumber, final int lastNumber, final Context context) { ITEMS.clear(); ITEM_MAP.clear(); - + final ForumDatabase db = new ForumDatabase(context); - List list = db.getThreads(); + final List list = db.getThreads(); int index = 0; - for(String item : list) + for (final String item : list) addItem(new ThreadItem(Integer.valueOf(index++).toString(), db.getThread(item))); } diff --git a/OSJ Forum/src/org/RickBarrette/osj/forum/content/TopicAdapter.java b/OSJ Forum/src/org/RickBarrette/osj/forum/content/TopicAdapter.java index 4738661..f2e23a2 100644 --- a/OSJ Forum/src/org/RickBarrette/osj/forum/content/TopicAdapter.java +++ b/OSJ Forum/src/org/RickBarrette/osj/forum/content/TopicAdapter.java @@ -130,7 +130,7 @@ public class TopicAdapter extends BaseAdapter { holder.title.setText((String) getItem(position).get("topic_title")); holder.user.setText((String) getItem(position).get("topic_author_name")); holder.lastestPost.setText((String) getItem(position).get("short_content")); - holder.newPosts.setVisibility((Boolean.parseBoolean((String) getItem(position).get("new_post")) ? View.VISIBLE : View.GONE)); + holder.newPosts.setVisibility(Boolean.parseBoolean((String) getItem(position).get("new_post")) ? View.VISIBLE : View.GONE); return convertView; } diff --git a/OSJ Forum/src/org/RickBarrette/osj/forum/content/TopicContent.java b/OSJ Forum/src/org/RickBarrette/osj/forum/content/TopicContent.java index d2fd0cc..4a0400c 100644 --- a/OSJ Forum/src/org/RickBarrette/osj/forum/content/TopicContent.java +++ b/OSJ Forum/src/org/RickBarrette/osj/forum/content/TopicContent.java @@ -86,12 +86,12 @@ public class TopicContent { public static void getTopics(final String forumId, final Context context) { ITEMS.clear(); ITEM_MAP.clear(); - + final ForumDatabase db = new ForumDatabase(context); - List list = db.getTopics(); + final List list = db.getTopics(); int index = 0; - for(String item : list) + for (final String item : list) addItem(new TopicItem(Integer.valueOf(index++).toString(), db.getTopic(item))); } } 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 9a900aa..6a85622 100644 --- a/OSJ Forum/src/org/RickBarrette/osj/forum/database/DatabaseListener.java +++ b/OSJ Forum/src/org/RickBarrette/osj/forum/database/DatabaseListener.java @@ -20,20 +20,21 @@ package org.RickBarrette.osj.forum.database; /** - * This interface will be used to listen to see when the database events are complete + * This interface will be used to listen to see when the database events are + * complete * * @author ricky barrette */ public interface DatabaseListener { - + public void onDatabaseCreate(); public void onDatabaseUpgrade(); public void onDatabaseUpgradeComplete(); + public void onDeletionComplete(); + public void onRestoreComplete(); - public void onDeletionComplete(); - } \ 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 030da5a..bb6d3c7 100644 --- a/OSJ Forum/src/org/RickBarrette/osj/forum/database/ForumDatabase.java +++ b/OSJ Forum/src/org/RickBarrette/osj/forum/database/ForumDatabase.java @@ -78,19 +78,20 @@ public class ForumDatabase { * @author ricky barrette */ private void createDatabase(final SQLiteDatabase db) { - + /* * 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)"); - db.execSQL("CREATE TABLE " + TABLE_THREADS + "( id TEXT PRIMARY KEY, "+ KEY_FORUM_ID + " TEXT, "+ KEY_TOPIC_ID + " TEXT)"); - + db.execSQL("CREATE TABLE " + TABLE_TOPICS + "( id TEXT PRIMARY KEY, " + KEY_FORUM_ID + " TEXT)"); + db.execSQL("CREATE TABLE " + TABLE_THREADS + "( id TEXT PRIMARY KEY, " + KEY_FORUM_ID + " TEXT, " + KEY_TOPIC_ID + " TEXT)"); + /* - * These tables will be used for storing forum, topic, and post information. + * These tables will be used for storing forum, topic, and post + * information. */ db.execSQL("CREATE TABLE " + TABLE_FORUM_INFO + "( id INTEGER PRIMARY KEY, " + KEY_FORUM_ID + " TEXT, " + KEY + " TEXT, " + KEY_VALUE + " TEXT)"); - db.execSQL("CREATE TABLE " + TABLE_TOPIC_INFO + "( id INTEGER PRIMARY KEY, " + KEY_TOPIC_ID + " TEXT, " + KEY + " TEXT, " + KEY_VALUE+ " TEXT)"); + db.execSQL("CREATE TABLE " + TABLE_TOPIC_INFO + "( id INTEGER PRIMARY KEY, " + KEY_TOPIC_ID + " TEXT, " + KEY + " TEXT, " + KEY_VALUE + " TEXT)"); db.execSQL("CREATE TABLE " + TABLE_THREAD_INFO + "( id INTEGER PRIMARY KEY, " + KEY_POST_ID + " TEXT, " + KEY + " TEXT, " + KEY_VALUE + " TEXT)"); } @@ -107,7 +108,7 @@ public class ForumDatabase { db.execSQL("DROP TABLE IF EXISTS " + TABLE_FORUMS); db.execSQL("DROP TABLE IF EXISTS " + TABLE_TOPICS); db.execSQL("DROP TABLE IF EXISTS " + TABLE_THREADS); - + db.execSQL("DROP TABLE IF EXISTS " + TABLE_FORUM_INFO); db.execSQL("DROP TABLE IF EXISTS " + TABLE_TOPIC_INFO); db.execSQL("DROP TABLE IF EXISTS " + TABLE_THREAD_INFO); @@ -337,6 +338,22 @@ public class ForumDatabase { } } + /** + * Converts a cursor with key-value pairs into a hash map + * + * @param c + * @return + * @author ricky barrette + */ + private HashMap cursorToHashMap(final Cursor c) { + final HashMap h = new HashMap(); + if (c.moveToFirst()) + do + h.put(c.getString(c.getColumnIndex(KEY)), c.getString(c.getColumnIndex(KEY_VALUE))); + while (c.moveToNext()); + return h; + } + /** * deletes a note by its row id * @@ -361,96 +378,90 @@ public class ForumDatabase { }).start(); } - /** - * Retrieves a list of forum ids - * @return - * @author ricky barrette - */ - public List getForums(){ - ArrayList list = new ArrayList(); - Cursor c = mDb.query(TABLE_FORUMS, new String[] { KEY_ID }, null, null, null, null, null); - if(c.moveToFirst()) - do{ - list.add(c.getString(0)); - } while(c.moveToNext()); - return list; - } - - /** - * Retrieves a list of topic ids - * @return - * @author ricky barrette - */ - public List getTopics(){ - ArrayList list = new ArrayList(); - Cursor c = mDb.query(TABLE_TOPICS, new String[] { KEY_ID }, null, null, null, null, null); - if(c.moveToFirst()) - do{ - list.add(c.getString(0)); - } while(c.moveToNext()); - return list; - } - - /** - * Retrieves a list of thread ids - * @return - * @author ricky barrette - */ - public List getThreads(){ - ArrayList list = new ArrayList(); - Cursor c = mDb.query(TABLE_THREADS, new String[] { KEY_ID }, null, null, null, null, null); - if(c.moveToFirst()) - do{ - list.add(c.getString(0)); - } while(c.moveToNext()); - return list; - } - /** * Retrieves a forum from the database + * * @param id * @return * @author ricky barrette */ - public HashMap getForum(String id){ - return cursorToHashMap(mDb.query(TABLE_FORUM_INFO, new String[] { KEY, KEY_VALUE }, KEY_FORUM_ID +" = "+ DatabaseUtils.sqlEscapeString(id), null, null, null, null)); + public HashMap getForum(final String id) { + return cursorToHashMap(mDb.query(TABLE_FORUM_INFO, new String[] { KEY, KEY_VALUE }, KEY_FORUM_ID + " = " + DatabaseUtils.sqlEscapeString(id), null, null, null, + null)); } - + /** - * Retrieves a topic from the database - * @param id + * Retrieves a list of forum ids + * * @return * @author ricky barrette */ - public HashMap getTopic(String id) { - return cursorToHashMap(mDb.query(TABLE_TOPIC_INFO, new String[] { KEY, KEY_VALUE }, KEY_TOPIC_ID +" = "+ DatabaseUtils.sqlEscapeString(id), null, null, null, null)); + public List getForums() { + final ArrayList list = new ArrayList(); + final Cursor c = mDb.query(TABLE_FORUMS, new String[] { KEY_ID }, null, null, null, null, null); + if (c.moveToFirst()) + do + list.add(c.getString(0)); + while (c.moveToNext()); + return list; } - + /** * Retrieves a post from the threads table of the database + * * @param id * @return * @author ricky barrette */ - public HashMap getThread(String id) { - return cursorToHashMap(mDb.query(TABLE_THREAD_INFO, new String[] { KEY, KEY_VALUE }, KEY_POST_ID +" = "+ DatabaseUtils.sqlEscapeString(id), null, null, null, null)); + public HashMap getThread(final String id) { + return cursorToHashMap(mDb.query(TABLE_THREAD_INFO, new String[] { KEY, KEY_VALUE }, KEY_POST_ID + " = " + DatabaseUtils.sqlEscapeString(id), null, null, null, + null)); } - + /** - * Converts a cursor with key-value pairs into a hash map - * @param c + * Retrieves a list of thread ids + * * @return * @author ricky barrette */ - private HashMap cursorToHashMap(Cursor c){ - HashMap h = new HashMap(); - if(c.moveToFirst()) - do{ - h.put(c.getString(c.getColumnIndex(KEY)), c.getString(c.getColumnIndex(KEY_VALUE))); - } while(c.moveToNext()); - return h; + public List getThreads() { + final ArrayList list = new ArrayList(); + final Cursor c = mDb.query(TABLE_THREADS, new String[] { KEY_ID }, null, null, null, null, null); + if (c.moveToFirst()) + do + list.add(c.getString(0)); + while (c.moveToNext()); + return list; } - + + /** + * Retrieves a topic from the database + * + * @param id + * @return + * @author ricky barrette + */ + public HashMap getTopic(final String id) { + return cursorToHashMap(mDb.query(TABLE_TOPIC_INFO, new String[] { KEY, KEY_VALUE }, KEY_TOPIC_ID + " = " + DatabaseUtils.sqlEscapeString(id), null, null, null, + null)); + } + + /** + * Retrieves a list of topic ids + * + * @return + * @author ricky barrette + */ + public List getTopics() { + final ArrayList list = new ArrayList(); + final Cursor c = mDb.query(TABLE_TOPICS, new String[] { KEY_ID }, null, null, null, null, null); + if (c.moveToFirst()) + do + list.add(c.getString(0)); + while (c.moveToNext()); + return list; + } + /** * Restores the database from the sdcard * @@ -504,7 +515,7 @@ public class ForumDatabase { /* * save the forums */ - if (result != null){ + if (result != null) { String id = null; for (final Object element : result) { final HashMap contentHash = (HashMap) element; @@ -577,14 +588,15 @@ public class ForumDatabase { private void upsertForumTable(final String forumId, final List values) throws NullPointerException { if (values == null) throw new NullPointerException("values was null"); - - ContentValues id = new ContentValues(); + + final ContentValues id = new ContentValues(); id.put(KEY_ID, forumId); - upsertTable(TABLE_FORUMS, KEY_ID +" = "+ DatabaseUtils.sqlEscapeString(forumId), id); + upsertTable(TABLE_FORUMS, KEY_ID + " = " + DatabaseUtils.sqlEscapeString(forumId), id); for (final ContentValues item : values) { item.put(KEY_FORUM_ID, forumId); - upsertTable(TABLE_FORUM_INFO, KEY + " = " + DatabaseUtils.sqlEscapeString(item.getAsString(KEY) + " AND " + KEY_FORUM_ID + " = " + DatabaseUtils.sqlEscapeString(forumId)), item); + upsertTable(TABLE_FORUM_INFO, + KEY + " = " + DatabaseUtils.sqlEscapeString(item.getAsString(KEY) + " AND " + KEY_FORUM_ID + " = " + DatabaseUtils.sqlEscapeString(forumId)), item); } } @@ -615,16 +627,17 @@ public class ForumDatabase { private void upsertThreadTable(final String forumId, final String topicId, final String postId, final List values) throws NullPointerException { if (values == null) throw new NullPointerException("values was null"); - - ContentValues id = new ContentValues(); + + final ContentValues id = new ContentValues(); id.put(KEY_ID, postId); id.put(KEY_FORUM_ID, forumId); id.put(KEY_TOPIC_ID, topicId); - upsertTable(TABLE_THREADS, KEY_ID +" = "+ DatabaseUtils.sqlEscapeString(postId), id); + upsertTable(TABLE_THREADS, KEY_ID + " = " + DatabaseUtils.sqlEscapeString(postId), id); for (final ContentValues item : values) { item.put(KEY_POST_ID, postId); - upsertTable( TABLE_THREAD_INFO, KEY + " = " + DatabaseUtils.sqlEscapeString(item.getAsString(KEY)) + " AND " + KEY_POST_ID + " = " + DatabaseUtils.sqlEscapeString(postId), item); + upsertTable(TABLE_THREAD_INFO, + KEY + " = " + DatabaseUtils.sqlEscapeString(item.getAsString(KEY)) + " AND " + KEY_POST_ID + " = " + DatabaseUtils.sqlEscapeString(postId), item); } } @@ -640,11 +653,11 @@ public class ForumDatabase { private void upsertTopicTable(final String forumId, final String topicId, final List values) throws NullPointerException { if (values == null) throw new NullPointerException("values was null"); - - ContentValues id = new ContentValues(); + + final ContentValues id = new ContentValues(); id.put(KEY_ID, topicId); id.put(KEY_FORUM_ID, forumId); - upsertTable(TABLE_TOPICS, KEY_ID +" = "+ DatabaseUtils.sqlEscapeString(topicId), id); + upsertTable(TABLE_TOPICS, KEY_ID + " = " + DatabaseUtils.sqlEscapeString(topicId), id); for (final ContentValues item : values) { item.put(KEY_TOPIC_ID, topicId);