Commit 877c7295 authored by Grigory Fedorov's avatar Grigory Fedorov

Chat: message context menu replaced by popup menu. This fixes #428

Problem was that context menu depend on activity and wrong fragment could be selected easily.
Popup menu implemented totally inside the fragment.
parent d5dfb71d
...@@ -12,11 +12,9 @@ import android.support.v7.widget.RecyclerView; ...@@ -12,11 +12,9 @@ import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar; import android.support.v7.widget.Toolbar;
import android.text.Editable; import android.text.Editable;
import android.text.TextWatcher; import android.text.TextWatcher;
import android.view.ContextMenu;
import android.view.KeyEvent; import android.view.KeyEvent;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.Menu; import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
...@@ -73,11 +71,11 @@ public class ChatViewerFragment extends Fragment implements PopupMenu.OnMenuItem ...@@ -73,11 +71,11 @@ public class ChatViewerFragment extends Fragment implements PopupMenu.OnMenuItem
private ChatViewerFragmentListener listener; private ChatViewerFragmentListener listener;
private Animation shakeAnimation = null; private Animation shakeAnimation = null;
private int contextMenuItemPosition;
private RecyclerView recyclerView; private RecyclerView recyclerView;
private View contactTitleView; private View contactTitleView;
private AbstractContact abstractContact; private AbstractContact abstractContact;
private LinearLayoutManager layoutManager; private LinearLayoutManager layoutManager;
private MessageItem clickedMessageItem;
public static ChatViewerFragment newInstance(String account, String user) { public static ChatViewerFragment newInstance(String account, String user) {
ChatViewerFragment fragment = new ChatViewerFragment(); ChatViewerFragment fragment = new ChatViewerFragment();
...@@ -361,52 +359,6 @@ public class ChatViewerFragment extends Fragment implements PopupMenu.OnMenuItem ...@@ -361,52 +359,6 @@ public class ChatViewerFragment extends Fragment implements PopupMenu.OnMenuItem
} }
} }
@Override
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, view, menuInfo);
int itemViewType = chatMessageAdapter.getItemViewType(contextMenuItemPosition);
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 (chatMessageAdapter.getMessageItem(contextMenuItemPosition).isError()) {
menu.findItem(R.id.action_message_repeat).setVisible(true);
}
}
}
@Override
public boolean onContextItemSelected(MenuItem item) {
final MessageItem message = chatMessageAdapter.getMessageItem(contextMenuItemPosition);
switch (item.getItemId()) {
case R.id.action_message_repeat:
sendMessage(message.getText());
return true;
case R.id.action_message_copy:
((ClipboardManager) getActivity().getSystemService(Context.CLIPBOARD_SERVICE))
.setPrimaryClip(ClipData.newPlainText(message.getSpannable(), message.getSpannable()));
return true;
case R.id.action_message_quote:
setInputText("> " + message.getText() + "\n");
return true;
case R.id.action_message_remove:
MessageManager.getInstance().removeMessage(message);
updateChat();
return true;
default:
return super.onContextItemSelected(item);
}
}
public void updateChat() { public void updateChat() {
ContactTitleInflater.updateTitle(contactTitleView, getActivity(), abstractContact); ContactTitleInflater.updateTitle(contactTitleView, getActivity(), abstractContact);
int itemCountBeforeUpdate = recyclerView.getAdapter().getItemCount(); int itemCountBeforeUpdate = recyclerView.getAdapter().getItemCount();
...@@ -533,6 +485,25 @@ public class ChatViewerFragment extends Fragment implements PopupMenu.OnMenuItem ...@@ -533,6 +485,25 @@ public class ChatViewerFragment extends Fragment implements PopupMenu.OnMenuItem
startActivity(OccupantList.createIntent(getActivity(), account, user)); startActivity(OccupantList.createIntent(getActivity(), account, user));
return true; return true;
/* message popup menu */
case R.id.action_message_repeat:
sendMessage(clickedMessageItem.getText());
return true;
case R.id.action_message_copy:
((ClipboardManager) getActivity().getSystemService(Context.CLIPBOARD_SERVICE))
.setPrimaryClip(ClipData.newPlainText(clickedMessageItem.getSpannable(), clickedMessageItem.getSpannable()));
return true;
case R.id.action_message_quote:
setInputText("> " + clickedMessageItem.getText() + "\n");
return true;
case R.id.action_message_remove:
MessageManager.getInstance().removeMessage(clickedMessageItem);
updateChat();
return true;
default: default:
return false; return false;
...@@ -608,11 +579,23 @@ public class ChatViewerFragment extends Fragment implements PopupMenu.OnMenuItem ...@@ -608,11 +579,23 @@ public class ChatViewerFragment extends Fragment implements PopupMenu.OnMenuItem
@Override @Override
public void onMessageClick(View caller, int position) { public void onMessageClick(View caller, int position) {
registerForContextMenu(caller); int itemViewType = chatMessageAdapter.getItemViewType(position);
this.contextMenuItemPosition = position;
caller.setOnCreateContextMenuListener(this); if (itemViewType == ChatMessageAdapter.VIEW_TYPE_INCOMING_MESSAGE
caller.showContextMenu(); || itemViewType == ChatMessageAdapter.VIEW_TYPE_OUTGOING_MESSAGE) {
unregisterForContextMenu(caller);
clickedMessageItem = chatMessageAdapter.getMessageItem(position);
PopupMenu popup = new PopupMenu(getActivity(), caller);
popup.inflate(R.menu.chat_context_menu);
popup.setOnMenuItemClickListener(this);
if (chatMessageAdapter.getMessageItem(position).isError()) {
popup.getMenu().findItem(R.id.action_message_repeat).setVisible(true);
}
popup.show();
}
} }
public void playIncomingAnimation() { public void playIncomingAnimation() {
......
...@@ -318,7 +318,7 @@ public class ChatMessageAdapter extends RecyclerView.Adapter<RecyclerView.ViewHo ...@@ -318,7 +318,7 @@ public class ChatMessageAdapter extends RecyclerView.Adapter<RecyclerView.ViewHo
@Override @Override
public void onClick(View v) { public void onClick(View v) {
onClickListener.onMessageClick(v, getPosition()); onClickListener.onMessageClick(messageBalloon, getPosition());
} }
public interface MessageClickListener { public interface MessageClickListener {
......
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