android-ibc-forum

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit 152997da423549e64cda29926dbbc542c09131e7
parent ea32e4c03279b793ab18a36d85658ba2f4f01ae0
Author: Jan Dankert <devnull@localhost>
Date:   Tue, 31 Jan 2012 22:21:40 +0100

Tapatalk-Client erweitert, alle Listviews mit Themen-Listen laden beim Scrollen automatisch nach und haben Kontext-Menü.

Diffstat:
src/de/mtbnews/android/EndlessListActivity.java | 2+-
src/de/mtbnews/android/ForumActivity.java | 415+++++++++++++++++++------------------------------------------------------------
src/de/mtbnews/android/ForumOverviewActivity.java | 15+++++++++------
src/de/mtbnews/android/SearchActivity.java | 242+++++++++++++++++++++++++++++++++++++++----------------------------------------
src/de/mtbnews/android/TopicActivity.java | 6++++--
src/de/mtbnews/android/tapatalk/TapatalkClient.java | 293+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------
src/de/mtbnews/android/tapatalk/wrapper/Forum.java | 1+
src/de/mtbnews/android/tapatalk/wrapper/Mailbox.java | 52++++++++++++++++++++++++++++++++++++++++++++++++++++
src/de/mtbnews/android/tapatalk/wrapper/Message.java | 60++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
9 files changed, 578 insertions(+), 508 deletions(-)

