diff options
Diffstat (limited to 'src/de/gultsch/chat/ui/ConversationFragment.java')
-rw-r--r-- | src/de/gultsch/chat/ui/ConversationFragment.java | 165 |
1 files changed, 115 insertions, 50 deletions
diff --git a/src/de/gultsch/chat/ui/ConversationFragment.java b/src/de/gultsch/chat/ui/ConversationFragment.java index fca5202a..5ba58bbf 100644 --- a/src/de/gultsch/chat/ui/ConversationFragment.java +++ b/src/de/gultsch/chat/ui/ConversationFragment.java @@ -1,11 +1,13 @@ package de.gultsch.chat.ui; +import java.util.ArrayList; +import java.util.List; + import de.gultsch.chat.R; import de.gultsch.chat.entities.Conversation; import de.gultsch.chat.entities.Message; import de.gultsch.chat.utils.Beautifier; import android.app.Fragment; -import android.content.Context; import android.database.Cursor; import android.graphics.Typeface; import android.net.Uri; @@ -18,85 +20,118 @@ import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.EditText; -import android.widget.ListAdapter; import android.widget.ListView; import android.widget.ImageButton; import android.widget.ImageView; import android.widget.TextView; public class ConversationFragment extends Fragment { + + protected Conversation conversation; + protected ListView messagesView; + protected LayoutInflater inflater; + protected List<Message> messageList = new ArrayList<Message>(); - Conversation conversation; + @Override + public View onCreateView(final LayoutInflater inflater, + ViewGroup container, Bundle savedInstanceState) { - public void setConversation(Conversation conv) { - this.conversation = conv; - } + this.inflater = inflater; - @Override - public View onCreateView(final LayoutInflater inflater, ViewGroup container, - Bundle savedInstanceState) { - - String[] mProjection = new String[] - { - Profile._ID, - Profile.PHOTO_THUMBNAIL_URI - }; - Cursor mProfileCursor = getActivity().getContentResolver().query( - Profile.CONTENT_URI, - mProjection , - null, - null, - null); - - mProfileCursor.moveToFirst(); - final Uri profilePicture = Uri.parse(mProfileCursor.getString(1)); - - Log.d("gultsch","found user profile pic "+profilePicture.toString()); - - final View view = inflater.inflate(R.layout.fragment_conversation, container, - false); + + + final View view = inflater.inflate(R.layout.fragment_conversation, + container, false); ((ImageButton) view.findViewById(R.id.textSendButton)) .setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { - EditText chatMsg = (EditText) view.findViewById(R.id.textinput); - if (chatMsg.getText().length() < 1) return; - Message message = new Message(conversation,chatMsg.getText().toString(), - Message.ENCRYPTION_NONE); - XmppActivity activity = (XmppActivity) getActivity(); + ConversationActivity activity = (ConversationActivity) getActivity(); + EditText chatMsg = (EditText) view + .findViewById(R.id.textinput); + if (chatMsg.getText().length() < 1) + return; + Message message = new Message(conversation, chatMsg + .getText().toString(), Message.ENCRYPTION_NONE); activity.xmppConnectionService.sendMessage(message); conversation.getMessages().add(message); chatMsg.setText(""); - ListView messagesView = (ListView) view.findViewById(R.id.messages_view); - ArrayAdapter<Message> adapter = (ArrayAdapter<Message>) messagesView.getAdapter(); - adapter.notifyDataSetChanged(); + messageList.add(message); + + activity.updateConversationList(); - messagesView.setSelection(conversation.getMessages().size() -1); + messagesView.setSelection(messageList.size() - 1); } }); - ListView messagesView = (ListView) view - .findViewById(R.id.messages_view); + messagesView = (ListView) view.findViewById(R.id.messages_view); + + String[] mProjection = new String[] { Profile._ID, + Profile.PHOTO_THUMBNAIL_URI }; + Cursor mProfileCursor = getActivity().getContentResolver().query( + Profile.CONTENT_URI, mProjection, null, null, null); + + mProfileCursor.moveToFirst(); + final Uri profilePicture = Uri.parse(mProfileCursor.getString(1)); + messagesView.setAdapter(new ArrayAdapter<Message>(this.getActivity() - .getApplicationContext(), R.layout.message_sent, - this.conversation.getMessages()) { + .getApplicationContext(), R.layout.message_sent, this.messageList) { + + private static final int SENT = 0; + private static final int RECIEVED = 1; + + @Override + public int getViewTypeCount() { + return 2; + } + + @Override + public int getItemViewType(int position) { + if (getItem(position).getStatus() == Message.STATUS_RECIEVED) { + return RECIEVED; + } else { + return SENT; + } + } @Override public View getView(int position, View view, ViewGroup parent) { Message item = getItem(position); - if ((item.getStatus() != Message.STATUS_RECIEVED) - || (item.getStatus() == Message.STATUS_SEND)) { - view = (View) inflater.inflate(R.layout.message_sent, null); - ((ImageView) view.findViewById(R.id.message_photo)).setImageURI(profilePicture); + int type = getItemViewType(position); + if (view == null) { + switch (type) { + case SENT: + view = (View) inflater.inflate(R.layout.message_sent, + null); + Log.d("gultsch", "inflated new message_sent view"); + break; + case RECIEVED: + view = (View) inflater.inflate( + R.layout.message_recieved, null); + Log.d("gultsch", "inflated new message_recieved view"); + break; + } + } else { + Log.d("gultsch", "recylecd a view"); + } + if (type == RECIEVED) { + ((ImageView) view.findViewById(R.id.message_photo)) + .setImageURI(item.getConversation() + .getProfilePhotoUri()); + } else { + ((ImageView) view.findViewById(R.id.message_photo)) + .setImageURI(profilePicture); } - ((TextView) view.findViewById(R.id.message_body)).setText(item.getBody()); + ((TextView) view.findViewById(R.id.message_body)).setText(item + .getBody()); TextView time = (TextView) view.findViewById(R.id.message_time); if (item.getStatus() == Message.STATUS_UNSEND) { time.setTypeface(null, Typeface.ITALIC); } else { - time.setText(Beautifier.readableTimeDifference(item.getTimeSent())); + time.setText(Beautifier.readableTimeDifference(item + .getTimeSent())); } return view; } @@ -105,7 +140,37 @@ public class ConversationFragment extends Fragment { return view; } - public Conversation getConversation() { - return conversation; + @Override + public void onStart() { + super.onStart(); + + Log.d("gultsch","conversationfragment onStart"); + + final ConversationActivity activity = (ConversationActivity) getActivity(); + + // TODO check if bond and get data back + + if (activity.xmppConnectionServiceBound) { + this.conversation = activity.getConversationList().get(activity.getSelectedConversation()); + this.messageList.clear(); + this.messageList.addAll(this.conversation.getMessages()); + } + + + // rendering complete. now go tell activity to close pane + if (!activity.shouldPaneBeOpen()) { + activity.getSlidingPaneLayout().closePane(); + } + + int size = this.messageList.size(); + if (size >= 1) + messagesView.setSelection(size - 1); + } + + public void onBackendConnected() { + final ConversationActivity activity = (ConversationActivity) getActivity(); + this.conversation = activity.getConversationList().get(activity.getSelectedConversation()); + this.messageList.clear(); + this.messageList.addAll(this.conversation.getMessages()); } } |