Started working on updating the UI after a database sync

Signed-off-by: Ricky Barrette <rickbarrette@gmail.com>
This commit is contained in:
2012-10-07 09:00:48 -04:00
parent e7e59f9d1b
commit b96e16f9ab
6 changed files with 81 additions and 33 deletions

View File

@@ -1,5 +1,5 @@
<manifest package="org.RickBarrette.osj.forum"
android:versionCode="328"
android:versionCode="344"
android:versionName="1.0" xmlns:android="http://schemas.android.com/apk/res/android" android:installLocation="auto">
<uses-sdk

View File

@@ -20,11 +20,9 @@
package org.RickBarrette.osj.forum;
import org.RickBarrette.osj.forum.content.OnItemSelectedListener;
import org.RickBarrette.osj.forum.database.ForumDatabase;
import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
@@ -48,14 +46,6 @@ public class ForumListActivity extends FragmentActivity implements OnItemSelecte
setContentView(R.layout.activity_forum_list);
new Thread(new Runnable() {
@Override
public void run() {
Looper.prepare();
new ForumDatabase(ForumListActivity.this).saveForums(true);
}
}).start();
if (findViewById(R.id.forum_detail_container) != null) {
mTwoPane = true;
((ForumListFragment) getSupportFragmentManager().findFragmentById(R.id.forum_list)).setActivateOnItemClick(true);

View File

@@ -22,6 +22,8 @@ package org.RickBarrette.osj.forum;
import org.RickBarrette.osj.forum.content.ForumAdapter;
import org.RickBarrette.osj.forum.content.ForumContent;
import org.RickBarrette.osj.forum.content.OnItemSelectedListener;
import org.RickBarrette.osj.forum.database.DatabaseListener;
import org.RickBarrette.osj.forum.database.ForumDatabase;
import android.app.Activity;
import android.os.Bundle;
@@ -36,11 +38,12 @@ import android.widget.ListView;
*
* @author ricky barrette
*/
public class ForumListFragment extends ListFragment {
public class ForumListFragment extends ListFragment implements DatabaseListener {
private ForumDatabase mDb;
private static final String STATE_ACTIVATED_POSITION = "activated_position";
private static final String TAG = "ForumListFragment";
private OnItemSelectedListener mCallbacks = sForumCallbacks;
private int mActivatedPosition = ListView.INVALID_POSITION;
private static OnItemSelectedListener sForumCallbacks = new OnItemSelectedListener() {
@@ -64,23 +67,19 @@ public class ForumListFragment extends ListFragment {
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.v(TAG, "onCreate()");
if (ForumContent.ITEMS.size() == 0) {
// new Thread(new Runnable() {
// @Override
// public void run() {
ForumContent.getForum(getActivity());
getActivity().runOnUiThread(new Runnable() {
mDb = new ForumDatabase(this.getActivity(), this);
new Thread(new Runnable() {
@Override
public void run() {
setListAdapter(new ForumAdapter(getActivity()));
mDb.saveForums(true);
}
});
// }
// }).start();
} else
setListAdapter(new ForumAdapter(getActivity()));
}).start();
if (ForumContent.ITEMS.size() != 0)
setListAdapter(new ForumAdapter(getActivity()));
}
@Override
@@ -121,4 +120,45 @@ public class ForumListFragment extends ListFragment {
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

@@ -27,8 +27,6 @@ import java.util.Map;
import org.RickBarrette.osj.forum.Log;
import org.RickBarrette.osj.forum.database.ForumDatabase;
import android.content.Context;
/**
* This class is used to maintain an instance of a forum's content data
*
@@ -102,11 +100,10 @@ public class ForumContent {
* @param context
* @author ricky barrette
*/
public static void getForum(final Context context) {
public static void getForum(final ForumDatabase db) {
ITEMS.clear();
ITEM_MAP.clear();
final ForumDatabase db = new ForumDatabase(context);
final List<String> list = db.getForums();
int index = 0;

View File

@@ -37,4 +37,6 @@ public interface DatabaseListener {
public void onRestoreComplete();
public void onSyncComplete();
}

View File

@@ -539,6 +539,8 @@ public class ForumDatabase {
}
}
if(mListener != null)
mListener.onSyncComplete();
}
/**
@@ -549,6 +551,19 @@ public class ForumDatabase {
* @author ricky barrette
*/
public void saveThreads(final XMLRPCClient client, final Object forumId, final Object topicId) {
saveThreads(client, forumId, topicId, false);
}
/**
* Downloads and save threads into the database
*
* @param client
* @param forumId
* @param topicId
* @param isRecursive
* @author ricky barrette
*/
private void saveThreads(final XMLRPCClient client, final Object forumId, final Object topicId, final boolean isRecursive) {
Object[] result = null;
try {
result = (Object[]) ((HashMap<?, ?>) client.call("get_thread", topicId, 0, 0)).get("posts");
@@ -562,6 +577,8 @@ public class ForumDatabase {
upsertThreadTable((String) forumId, (String) topicId, (String) contentHash.get("post_id"), (Date) contentHash.get("post_time"), convertHashMapToContentValues(contentHash));
}
if(!isRecursive && mListener != null)
mListener.onSyncComplete();
}
/**
@@ -585,9 +602,11 @@ public class ForumDatabase {
final HashMap<?, ?> contentHash = (HashMap<?, ?>) element;
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"));
saveThreads(client, forumId, contentHash.get("topic_id"), isRecursive);
}
if(!isRecursive && mListener != null)
mListener.onSyncComplete();
}
/**