Commit 5f6e47ca authored by Tiago Cunha's avatar Tiago Cunha Committed by GitHub

Merge pull request #160 from RocketChat/fix/attachment-ui

Using a dialog fragment
parents ec29bb97 3325f48e
...@@ -7,6 +7,7 @@ import android.os.Bundle; ...@@ -7,6 +7,7 @@ import android.os.Bundle;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar; import android.support.design.widget.Snackbar;
import android.support.v4.app.DialogFragment;
import android.support.v4.view.GravityCompat; import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout; import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.SlidingPaneLayout; import android.support.v4.widget.SlidingPaneLayout;
...@@ -19,6 +20,8 @@ import io.realm.Sort; ...@@ -19,6 +20,8 @@ import io.realm.Sort;
import org.json.JSONObject; import org.json.JSONObject;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID; import java.util.UUID;
import chat.rocket.android.R; import chat.rocket.android.R;
import chat.rocket.android.api.MethodCallHelper; import chat.rocket.android.api.MethodCallHelper;
...@@ -35,6 +38,7 @@ import chat.rocket.android.layouthelper.chatroom.MessageFormManager; ...@@ -35,6 +38,7 @@ import chat.rocket.android.layouthelper.chatroom.MessageFormManager;
import chat.rocket.android.layouthelper.chatroom.MessageListAdapter; import chat.rocket.android.layouthelper.chatroom.MessageListAdapter;
import chat.rocket.android.layouthelper.chatroom.AbstractNewMessageIndicatorManager; import chat.rocket.android.layouthelper.chatroom.AbstractNewMessageIndicatorManager;
import chat.rocket.android.layouthelper.chatroom.PairedMessage; import chat.rocket.android.layouthelper.chatroom.PairedMessage;
import chat.rocket.android.layouthelper.extra_action.AbstractExtraActionItem;
import chat.rocket.android.layouthelper.extra_action.MessageExtraActionBehavior; import chat.rocket.android.layouthelper.extra_action.MessageExtraActionBehavior;
import chat.rocket.android.layouthelper.extra_action.upload.AbstractUploadActionItem; import chat.rocket.android.layouthelper.extra_action.upload.AbstractUploadActionItem;
import chat.rocket.android.layouthelper.extra_action.upload.AudioUploadActionItem; import chat.rocket.android.layouthelper.extra_action.upload.AudioUploadActionItem;
...@@ -53,6 +57,7 @@ import chat.rocket.android.realm_helper.RealmModelListAdapter; ...@@ -53,6 +57,7 @@ import chat.rocket.android.realm_helper.RealmModelListAdapter;
import chat.rocket.android.realm_helper.RealmObjectObserver; import chat.rocket.android.realm_helper.RealmObjectObserver;
import chat.rocket.android.realm_helper.RealmStore; import chat.rocket.android.realm_helper.RealmStore;
import chat.rocket.android.service.RocketChatService; import chat.rocket.android.service.RocketChatService;
import chat.rocket.android.widget.internal.ExtraActionPickerDialogFragment;
import chat.rocket.android.widget.message.MessageFormLayout; import chat.rocket.android.widget.message.MessageFormLayout;
import permissions.dispatcher.NeedsPermission; import permissions.dispatcher.NeedsPermission;
import permissions.dispatcher.RuntimePermissions; import permissions.dispatcher.RuntimePermissions;
...@@ -62,7 +67,10 @@ import permissions.dispatcher.RuntimePermissions; ...@@ -62,7 +67,10 @@ import permissions.dispatcher.RuntimePermissions;
*/ */
@RuntimePermissions @RuntimePermissions
public class RoomFragment extends AbstractChatRoomFragment public class RoomFragment extends AbstractChatRoomFragment
implements OnBackPressListener, RealmModelListAdapter.OnItemClickListener<PairedMessage> { implements OnBackPressListener, ExtraActionPickerDialogFragment.Callback,
RealmModelListAdapter.OnItemClickListener<PairedMessage> {
private static final int DIALOG_ID = 1;
private String serverConfigId; private String serverConfigId;
private RealmHelper realmHelper; private RealmHelper realmHelper;
...@@ -79,6 +87,8 @@ public class RoomFragment extends AbstractChatRoomFragment ...@@ -79,6 +87,8 @@ public class RoomFragment extends AbstractChatRoomFragment
private Snackbar unreadIndicator; private Snackbar unreadIndicator;
private boolean previousUnreadMessageExists; private boolean previousUnreadMessageExists;
private List<AbstractExtraActionItem> extraActionItems;
public RoomFragment() { public RoomFragment() {
} }
...@@ -193,6 +203,14 @@ public class RoomFragment extends AbstractChatRoomFragment ...@@ -193,6 +203,14 @@ public class RoomFragment extends AbstractChatRoomFragment
setupSideMenu(); setupSideMenu();
setupMessageComposer(); setupMessageComposer();
setupMessageActions();
}
private void setupMessageActions() {
extraActionItems = new ArrayList<>(3); // fixed number as of now
extraActionItems.add(new ImageUploadActionItem());
extraActionItems.add(new AudioUploadActionItem());
extraActionItems.add(new VideoUploadActionItem());
} }
private void scrollToLatestMessage() { private void scrollToLatestMessage() {
...@@ -297,7 +315,8 @@ public class RoomFragment extends AbstractChatRoomFragment ...@@ -297,7 +315,8 @@ public class RoomFragment extends AbstractChatRoomFragment
private void setupMessageComposer() { private void setupMessageComposer() {
final MessageFormLayout messageFormLayout = final MessageFormLayout messageFormLayout =
(MessageFormLayout) rootView.findViewById(R.id.message_composer); (MessageFormLayout) rootView.findViewById(R.id.message_composer);
messageFormManager = new MessageFormManager(messageFormLayout); messageFormManager =
new MessageFormManager(messageFormLayout, this::showExtraActionSelectionDialog);
messageFormManager.setSendMessageCallback(messageText -> messageFormManager.setSendMessageCallback(messageText ->
realmHelper.executeTransaction(realm -> realmHelper.executeTransaction(realm ->
realm.createOrUpdateObjectFromJson(Message.class, new JSONObject() realm.createOrUpdateObjectFromJson(Message.class, new JSONObject()
...@@ -312,11 +331,6 @@ public class RoomFragment extends AbstractChatRoomFragment ...@@ -312,11 +331,6 @@ public class RoomFragment extends AbstractChatRoomFragment
scrollToLatestMessage(); scrollToLatestMessage();
return null; return null;
})); }));
messageFormManager.registerExtraActionItem(new ImageUploadActionItem());
messageFormManager.registerExtraActionItem(new AudioUploadActionItem());
messageFormManager.registerExtraActionItem(new VideoUploadActionItem());
messageFormManager.setExtraActionPickerCallback(item ->
RoomFragmentPermissionsDispatcher.onExtraActionSelectedWithCheck(RoomFragment.this, item));
} }
@Override @Override
...@@ -336,7 +350,7 @@ public class RoomFragment extends AbstractChatRoomFragment ...@@ -336,7 +350,7 @@ public class RoomFragment extends AbstractChatRoomFragment
FileUploadProgressDialogFragment.create(serverConfigId, roomId, uplId) FileUploadProgressDialogFragment.create(serverConfigId, roomId, uplId)
.show(getFragmentManager(), FileUploadProgressDialogFragment.class.getSimpleName()); .show(getFragmentManager(), FileUploadProgressDialogFragment.class.getSimpleName());
} else { } else {
//show error. // show error.
} }
} }
...@@ -442,6 +456,24 @@ public class RoomFragment extends AbstractChatRoomFragment ...@@ -442,6 +456,24 @@ public class RoomFragment extends AbstractChatRoomFragment
super.onPause(); super.onPause();
} }
private void showExtraActionSelectionDialog() {
final DialogFragment fragment = ExtraActionPickerDialogFragment
.create(new ArrayList<>(extraActionItems));
fragment.setTargetFragment(this, DIALOG_ID);
fragment.show(getFragmentManager(), "ExtraActionPickerDialogFragment");
}
@Override
public void onItemSelected(int itemId) {
for (AbstractExtraActionItem extraActionItem : extraActionItems) {
if (extraActionItem.getItemId() == itemId) {
RoomFragmentPermissionsDispatcher
.onExtraActionSelectedWithCheck(RoomFragment.this, extraActionItem);
return;
}
}
}
@Override @Override
public boolean onBackPressed() { public boolean onBackPressed() {
return closeSideMenuIfNeeded(); return closeSideMenuIfNeeded();
......
package chat.rocket.android.layouthelper.chatroom; package chat.rocket.android.layouthelper.chatroom;
import java.util.HashMap;
import bolts.Task; import bolts.Task;
import chat.rocket.android.layouthelper.extra_action.AbstractExtraActionItem;
import chat.rocket.android.widget.message.MessageFormLayout; import chat.rocket.android.widget.message.MessageFormLayout;
/** /**
...@@ -11,42 +9,22 @@ import chat.rocket.android.widget.message.MessageFormLayout; ...@@ -11,42 +9,22 @@ import chat.rocket.android.widget.message.MessageFormLayout;
public class MessageFormManager { public class MessageFormManager {
private final MessageFormLayout messageFormLayout; private final MessageFormLayout messageFormLayout;
private SendMessageCallback sendMessageCallback; private SendMessageCallback sendMessageCallback;
private ExtraActionPickerCallback extraActionPickerCallback;
private final HashMap<Integer, AbstractExtraActionItem> extraActionItemMap;
public MessageFormManager(MessageFormLayout messageFormLayout) { public MessageFormManager(MessageFormLayout messageFormLayout,
MessageFormLayout.ExtraActionSelectionClickListener callback) {
this.messageFormLayout = messageFormLayout; this.messageFormLayout = messageFormLayout;
this.extraActionItemMap = new HashMap<>(); init(callback);
init();
} }
private void init() { private void init(MessageFormLayout.ExtraActionSelectionClickListener listener) {
messageFormLayout.setOnActionListener(new MessageFormLayout.ActionListener() { messageFormLayout.setExtraActionSelectionClickListener(listener);
@Override messageFormLayout.setSubmitTextListener(this::sendMessage);
public void onSubmitText(String message) {
sendMessage(message);
}
@Override
public void onExtraActionSelected(int itemId) {
if (extraActionItemMap.containsKey(itemId)) {
AbstractExtraActionItem item = extraActionItemMap.get(itemId);
if (extraActionPickerCallback != null) {
extraActionPickerCallback.onExtraActionSelected(item);
}
}
}
});
} }
public void setSendMessageCallback(SendMessageCallback sendMessageCallback) { public void setSendMessageCallback(SendMessageCallback sendMessageCallback) {
this.sendMessageCallback = sendMessageCallback; this.sendMessageCallback = sendMessageCallback;
} }
public void setExtraActionPickerCallback(ExtraActionPickerCallback extraActionPickerCallback) {
this.extraActionPickerCallback = extraActionPickerCallback;
}
public void clearComposingText() { public void clearComposingText() {
messageFormLayout.setText(""); messageFormLayout.setText("");
} }
...@@ -66,16 +44,7 @@ public class MessageFormManager { ...@@ -66,16 +44,7 @@ public class MessageFormManager {
}); });
} }
public void registerExtraActionItem(AbstractExtraActionItem actionItem) {
messageFormLayout.addExtraActionItem(actionItem);
extraActionItemMap.put(actionItem.getItemId(), actionItem);
}
public interface SendMessageCallback { public interface SendMessageCallback {
Task<Void> onSubmitText(String messageText); Task<Void> onSubmitText(String messageText);
} }
public interface ExtraActionPickerCallback {
void onExtraActionSelected(AbstractExtraActionItem item);
}
} }
package chat.rocket.android.widget.internal;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.OperationCanceledException;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomSheetDialog;
import android.support.v7.widget.RecyclerView;
import java.util.List;
import bolts.Task;
import bolts.TaskCompletionSource;
import chat.rocket.android.widget.R;
import chat.rocket.android.widget.layouthelper.MessageExtraActionListAdapter;
import chat.rocket.android.widget.message.MessageExtraActionItemPresenter;
public class ExtraActionPickerDialog {
/**
* show extra actions picker diaog.
*/
public static Task<Integer> showAsTask(Context context,
List<MessageExtraActionItemPresenter> actionItems) {
final TaskCompletionSource<Integer> task = new TaskCompletionSource<>();
Impl dialog = new Impl(context, actionItems);
dialog.setCallback(new Impl.Callback() {
@Override
public void onItemSelected(int itemId) {
task.setResult(itemId);
}
@Override
public void onCanceled() {
task.setError(new OperationCanceledException());
}
});
dialog.show();
return task.getTask();
}
private static class Impl extends BottomSheetDialog {
private interface Callback {
void onItemSelected(int itemId);
void onCanceled();
}
private Callback callback;
private final List<MessageExtraActionItemPresenter> actionItems;
public void setCallback(Callback callback) {
this.callback = callback;
}
public Impl(@NonNull Context context, List<MessageExtraActionItemPresenter> actionItems) {
super(context);
this.actionItems = actionItems;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_message_extra_action_picker);
MessageExtraActionListAdapter adapter = new MessageExtraActionListAdapter(actionItems);
adapter.setOnItemClickListener(new MessageExtraActionListAdapter.OnItemClickListener() {
@Override
public void onItemClick(int itemId) {
doCallback(itemId);
dismiss();
}
});
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.message_extra_action_listview);
recyclerView.setAdapter(adapter);
setOnCancelListener(new OnCancelListener() {
@Override
public void onCancel(DialogInterface dialogInterface) {
if (callback != null) {
callback.onCanceled();
}
}
});
}
private void doCallback(int itemId) {
if (callback != null) {
callback.onItemSelected(itemId);
}
}
}
}
\ No newline at end of file
package chat.rocket.android.widget.internal;
import android.app.Dialog;
import android.support.design.widget.BottomSheetDialogFragment;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import java.util.List;
import chat.rocket.android.widget.R;
import chat.rocket.android.widget.layouthelper.MessageExtraActionListAdapter;
import chat.rocket.android.widget.message.MessageExtraActionItemPresenter;
public class ExtraActionPickerDialogFragment extends BottomSheetDialogFragment {
private List<MessageExtraActionItemPresenter> actionItems;
public static ExtraActionPickerDialogFragment create(
List<MessageExtraActionItemPresenter> actionItems) {
ExtraActionPickerDialogFragment fragment = new ExtraActionPickerDialogFragment();
fragment.setActionItems(actionItems);
return fragment;
}
public void setActionItems(List<MessageExtraActionItemPresenter> actionItems) {
this.actionItems = actionItems;
}
@Override
public final void setupDialog(Dialog dialog, int style) {
super.setupDialog(dialog, style);
dialog.setContentView(R.layout.dialog_message_extra_action_picker);
MessageExtraActionListAdapter adapter = new MessageExtraActionListAdapter(actionItems);
adapter.setOnItemClickListener(new MessageExtraActionListAdapter.OnItemClickListener() {
@Override
public void onItemClick(int itemId) {
callbackOnItemSelected(itemId);
dismiss();
}
});
RecyclerView recyclerView =
(RecyclerView) dialog.findViewById(R.id.message_extra_action_listview);
recyclerView.setAdapter(adapter);
}
private void callbackOnItemSelected(int itemId) {
final Fragment fragment = getTargetFragment();
if (fragment instanceof Callback) {
((Callback) fragment).onItemSelected(itemId);
}
}
public interface Callback {
void onItemSelected(int itemId);
}
}
\ No newline at end of file
...@@ -3,7 +3,6 @@ package chat.rocket.android.widget.message; ...@@ -3,7 +3,6 @@ package chat.rocket.android.widget.message;
import android.annotation.TargetApi; import android.annotation.TargetApi;
import android.content.Context; import android.content.Context;
import android.os.Build; import android.os.Build;
import android.support.annotation.Nullable;
import android.text.Editable; import android.text.Editable;
import android.text.TextUtils; import android.text.TextUtils;
import android.text.TextWatcher; import android.text.TextWatcher;
...@@ -15,21 +14,17 @@ import android.widget.EditText; ...@@ -15,21 +14,17 @@ import android.widget.EditText;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.TextView; import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import bolts.Continuation;
import bolts.Task;
import chat.rocket.android.widget.R; import chat.rocket.android.widget.R;
import chat.rocket.android.widget.internal.ExtraActionPickerDialog;
public class MessageFormLayout extends LinearLayout { public class MessageFormLayout extends LinearLayout {
protected ActionListener actionListener;
protected ViewGroup composer; protected ViewGroup composer;
private View btnExtra; private View btnExtra;
private View btnSubmit; private View btnSubmit;
private List<MessageExtraActionItemPresenter> extraActionItems;
private ExtraActionSelectionClickListener extraActionSelectionClickListener;
private SubmitTextListener submitTextListener;
public MessageFormLayout(Context context) { public MessageFormLayout(Context context) {
super(context); super(context);
...@@ -52,12 +47,7 @@ public class MessageFormLayout extends LinearLayout { ...@@ -52,12 +47,7 @@ public class MessageFormLayout extends LinearLayout {
init(); init();
} }
public final void setOnActionListener(@Nullable ActionListener listener) {
actionListener = listener;
}
private void init() { private void init() {
extraActionItems = new ArrayList<>();
composer = (ViewGroup) LayoutInflater.from(getContext()) composer = (ViewGroup) LayoutInflater.from(getContext())
.inflate(R.layout.message_composer, this, false); .inflate(R.layout.message_composer, this, false);
...@@ -66,7 +56,7 @@ public class MessageFormLayout extends LinearLayout { ...@@ -66,7 +56,7 @@ public class MessageFormLayout extends LinearLayout {
btnExtra.setOnClickListener(new OnClickListener() { btnExtra.setOnClickListener(new OnClickListener() {
@Override @Override
public void onClick(View view) { public void onClick(View view) {
showExtraActionSelectionDialog(); onExtraActionSelectionClick();
} }
}); });
...@@ -76,8 +66,8 @@ public class MessageFormLayout extends LinearLayout { ...@@ -76,8 +66,8 @@ public class MessageFormLayout extends LinearLayout {
@Override @Override
public void onClick(View view) { public void onClick(View view) {
String messageText = getText(); String messageText = getText();
if (messageText.length() > 0 && actionListener != null) { if (messageText.length() > 0 && submitTextListener != null) {
actionListener.onSubmitText(messageText); submitTextListener.onSubmitText(messageText);
} }
} }
}); });
...@@ -110,17 +100,18 @@ public class MessageFormLayout extends LinearLayout { ...@@ -110,17 +100,18 @@ public class MessageFormLayout extends LinearLayout {
addView(composer); addView(composer);
} }
public void addExtraActionItem(MessageExtraActionItemPresenter itemPresenter) { public void setExtraActionSelectionClickListener(
boolean found = false; ExtraActionSelectionClickListener extraActionSelectionClickListener) {
for (MessageExtraActionItemPresenter item : extraActionItems) { this.extraActionSelectionClickListener = extraActionSelectionClickListener;
if (item.getItemId() == itemPresenter.getItemId()) { }
found = true;
break; public void setSubmitTextListener(SubmitTextListener submitTextListener) {
} this.submitTextListener = submitTextListener;
} }
if (!found) { private void onExtraActionSelectionClick() {
extraActionItems.add(itemPresenter); if (extraActionSelectionClickListener != null) {
extraActionSelectionClickListener.onClick();
} }
} }
...@@ -159,23 +150,11 @@ public class MessageFormLayout extends LinearLayout { ...@@ -159,23 +150,11 @@ public class MessageFormLayout extends LinearLayout {
}); });
} }
private void showExtraActionSelectionDialog() { public interface ExtraActionSelectionClickListener {
ExtraActionPickerDialog.showAsTask(getContext(), extraActionItems) void onClick();
.onSuccess(new Continuation<Integer, Object>() {
@Override
public Object then(Task<Integer> task) throws Exception {
int which = task.getResult();
if (actionListener != null) {
actionListener.onExtraActionSelected(which);
}
return null;
}
});
} }
public interface ActionListener { public interface SubmitTextListener {
void onSubmitText(String message); void onSubmitText(String message);
void onExtraActionSelected(int itemId);
} }
} }
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