Commit 08d03ebb authored by Grigory Fedorov's avatar Grigory Fedorov

ChatMessageAdapter: action message extracted to separate layout.

ChatViewerFragment: context menu fixed.
parent c8f653e0
......@@ -139,10 +139,7 @@ public class ChatViewerFragment extends Fragment {
});
updateView();
chatMessageAdapter.onChange();
updateChat();
return view;
}
......@@ -222,25 +219,27 @@ public class ChatViewerFragment extends Fragment {
updateChat();
}
private void updateView() {
chatMessageAdapter.onChange();
}
@Override
public void onCreateContextMenu(ContextMenu menu, View view,
ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, view, menuInfo);
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
final MessageItem message = (MessageItem) listView.getAdapter().getItem(info.position);
ChatMessageAdapter chatMessageAdapter = (ChatMessageAdapter) listView.getAdapter();
int itemViewType = chatMessageAdapter.getItemViewType(info.position);
if (itemViewType == ChatMessageAdapter.VIEW_TYPE_INCOMING_MESSAGE
|| itemViewType == ChatMessageAdapter.VIEW_TYPE_OUTGOING_MESSAGE) {
MenuInflater inflater = getActivity().getMenuInflater();
inflater.inflate(R.menu.chat_context_menu, menu);
if (message.isError()) {
if (((MessageItem) chatMessageAdapter.getItem(info.position)).isError()) {
menu.findItem(R.id.action_message_repeat).setVisible(true);
}
}
}
@Override
public boolean onContextItemSelected(MenuItem item) {
......@@ -258,7 +257,7 @@ public class ChatViewerFragment extends Fragment {
return true;
case R.id.action_message_quote:
insertText("> " + message.getText() + "\n");
setInputText("> " + message.getText() + "\n");
return true;
case R.id.action_message_remove:
......@@ -271,12 +270,15 @@ public class ChatViewerFragment extends Fragment {
}
}
/**
* Insert additional text to the input.
*
* @param additional
*/
private void insertText(String additional) {
public void updateChat() {
chatMessageAdapter.onChange();
}
public boolean isEqual(String account, String user) {
return this.account.equals(account) && this.user.equals(user);
}
public void setInputText(String additional) {
String source = inputView.getText().toString();
int selection = inputView.getSelectionEnd();
if (selection == -1)
......@@ -291,18 +293,6 @@ public class ChatViewerFragment extends Fragment {
inputView.setSelection(selection + additional.length());
}
public void updateChat() {
updateView();
}
public boolean isEqual(String account, String user) {
return this.account.equals(account) && this.user.equals(user);
}
public void setInputText(String text) {
insertText(text);
}
public String getAccount() {
return account;
}
......
......@@ -54,12 +54,13 @@ import java.util.List;
*/
public class ChatMessageAdapter extends BaseAdapter implements UpdatableAdapter {
private static final int VIEW_TYPE_COUNT = 4;
private static final int VIEW_TYPE_COUNT = 5;
private static final int VIEW_TYPE_EMPTY = 0;
private static final int VIEW_TYPE_HINT = 1;
private static final int VIEW_TYPE_INCOMING_MESSAGE = 2;
private static final int VIEW_TYPE_OUTGOING_MESSAGE = 3;
public static final int VIEW_TYPE_INCOMING_MESSAGE = 2;
public static final int VIEW_TYPE_OUTGOING_MESSAGE = 3;
private static final int VIEW_TYPE_ACTION_MESSAGE = 4;
private final Activity activity;
private String account;
......@@ -129,8 +130,12 @@ public class ChatMessageAdapter extends BaseAdapter implements UpdatableAdapter
return hint == null ? VIEW_TYPE_EMPTY : VIEW_TYPE_HINT;
}
final boolean incoming = ((MessageItem) getItem(position)).isIncoming();
return incoming ? VIEW_TYPE_INCOMING_MESSAGE : VIEW_TYPE_OUTGOING_MESSAGE;
MessageItem messageItem = (MessageItem) getItem(position);
if (messageItem.getAction() != null) {
return VIEW_TYPE_ACTION_MESSAGE;
}
return messageItem.isIncoming() ? VIEW_TYPE_INCOMING_MESSAGE : VIEW_TYPE_OUTGOING_MESSAGE;
}
@Override
......@@ -157,6 +162,28 @@ public class ChatMessageAdapter extends BaseAdapter implements UpdatableAdapter
return view;
}
MessageItem messageItem = (MessageItem) getItem(position);
if (type == VIEW_TYPE_ACTION_MESSAGE) {
View view = convertView;
if (convertView == null) {
view = activity.getLayoutInflater().inflate(R.layout.chat_viewer_action_message, parent, false);
}
ChatAction action = messageItem.getAction();
Spannable text = Emoticons.newSpannable(
action.getText(activity, messageItem.getResource(), messageItem.getSpannable().toString()));
Emoticons.getSmiledText(activity.getApplication(), text);
String time = StringUtils.getSmartTimeText(activity, messageItem.getTimestamp());
((TextView)view.findViewById(R.id.action_message_text)).setText(time + ": " + text);
return view;
}
View view = convertView;
if (convertView == null) {
......@@ -173,19 +200,16 @@ public class ChatMessageAdapter extends BaseAdapter implements UpdatableAdapter
view = activity.getLayoutInflater().inflate(layoutId, parent, false);
}
return setUpMessageView((MessageItem) getItem(position), view);
return setUpMessageView(messageItem, view);
}
private View setUpMessageView(MessageItem messageItem, View view) {
final boolean incoming = messageItem.isIncoming();
ChatAction action = messageItem.getAction();
Spannable text = messageItem.getSpannable();
SpannableStringBuilder builder = new SpannableStringBuilder();
final String resource = messageItem.getResource();
if (action == null) {
if (!incoming) {
setStatusIcon(messageItem, view);
}
......@@ -195,11 +219,11 @@ public class ChatMessageAdapter extends BaseAdapter implements UpdatableAdapter
append(builder, divider, new TextAppearanceSpan(activity, R.style.ChatHeader));
}
Date timeStamp = messageItem.getDelayTimestamp();
Date delayTimestamp = messageItem.getDelayTimestamp();
if (timeStamp != null) {
if (delayTimestamp != null) {
String delay = activity.getString(incoming ? R.string.chat_delay : R.string.chat_typed,
StringUtils.getSmartTimeText(activity, timeStamp));
StringUtils.getSmartTimeText(activity, delayTimestamp));
append(builder, delay, new TextAppearanceSpan(activity, R.style.ChatHeader_Delay));
append(builder, divider, new TextAppearanceSpan(activity, R.style.ChatHeader));
}
......@@ -209,16 +233,14 @@ public class ChatMessageAdapter extends BaseAdapter implements UpdatableAdapter
new TextAppearanceSpan(activity, R.style.ChatHeader_Delay));
append(builder, divider, new TextAppearanceSpan(activity, R.style.ChatHeader));
}
Spannable text = messageItem.getSpannable();
Emoticons.getSmiledText(activity.getApplication(), text);
if (messageItem.getTag() == null) {
builder.append(text);
} else {
append(builder, text, new TextAppearanceSpan(activity, R.style.ChatRead));
}
} else {
text = Emoticons.newSpannable(action.getText(activity, resource, text.toString()));
Emoticons.getSmiledText(activity.getApplication(), text);
}
TextView textView = (TextView) view.findViewById(R.id.message_text);
textView.setTextAppearance(activity, appearanceStyle);
......
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/action_message_text"
android:textAppearance="@style/ChatHeader.Delay"
android:background="@color/blue_grey_500"
android:padding="6dp"
android:layout_margin="3dp"
android:text="Message"
/>
</LinearLayout>
\ No newline at end of file
......@@ -30,6 +30,7 @@
android:dividerHeight="0dp"
android:layout_above="@+id/chat_input"
tools:listitem="@layout/chat_viewer_outgoing_message"
android:listSelector="@android:color/transparent"
/>
<EditText
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment