From 12f4f727296065e7f7d7e12498bb3890a9b793b7 Mon Sep 17 00:00:00 2001 From: Ricky Barrette Date: Thu, 13 Sep 2012 12:29:35 -0400 Subject: [PATCH] Cleaned up topic content data model Signed-off-by: Ricky Barrette --- OSJ Forum/AndroidManifest.xml | 3 +- OSJ Forum/res/layout/topic_item.xml | 17 ++- OSJ Forum/res/values/strings.xml | 5 +- .../RickBarrette/osj/forum/Constraints.java | 41 +++++++ .../osj/forum/ForumDetailFragment.java | 33 +++++- .../osj/forum/ForumListActivity.java | 6 +- .../osj/forum/ForumListFragment.java | 19 ++-- .../osj/forum/TopicListActivity.java | 6 +- .../osj/forum/TopicListFragment.java | 20 ++-- .../osj/forum/content/ForumContent.java | 34 ++---- .../forum/content/OnItemSelectedListener.java | 36 ++++++ .../osj/forum/content/TopicAdapter.java | 14 +-- .../osj/forum/content/TopicContent.java | 104 ++++++++++++++++++ 13 files changed, 272 insertions(+), 66 deletions(-) create mode 100644 OSJ Forum/src/com/RickBarrette/osj/forum/Constraints.java create mode 100644 OSJ Forum/src/com/RickBarrette/osj/forum/content/OnItemSelectedListener.java create mode 100644 OSJ Forum/src/com/RickBarrette/osj/forum/content/TopicContent.java diff --git a/OSJ Forum/AndroidManifest.xml b/OSJ Forum/AndroidManifest.xml index 297d3cd..0277911 100644 --- a/OSJ Forum/AndroidManifest.xml +++ b/OSJ Forum/AndroidManifest.xml @@ -1,5 +1,5 @@ + \ No newline at end of file diff --git a/OSJ Forum/res/layout/topic_item.xml b/OSJ Forum/res/layout/topic_item.xml index 84e0c68..cc32789 100644 --- a/OSJ Forum/res/layout/topic_item.xml +++ b/OSJ Forum/res/layout/topic_item.xml @@ -1,14 +1,15 @@ + android:layout_height="wrap_content" > + android:layout_centerVertical="true" + android:src="@drawable/ic_launcher" /> + + \ No newline at end of file diff --git a/OSJ Forum/res/values/strings.xml b/OSJ Forum/res/values/strings.xml index a495166..fefac87 100644 --- a/OSJ Forum/res/values/strings.xml +++ b/OSJ Forum/res/values/strings.xml @@ -1,5 +1,4 @@ - OSJ Forum Topic Detail Topics @@ -11,5 +10,7 @@ Post User Lastest Post - + New Posts + test + Test1 \ No newline at end of file diff --git a/OSJ Forum/src/com/RickBarrette/osj/forum/Constraints.java b/OSJ Forum/src/com/RickBarrette/osj/forum/Constraints.java new file mode 100644 index 0000000..6895866 --- /dev/null +++ b/OSJ Forum/src/com/RickBarrette/osj/forum/Constraints.java @@ -0,0 +1,41 @@ +/** + * Constraints.java + * @date Sep 13, 2012 + * @author ricky barrette + * + * Copyright 2012 Richard Barrette + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ +package com.RickBarrette.osj.forum; + +import org.xmlrpc.android.XMLRPCClient; + +import android.content.Context; + +/** + * This class will be used to house the constraints of this application + * @author ricky barrette + */ +public class Constraints { + + /** + * Creates an XML RPC Client + * @param Context + * @return + * @author ricky barrette + */ + public static XMLRPCClient getClient(Context context) { + return new XMLRPCClient(context.getString(R.string.server), context.getString(R.string.username), context.getString(R.string.password)); + } +} \ No newline at end of file diff --git a/OSJ Forum/src/com/RickBarrette/osj/forum/ForumDetailFragment.java b/OSJ Forum/src/com/RickBarrette/osj/forum/ForumDetailFragment.java index 76a3938..8e48cd8 100644 --- a/OSJ Forum/src/com/RickBarrette/osj/forum/ForumDetailFragment.java +++ b/OSJ Forum/src/com/RickBarrette/osj/forum/ForumDetailFragment.java @@ -19,6 +19,7 @@ */ package com.RickBarrette.osj.forum; +import android.app.Activity; import android.os.Bundle; import android.support.v4.app.ListFragment; import android.view.View; @@ -28,7 +29,9 @@ import android.widget.ListView; import com.RickBarrette.osj.forum.content.ForumContent; import com.RickBarrette.osj.forum.content.ForumContent.ForumItem; +import com.RickBarrette.osj.forum.content.OnItemSelectedListener; import com.RickBarrette.osj.forum.content.TopicAdapter; +import com.RickBarrette.osj.forum.content.TopicContent; /** * This fragment will be used to display information about the forum @@ -41,31 +44,55 @@ public class ForumDetailFragment extends ListFragment { private static final String STATE_ACTIVATED_POSITION = "activated_position"; ForumItem mItem; + private OnItemSelectedListener mCallbacks; public ForumDetailFragment() { } + /** + * (non-Javadoc) + * @see android.support.v4.app.Fragment#onAttach(android.app.Activity) + */ + @Override + public void onAttach(Activity activity) { + super.onAttach(activity); + if (!(activity instanceof OnItemSelectedListener)) + throw new IllegalStateException("Activity must implement fragment's callbacks."); + + mCallbacks = (OnItemSelectedListener) activity; + } + @Override public void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getArguments().containsKey(ARG_ITEM_ID)) mItem = ForumContent.ITEM_MAP.get(getArguments().getString(ARG_ITEM_ID)); - new Thread( new Runnable(){ @Override public void run(){ - final Object[] result = mItem.getTopics(getActivity()); + TopicContent.getTopics((String) mItem.content.get("forum_id"), getActivity()); getActivity().runOnUiThread(new Runnable(){ @Override public void run(){ - setListAdapter(new TopicAdapter(getActivity(), result)); + setListAdapter(new TopicAdapter(getActivity())); } }); } }).start(); } + + /** + * (non-Javadoc) + * @see android.support.v4.app.ListFragment#onListItemClick(android.widget.ListView, android.view.View, int, long) + */ + @Override + public void onListItemClick(ListView l, View v, int position, long id) { + super.onListItemClick(l, v, position, id); + mCallbacks.onItemSelected(this, TopicContent.ITEMS.get(position).id); + } + @Override public void onSaveInstanceState(final Bundle outState) { super.onSaveInstanceState(outState); diff --git a/OSJ Forum/src/com/RickBarrette/osj/forum/ForumListActivity.java b/OSJ Forum/src/com/RickBarrette/osj/forum/ForumListActivity.java index 49d97f3..fca4783 100644 --- a/OSJ Forum/src/com/RickBarrette/osj/forum/ForumListActivity.java +++ b/OSJ Forum/src/com/RickBarrette/osj/forum/ForumListActivity.java @@ -21,15 +21,17 @@ package com.RickBarrette.osj.forum; import android.content.Intent; import android.os.Bundle; +import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; +import com.RickBarrette.osj.forum.content.OnItemSelectedListener; import com.TwentyCodes.android.exception.ExceptionHandler; /** * * @author ricky barrette */ -public class ForumListActivity extends FragmentActivity implements ForumListFragment.Callbacks { +public class ForumListActivity extends FragmentActivity implements OnItemSelectedListener { private boolean mTwoPane; @@ -47,7 +49,7 @@ public class ForumListActivity extends FragmentActivity implements ForumListFrag } @Override - public void onItemSelected(final String id) { + public void onItemSelected(final Fragment listFragment, final String id) { if (mTwoPane) { final Bundle arguments = new Bundle(); arguments.putString(ForumDetailFragment.ARG_ITEM_ID, id); diff --git a/OSJ Forum/src/com/RickBarrette/osj/forum/ForumListFragment.java b/OSJ Forum/src/com/RickBarrette/osj/forum/ForumListFragment.java index feea6d6..79f8734 100644 --- a/OSJ Forum/src/com/RickBarrette/osj/forum/ForumListFragment.java +++ b/OSJ Forum/src/com/RickBarrette/osj/forum/ForumListFragment.java @@ -21,6 +21,7 @@ package com.RickBarrette.osj.forum; import android.app.Activity; import android.os.Bundle; +import android.support.v4.app.Fragment; import android.support.v4.app.ListFragment; import android.view.View; import android.widget.AbsListView; @@ -29,6 +30,7 @@ import android.widget.ListView; import com.RickBarrette.osj.forum.content.ForumAdapter; import com.RickBarrette.osj.forum.content.ForumContent; +import com.RickBarrette.osj.forum.content.OnItemSelectedListener; /** * @@ -36,19 +38,14 @@ import com.RickBarrette.osj.forum.content.ForumContent; */ public class ForumListFragment extends ListFragment { - public interface Callbacks { - - public void onItemSelected(String id); - } - private static final String STATE_ACTIVATED_POSITION = "activated_position"; - private Callbacks mCallbacks = sForumCallbacks; + private OnItemSelectedListener mCallbacks = sForumCallbacks; private int mActivatedPosition = ListView.INVALID_POSITION; - private static Callbacks sForumCallbacks = new Callbacks() { + private static OnItemSelectedListener sForumCallbacks = new OnItemSelectedListener() { @Override - public void onItemSelected(final String id) { + public void onItemSelected(final Fragment listFragment, final String id) { } }; @@ -58,10 +55,10 @@ public class ForumListFragment extends ListFragment { @Override public void onAttach(final Activity activity) { super.onAttach(activity); - if (!(activity instanceof Callbacks)) + if (!(activity instanceof OnItemSelectedListener)) throw new IllegalStateException("Activity must implement fragment's callbacks."); - mCallbacks = (Callbacks) activity; + mCallbacks = (OnItemSelectedListener) activity; } @Override @@ -95,7 +92,7 @@ public class ForumListFragment extends ListFragment { @Override public void onListItemClick(final ListView listView, final View view, final int position, final long id) { super.onListItemClick(listView, view, position, id); - mCallbacks.onItemSelected(ForumContent.ITEMS.get(position).id); + mCallbacks.onItemSelected(this, ForumContent.ITEMS.get(position).id); } @Override diff --git a/OSJ Forum/src/com/RickBarrette/osj/forum/TopicListActivity.java b/OSJ Forum/src/com/RickBarrette/osj/forum/TopicListActivity.java index 1b4dad3..026ce75 100644 --- a/OSJ Forum/src/com/RickBarrette/osj/forum/TopicListActivity.java +++ b/OSJ Forum/src/com/RickBarrette/osj/forum/TopicListActivity.java @@ -21,11 +21,13 @@ package com.RickBarrette.osj.forum; import android.content.Intent; import android.os.Bundle; +import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; +import com.RickBarrette.osj.forum.content.OnItemSelectedListener; import com.TwentyCodes.android.exception.ExceptionHandler; -public class TopicListActivity extends FragmentActivity implements TopicListFragment.Callbacks { +public class TopicListActivity extends FragmentActivity implements OnItemSelectedListener { private boolean mTwoPane; @@ -43,7 +45,7 @@ public class TopicListActivity extends FragmentActivity implements TopicListFrag } @Override - public void onItemSelected(final String id) { + public void onItemSelected(final Fragment listFragment, final String id) { if (mTwoPane) { final Bundle arguments = new Bundle(); arguments.putString(TopicDetailFragment.ARG_ITEM_ID, id); diff --git a/OSJ Forum/src/com/RickBarrette/osj/forum/TopicListFragment.java b/OSJ Forum/src/com/RickBarrette/osj/forum/TopicListFragment.java index fcc2a3c..0e45c55 100644 --- a/OSJ Forum/src/com/RickBarrette/osj/forum/TopicListFragment.java +++ b/OSJ Forum/src/com/RickBarrette/osj/forum/TopicListFragment.java @@ -21,6 +21,7 @@ package com.RickBarrette.osj.forum; import android.app.Activity; import android.os.Bundle; +import android.support.v4.app.Fragment; import android.support.v4.app.ListFragment; import android.view.View; import android.widget.AbsListView; @@ -28,23 +29,19 @@ import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; +import com.RickBarrette.osj.forum.content.OnItemSelectedListener; import com.RickBarrette.osj.forum.dummy.DummyContent; public class TopicListFragment extends ListFragment { - public interface Callbacks { - - public void onItemSelected(String id); - } - private static final String STATE_ACTIVATED_POSITION = "activated_position"; - private Callbacks mCallbacks = sDummyCallbacks; + private OnItemSelectedListener mCallbacks = sDummyCallbacks; private int mActivatedPosition = ListView.INVALID_POSITION; - private static Callbacks sDummyCallbacks = new Callbacks() { + private static OnItemSelectedListener sDummyCallbacks = new OnItemSelectedListener() { @Override - public void onItemSelected(final String id) { + public void onItemSelected(final Fragment listFragment, final String id) { } }; @@ -54,10 +51,11 @@ public class TopicListFragment extends ListFragment { @Override public void onAttach(final Activity activity) { super.onAttach(activity); - if (!(activity instanceof Callbacks)) + if (!(activity instanceof OnItemSelectedListener)) + throw new IllegalStateException("Activity must implement fragment's callbacks."); - mCallbacks = (Callbacks) activity; + mCallbacks = (OnItemSelectedListener) activity; } @Override @@ -75,7 +73,7 @@ public class TopicListFragment extends ListFragment { @Override public void onListItemClick(final ListView listView, final View view, final int position, final long id) { super.onListItemClick(listView, view, position, id); - mCallbacks.onItemSelected(DummyContent.ITEMS.get(position).id); + mCallbacks.onItemSelected(this, DummyContent.ITEMS.get(position).id); } @Override diff --git a/OSJ Forum/src/com/RickBarrette/osj/forum/content/ForumContent.java b/OSJ Forum/src/com/RickBarrette/osj/forum/content/ForumContent.java index 01096a7..f4cca9f 100644 --- a/OSJ Forum/src/com/RickBarrette/osj/forum/content/ForumContent.java +++ b/OSJ Forum/src/com/RickBarrette/osj/forum/content/ForumContent.java @@ -33,7 +33,7 @@ import android.util.Log; import com.RickBarrette.osj.forum.R; /** - * This + * This class is used to maintian the forum's content data * @author ricky barrette */ public class ForumContent { @@ -62,14 +62,6 @@ public class ForumContent { Log.d(TAG, key.toString()); } - /** - * Returns the name of this forum object - */ - @Override - public String toString() { - return (String) content.get("forum_name"); - } - /** * This gets all the categories and sub-forums * @return array of HashMap @@ -80,24 +72,17 @@ public class ForumContent { } /** - * This gets all the topics for the current forum item - * @param context - * @return - * @author ricky barrette + * Returns the name of this forum object */ - public Object[] getTopics(final Context context){ - final XMLRPCClient client = new XMLRPCClient((context.getString(R.string.server))); - try { - return (Object[]) ((HashMap) client.call("get_topic", this.content.get("forum_id"), 0, 0)).get("topics"); - } catch (XMLRPCException e) { - e.printStackTrace(); - } - return null; + @Override + public String toString() { + return (String) content.get("forum_name"); } + } - public static List ITEMS = new ArrayList(); - public static Map ITEM_MAP = new HashMap(); + public static final List ITEMS = new ArrayList(); + public static final Map ITEM_MAP = new HashMap(); /** * Adds a new Forum Item to the list @@ -115,6 +100,9 @@ public class ForumContent { * @author ricky barrette */ public static void getForum(Context context){ + ITEMS.clear(); + ITEM_MAP.clear(); + final XMLRPCClient client = new XMLRPCClient((context.getString(R.string.server))); Object[] result = null; diff --git a/OSJ Forum/src/com/RickBarrette/osj/forum/content/OnItemSelectedListener.java b/OSJ Forum/src/com/RickBarrette/osj/forum/content/OnItemSelectedListener.java new file mode 100644 index 0000000..ad9c9af --- /dev/null +++ b/OSJ Forum/src/com/RickBarrette/osj/forum/content/OnItemSelectedListener.java @@ -0,0 +1,36 @@ +/** + * OnItemSelectedListener.java + * @date Sep 13, 2012 + * @author ricky barrette + * + * Copyright 2012 Richard Barrette + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ +package com.RickBarrette.osj.forum.content; + +import android.support.v4.app.Fragment; + +/** + * This is a simple on item selcted callback for list fragments + * @author ricky barrette + */ +public interface OnItemSelectedListener { + /** + * Called when a list item from a list fragment is selected + * @param listFragment + * @param id of selected item + * @author ricky barrette + */ + public void onItemSelected(final Fragment listFragment, final String id); +} \ No newline at end of file diff --git a/OSJ Forum/src/com/RickBarrette/osj/forum/content/TopicAdapter.java b/OSJ Forum/src/com/RickBarrette/osj/forum/content/TopicAdapter.java index 519b95c..0595ddb 100644 --- a/OSJ Forum/src/com/RickBarrette/osj/forum/content/TopicAdapter.java +++ b/OSJ Forum/src/com/RickBarrette/osj/forum/content/TopicAdapter.java @@ -42,21 +42,20 @@ public class TopicAdapter extends BaseAdapter { TextView title; TextView lastestPost; TextView user; + TextView newPosts; QuickContactBadge badage; } private static final String TAG = "TopicAdapter"; private LayoutInflater mInflater; - private Object[] mTopics; /** * * @author ricky barrette */ - public TopicAdapter(final Context context, final Object[] topics) { + public TopicAdapter(final Context context) { mInflater = LayoutInflater.from(context); - mTopics = topics; } /** @@ -65,10 +64,7 @@ public class TopicAdapter extends BaseAdapter { */ @Override public int getCount() { - if(mTopics != null) - return mTopics.length; - else - return 0; + return TopicContent.ITEMS.size(); } /** @@ -77,7 +73,7 @@ public class TopicAdapter extends BaseAdapter { */ @Override public HashMap getItem(int position) { - return (HashMap) mTopics[position]; + return (HashMap) TopicContent.ITEMS.get(position).content; } /** @@ -114,6 +110,7 @@ public class TopicAdapter extends BaseAdapter { holder.title = (TextView) convertView.findViewById(R.id.textView1); holder.lastestPost = (TextView) convertView.findViewById(R.id.textView2); holder.user = (TextView) convertView.findViewById(R.id.textView3); + holder.newPosts = (TextView) convertView.findViewById(R.id.textView4); holder.badage = (QuickContactBadge) convertView.findViewById(R.id.quickContactBadge1); convertView.setTag(holder); @@ -131,6 +128,7 @@ public class TopicAdapter extends BaseAdapter { holder.title.setText(new String((byte[]) getItem(position).get("topic_title"))); holder.user.setText(new String((byte[]) getItem(position).get("topic_author_name"))); holder.lastestPost.setText(new String((byte[]) getItem(position).get("short_content"))); + holder.newPosts.setVisibility(((Boolean) getItem(position).get("new_post") ? View.VISIBLE : View.GONE)); for(Object key: getItem(position).keySet()) Log.d(TAG, key.toString()); diff --git a/OSJ Forum/src/com/RickBarrette/osj/forum/content/TopicContent.java b/OSJ Forum/src/com/RickBarrette/osj/forum/content/TopicContent.java new file mode 100644 index 0000000..ebafd87 --- /dev/null +++ b/OSJ Forum/src/com/RickBarrette/osj/forum/content/TopicContent.java @@ -0,0 +1,104 @@ +/** + * TopicContent.java + * @date Sep 13, 2012 + * @author ricky barrette + * + * Copyright 2012 Richard Barrette + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ +package com.RickBarrette.osj.forum.content; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.xmlrpc.android.XMLRPCClient; +import org.xmlrpc.android.XMLRPCException; + +import android.content.Context; +import android.util.Log; + +import com.RickBarrette.osj.forum.Constraints; + +/** + * This class is used to maintain a topic's content data + * @author ricky barrette + */ +public class TopicContent { + + /** + * This Forum Object + * @author ricky barrette + */ + public static class TopicItem { + private static final String TAG = "TopicItem"; + public String id; + public HashMap content; + + /** + * Creates a new Topic Item + * @param id + * @param content + * @author ricky barrette + */ + public TopicItem(final String id, final HashMap content) { + this.id = id; + this.content = content; + + for(Object key: content.keySet()) + Log.d(TAG, key.toString()); + } + } + + public static final List ITEMS = new ArrayList(); + public static final Map ITEM_MAP = new HashMap(); + + /** + * Adds a new Forum Item to the list + * @param item + * @author ricky barrette + */ + private static void addItem(final TopicItem item) { + ITEMS.add(item); + ITEM_MAP.put(item.id, item); + } + + /** + * This gets all the topics for the current forum item + * @param context + * @return + * @author ricky barrette + */ + public static void getTopics(String forumId, final Context context){ + final XMLRPCClient client = Constraints.getClient(context); + + ITEMS.clear(); + ITEM_MAP.clear(); + + Object[] result = null; + try { + result = (Object[]) ((HashMap) client.call("get_topic", forumId, 0, 0)).get("topics"); + } catch (XMLRPCException e) { + e.printStackTrace(); + } + + if(result!= null){ + for(int i = 0; i < result.length; i++){ + HashMap contentHash = (HashMap) result[i]; + addItem(new TopicItem(Integer.valueOf(i).toString(), contentHash)); + } + } + } +}