Commit 7db4b7cf authored by Yusuke Iwaki's avatar Yusuke Iwaki Committed by GitHub

Merge pull request #109 from RocketChat/refactor_message_composer

Refactor message composer
parents 8af0b01f 0b3f313e
......@@ -22,22 +22,21 @@ import java.util.UUID;
import chat.rocket.android.R;
import chat.rocket.android.api.MethodCallHelper;
import chat.rocket.android.fragment.chatroom.dialog.FileUploadProgressDialogFragment;
import chat.rocket.android.fragment.chatroom.dialog.MessageSelectionDialogFragment;
import chat.rocket.android.fragment.chatroom.dialog.UsersOfRoomDialogFragment;
import chat.rocket.android.helper.FileUploadHelper;
import chat.rocket.android.helper.LoadMoreScrollListener;
import chat.rocket.android.helper.LogcatIfError;
import chat.rocket.android.helper.OnBackPressListener;
import chat.rocket.android.helper.TextUtils;
import chat.rocket.android.layouthelper.chatroom.MessageComposerManager;
import chat.rocket.android.layouthelper.chatroom.MessageFormManager;
import chat.rocket.android.layouthelper.chatroom.MessageListAdapter;
import chat.rocket.android.layouthelper.chatroom.PairedMessage;
import chat.rocket.android.layouthelper.extra_action.MessageExtraActionBehavior;
import chat.rocket.android.log.RCLog;
import chat.rocket.android.message.AbstractMessageSpec;
import chat.rocket.android.message.AudioUploadMessageSpec;
import chat.rocket.android.message.AbstractUploadMessageSpec;
import chat.rocket.android.message.ImageUploadMessageSpec;
import chat.rocket.android.message.VideoUploadMessageSpec;
import chat.rocket.android.layouthelper.extra_action.upload.AudioUploadActionItem;
import chat.rocket.android.layouthelper.extra_action.upload.AbstractUploadActionItem;
import chat.rocket.android.layouthelper.extra_action.upload.ImageUploadActionItem;
import chat.rocket.android.layouthelper.extra_action.upload.VideoUploadActionItem;
import chat.rocket.android.model.ServerConfig;
import chat.rocket.android.model.SyncState;
import chat.rocket.android.model.ddp.Message;
......@@ -50,7 +49,7 @@ import chat.rocket.android.realm_helper.RealmModelListAdapter;
import chat.rocket.android.realm_helper.RealmObjectObserver;
import chat.rocket.android.realm_helper.RealmStore;
import chat.rocket.android.service.RocketChatService;
import chat.rocket.android.widget.message.MessageComposer;
import chat.rocket.android.widget.message.MessageFormLayout;
import permissions.dispatcher.NeedsPermission;
import permissions.dispatcher.RuntimePermissions;
......@@ -70,11 +69,7 @@ public class RoomFragment extends AbstractChatRoomFragment
private String token;
private LoadMoreScrollListener scrollListener;
private RealmObjectObserver<LoadMessageProcedure> procedureObserver;
private MessageComposerManager messageComposerManager;
private MessageSelectionDialogFragment.ClickListener messageSelectionClickListener =
messageSpec -> RoomFragmentPermissionsDispatcher
.onMessageSpecSelectedWithCheck(RoomFragment.this, messageSpec);
private MessageFormManager messageFormManager;
public RoomFragment() {
}
......@@ -224,10 +219,10 @@ public class RoomFragment extends AbstractChatRoomFragment
}
private void setupMessageComposer() {
final MessageComposer messageComposer =
(MessageComposer) rootView.findViewById(R.id.message_composer);
messageComposerManager = new MessageComposerManager(messageComposer);
messageComposerManager.setSendMessageCallback(messageText ->
final MessageFormLayout messageFormLayout =
(MessageFormLayout) rootView.findViewById(R.id.message_composer);
messageFormManager = new MessageFormManager(messageFormLayout);
messageFormManager.setSendMessageCallback(messageText ->
realmHelper.executeTransaction(realm ->
realm.createOrUpdateObjectFromJson(Message.class, new JSONObject()
.put("_id", UUID.randomUUID().toString())
......@@ -235,25 +230,17 @@ public class RoomFragment extends AbstractChatRoomFragment
.put("ts", System.currentTimeMillis())
.put("rid", roomId)
.put("msg", messageText))));
messageComposerManager.setExtrasPickerListener(() -> {
MessageSelectionDialogFragment fragment = MessageSelectionDialogFragment.create();
fragment.addMessageSpec(new ImageUploadMessageSpec());
fragment.addMessageSpec(new AudioUploadMessageSpec());
fragment.addMessageSpec(new VideoUploadMessageSpec());
fragment.setListener(messageSelectionClickListener);
fragment.show(getFragmentManager(), MessageSelectionDialogFragment.TAG);
closeSideMenuIfNeeded();
});
messageFormManager.registerExtraActionItem(new ImageUploadActionItem());
messageFormManager.registerExtraActionItem(new AudioUploadActionItem());
messageFormManager.registerExtraActionItem(new VideoUploadActionItem());
messageFormManager.setExtraActionPickerCallback(item ->
RoomFragmentPermissionsDispatcher.onExtraActionSelectedWithCheck(RoomFragment.this, item));
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode != AbstractUploadMessageSpec.RC_UPL || resultCode != Activity.RESULT_OK) {
if (requestCode != AbstractUploadActionItem.RC_UPL || resultCode != Activity.RESULT_OK) {
return;
}
......@@ -381,7 +368,7 @@ public class RoomFragment extends AbstractChatRoomFragment
}
@NeedsPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
protected void onMessageSpecSelected(AbstractMessageSpec messageSpec) {
messageSpec.onSelect(RoomFragment.this);
protected void onExtraActionSelected(MessageExtraActionBehavior action) {
action.handleItemSelectedOnFragment(RoomFragment.this);
}
}
package chat.rocket.android.fragment.chatroom.dialog;
import android.support.v7.widget.RecyclerView;
import chat.rocket.android.R;
import chat.rocket.android.layouthelper.chatroom.dialog.MessageSelectionAdapter;
import chat.rocket.android.message.AbstractMessageSpec;
public class MessageSelectionDialogFragment extends AbstractChatRoomDialogFragment {
public static final String TAG = "MessageSelectionDialogFragment";
private MessageSelectionAdapter adapter;
private ClickListener listener;
public static MessageSelectionDialogFragment create() {
return new MessageSelectionDialogFragment();
}
public MessageSelectionDialogFragment() {
adapter = new MessageSelectionAdapter();
adapter.setListener(messageSpec -> {
if (listener != null) {
listener.onClick(messageSpec);
}
dismiss();
});
}
public void addMessageSpec(AbstractMessageSpec abstractMessageSpec) {
adapter.addMessageSpec(abstractMessageSpec);
}
public void setListener(ClickListener listener) {
this.listener = listener;
}
@Override
protected int getLayout() {
return R.layout.dialog_message_selection;
}
@Override
protected void onSetupDialog() {
RecyclerView messageSpecList = (RecyclerView) getDialog().findViewById(R.id.message_spec_list);
messageSpecList.setAdapter(adapter);
}
public interface ClickListener {
void onClick(AbstractMessageSpec abstractMessageSpec);
}
}
package chat.rocket.android.layouthelper.chatroom;
import bolts.Task;
import chat.rocket.android.widget.message.MessageComposer;
/**
* handling visibility of FAB-compose and MessageComposer.
*/
public class MessageComposerManager {
private final MessageComposer messageComposer;
private SendMessageCallback sendMessageCallback;
private ExtrasPickerListener extrasPickerListener;
public MessageComposerManager(MessageComposer messageComposer) {
this.messageComposer = messageComposer;
init();
}
private void init() {
messageComposer.setOnActionListener(new MessageComposer.ActionListener() {
@Override
public void onSubmit(String message) {
sendMessage(message);
}
@Override
public void onExtra() {
openExtras();
}
@Override
public void onCancel() {
}
});
}
public void setSendMessageCallback(SendMessageCallback sendMessageCallback) {
this.sendMessageCallback = sendMessageCallback;
}
public void setExtrasPickerListener(ExtrasPickerListener listener) {
extrasPickerListener = listener;
}
public void clearComposingText() {
messageComposer.setText("");
}
private void sendMessage(String message) {
if (sendMessageCallback == null) {
return;
}
messageComposer.setEnabled(false);
sendMessageCallback.onSubmit(message).onSuccess(task -> {
clearComposingText();
return null;
}).continueWith(task -> {
messageComposer.setEnabled(true);
return null;
});
}
private void openExtras() {
if (extrasPickerListener == null) {
return;
}
extrasPickerListener.onOpen();
}
public interface SendMessageCallback {
Task<Void> onSubmit(String messageText);
}
public interface ExtrasPickerListener {
void onOpen();
}
}
package chat.rocket.android.layouthelper.chatroom;
import java.util.HashMap;
import bolts.Task;
import chat.rocket.android.layouthelper.extra_action.AbstractExtraActionItem;
import chat.rocket.android.widget.message.MessageFormLayout;
/**
* handling MessageForm.
*/
public class MessageFormManager {
private final MessageFormLayout messageFormLayout;
private SendMessageCallback sendMessageCallback;
private ExtraActionPickerCallback extraActionPickerCallback;
private final HashMap<Integer, AbstractExtraActionItem> extraActionItemMap;
public MessageFormManager(MessageFormLayout messageFormLayout) {
this.messageFormLayout = messageFormLayout;
this.extraActionItemMap = new HashMap<>();
init();
}
private void init() {
messageFormLayout.setOnActionListener(new MessageFormLayout.ActionListener() {
@Override
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) {
this.sendMessageCallback = sendMessageCallback;
}
public void setExtraActionPickerCallback(ExtraActionPickerCallback extraActionPickerCallback) {
this.extraActionPickerCallback = extraActionPickerCallback;
}
public void clearComposingText() {
messageFormLayout.setText("");
}
private void sendMessage(String message) {
if (sendMessageCallback == null) {
return;
}
messageFormLayout.setEnabled(false);
sendMessageCallback.onSubmitText(message).onSuccess(task -> {
clearComposingText();
return null;
}).continueWith(task -> {
messageFormLayout.setEnabled(true);
return null;
});
}
public void registerExtraActionItem(AbstractExtraActionItem actionItem) {
messageFormLayout.addExtraActionItem(actionItem);
extraActionItemMap.put(actionItem.getItemId(), actionItem);
}
public interface SendMessageCallback {
Task<Void> onSubmitText(String messageText);
}
public interface ExtraActionPickerCallback {
void onExtraActionSelected(AbstractExtraActionItem item);
}
}
package chat.rocket.android.layouthelper.chatroom.dialog;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
import java.util.List;
import chat.rocket.android.R;
import chat.rocket.android.message.AbstractMessageSpec;
public class MessageSelectionAdapter
extends RecyclerView.Adapter<MessageSelectionViewHolder> {
private List<AbstractMessageSpec> abstractMessageSpecs = new ArrayList<>();
private ClickListener listener;
public void setListener(ClickListener listener) {
this.listener = listener;
}
public void addMessageSpec(AbstractMessageSpec abstractMessageSpec) {
abstractMessageSpecs.add(abstractMessageSpec);
notifyDataSetChanged();
}
@Override
public MessageSelectionViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.dialog_message_selection_item, parent, false);
itemView.setOnClickListener(view -> {
if (listener != null) {
listener.onClick((AbstractMessageSpec) itemView.getTag());
}
});
return new MessageSelectionViewHolder(itemView);
}
@Override
public void onBindViewHolder(MessageSelectionViewHolder holder,
int position) {
holder.onBind(abstractMessageSpecs.get(position));
}
@Override
public int getItemCount() {
return abstractMessageSpecs.size();
}
public interface ClickListener {
void onClick(AbstractMessageSpec abstractMessageSpec);
}
}
package chat.rocket.android.layouthelper.extra_action;
import chat.rocket.android.widget.message.MessageExtraActionItemPresenter;
/**
* Simple base class that implements MessageExtraActionBehavior, MessageExtraActionItemPresenter.
*/
public abstract class AbstractExtraActionItem
implements MessageExtraActionBehavior, MessageExtraActionItemPresenter {
}
package chat.rocket.android.layouthelper.extra_action;
import android.app.Activity;
import android.support.v4.app.Fragment;
public interface MessageExtraActionBehavior {
void handleItemSelectedOnActivity(Activity activity);
void handleItemSelectedOnFragment(Fragment fragment);
}
package chat.rocket.android.layouthelper.extra_action.upload;
import android.app.Activity;
import android.content.Intent;
import android.support.v4.app.Fragment;
import chat.rocket.android.R;
import chat.rocket.android.layouthelper.extra_action.AbstractExtraActionItem;
public abstract class AbstractUploadActionItem extends AbstractExtraActionItem {
public static final int RC_UPL = 0x12;
@Override
public void handleItemSelectedOnActivity(Activity activity) {
activity.startActivityForResult(getIntentForPickFile(), RC_UPL);
}
@Override
public void handleItemSelectedOnFragment(Fragment fragment) {
fragment.startActivityForResult(getIntentForPickFile(), RC_UPL);
}
protected abstract Intent getIntentForPickFile();
@Override
public int getBackgroundTint() {
return R.color.colorAccent;
}
}
package chat.rocket.android.message;
package chat.rocket.android.layouthelper.extra_action.upload;
import android.content.Intent;
import chat.rocket.android.R;
public class AudioUploadMessageSpec extends AbstractUploadMessageSpec {
public class AudioUploadActionItem extends AbstractUploadActionItem {
@Override
protected Intent getIntent() {
public int getItemId() {
return 11;
}
@Override
protected Intent getIntentForPickFile() {
Intent intent = new Intent();
intent.setType("audio/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
return Intent.createChooser(intent, "Select Audio to Upload");
}
@Override
public ViewData getSpecificViewData() {
return new AudioUploadViewData();
}
private static class AudioUploadViewData implements AbstractMessageSpec.ViewData {
@Override
public int getBackgroundTint() {
return R.color.colorAccent;
}
@Override
public int getIcon() {
return R.drawable.ic_audiotrack_white_24dp;
......@@ -34,5 +28,4 @@ public class AudioUploadMessageSpec extends AbstractUploadMessageSpec {
public int getTitle() {
return R.string.audio_upload_message_spec_title;
}
}
}
package chat.rocket.android.message;
package chat.rocket.android.layouthelper.extra_action.upload;
import android.content.Intent;
import chat.rocket.android.R;
public class ImageUploadMessageSpec extends AbstractUploadMessageSpec {
public class ImageUploadActionItem extends AbstractUploadActionItem {
@Override
public ViewData getSpecificViewData() {
return new ImageUploadViewData();
public int getItemId() {
return 10;
}
@Override
protected Intent getIntent() {
protected Intent getIntentForPickFile() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
return Intent.createChooser(intent, "Select Picture to Upload");
}
private static class ImageUploadViewData implements AbstractMessageSpec.ViewData {
@Override
public int getBackgroundTint() {
return R.color.colorAccent;
}
@Override
public int getIcon() {
return R.drawable.ic_insert_photo_white_24dp;
......@@ -34,5 +28,4 @@ public class ImageUploadMessageSpec extends AbstractUploadMessageSpec {
public int getTitle() {
return R.string.image_upload_message_spec_title;
}
}
}
package chat.rocket.android.message;
package chat.rocket.android.layouthelper.extra_action.upload;
import android.content.Intent;
import chat.rocket.android.R;
public class VideoUploadMessageSpec extends AbstractUploadMessageSpec {
public class VideoUploadActionItem extends AbstractUploadActionItem {
@Override
public ViewData getSpecificViewData() {
return new VideoUploadViewData();
public int getItemId() {
return 12;
}
@Override
protected Intent getIntent() {
protected Intent getIntentForPickFile() {
Intent intent = new Intent();
intent.setType("video/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
return Intent.createChooser(intent, "Select Video to Upload");
}
private static class VideoUploadViewData implements AbstractMessageSpec.ViewData {
@Override
public int getBackgroundTint() {
return R.color.colorAccent;
}
@Override
public int getIcon() {
return R.drawable.ic_video_call_white_24dp;
......@@ -34,5 +28,4 @@ public class VideoUploadMessageSpec extends AbstractUploadMessageSpec {
public int getTitle() {
return R.string.video_upload_message_spec_title;
}
}
}
package chat.rocket.android.message;
import android.app.Activity;
import android.support.v4.app.Fragment;
import android.support.annotation.ColorRes;
import android.support.annotation.DrawableRes;
import android.support.annotation.StringRes;
public abstract class AbstractMessageSpec {
public abstract ViewData getViewData();
public abstract void onSelect(Activity activity);
public abstract void onSelect(Fragment fragment);
public interface ViewData {
@ColorRes
int getBackgroundTint();
@DrawableRes
int getIcon();
@StringRes
int getTitle();
}
}
package chat.rocket.android.message;
import android.app.Activity;
import android.support.v4.app.Fragment;
import android.content.Intent;
public abstract class AbstractUploadMessageSpec extends AbstractMessageSpec {
public static final int RC_UPL = 0x12;
private ViewData viewData;
@Override
public ViewData getViewData() {
if (viewData == null) {
viewData = getSpecificViewData();
}
return viewData;
}
@Override
public void onSelect(Activity activity) {
activity.startActivityForResult(getIntent(), RC_UPL);
}
@Override
public void onSelect(Fragment fragment) {
fragment.startActivityForResult(getIntent(), RC_UPL);
}
protected abstract Intent getIntent();
protected abstract ViewData getSpecificViewData();
}
......@@ -9,7 +9,7 @@
android:layout_height="match_parent"
android:layout_above="@+id/message_composer" />
<chat.rocket.android.widget.message.MessageComposer
<chat.rocket.android.widget.message.MessageFormLayout
android:id="@+id/message_composer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
......
......@@ -38,4 +38,5 @@ dependencies {
compile rootProject.ext.okhttp3
compile rootProject.ext.picasso
compile rootProject.ext.picasso2Okhttp3Downloader
compile rootProject.ext.boltsTask
}
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.layouthelper.chatroom.dialog;
package chat.rocket.android.widget.layouthelper;
import android.support.annotation.ColorRes;
import android.support.annotation.DrawableRes;
......@@ -10,40 +10,39 @@ import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import chat.rocket.android.R;
import chat.rocket.android.message.AbstractMessageSpec;
import chat.rocket.android.widget.R;
import chat.rocket.android.widget.message.MessageExtraActionItemPresenter;
public class MessageSelectionViewHolder extends RecyclerView.ViewHolder {
public class MessageExtraActionItemViewHolder extends RecyclerView.ViewHolder {
private ImageView messageSpecIcon;
private TextView messageSpecTitle;
private ImageView iconView;
private TextView titleView;
public MessageSelectionViewHolder(View itemView) {
public MessageExtraActionItemViewHolder(View itemView) {
super(itemView);
messageSpecIcon = (ImageView) itemView.findViewById(R.id.message_spec_icon);
messageSpecTitle = (TextView) itemView.findViewById(R.id.message_spec_title);
iconView = (ImageView) itemView.findViewById(R.id.icon);
titleView = (TextView) itemView.findViewById(R.id.title);
}
public void onBind(AbstractMessageSpec abstractMessageSpec) {
itemView.setTag(abstractMessageSpec);
public void onBind(MessageExtraActionItemPresenter actionItem) {
itemView.setTag(actionItem.getItemId());
AbstractMessageSpec.ViewData viewData = abstractMessageSpec.getViewData();
setIconBackgroundColorTint(viewData.getBackgroundTint());
setIcon(viewData.getIcon());
setTitle(viewData.getTitle());
setIconBackgroundColorTint(actionItem.getBackgroundTint());
setIcon(actionItem.getIcon());
setTitle(actionItem.getTitle());
}
public void setIconBackgroundColorTint(@ColorRes int color) {
// Drawable background = DrawableCompat.wrap(messageSpecIcon.getBackground());
DrawableCompat.setTint(messageSpecIcon.getBackground(),
ContextCompat.getColor(messageSpecIcon.getContext(), color));
DrawableCompat.setTint(iconView.getBackground(),
ContextCompat.getColor(iconView.getContext(), color));
}
public void setIcon(@DrawableRes int icon) {
messageSpecIcon.setImageResource(icon);
iconView.setImageResource(icon);
}
public void setTitle(@StringRes int title) {
messageSpecTitle.setText(title);
titleView.setText(title);
}
}
package chat.rocket.android.widget.layouthelper;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.List;
import chat.rocket.android.widget.R;
import chat.rocket.android.widget.message.MessageExtraActionItemPresenter;
public class MessageExtraActionListAdapter
extends RecyclerView.Adapter<MessageExtraActionItemViewHolder> {
private final List<MessageExtraActionItemPresenter> actionItems;
private OnItemClickListener listener;
public MessageExtraActionListAdapter(List<MessageExtraActionItemPresenter> actionItems) {
this.actionItems = actionItems;
}
public void setOnItemClickListener(OnItemClickListener listener) {
this.listener = listener;
}
@Override
public MessageExtraActionItemViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.message_extra_action_item, parent, false);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (listener != null) {
if (view.getTag() != null) {
listener.onItemClick((int) view.getTag());
}
}
}
});
return new MessageExtraActionItemViewHolder(itemView);
}
@Override
public void onBindViewHolder(MessageExtraActionItemViewHolder holder,
int position) {
holder.onBind(actionItems.get(position));
}
@Override
public int getItemCount() {
return actionItems.size();
}
public interface OnItemClickListener {
void onItemClick(int itemId);
}
}
package chat.rocket.android.widget.message;
import android.support.annotation.ColorRes;
import android.support.annotation.DrawableRes;
import android.support.annotation.StringRes;
/**
* View Data for message extra action item.
*/
public interface MessageExtraActionItemPresenter {
int getItemId();
@ColorRes
int getBackgroundTint();
@DrawableRes
int getIcon();
@StringRes
int getTitle();
}
......@@ -5,43 +5,49 @@ import android.content.Context;
import android.os.Build;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;
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.internal.ExtraActionPickerDialog;
public class MessageComposer extends LinearLayout {
public class MessageFormLayout extends LinearLayout {
protected ActionListener actionListener;
protected ViewGroup composer;
private View btnExtra;
private View btnSubmit;
private List<MessageExtraActionItemPresenter> extraActionItems;
public MessageComposer(Context context) {
public MessageFormLayout(Context context) {
super(context);
init();
}
public MessageComposer(Context context, AttributeSet attrs) {
public MessageFormLayout(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MessageComposer(Context context, AttributeSet attrs, int defStyleAttr) {
public MessageFormLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public MessageComposer(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
public MessageFormLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init();
}
......@@ -51,6 +57,7 @@ public class MessageComposer extends LinearLayout {
}
private void init() {
extraActionItems = new ArrayList<>();
composer = (ViewGroup) LayoutInflater.from(getContext())
.inflate(R.layout.message_composer, this, false);
......@@ -58,10 +65,8 @@ public class MessageComposer extends LinearLayout {
btnExtra.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (actionListener != null) {
actionListener.onExtra();
}
public void onClick(View view) {
showExtraActionSelectionDialog();
}
});
......@@ -72,12 +77,13 @@ public class MessageComposer extends LinearLayout {
public void onClick(View view) {
String messageText = getText();
if (messageText.length() > 0 && actionListener != null) {
actionListener.onSubmit(messageText);
actionListener.onSubmitText(messageText);
}
}
});
btnSubmit.animate().scaleX(0).scaleY(0).setDuration(0);
btnSubmit.setScaleX(0);
btnSubmit.setScaleY(0);
btnSubmit.setVisibility(GONE);
((EditText) composer.findViewById(R.id.editor)).addTextChangedListener(new TextWatcher() {
......@@ -91,7 +97,7 @@ public class MessageComposer extends LinearLayout {
@Override
public void afterTextChanged(Editable s) {
if (s.toString().trim().length() > 0) {
if (TextUtils.getTrimmedLength(s) > 0) {
animateHide(btnExtra);
animateShow(btnSubmit);
} else {
......@@ -104,6 +110,20 @@ public class MessageComposer extends LinearLayout {
addView(composer);
}
public void addExtraActionItem(MessageExtraActionItemPresenter itemPresenter) {
boolean found = false;
for (MessageExtraActionItemPresenter item : extraActionItems) {
if (item.getItemId() == itemPresenter.getItemId()) {
found = true;
break;
}
}
if (!found) {
extraActionItems.add(itemPresenter);
}
}
private TextView getEditor() {
return (TextView) composer.findViewById(R.id.editor);
}
......@@ -121,40 +141,6 @@ public class MessageComposer extends LinearLayout {
composer.findViewById(R.id.btn_submit).setEnabled(enabled);
}
protected final void focusToEditor() {
final TextView editor = getEditor();
editor.requestFocus();
InputMethodManager imm =
(InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editor, InputMethodManager.SHOW_IMPLICIT);
}
protected final void unFocusEditor() {
InputMethodManager imm =
(InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
public void show(@Nullable Runnable callback) {
focusToEditor();
setVisibility(View.VISIBLE);
if (callback != null) {
callback.run();
}
}
public void hide(@Nullable Runnable callback) {
unFocusEditor();
setVisibility(View.GONE);
if (callback != null) {
callback.run();
}
}
public boolean isShown() {
return getVisibility() == View.VISIBLE;
}
private void animateHide(final View view) {
view.animate().scaleX(0).scaleY(0).setDuration(150).withEndAction(new Runnable() {
@Override
......@@ -173,11 +159,23 @@ public class MessageComposer extends LinearLayout {
});
}
public interface ActionListener {
void onSubmit(String message);
private void showExtraActionSelectionDialog() {
ExtraActionPickerDialog.showAsTask(getContext(), extraActionItems)
.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;
}
});
}
void onExtra();
public interface ActionListener {
void onSubmitText(String message);
void onCancel();
void onExtraActionSelected(int itemId);
}
}
<?xml version="1.0" encoding="utf-8"?>
<chat.rocket.android.widget.AutofitRecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/message_spec_list"
android:id="@+id/message_extra_action_listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="80dp"
tools:listitem="@layout/dialog_message_selection_item"
tools:listitem="@layout/message_extra_action_item"
android:clipToPadding="false">
</chat.rocket.android.widget.AutofitRecyclerView>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:padding="@dimen/margin_8">
android:padding="8dp">
<ImageView
android:id="@+id/message_spec_icon"
android:id="@+id/icon"
android:layout_width="64dp"
android:layout_height="64dp"
android:padding="@dimen/margin_16"
tools:src="@drawable/ic_insert_photo_white_24dp"
android:padding="16dp"
android:layout_marginBottom="4dp"
android:background="@drawable/circle_black" />
<TextView
android:id="@+id/message_spec_title"
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
tools:text="Attach file" />
android:gravity="center_horizontal"/>
</LinearLayout>
\ No newline at end of file
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