Commit 044a9d5b authored by Tiago Cunha's avatar Tiago Cunha

Using a dialog fragment

parent c8493fbb
...@@ -19,6 +19,8 @@ import io.realm.Sort; ...@@ -19,6 +19,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 +37,7 @@ import chat.rocket.android.layouthelper.chatroom.MessageFormManager; ...@@ -35,6 +37,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 +56,7 @@ import chat.rocket.android.realm_helper.RealmModelListAdapter; ...@@ -53,6 +56,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;
...@@ -79,6 +83,8 @@ public class RoomFragment extends AbstractChatRoomFragment ...@@ -79,6 +83,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() {
} }
...@@ -295,9 +301,15 @@ public class RoomFragment extends AbstractChatRoomFragment ...@@ -295,9 +301,15 @@ public class RoomFragment extends AbstractChatRoomFragment
} }
private void setupMessageComposer() { private void setupMessageComposer() {
extraActionItems = new ArrayList<>(3); // fixed number as of now
extraActionItems.add(new ImageUploadActionItem());
extraActionItems.add(new AudioUploadActionItem());
extraActionItems.add(new VideoUploadActionItem());
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 +324,6 @@ public class RoomFragment extends AbstractChatRoomFragment ...@@ -312,11 +324,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
...@@ -442,6 +449,22 @@ public class RoomFragment extends AbstractChatRoomFragment ...@@ -442,6 +449,22 @@ public class RoomFragment extends AbstractChatRoomFragment
super.onPause(); super.onPause();
} }
private void showExtraActionSelectionDialog() {
ExtraActionPickerDialogFragment
.create(new ArrayList<>(extraActionItems), this::onExtraItemSelected)
.show(getFragmentManager(), "ExtraActionPickerDialogFragment");
}
private void onExtraItemSelected(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.ShowExtraActionSelectionCallback callback) {
this.messageFormLayout = messageFormLayout; this.messageFormLayout = messageFormLayout;
this.extraActionItemMap = new HashMap<>(); init(callback);
init();
} }
private void init() { private void init(MessageFormLayout.ShowExtraActionSelectionCallback callback) {
messageFormLayout.setOnActionListener(new MessageFormLayout.ActionListener() { messageFormLayout.setShowExtraActionSelectionCallback(callback);
@Override messageFormLayout.setTextListener(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.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;
private Callback callback;
public static ExtraActionPickerDialogFragment create(
List<MessageExtraActionItemPresenter> actionItems, Callback callback) {
ExtraActionPickerDialogFragment fragment = new ExtraActionPickerDialogFragment();
fragment.setActionItems(actionItems);
fragment.setCallback(callback);
return fragment;
}
public void setActionItems(List<MessageExtraActionItemPresenter> actionItems) {
this.actionItems = actionItems;
}
public void setCallback(Callback callback) {
this.callback = callback;
}
@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) {
if (callback != null) {
callback.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;
...@@ -16,20 +15,17 @@ import android.widget.LinearLayout; ...@@ -16,20 +15,17 @@ import android.widget.LinearLayout;
import android.widget.TextView; import android.widget.TextView;
import java.util.ArrayList; 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 ShowExtraActionSelectionCallback showExtraActionSelectionCallback;
private TextListener textListener;
public MessageFormLayout(Context context) { public MessageFormLayout(Context context) {
super(context); super(context);
...@@ -52,12 +48,7 @@ public class MessageFormLayout extends LinearLayout { ...@@ -52,12 +48,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);
...@@ -76,8 +67,8 @@ public class MessageFormLayout extends LinearLayout { ...@@ -76,8 +67,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 && textListener != null) {
actionListener.onSubmitText(messageText); textListener.onSubmitText(messageText);
} }
} }
}); });
...@@ -110,17 +101,18 @@ public class MessageFormLayout extends LinearLayout { ...@@ -110,17 +101,18 @@ public class MessageFormLayout extends LinearLayout {
addView(composer); addView(composer);
} }
public void addExtraActionItem(MessageExtraActionItemPresenter itemPresenter) { public void setShowExtraActionSelectionCallback(
boolean found = false; ShowExtraActionSelectionCallback showExtraActionSelectionCallback) {
for (MessageExtraActionItemPresenter item : extraActionItems) { this.showExtraActionSelectionCallback = showExtraActionSelectionCallback;
if (item.getItemId() == itemPresenter.getItemId()) { }
found = true;
break;
}
}
if (!found) { public void setTextListener(TextListener textListener) {
extraActionItems.add(itemPresenter); this.textListener = textListener;
}
private void showExtraActionSelectionDialog() {
if (showExtraActionSelectionCallback != null) {
showExtraActionSelectionCallback.show();
} }
} }
...@@ -159,23 +151,11 @@ public class MessageFormLayout extends LinearLayout { ...@@ -159,23 +151,11 @@ public class MessageFormLayout extends LinearLayout {
}); });
} }
private void showExtraActionSelectionDialog() { public interface ShowExtraActionSelectionCallback {
ExtraActionPickerDialog.showAsTask(getContext(), extraActionItems) void show();
.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 {
void onSubmitText(String message);
void onExtraActionSelected(int itemId); public interface TextListener {
void onSubmitText(String message);
} }
} }
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