diff --git a/application/src/org/yaaic/activity/ConversationActivity.java b/application/src/org/yaaic/activity/ConversationActivity.java index ecfa693..5405b4c 100644 --- a/application/src/org/yaaic/activity/ConversationActivity.java +++ b/application/src/org/yaaic/activity/ConversationActivity.java @@ -52,6 +52,7 @@ import org.yaaic.model.User; import org.yaaic.receiver.ConversationReceiver; import org.yaaic.receiver.ServerReceiver; import org.yaaic.view.ConversationSwitcher; +import org.yaaic.view.MessageListView; import android.app.Activity; import android.app.AlertDialog; @@ -235,7 +236,7 @@ public class ConversationActivity extends Activity implements ServiceConnection, // Fill view with messages that have been buffered while paused for (Conversation conversation : mConversations) { - mAdapter = conversation.getMessageListAdapter(); + mAdapter = deckAdapter.getItemAdapter(conversation.getName()); if (mAdapter != null) { mAdapter.addBulkMessages(conversation.getBuffer()); @@ -401,7 +402,7 @@ public class ConversationActivity extends Activity implements ServiceConnection, return; } - MessageListAdapter adapter = conversation.getMessageListAdapter(); + MessageListAdapter adapter = deckAdapter.getItemAdapter(target); while(conversation.hasBufferedMessages()) { Message message = conversation.pollBufferedMessage(); @@ -526,8 +527,10 @@ public class ConversationActivity extends Activity implements ServiceConnection, { if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) { if (deckAdapter.isSwitched()) { - switcher.showNext(); - switcher.removeView(deckAdapter.getSwitchedView()); + MessageListView canvas = (MessageListView) deckAdapter.getView(deckAdapter.getPositionByName(deckAdapter.getSwitchedName()), null, switcher); + canvas.setLayoutParams(new Gallery.LayoutParams(switcher.getWidth()*85/100, switcher.getHeight())); + canvas.setTranscriptMode(MessageListView.TRANSCRIPT_MODE_ALWAYS_SCROLL); + canvas.setDelegateTouchEvents(true); deckAdapter.setSwitched(null, null); return true; } diff --git a/application/src/org/yaaic/activity/ServersActivity.java b/application/src/org/yaaic/activity/ServersActivity.java index f516dc2..cfcf38d 100644 --- a/application/src/org/yaaic/activity/ServersActivity.java +++ b/application/src/org/yaaic/activity/ServersActivity.java @@ -344,4 +344,4 @@ public class ServersActivity extends ListActivity implements ServiceConnection, } } -} \ No newline at end of file +} diff --git a/application/src/org/yaaic/adapter/DeckAdapter.java b/application/src/org/yaaic/adapter/DeckAdapter.java index a316e26..b6edd24 100644 --- a/application/src/org/yaaic/adapter/DeckAdapter.java +++ b/application/src/org/yaaic/adapter/DeckAdapter.java @@ -41,16 +41,28 @@ import android.widget.TextView; */ public class DeckAdapter extends BaseAdapter { - private LinkedList conversations; + private LinkedList conversations; private MessageListView currentView; private String currentChannel; + public class ConversationInfo { + public Conversation conv; + public MessageListAdapter adapter; + public MessageListView view; + + public ConversationInfo(Conversation conv) { + this.conv = conv; + this.adapter = null; + this.view = null; + } + } + /** * Create a new DeckAdapter instance */ public DeckAdapter() { - conversations = new LinkedList(); + conversations = new LinkedList(); } /** @@ -58,7 +70,7 @@ public class DeckAdapter extends BaseAdapter */ public void clearConversations() { - conversations = new LinkedList(); + conversations = new LinkedList(); } /** @@ -70,16 +82,51 @@ public class DeckAdapter extends BaseAdapter return conversations.size(); } + /** + * Get ConversationInfo on item at position + */ + private ConversationInfo getItemInfo(int position) { + if (position >= 0 && position < conversations.size()) { + return conversations.get(position); + } + return null; + } + /** * Get item at position */ @Override public Conversation getItem(int position) { - if (position >= 0 && position < conversations.size()) { - return conversations.get(position); + ConversationInfo convInfo = getItemInfo(position); + if (convInfo != null) { + return convInfo.conv; + } else { + return null; } - return null; + } + + /** + * Get MessageListAdapter belonging to a conversation + * + * @param position Position of the conversation in the deck + */ + public MessageListAdapter getItemAdapter(int position) { + ConversationInfo convInfo = getItemInfo(position); + if (convInfo != null) { + return convInfo.adapter; + } else { + return null; + } + } + + /** + * Get MessageListAdapter belonging to a conversation + * + * @param name Name of the conversation + */ + public MessageListAdapter getItemAdapter(String name) { + return getItemAdapter(getPositionByName(name)); } /** @@ -99,7 +146,7 @@ public class DeckAdapter extends BaseAdapter */ public void addItem(Conversation conversation) { - conversations.add(conversation); + conversations.add(new ConversationInfo(conversation)); notifyDataSetChanged(); } @@ -114,10 +161,10 @@ public class DeckAdapter extends BaseAdapter { // Optimization - cache field lookups int mSize = conversations.size(); - LinkedList mItems = this.conversations; + LinkedList mItems = this.conversations; for (int i = 0; i < mSize; i++) { - if (mItems.get(i).getName().equalsIgnoreCase(name)) { + if (mItems.get(i).conv.getName().equalsIgnoreCase(name)) { return i; } } @@ -187,17 +234,21 @@ public class DeckAdapter extends BaseAdapter @Override public View getView(int position, View convertView, ViewGroup parent) { - Conversation conversation = getItem(position); + ConversationInfo convInfo = getItemInfo(position); // Market stack traces prove that sometimes we get a null converstion // because the collection changed while a view is requested for an // item that does not exist anymore... so we just need to reply with // some kind of view here. - if (conversation == null) { + if (convInfo == null || convInfo.conv == null) { return new TextView(parent.getContext()); } - return renderConversation(conversation, parent); + if (convInfo.view != null) { + return convInfo.view; + } else { + return renderConversation(convInfo, parent); + } } /** @@ -207,16 +258,17 @@ public class DeckAdapter extends BaseAdapter * @param parent The parent view (context) * @return The rendered MessageListView */ - public MessageListView renderConversation(Conversation conversation, ViewGroup parent) + private MessageListView renderConversation(ConversationInfo convInfo, ViewGroup parent) { MessageListView list = new MessageListView(parent.getContext(), parent); + convInfo.view = list; list.setOnItemClickListener(MessageClickListener.getInstance()); - MessageListAdapter adapter = conversation.getMessageListAdapter(); + MessageListAdapter adapter = convInfo.adapter; if (adapter == null) { - adapter = new MessageListAdapter(conversation, parent.getContext()); - conversation.setMessageListAdapter(adapter); + adapter = new MessageListAdapter(convInfo.conv, parent.getContext()); + convInfo.adapter = adapter; } list.setAdapter(adapter); diff --git a/application/src/org/yaaic/listener/ConversationClickListener.java b/application/src/org/yaaic/listener/ConversationClickListener.java index 0b02db7..a3d8b91 100644 --- a/application/src/org/yaaic/listener/ConversationClickListener.java +++ b/application/src/org/yaaic/listener/ConversationClickListener.java @@ -28,7 +28,7 @@ import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ListView; -import android.widget.TableLayout.LayoutParams; +import android.widget.Gallery.LayoutParams; import android.widget.ViewSwitcher; /** @@ -61,13 +61,11 @@ public class ConversationClickListener implements OnItemClickListener { Conversation conversation = adapter.getItem(position); - MessageListView canvas = adapter.renderConversation(conversation, switcher); + MessageListView canvas = (MessageListView) adapter.getView(position, null, switcher); canvas.setTranscriptMode(ListView.TRANSCRIPT_MODE_NORMAL); canvas.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); canvas.setDelegateTouchEvents(false); // Do not delegate adapter.setSwitched(conversation.getName(), canvas); - switcher.addView(canvas); - switcher.showNext(); } } diff --git a/application/src/org/yaaic/model/Conversation.java b/application/src/org/yaaic/model/Conversation.java index 0d32bc4..43f3013 100644 --- a/application/src/org/yaaic/model/Conversation.java +++ b/application/src/org/yaaic/model/Conversation.java @@ -22,8 +22,6 @@ package org.yaaic.model; import java.util.LinkedList; -import org.yaaic.adapter.MessageListAdapter; - /** * Base class for conversations * @@ -48,7 +46,6 @@ public abstract class Conversation private final LinkedList buffer; private final LinkedList history; private final String name; - private MessageListAdapter adapter; private int status = 1; /** @@ -148,22 +145,6 @@ public abstract class Conversation buffer.clear(); } - /** - * Store the adapter of this conversation - */ - public void setMessageListAdapter(MessageListAdapter adapter) - { - this.adapter = adapter; - } - - /** - * Get the MessageList Adapter of this conversation if known - */ - public MessageListAdapter getMessageListAdapter() - { - return adapter; - } - /** * Set status of conversation *