diff --git a/src/de/mtbnews/android/EndlessListActivity.java b/src/de/mtbnews/android/EndlessListActivity.java @@ -130,7 +130,7 @@ public abstract class EndlessListActivity<T> extends ListActivity * @return gelandene Elemente */ abstract protected void loadEntries(OnListLoadedListener<T> onListLoaded, - int from, int to, boolean firstLoad); + final int from, final int to, boolean firstLoad); /** * @param onListLoaded diff --git a/src/de/mtbnews/android/ForumActivity.java b/src/de/mtbnews/android/ForumActivity.java @@ -4,14 +4,8 @@ package de.mtbnews.android; import java.io.IOException; -import java.util.ArrayList; -import java.util.List; import java.util.Map; -import org.xmlrpc.android.XMLRPCClient; -import org.xmlrpc.android.XMLRPCException; - -import android.app.ListActivity; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; @@ -30,26 +24,26 @@ import android.widget.ListView; import android.widget.Toast; import android.widget.AdapterView.OnItemClickListener; import de.mtbnews.android.adapter.ListEntryContentAdapter; -import de.mtbnews.android.adapter.MapContentAdapter; import de.mtbnews.android.tapatalk.TapatalkClient; import de.mtbnews.android.tapatalk.TapatalkException; import de.mtbnews.android.tapatalk.wrapper.Forum; +import de.mtbnews.android.tapatalk.wrapper.Topic; import de.mtbnews.android.util.ServerAsyncTask; /** * @author dankert * */ -public class ForumActivity extends ListActivity +public class ForumActivity extends EndlessListActivity<Topic> { - private Object[] forumList; - private Forum forum; private SharedPreferences prefs; + private int totalSize; + private String forumId; @Override protected void onCreate(Bundle savedInstanceState) { - if ( ((IBCApplication)getApplication()).ibcTheme ) + if (((IBCApplication) getApplication()).ibcTheme) setTheme(R.style.IBC); super.onCreate(savedInstanceState); @@ -58,33 +52,52 @@ public class ForumActivity extends ListActivity prefs = PreferenceManager.getDefaultSharedPreferences(this); - TapatalkClient client = ((IBCApplication)getApplication()).getTapatalkClient(); + TapatalkClient client = ((IBCApplication) getApplication()) + .getTapatalkClient(); if (!client.loggedIn && prefs.getBoolean("auto_login", false)) { login(); } - if (getIntent().getBooleanExtra("latest", false)) - { - loadLatest(); - } - else if (getIntent().getBooleanExtra("participated", false)) - { - loadParticipated(); - } - else if (getIntent().getBooleanExtra("unread", false)) - { - loadUnread(); - } - else + forumId = getIntent().getStringExtra("forum_id"); + + ListAdapter adapter = new ListEntryContentAdapter(ForumActivity.this, + entries); + setListAdapter(adapter); + initialLoad(); + + // TODO: ggf. das hier in die Oberklasse? + ListView list = getListView(); + list.setOnCreateContextMenuListener(new OnCreateContextMenuListener() { - String forumId = getIntent().getStringExtra("forum_id"); - loadForum(forumId); - } + @Override + public void onCreateContextMenu(ContextMenu menu, View v, + ContextMenuInfo menuInfo) + { + MenuInflater menuInflater = new MenuInflater(getApplication()); + menuInflater.inflate(R.menu.topic_context, menu); + + } + }); + list.setOnItemClickListener(new OnItemClickListener() + { + @Override + public void onItemClick(AdapterView<?> parent, View view, + int position, long id) + { + // int aktPosition = displayFrom + position + 1; + final Intent intent = new Intent(ForumActivity.this, + TopicActivity.class); + Topic topic = ForumActivity.super.entries.get(position); + intent.putExtra(TopicActivity.TOPIC_ID, topic.getId()); + startActivity(intent); + } + }); } + // TODO: Das auch in die anderen Listviews einbauen. @Override public boolean onContextItemSelected(MenuItem item) { @@ -97,8 +110,8 @@ public class ForumActivity extends ListActivity final Intent intent = new Intent(ForumActivity.this, TopicActivity.class); - intent.putExtra("topic_id", forum.getTopics().get( - menuInfo.position).getId()); + intent.putExtra("topic_id", super.entries + .get(menuInfo.position).getId()); intent.putExtra("first_post", true); startActivity(intent); return true; @@ -107,7 +120,7 @@ public class ForumActivity extends ListActivity final Intent intent2 = new Intent(ForumActivity.this, TopicActivity.class); - intent2.putExtra("topic_id", forum.getTopics().get( + intent2.putExtra("topic_id", super.entries.get( menuInfo.position).getId()); intent2.putExtra("last_post", true); startActivity(intent2); @@ -119,7 +132,7 @@ public class ForumActivity extends ListActivity private void login() { - final TapatalkClient client = ((IBCApplication)getApplication()).client; + final TapatalkClient client = ((IBCApplication) getApplication()).client; new ServerAsyncTask(this, R.string.waitingfor_login) { @@ -146,7 +159,7 @@ public class ForumActivity extends ListActivity private void logout() { - final TapatalkClient client = ((IBCApplication)getApplication()).client; + final TapatalkClient client = ((IBCApplication) getApplication()).client; new ServerAsyncTask(this, R.string.waitingfor_logout) { @@ -171,291 +184,13 @@ public class ForumActivity extends ListActivity }.executeSynchronized(); } - private void loadUnread() - { - final SharedPreferences prefs = PreferenceManager - .getDefaultSharedPreferences(this); - final XMLRPCClient client = ((IBCApplication)getApplication()).client.getXMLRPCClient(); - - new ServerAsyncTask(this, R.string.waitingforcontent) - { - @Override - protected void callServer() throws IOException - { - - // add 2 to 4 - Object[] params = new Object[] { - prefs.getString("username", "").getBytes(), - prefs.getString("password", "").getBytes() }; - - try - { - Object sum = client.callEx("login", params); - - // Object l = client.call("get_inbox_stat"); - // System.out.println(l.toString() ); - Object l = client.call("get_unread_topic"); - - Object k = ((Map) l).get("topics"); - forumList = (Object[]) k; - - System.out.println(l.toString()); - - // Object i = client.call("get_box_info"); - // System.out.println(i.toString() ); - - } - catch (XMLRPCException e) - { - e.printStackTrace(); - throw new RuntimeException(e); - } - - } - - protected void doOnSuccess() - { - List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); - for (Object o : forumList) - { - list.add((Map) o); - } - ListAdapter adapter = new MapContentAdapter(ForumActivity.this, - list, "post_time", "topic_title", "short_content"); - // IBCActivity.this.setTitle(feed.getTitle()); - setListAdapter(adapter); - - } - - }.execute(); - final ListView list = getListView(); - - list.setOnItemClickListener(new OnItemClickListener() - { - - @Override - public void onItemClick(AdapterView<?> parent, View view, - int position, long id) - { - - final Intent intent = new Intent(ForumActivity.this, - TopicActivity.class); - intent.putExtra("topic_id", - (String) ((Map) forumList[position]).get("topic_id")); - startActivity(intent); - } - }); - } - - private void loadLatest() - { - final SharedPreferences prefs = PreferenceManager - .getDefaultSharedPreferences(this); - final XMLRPCClient client = ((IBCApplication)getApplication()).client.getXMLRPCClient(); - - new ServerAsyncTask(this, R.string.waitingforcontent) - { - - @Override - protected void callServer() throws IOException - { - - try - { - // Object l = client.call("get_inbox_stat"); - // System.out.println(l.toString() ); - Object l = client.call("get_latest_topic"); - - forumList = (Object[]) ((Map) l).get("topics"); - - System.out.println(l.toString()); - - // Object i = client.call("get_box_info"); - // System.out.println(i.toString() ); - - } - catch (XMLRPCException e) - { - e.printStackTrace(); - throw new RuntimeException(e); - } - - } - - protected void doOnSuccess() - { - List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); - for (Object o : forumList) - { - list.add((Map) o); - } - ListAdapter adapter = new MapContentAdapter(ForumActivity.this, - list, "post_time", "topic_title", "short_content"); - // IBCActivity.this.setTitle(feed.getTitle()); - setListAdapter(adapter); - - } - - }.execute(); - final ListView list = getListView(); - - list.setOnItemClickListener(new OnItemClickListener() - { - - @Override - public void onItemClick(AdapterView<?> parent, View view, - int position, long id) - { - - final Intent intent = new Intent(ForumActivity.this, - TopicActivity.class); - intent.putExtra("topic_id", - (String) ((Map) forumList[position]).get("topic_id")); - startActivity(intent); - - } - }); - } - - private void loadParticipated() - { - final SharedPreferences prefs = PreferenceManager - .getDefaultSharedPreferences(this); - final XMLRPCClient client = ((IBCApplication)getApplication()).client.getXMLRPCClient(); - - new ServerAsyncTask(this, R.string.waitingfor_forum) - { - - @Override - protected void callServer() throws IOException - { - - Object[] params = new Object[] { prefs - .getString("username", "").getBytes() }; - - try - { - Map map = (Map) client.callEx("get_participated_topic",params); - - forumList = (Object[]) map.get("topics"); - } - catch (XMLRPCException e) - { - e.printStackTrace(); - throw new RuntimeException(e); - } - - } - - protected void doOnSuccess() - { - List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); - for (Object o : forumList) - { - list.add((Map) o); - } - ListAdapter adapter = new MapContentAdapter(ForumActivity.this, - list, null, "topic_title", "short_content"); - // IBCActivity.this.setTitle(feed.getTitle()); - setListAdapter(adapter); - - } - - }.execute(); - final ListView list = getListView(); - - list.setOnItemClickListener(new OnItemClickListener() - { - - @Override - public void onItemClick(AdapterView<?> parent, View view, - int position, long id) - { - - final Intent intent = new Intent(ForumActivity.this, - TopicActivity.class); - intent.putExtra("topic_id", - (String) ((Map) forumList[position]).get("topic_id")); - startActivity(intent); - - } - }); - } - - private void loadForum(final String forumId) - { - - final TapatalkClient client = ((IBCApplication)getApplication()).client; - - new ServerAsyncTask(this, R.string.waitingfor_forum) - { - - // private Object[] forumList; - - @Override - protected void callServer() throws IOException - { - - try - { - forum = client.getForum(forumId); - } - catch (TapatalkException e) - { - throw new RuntimeException(e); - } - - } - - protected void doOnSuccess() - { - ForumActivity.this.setTitle(forum.getTitle()); - - ListAdapter adapter = new ListEntryContentAdapter( - ForumActivity.this, forum.getTopics()); - setListAdapter(adapter); - } - - }.execute(); - - final ListView list = getListView(); - - list.setOnItemClickListener(new OnItemClickListener() - { - @Override - public void onItemClick(AdapterView<?> parent, View view, - int position, long id) - { - final Intent intent = new Intent(ForumActivity.this, - TopicActivity.class); - intent.putExtra("topic_id", forum.getTopics().get(position) - .getId()); - startActivity(intent); - - } - }); - - list.setOnCreateContextMenuListener(new OnCreateContextMenuListener() - { - - @Override - public void onCreateContextMenu(ContextMenu menu, View v, - ContextMenuInfo menuInfo) - { - MenuInflater menuInflater = new MenuInflater(getApplication()); - menuInflater.inflate(R.menu.topic_context, menu); - - } - }); - } - @Override public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); MenuInflater mi = new MenuInflater(getApplication()); - if (((IBCApplication)getApplication()).client.loggedIn) + if (((IBCApplication) getApplication()).client.loggedIn) mi.inflate(R.menu.forum, menu); else mi.inflate(R.menu.forum_guest, menu); @@ -472,19 +207,20 @@ public class ForumActivity extends ListActivity return true; case R.id.menu_participated_topics: - Intent intent = new Intent(this, ForumActivity.class); - intent.putExtra("participated", true); + Intent intent = new Intent(this, SearchActivity.class); + intent + .setAction(SearchActivity.ACTION_SEARCH_PARTICIPATED_TOPICS); startActivity(intent); return true; case R.id.menu_latest_topics: - Intent intent2 = new Intent(this, ForumActivity.class); - intent2.putExtra("latest", true); + Intent intent2 = new Intent(this, SearchActivity.class); + intent2.setAction(SearchActivity.ACTION_SEARCH_LATEST_TOPICS); startActivity(intent2); return true; case R.id.menu_unread_topics: - Intent intent3 = new Intent(this, ForumActivity.class); - intent3.putExtra("unread", true); + Intent intent3 = new Intent(this, SearchActivity.class); + intent3.setAction(SearchActivity.ACTION_SEARCH_UNREAD_TOPICS); startActivity(intent3); return true; @@ -520,4 +256,47 @@ public class ForumActivity extends ListActivity } return false; } + + @Override + protected int getTotalSize() + { + return totalSize; + } + + @Override + protected void loadEntries( + final de.mtbnews.android.EndlessListActivity.OnListLoadedListener<Topic> onListLoaded, + final int from, final int to, boolean firstLoad) + { + + new ServerAsyncTask(this, R.string.waitingfor_forum) + { + private Forum forum; + + @Override + protected void callServer() throws IOException + { + + TapatalkClient client = ((IBCApplication) getApplication()).client; + try + { + this.forum = client.getForum(forumId, from, to); + totalSize = this.forum.topicCount; + } + catch (TapatalkException e) + { + throw new RuntimeException(e); + } + } + + protected void doOnSuccess() + { + ForumActivity.this.setTitle(forum.getTitle()); + onListLoaded.listLoaded(this.forum.getTopics()); + } + + }.execute(); + + } + } diff --git a/src/de/mtbnews/android/ForumOverviewActivity.java b/src/de/mtbnews/android/ForumOverviewActivity.java @@ -244,22 +244,25 @@ public class ForumOverviewActivity extends ExpandableListActivity return true; case R.id.menu_participated_topics: - Intent intent = new Intent(this, ForumActivity.class); - intent.putExtra("participated", true); + Intent intent = new Intent(this, SearchActivity.class); + intent + .setAction(SearchActivity.ACTION_SEARCH_PARTICIPATED_TOPICS); startActivity(intent); return true; case R.id.menu_latest_topics: - Intent intent2 = new Intent(this, ForumActivity.class); - intent2.putExtra("latest", true); + Intent intent2 = new Intent(this, SearchActivity.class); + intent2.setAction(SearchActivity.ACTION_SEARCH_LATEST_TOPICS); startActivity(intent2); return true; + case R.id.menu_unread_topics: - Intent intent3 = new Intent(this, ForumActivity.class); - intent3.putExtra("unread", true); + Intent intent3 = new Intent(this, SearchActivity.class); + intent3.setAction(SearchActivity.ACTION_SEARCH_UNREAD_TOPICS); startActivity(intent3); return true; + case R.id.menu_logout: logout(); return true; diff --git a/src/de/mtbnews/android/SearchActivity.java b/src/de/mtbnews/android/SearchActivity.java @@ -4,23 +4,21 @@ package de.mtbnews.android; import java.io.IOException; -import java.util.List; -import java.util.Map; -import android.app.ListActivity; import android.app.SearchManager; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.preference.PreferenceManager; -import android.util.Log; +import android.view.ContextMenu; +import android.view.MenuInflater; +import android.view.MenuItem; import android.view.View; -import android.widget.AbsListView; +import android.view.ContextMenu.ContextMenuInfo; +import android.view.View.OnCreateContextMenuListener; import android.widget.AdapterView; -import android.widget.BaseAdapter; import android.widget.ListAdapter; import android.widget.ListView; -import android.widget.AbsListView.OnScrollListener; import android.widget.AdapterView.OnItemClickListener; import de.mtbnews.android.adapter.ListEntryContentAdapter; import de.mtbnews.android.tapatalk.TapatalkClient; @@ -33,165 +31,165 @@ import de.mtbnews.android.util.ServerAsyncTask; * @author dankert * */ -public class SearchActivity extends ListActivity +public class SearchActivity extends EndlessListActivity<Topic> { - public static final String ID = "id"; - public static final String CLIENT = "client"; - - private boolean loadingMore = true; - - private int displayFrom; - private int displayTo; - private int postCount; - - private List<Topic> topics; - private Search search; - - Map<String, String> data; + public static final String ACTION_SEARCH_UNREAD_TOPICS = "de.mtbnews.android.UNREAD_TOPICS"; + public static final String ACTION_SEARCH_PARTICIPATED_TOPICS = "de.mtbnews.android.PARTICIPATED_TOPICS"; + public static final String ACTION_SEARCH_LATEST_TOPICS = "de.mtbnews.android.LATEST_TOPICS"; + private static final String ACTION_SEARCH_TOPICS_BY_QUERY = Intent.ACTION_SEARCH; + + private int totalSize; + private String searchId; + private SharedPreferences prefs; private String query; + private int searchType; + private String username; @Override protected void onCreate(Bundle savedInstanceState) { - if ( ((IBCApplication)getApplication()).ibcTheme ) + if (((IBCApplication) getApplication()).ibcTheme) setTheme(R.style.IBC); - - final SharedPreferences prefs = PreferenceManager - .getDefaultSharedPreferences(this); - + prefs = PreferenceManager.getDefaultSharedPreferences(this); super.onCreate(savedInstanceState); setContentView(R.layout.listing); - Intent intent = getIntent(); + username = prefs.getString("username", ""); + + final Intent intent = getIntent(); - if (Intent.ACTION_SEARCH.equals(intent.getAction())) + if (ACTION_SEARCH_TOPICS_BY_QUERY.equals(intent.getAction())) { + searchType = TapatalkClient.SEARCHTYPE_QUERY; query = intent.getStringExtra(SearchManager.QUERY); } - - Log.d("IBC", "Searching for '" + query + "'"); - - new ServerAsyncTask(this, R.string.waitingfor_search) + else if (ACTION_SEARCH_LATEST_TOPICS.equals(intent.getAction())) { + searchType = TapatalkClient.SEARCHTYPE_LATEST; + query = null; + } + else if (ACTION_SEARCH_PARTICIPATED_TOPICS.equals(intent.getAction())) + { + searchType = TapatalkClient.SEARCHTYPE_PARTICIPATED; + query = null; + } + else if (ACTION_SEARCH_UNREAD_TOPICS.equals(intent.getAction())) + { + searchType = TapatalkClient.SEARCHTYPE_UNREAD; + query = null; + } + else + { + throw new RuntimeException("Unknown search action: " + + intent.getAction()); + } - @Override - protected void callServer() throws IOException - { - TapatalkClient client = ((IBCApplication)getApplication()).getTapatalkClient(); - - try - { - int start = 0; - int end = Integer.parseInt(prefs - .getString("num_load", "10")) - 1; - - displayFrom = start; - displayTo = end; - - search = client.searchTopics(query, start, end, null); + ListAdapter adapter = new ListEntryContentAdapter(SearchActivity.this, + entries); + setListAdapter(adapter); - postCount = search.topicCount; - topics = search.getTopics(); + initialLoad(); - loadingMore = false; - } - catch (TapatalkException e) - { - throw new RuntimeException(e); - } - } + ListView list = getListView(); + list.setOnCreateContextMenuListener(new OnCreateContextMenuListener() + { - protected void doOnSuccess() + @Override + public void onCreateContextMenu(ContextMenu menu, View v, + ContextMenuInfo menuInfo) { - SearchActivity.this.setTitle(query); - ListAdapter adapter = new ListEntryContentAdapter( - SearchActivity.this, topics); - setListAdapter(adapter); + MenuInflater menuInflater = new MenuInflater(getApplication()); + menuInflater.inflate(R.menu.topic_context, menu); } - - }.execute(); - final ListView list = getListView(); - + }); list.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { + // int aktPosition = displayFrom + position + 1; final Intent intent = new Intent(SearchActivity.this, TopicActivity.class); - Topic topic = topics.get(position); - intent.putExtra("topic_id", topic.getId()); + Topic topic = SearchActivity.super.entries.get(position); + intent.putExtra(TopicActivity.TOPIC_ID, topic.getId()); startActivity(intent); } }); + } + + @Override + protected int getTotalSize() + { + return totalSize; + } - /** - * Weitere List-Einträge automatisch nachladen. - */ - list.setOnScrollListener(new OnScrollListener() + // TODO: Das auch in die anderen Listviews einbauen. + @Override + public boolean onContextItemSelected(MenuItem item) + { + AdapterView.AdapterContextMenuInfo menuInfo = (AdapterView.AdapterContextMenuInfo) item + .getMenuInfo(); + + switch (item.getItemId()) { + case R.id.menu_goto_top: - @Override - public void onScrollStateChanged(AbsListView view, int scrollState) - { - } + final Intent intent = new Intent(SearchActivity.this, + TopicActivity.class); + intent.putExtra(TopicActivity.TOPIC_ID, super.entries + .get(menuInfo.position).getId()); + intent.putExtra("first_post", true); + startActivity(intent); + return true; + + case R.id.menu_goto_bottom: + + final Intent intent2 = new Intent(SearchActivity.this, + TopicActivity.class); + intent2.putExtra(TopicActivity.TOPIC_ID, super.entries.get( + menuInfo.position).getId()); + intent2.putExtra("last_post", true); + startActivity(intent2); + return true; + } + + return super.onContextItemSelected(item); + } + + @Override + protected void loadEntries(final OnListLoadedListener<Topic> onListLoaded, + final int from, final int to, boolean firstLoad) + { + new ServerAsyncTask(SearchActivity.this, R.string.waitingfor_loadmore) + { + private Search search; @Override - public void onScroll(AbsListView view, int firstVisibleItem, - int visibleItemCount, int totalItemCount) + protected void callServer() throws IOException { + TapatalkClient client = ((IBCApplication) getApplication()).client; - // what is the bottom iten that is visible - int lastInScreen = firstVisibleItem + visibleItemCount; - - // is the bottom item visible & not loading more already ? Load - // more ! - if ((lastInScreen == totalItemCount) && !(loadingMore) - /*&& !(totalItemCount >= search.topicCount)*/) + try { - loadingMore = true; - new ServerAsyncTask(SearchActivity.this, - R.string.waitingfor_loadmore) - { - @Override - protected void callServer() throws IOException - { - TapatalkClient client = ((IBCApplication)getApplication()).client; - - try - { - int start = displayTo + 1; - int end = start - + Integer.parseInt(prefs.getString( - "num_load", "10")) - 1; - displayTo = end; - - search = client.searchTopics(null, start, end, - search.searchId); - - topics.addAll(search.getTopics()); - - loadingMore = false; - } - catch (TapatalkException e) - { - throw new RuntimeException(e); - } - } - - protected void doOnSuccess() - { - ((BaseAdapter) getListAdapter()) - .notifyDataSetChanged(); - } - - }.execute(); + search = client.searchTopics(searchType, query, username, + from, to, searchId); + totalSize = search.topicCount; + searchId = search.searchId; + } + catch (TapatalkException e) + { + throw new RuntimeException(e); } } - }); + protected void doOnSuccess() + { + onListLoaded.listLoaded(search.getTopics()); + } + + }.execute(); } } diff --git a/src/de/mtbnews/android/TopicActivity.java b/src/de/mtbnews/android/TopicActivity.java @@ -21,13 +21,15 @@ import de.mtbnews.android.tapatalk.wrapper.Topic; import de.mtbnews.android.util.ServerAsyncTask; /** - * Anzeige eines Themas. + * Anzeige aller Beiträge eines Themas. * * @author dankert * */ public class TopicActivity extends EndlessListActivity<Post> { + public static final String TOPIC_ID = "topic_id"; + private int totalSize; @Override @@ -92,7 +94,7 @@ public class TopicActivity extends EndlessListActivity<Post> try { String topicId = TopicActivity.this.getIntent() - .getStringExtra("topic_id"); + .getStringExtra(TOPIC_ID); topic = client.getTopic(topicId, from, to); diff --git a/src/de/mtbnews/android/tapatalk/TapatalkClient.java b/src/de/mtbnews/android/tapatalk/TapatalkClient.java @@ -11,6 +11,8 @@ import org.xmlrpc.android.XMLRPCException; import android.util.Log; import de.mtbnews.android.tapatalk.wrapper.Forum; +import de.mtbnews.android.tapatalk.wrapper.Mailbox; +import de.mtbnews.android.tapatalk.wrapper.Message; import de.mtbnews.android.tapatalk.wrapper.Post; import de.mtbnews.android.tapatalk.wrapper.Search; import de.mtbnews.android.tapatalk.wrapper.Topic; @@ -99,14 +101,16 @@ public class TapatalkClient int postCount = toInt(map.get("total_post_num")); List<Post> posts = new ArrayList<Post>(); - Topic topic = new Topic(id, posts, title, null, null,null, postCount); + Topic topic = new Topic(id, posts, title, null, null, null, + postCount); for (Object o1 : (Object[]) map.get("posts")) { Map postMap = (Map) o1; Post post = new Post((Date) postMap.get("post_time"), new String((byte[]) postMap.get("post_title")), - new String((byte[]) postMap.get("post_content")),new String((byte[]) postMap.get("post_author_name"))); + new String((byte[]) postMap.get("post_content")), + new String((byte[]) postMap.get("post_author_name"))); posts.add(post); } @@ -148,11 +152,14 @@ public class TapatalkClient } @SuppressWarnings("unchecked") - public Forum getForum(String forumId) throws TapatalkException + public Forum getForum(String forumId, int from, int to) + throws TapatalkException { try { - Object o = client.call("get_topic", forumId); + final Object[] params = new Object[] { forumId, from, to }; + // TODO Paging! + Object o = client.callEx("get_topic", params); Map map = (Map) o; String title = new String((byte[]) map.get("forum_name")); @@ -161,16 +168,20 @@ public class TapatalkClient final List<Topic> topics = new ArrayList<Topic>(); final List<Post> posts = new ArrayList<Post>(); - Forum forum = new Forum(id, topics, title, null, null); + final Forum forum = new Forum(id, topics, title, null, null); + forum.topicCount = (Integer) map.get("total_topic_num"); + for (Object o1 : (Object[]) map.get("topics")) { Map topicMap = (Map) o1; - Topic topic = new Topic((String) topicMap.get("topic_id"), + Topic topic = new Topic( + (String) topicMap.get("topic_id"), posts, // byteArrayToString(topicMap.get("topic_title")),// (Date) topicMap.get("last_reply_time"), // new String((byte[]) topicMap.get("short_content")),// - new String((byte[]) topicMap.get("topic_author_name")), 0); + new String((byte[]) topicMap.get("topic_author_name")), + 0); topics.add(topic); } @@ -182,19 +193,53 @@ public class TapatalkClient } } - public Search searchTopics(String query, int start, int end, String searchId) - throws TapatalkException + public static final int SEARCHTYPE_QUERY = 1; + public static final int SEARCHTYPE_LATEST = 2; + public static final int SEARCHTYPE_PARTICIPATED = 3; + public static final int SEARCHTYPE_UNREAD = 4; + + public Search searchTopics(int searchType, String query, String username, + int start, int end, String searchId) throws TapatalkException { try { - final Object[] params; + Object[] params = null; + String method = null; - if (searchId == null) - params = new Object[] { query.getBytes(), start, end }; - else - params = new Object[] { "".getBytes(), start, end, searchId }; + switch (searchType) + { + case SEARCHTYPE_QUERY: + method = "search_topic"; + if (searchId == null) + params = new Object[] { query.getBytes(), start, end }; + else + params = new Object[] { "".getBytes(), start, end, + searchId }; + break; + case SEARCHTYPE_LATEST: + method = "get_latest_topic"; + if (searchId == null) + params = new Object[] { start, end }; + else + params = new Object[] { start, end, searchId }; + break; + case SEARCHTYPE_PARTICIPATED: + method = "get_participated_topic"; + if (searchId == null) + params = new Object[] { username.getBytes(),start, end }; + else + params = new Object[] { username.getBytes(), start, end, searchId }; + break; + case SEARCHTYPE_UNREAD: + method = "get_unread_topic"; + if (searchId == null) + params = new Object[] { start, end }; + else + params = new Object[] { start, end, searchId }; + break; + } - Map map = toMap(client.callEx("search_topic", params)); + Map map = toMap(client.callEx(method, params)); Integer topicCount = (Integer) map.get("total_topic_num"); String newSearchId = (String) map.get("search_id"); @@ -206,12 +251,14 @@ public class TapatalkClient for (Object o1 : (Object[]) map.get("topics")) { Map topicMap = toMap(o1); - Topic topic = new Topic((String) topicMap.get("topic_id"), + Topic topic = new Topic( + (String) topicMap.get("topic_id"), posts, // byteArrayToString(topicMap.get("topic_title")),// (Date) topicMap.get("post_time"), // new String((byte[]) topicMap.get("short_content")),// - new String((byte[]) topicMap.get("post_author_name")), 0); + new String((byte[]) topicMap.get("post_author_name")), + 0); topics.add(topic); } @@ -223,35 +270,87 @@ public class TapatalkClient } } - public Search getBoxContent(String boxId, int start, int end) + /** + * Reads the list of all available mailboxes. + * + * @param boxId + * @param start + * @param end + * @return + * @throws TapatalkException + */ + public List<Mailbox> getMailbox(String boxId, int start, int end) throws TapatalkException { try { - final Object[] params = new Object[] { boxId, start, end }; + Map map = toMap(client.call("get_box_info")); - Map map = toMap(client.callEx("get_box", params)); + final List<Mailbox> boxList = new ArrayList<Mailbox>(); - Integer topicCount = (Integer) map.get("total_topic_num"); - String newSearchId = (String) map.get("search_id"); - final List<Topic> topics = new ArrayList<Topic>(); - Search search = new Search(topicCount, newSearchId, topics); + for (Object o1 : (Object[]) map.get("list")) + { + Map mapMap = toMap(o1); + Mailbox box = new Mailbox((String) mapMap.get("box_id"), + byteArrayToString(mapMap.get("box_name")),// + (Integer) mapMap.get("msg_count"), // + (Integer) mapMap.get("unread_count")); + boxList.add(box); + box.messages = new ArrayList<Message>(); + } - final List<Post> posts = new ArrayList<Post>(); + return boxList; + } + catch (XMLRPCException e) + { + throw new TapatalkException("Could not search", e); + } + } - for (Object o1 : (Object[]) map.get("topics")) + /** + * Gets all messsages in a box. + * + * @param boxId + * @param start + * @param end + * @return + * @throws TapatalkException + */ + public Mailbox getBoxContent(String boxId, int start, int end) + throws TapatalkException + { + try + { + Object[] params = new Object[] { boxId, start, end }; + Map map = toMap(client.callEx("get_box_info", params)); + + Mailbox mailbox = new Mailbox(boxId, "", (Integer) map.get("size"), + (Integer) map.get("")); + + final List<Message> messageList = new ArrayList<Message>(); + mailbox.messages = messageList; + + for (Object o1 : (Object[]) map.get("list")) { - Map topicMap = toMap(o1); - Topic topic = new Topic((String) topicMap.get("topic_id"), - posts, // - byteArrayToString(topicMap.get("topic_title")),// - (Date) topicMap.get("post_time"), // - new String((byte[]) topicMap.get("short_content")),// - "", 0); - topics.add(topic); + Map mapMap = toMap(o1); + + Object[] objects = (Object[]) mapMap.get("msg_to"); + String[] msgTo = new String[objects.length]; + for (int j = 0; j < objects.length; j++) + msgTo[j] = byteArrayToString((toMap(objects[j]) + .get("username"))); + + Message message = new Message((String) map.get("msg_id"), + ((Integer) mapMap.get("msg_state")).equals(1), // + (Date) mapMap.get("sent_date"),// + byteArrayToString(mapMap.get("msg_from")),// + msgTo,// + byteArrayToString(mapMap.get("msg_subject")), // + byteArrayToString(mapMap.get("short_content"))); + messageList.add(message); } - return search; + return mailbox; } catch (XMLRPCException e) { @@ -259,40 +358,115 @@ public class TapatalkClient } } - public Search getMessage(String query, int start, int end, String searchId) + /** + * Reads a message + * + * @param boxId + * @param messageId + * @param asHtml + * @return + * @throws TapatalkException + */ + public Message getMessage(String boxId, String messageId, boolean asHtml) throws TapatalkException { try { - final Object[] params; + final Object[] params = new Object[] { messageId, boxId, asHtml }; - if (searchId == null) - params = new Object[] { query.getBytes(), start, end }; - else - params = new Object[] { "".getBytes(), start, end, searchId }; + Map mapMap = toMap(client.callEx("get_message", params)); - Map map = toMap(client.callEx("search_topic", params)); + Object[] objects = (Object[]) mapMap.get("msg_to"); + String[] msgTo = new String[objects.length]; + for (int j = 0; j < objects.length; j++) + msgTo[j] = byteArrayToString((toMap(objects[j]).get("username"))); - Integer topicCount = (Integer) map.get("total_topic_num"); - String newSearchId = (String) map.get("search_id"); - final List<Topic> topics = new ArrayList<Topic>(); - Search search = new Search(topicCount, newSearchId, topics); + Message message = new Message(messageId, false, // + (Date) mapMap.get("sent_date"),// + byteArrayToString(mapMap.get("msg_from")),// + msgTo,// + byteArrayToString(mapMap.get("msg_subject")), // + byteArrayToString(mapMap.get("text_body"))); + return message; - final List<Post> posts = new ArrayList<Post>(); + } + catch (XMLRPCException e) + { + throw new TapatalkException("Could not search", e); + } + } - for (Object o1 : (Object[]) map.get("topics")) - { - Map topicMap = toMap(o1); - Topic topic = new Topic((String) topicMap.get("topic_id"), - posts, // - byteArrayToString(topicMap.get("topic_title")),// - (Date) topicMap.get("post_time"), // - new String((byte[]) topicMap.get("short_content")),// - "", 0); - topics.add(topic); - } + /** + * Create a topic + * + * @param boxId + * @param messageId + * @param asHtml + * @return + * @throws TapatalkException + */ + public void createTopic(String forumId, String subject, String content) + throws TapatalkException + { + try + { + final Object[] params = new Object[] { forumId, subject.getBytes(), + content.getBytes() }; - return search; + Map mapMap = toMap(client.callEx("new_topic", params)); + } + catch (XMLRPCException e) + { + throw new TapatalkException("Could not search", e); + } + } + + /** + * Create a post. + * + * @param boxId + * @param messageId + * @param asHtml + * @return + * @throws TapatalkException + */ + public void createReply(String forumId, String topicId, String subject, + String content) throws TapatalkException + { + try + { + final Object[] params = new Object[] { forumId, topicId, + subject.getBytes(), content.getBytes() }; + + Map mapMap = toMap(client.callEx("reply_post", params)); + + @SuppressWarnings("unused") + String msgId = (String) mapMap.get("msg_id"); + } + catch (XMLRPCException e) + { + throw new TapatalkException("Could not search", e); + } + } + + /** + * Create a message. + * + * @param boxId + * @param messageId + * @param asHtml + * @return + * @throws TapatalkException + */ + public void createMessage(String[] to, String subject, String content) + throws TapatalkException + { + try + { + final Object[] params = new Object[] { to, subject.getBytes(), + content.getBytes() }; + + Map mapMap = toMap(client.callEx("create_message", params)); } catch (XMLRPCException e) { @@ -300,6 +474,7 @@ public class TapatalkClient } } + @Deprecated public XMLRPCClient getXMLRPCClient() { return client; diff --git a/src/de/mtbnews/android/tapatalk/wrapper/Forum.java b/src/de/mtbnews/android/tapatalk/wrapper/Forum.java @@ -9,6 +9,7 @@ public class Forum implements ListEntry private String id; private List<Topic> topics; + public int topicCount; private String title; private Date date; diff --git a/src/de/mtbnews/android/tapatalk/wrapper/Mailbox.java b/src/de/mtbnews/android/tapatalk/wrapper/Mailbox.java @@ -0,0 +1,52 @@ +package de.mtbnews.android.tapatalk.wrapper; + +import java.util.Date; +import java.util.List; + +public class Mailbox implements ListEntry +{ + + private String id; + private String name; + private int countAll; + private int countUnread; + public List<Message> messages; + + public Mailbox(String id, String name, Integer countAll, Integer countUnread) + { + this.id = id; + this.name = name; + this.countAll = countAll; + this.countUnread = countUnread; + } + + public String getId() + { + return id; + } + + @Override + public String getContent() + { + return null; + } + + @Override + public Date getDate() + { + return null; + } + + @Override + public String getName() + { + return name; + } + + @Override + public String getTitle() + { + return "" + countAll + " (" + countUnread + ")"; + } + +} diff --git a/src/de/mtbnews/android/tapatalk/wrapper/Message.java b/src/de/mtbnews/android/tapatalk/wrapper/Message.java @@ -0,0 +1,60 @@ +package de.mtbnews.android.tapatalk.wrapper; + +import java.util.Date; + +public class Message implements ListEntry +{ + public String id; + public boolean unread; + private Date date; + public String from; + public String[] to; + public String subject; + public String content; + + /** + * @param id + * @param unread + * @param date + * @param from + * @param to + * @param subject + * @param content + */ + public Message(String id, boolean unread, Date date, String from, String[] to, + String subject, String content) + { + super(); + this.id = id; + this.unread = unread; + this.date = date; + this.from = from; + this.to = to; + this.subject = subject; + this.content = content; + } + + @Override + public String getContent() + { + return content; + } + + @Override + public Date getDate() + { + return date; + } + + @Override + public String getName() + { + return from; + } + + @Override + public String getTitle() + { + return subject; + } +}