Cleaned up code

Signed-off-by: Ricky Barrette <rickbarrette@gmail.com>
This commit is contained in:
2012-10-06 11:20:12 -04:00
parent 0eac47a347
commit 7ec5e1fa35
8 changed files with 131 additions and 117 deletions

View File

@@ -47,9 +47,10 @@ public class ForumListActivity extends FragmentActivity implements OnItemSelecte
Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(this)); Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(this));
setContentView(R.layout.activity_forum_list); setContentView(R.layout.activity_forum_list);
new Thread(new Runnable(){ new Thread(new Runnable() {
public void run(){ @Override
public void run() {
Looper.prepare(); Looper.prepare();
new ForumDatabase(ForumListActivity.this).saveForums(true); new ForumDatabase(ForumListActivity.this).saveForums(true);
} }

View File

@@ -65,21 +65,20 @@ public class ForumListFragment extends ListFragment {
public void onCreate(final Bundle savedInstanceState) { public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
if (ForumContent.ITEMS.size() == 0){ if (ForumContent.ITEMS.size() == 0) {
// new Thread(new Runnable() { // new Thread(new Runnable() {
// @Override // @Override
// public void run() { // public void run() {
ForumContent.getForum(getActivity()); ForumContent.getForum(getActivity());
getActivity().runOnUiThread(new Runnable() { getActivity().runOnUiThread(new Runnable() {
@Override @Override
public void run() { public void run() {
setListAdapter(new ForumAdapter(getActivity())); setListAdapter(new ForumAdapter(getActivity()));
} }
}); });
// } // }
// }).start(); // }).start();
} } else
else
setListAdapter(new ForumAdapter(getActivity())); setListAdapter(new ForumAdapter(getActivity()));
} }

View File

@@ -105,12 +105,12 @@ public class ForumContent {
public static void getForum(final Context context) { public static void getForum(final Context context) {
ITEMS.clear(); ITEMS.clear();
ITEM_MAP.clear(); ITEM_MAP.clear();
final ForumDatabase db = new ForumDatabase(context); final ForumDatabase db = new ForumDatabase(context);
List<String> list = db.getForums(); final List<String> list = db.getForums();
int index = 0; int index = 0;
for(String item : list) for (final String item : list)
addItem(new ForumItem(Integer.valueOf(index++).toString(), db.getForum(item))); addItem(new ForumItem(Integer.valueOf(index++).toString(), db.getForum(item)));
} }
} }

View File

@@ -88,12 +88,12 @@ public class ThreadContent {
public static void getThread(final String topicId, final int startNumber, final int lastNumber, final Context context) { public static void getThread(final String topicId, final int startNumber, final int lastNumber, final Context context) {
ITEMS.clear(); ITEMS.clear();
ITEM_MAP.clear(); ITEM_MAP.clear();
final ForumDatabase db = new ForumDatabase(context); final ForumDatabase db = new ForumDatabase(context);
List<String> list = db.getThreads(); final List<String> list = db.getThreads();
int index = 0; int index = 0;
for(String item : list) for (final String item : list)
addItem(new ThreadItem(Integer.valueOf(index++).toString(), db.getThread(item))); addItem(new ThreadItem(Integer.valueOf(index++).toString(), db.getThread(item)));
} }

View File

@@ -130,7 +130,7 @@ public class TopicAdapter extends BaseAdapter {
holder.title.setText((String) getItem(position).get("topic_title")); holder.title.setText((String) getItem(position).get("topic_title"));
holder.user.setText((String) getItem(position).get("topic_author_name")); holder.user.setText((String) getItem(position).get("topic_author_name"));
holder.lastestPost.setText((String) getItem(position).get("short_content")); 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; return convertView;
} }

View File

@@ -86,12 +86,12 @@ public class TopicContent {
public static void getTopics(final String forumId, final Context context) { public static void getTopics(final String forumId, final Context context) {
ITEMS.clear(); ITEMS.clear();
ITEM_MAP.clear(); ITEM_MAP.clear();
final ForumDatabase db = new ForumDatabase(context); final ForumDatabase db = new ForumDatabase(context);
List<String> list = db.getTopics(); final List<String> list = db.getTopics();
int index = 0; int index = 0;
for(String item : list) for (final String item : list)
addItem(new TopicItem(Integer.valueOf(index++).toString(), db.getTopic(item))); addItem(new TopicItem(Integer.valueOf(index++).toString(), db.getTopic(item)));
} }
} }

View File

@@ -20,20 +20,21 @@
package org.RickBarrette.osj.forum.database; 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 * @author ricky barrette
*/ */
public interface DatabaseListener { public interface DatabaseListener {
public void onDatabaseCreate(); public void onDatabaseCreate();
public void onDatabaseUpgrade(); public void onDatabaseUpgrade();
public void onDatabaseUpgradeComplete(); public void onDatabaseUpgradeComplete();
public void onDeletionComplete();
public void onRestoreComplete(); public void onRestoreComplete();
public void onDeletionComplete();
} }

View File

@@ -78,19 +78,20 @@ public class ForumDatabase {
* @author ricky barrette * @author ricky barrette
*/ */
private void createDatabase(final SQLiteDatabase db) { private void createDatabase(final SQLiteDatabase db) {
/* /*
* These tables will be used for storing forum, topic, and post id's * 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_FORUMS + "( id TEXT PRIMARY KEY)");
db.execSQL("CREATE TABLE " + TABLE_TOPICS + "( id TEXT PRIMARY KEY, "+ KEY_FORUM_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)"); 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_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)"); 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_FORUMS);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_TOPICS); db.execSQL("DROP TABLE IF EXISTS " + TABLE_TOPICS);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_THREADS); db.execSQL("DROP TABLE IF EXISTS " + TABLE_THREADS);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_FORUM_INFO); 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_TOPIC_INFO);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_THREAD_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<String, String> cursorToHashMap(final Cursor c) {
final HashMap<String, String> h = new HashMap<String, String>();
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 * deletes a note by its row id
* *
@@ -361,96 +378,90 @@ public class ForumDatabase {
}).start(); }).start();
} }
/**
* Retrieves a list of forum ids
* @return
* @author ricky barrette
*/
public List<String> getForums(){
ArrayList<String> list = new ArrayList<String>();
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<String> getTopics(){
ArrayList<String> list = new ArrayList<String>();
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<String> getThreads(){
ArrayList<String> list = new ArrayList<String>();
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 * Retrieves a forum from the database
*
* @param id * @param id
* @return * @return
* @author ricky barrette * @author ricky barrette
*/ */
public HashMap<String, String> getForum(String id){ public HashMap<String, String> 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)); 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 * Retrieves a list of forum ids
* @param id *
* @return * @return
* @author ricky barrette * @author ricky barrette
*/ */
public HashMap<String, String> getTopic(String id) { public List<String> getForums() {
return cursorToHashMap(mDb.query(TABLE_TOPIC_INFO, new String[] { KEY, KEY_VALUE }, KEY_TOPIC_ID +" = "+ DatabaseUtils.sqlEscapeString(id), null, null, null, null)); final ArrayList<String> list = new ArrayList<String>();
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 * Retrieves a post from the threads table of the database
*
* @param id * @param id
* @return * @return
* @author ricky barrette * @author ricky barrette
*/ */
public HashMap<String, String> getThread(String id) { public HashMap<String, String> 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)); 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 * Retrieves a list of thread ids
* @param c *
* @return * @return
* @author ricky barrette * @author ricky barrette
*/ */
private HashMap<String, String> cursorToHashMap(Cursor c){ public List<String> getThreads() {
HashMap<String, String> h = new HashMap<String, String>(); final ArrayList<String> list = new ArrayList<String>();
if(c.moveToFirst()) final Cursor c = mDb.query(TABLE_THREADS, new String[] { KEY_ID }, null, null, null, null, null);
do{ if (c.moveToFirst())
h.put(c.getString(c.getColumnIndex(KEY)), c.getString(c.getColumnIndex(KEY_VALUE))); do
} while(c.moveToNext()); list.add(c.getString(0));
return h; while (c.moveToNext());
return list;
} }
/**
* Retrieves a topic from the database
*
* @param id
* @return
* @author ricky barrette
*/
public HashMap<String, String> 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<String> getTopics() {
final ArrayList<String> list = new ArrayList<String>();
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 * Restores the database from the sdcard
* *
@@ -504,7 +515,7 @@ public class ForumDatabase {
/* /*
* save the forums * save the forums
*/ */
if (result != null){ if (result != null) {
String id = null; String id = null;
for (final Object element : result) { for (final Object element : result) {
final HashMap<?, ?> contentHash = (HashMap<?, ?>) element; final HashMap<?, ?> contentHash = (HashMap<?, ?>) element;
@@ -577,14 +588,15 @@ public class ForumDatabase {
private void upsertForumTable(final String forumId, final List<ContentValues> values) throws NullPointerException { private void upsertForumTable(final String forumId, final List<ContentValues> values) throws NullPointerException {
if (values == null) if (values == null)
throw new NullPointerException("values was null"); throw new NullPointerException("values was null");
ContentValues id = new ContentValues(); final ContentValues id = new ContentValues();
id.put(KEY_ID, forumId); 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) { for (final ContentValues item : values) {
item.put(KEY_FORUM_ID, forumId); 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<ContentValues> values) throws NullPointerException { private void upsertThreadTable(final String forumId, final String topicId, final String postId, final List<ContentValues> values) throws NullPointerException {
if (values == null) if (values == null)
throw new NullPointerException("values was null"); throw new NullPointerException("values was null");
ContentValues id = new ContentValues(); final ContentValues id = new ContentValues();
id.put(KEY_ID, postId); id.put(KEY_ID, postId);
id.put(KEY_FORUM_ID, forumId); id.put(KEY_FORUM_ID, forumId);
id.put(KEY_TOPIC_ID, topicId); 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) { for (final ContentValues item : values) {
item.put(KEY_POST_ID, postId); 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<ContentValues> values) throws NullPointerException { private void upsertTopicTable(final String forumId, final String topicId, final List<ContentValues> values) throws NullPointerException {
if (values == null) if (values == null)
throw new NullPointerException("values was null"); throw new NullPointerException("values was null");
ContentValues id = new ContentValues(); final ContentValues id = new ContentValues();
id.put(KEY_ID, topicId); id.put(KEY_ID, topicId);
id.put(KEY_FORUM_ID, forumId); 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) { for (final ContentValues item : values) {
item.put(KEY_TOPIC_ID, topicId); item.put(KEY_TOPIC_ID, topicId);