Commit 0b3f313e authored by Yusuke Iwaki's avatar Yusuke Iwaki

refactor

parent baa0ddaf
...@@ -22,7 +22,6 @@ import java.util.UUID; ...@@ -22,7 +22,6 @@ 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;
import chat.rocket.android.fragment.chatroom.dialog.FileUploadProgressDialogFragment; 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.fragment.chatroom.dialog.UsersOfRoomDialogFragment;
import chat.rocket.android.helper.FileUploadHelper; import chat.rocket.android.helper.FileUploadHelper;
import chat.rocket.android.helper.LoadMoreScrollListener; import chat.rocket.android.helper.LoadMoreScrollListener;
...@@ -32,12 +31,12 @@ import chat.rocket.android.helper.TextUtils; ...@@ -32,12 +31,12 @@ import chat.rocket.android.helper.TextUtils;
import chat.rocket.android.layouthelper.chatroom.MessageFormManager; 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.PairedMessage; 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.log.RCLog;
import chat.rocket.android.message.AbstractMessageSpec; import chat.rocket.android.layouthelper.extra_action.upload.AudioUploadActionItem;
import chat.rocket.android.message.AudioUploadMessageSpec; import chat.rocket.android.layouthelper.extra_action.upload.AbstractUploadActionItem;
import chat.rocket.android.message.AbstractUploadMessageSpec; import chat.rocket.android.layouthelper.extra_action.upload.ImageUploadActionItem;
import chat.rocket.android.message.ImageUploadMessageSpec; import chat.rocket.android.layouthelper.extra_action.upload.VideoUploadActionItem;
import chat.rocket.android.message.VideoUploadMessageSpec;
import chat.rocket.android.model.ServerConfig; import chat.rocket.android.model.ServerConfig;
import chat.rocket.android.model.SyncState; import chat.rocket.android.model.SyncState;
import chat.rocket.android.model.ddp.Message; import chat.rocket.android.model.ddp.Message;
...@@ -70,11 +69,7 @@ public class RoomFragment extends AbstractChatRoomFragment ...@@ -70,11 +69,7 @@ public class RoomFragment extends AbstractChatRoomFragment
private String token; private String token;
private LoadMoreScrollListener scrollListener; private LoadMoreScrollListener scrollListener;
private RealmObjectObserver<LoadMessageProcedure> procedureObserver; private RealmObjectObserver<LoadMessageProcedure> procedureObserver;
private MessageFormManager messageComposerManager; private MessageFormManager messageFormManager;
private MessageSelectionDialogFragment.ClickListener messageSelectionClickListener =
messageSpec -> RoomFragmentPermissionsDispatcher
.onMessageSpecSelectedWithCheck(RoomFragment.this, messageSpec);
public RoomFragment() { public RoomFragment() {
} }
...@@ -226,8 +221,8 @@ public class RoomFragment extends AbstractChatRoomFragment ...@@ -226,8 +221,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);
messageComposerManager = new MessageFormManager(messageFormLayout); messageFormManager = new MessageFormManager(messageFormLayout);
messageComposerManager.setSendMessageCallback(messageText -> messageFormManager.setSendMessageCallback(messageText ->
realmHelper.executeTransaction(realm -> realmHelper.executeTransaction(realm ->
realm.createOrUpdateObjectFromJson(Message.class, new JSONObject() realm.createOrUpdateObjectFromJson(Message.class, new JSONObject()
.put("_id", UUID.randomUUID().toString()) .put("_id", UUID.randomUUID().toString())
...@@ -235,25 +230,17 @@ public class RoomFragment extends AbstractChatRoomFragment ...@@ -235,25 +230,17 @@ public class RoomFragment extends AbstractChatRoomFragment
.put("ts", System.currentTimeMillis()) .put("ts", System.currentTimeMillis())
.put("rid", roomId) .put("rid", roomId)
.put("msg", messageText)))); .put("msg", messageText))));
messageComposerManager.setExtrasPickerListener(() -> { messageFormManager.registerExtraActionItem(new ImageUploadActionItem());
MessageSelectionDialogFragment fragment = MessageSelectionDialogFragment.create(); messageFormManager.registerExtraActionItem(new AudioUploadActionItem());
messageFormManager.registerExtraActionItem(new VideoUploadActionItem());
fragment.addMessageSpec(new ImageUploadMessageSpec()); messageFormManager.setExtraActionPickerCallback(item ->
fragment.addMessageSpec(new AudioUploadMessageSpec()); RoomFragmentPermissionsDispatcher.onExtraActionSelectedWithCheck(RoomFragment.this, item));
fragment.addMessageSpec(new VideoUploadMessageSpec());
fragment.setListener(messageSelectionClickListener);
fragment.show(getFragmentManager(), MessageSelectionDialogFragment.TAG);
closeSideMenuIfNeeded();
});
} }
@Override @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) { public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, 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; return;
} }
...@@ -381,7 +368,7 @@ public class RoomFragment extends AbstractChatRoomFragment ...@@ -381,7 +368,7 @@ public class RoomFragment extends AbstractChatRoomFragment
} }
@NeedsPermission(Manifest.permission.READ_EXTERNAL_STORAGE) @NeedsPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
protected void onMessageSpecSelected(AbstractMessageSpec messageSpec) { protected void onExtraActionSelected(MessageExtraActionBehavior action) {
messageSpec.onSelect(RoomFragment.this); 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; 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;
/** /**
...@@ -9,27 +11,30 @@ import chat.rocket.android.widget.message.MessageFormLayout; ...@@ -9,27 +11,30 @@ 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 ExtrasPickerListener extrasPickerListener; private ExtraActionPickerCallback extraActionPickerCallback;
private final HashMap<Integer, AbstractExtraActionItem> extraActionItemMap;
public MessageFormManager(MessageFormLayout messageFormLayout) { public MessageFormManager(MessageFormLayout messageFormLayout) {
this.messageFormLayout = messageFormLayout; this.messageFormLayout = messageFormLayout;
this.extraActionItemMap = new HashMap<>();
init(); init();
} }
private void init() { private void init() {
messageFormLayout.setOnActionListener(new MessageFormLayout.ActionListener() { messageFormLayout.setOnActionListener(new MessageFormLayout.ActionListener() {
@Override @Override
public void onSubmit(String message) { public void onSubmitText(String message) {
sendMessage(message); sendMessage(message);
} }
@Override @Override
public void onExtra() { public void onExtraActionSelected(int itemId) {
openExtras(); if (extraActionItemMap.containsKey(itemId)) {
} AbstractExtraActionItem item = extraActionItemMap.get(itemId);
if (extraActionPickerCallback != null) {
@Override extraActionPickerCallback.onExtraActionSelected(item);
public void onCancel() { }
}
} }
}); });
} }
...@@ -38,8 +43,8 @@ public class MessageFormManager { ...@@ -38,8 +43,8 @@ public class MessageFormManager {
this.sendMessageCallback = sendMessageCallback; this.sendMessageCallback = sendMessageCallback;
} }
public void setExtrasPickerListener(ExtrasPickerListener listener) { public void setExtraActionPickerCallback(ExtraActionPickerCallback extraActionPickerCallback) {
extrasPickerListener = listener; this.extraActionPickerCallback = extraActionPickerCallback;
} }
public void clearComposingText() { public void clearComposingText() {
...@@ -52,7 +57,7 @@ public class MessageFormManager { ...@@ -52,7 +57,7 @@ public class MessageFormManager {
} }
messageFormLayout.setEnabled(false); messageFormLayout.setEnabled(false);
sendMessageCallback.onSubmit(message).onSuccess(task -> { sendMessageCallback.onSubmitText(message).onSuccess(task -> {
clearComposingText(); clearComposingText();
return null; return null;
}).continueWith(task -> { }).continueWith(task -> {
...@@ -61,19 +66,16 @@ public class MessageFormManager { ...@@ -61,19 +66,16 @@ public class MessageFormManager {
}); });
} }
private void openExtras() { public void registerExtraActionItem(AbstractExtraActionItem actionItem) {
if (extrasPickerListener == null) { messageFormLayout.addExtraActionItem(actionItem);
return; extraActionItemMap.put(actionItem.getItemId(), actionItem);
}
extrasPickerListener.onOpen();
} }
public interface SendMessageCallback { public interface SendMessageCallback {
Task<Void> onSubmit(String messageText); Task<Void> onSubmitText(String messageText);
} }
public interface ExtrasPickerListener { public interface ExtraActionPickerCallback {
void onOpen(); 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 android.content.Intent;
import chat.rocket.android.R; import chat.rocket.android.R;
public class AudioUploadMessageSpec extends AbstractUploadMessageSpec { public class AudioUploadActionItem extends AbstractUploadActionItem {
@Override @Override
protected Intent getIntent() { public int getItemId() {
return 11;
}
@Override
protected Intent getIntentForPickFile() {
Intent intent = new Intent(); Intent intent = new Intent();
intent.setType("audio/*"); intent.setType("audio/*");
intent.setAction(Intent.ACTION_GET_CONTENT); intent.setAction(Intent.ACTION_GET_CONTENT);
...@@ -15,24 +20,12 @@ public class AudioUploadMessageSpec extends AbstractUploadMessageSpec { ...@@ -15,24 +20,12 @@ public class AudioUploadMessageSpec extends AbstractUploadMessageSpec {
} }
@Override @Override
public ViewData getSpecificViewData() { public int getIcon() {
return new AudioUploadViewData(); return R.drawable.ic_audiotrack_white_24dp;
} }
private static class AudioUploadViewData implements AbstractMessageSpec.ViewData { @Override
@Override public int getTitle() {
public int getBackgroundTint() { return R.string.audio_upload_message_spec_title;
return R.color.colorAccent;
}
@Override
public int getIcon() {
return R.drawable.ic_audiotrack_white_24dp;
}
@Override
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 android.content.Intent;
import chat.rocket.android.R; import chat.rocket.android.R;
public class ImageUploadMessageSpec extends AbstractUploadMessageSpec { public class ImageUploadActionItem extends AbstractUploadActionItem {
@Override @Override
public ViewData getSpecificViewData() { public int getItemId() {
return new ImageUploadViewData(); return 10;
} }
@Override @Override
protected Intent getIntent() { protected Intent getIntentForPickFile() {
Intent intent = new Intent(); Intent intent = new Intent();
intent.setType("image/*"); intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT); intent.setAction(Intent.ACTION_GET_CONTENT);
return Intent.createChooser(intent, "Select Picture to Upload"); return Intent.createChooser(intent, "Select Picture to Upload");
} }
private static class ImageUploadViewData implements AbstractMessageSpec.ViewData { @Override
@Override public int getIcon() {
public int getBackgroundTint() { return R.drawable.ic_insert_photo_white_24dp;
return R.color.colorAccent; }
}
@Override
public int getIcon() {
return R.drawable.ic_insert_photo_white_24dp;
}
@Override @Override
public int getTitle() { public int getTitle() {
return R.string.image_upload_message_spec_title; 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 android.content.Intent;
import chat.rocket.android.R; import chat.rocket.android.R;
public class VideoUploadMessageSpec extends AbstractUploadMessageSpec { public class VideoUploadActionItem extends AbstractUploadActionItem {
@Override @Override
public ViewData getSpecificViewData() { public int getItemId() {
return new VideoUploadViewData(); return 12;
} }
@Override @Override
protected Intent getIntent() { protected Intent getIntentForPickFile() {
Intent intent = new Intent(); Intent intent = new Intent();
intent.setType("video/*"); intent.setType("video/*");
intent.setAction(Intent.ACTION_GET_CONTENT); intent.setAction(Intent.ACTION_GET_CONTENT);
return Intent.createChooser(intent, "Select Video to Upload"); return Intent.createChooser(intent, "Select Video to Upload");
} }
private static class VideoUploadViewData implements AbstractMessageSpec.ViewData { @Override
@Override public int getIcon() {
public int getBackgroundTint() { return R.drawable.ic_video_call_white_24dp;
return R.color.colorAccent; }
}
@Override
public int getIcon() {
return R.drawable.ic_video_call_white_24dp;
}
@Override @Override
public int getTitle() { public int getTitle() {
return R.string.video_upload_message_spec_title; 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();
}
...@@ -38,4 +38,5 @@ dependencies { ...@@ -38,4 +38,5 @@ dependencies {
compile rootProject.ext.okhttp3 compile rootProject.ext.okhttp3
compile rootProject.ext.picasso compile rootProject.ext.picasso
compile rootProject.ext.picasso2Okhttp3Downloader 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.ColorRes;
import android.support.annotation.DrawableRes; import android.support.annotation.DrawableRes;
...@@ -10,40 +10,39 @@ import android.view.View; ...@@ -10,40 +10,39 @@ import android.view.View;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
import chat.rocket.android.R; import chat.rocket.android.widget.R;
import chat.rocket.android.message.AbstractMessageSpec; import chat.rocket.android.widget.message.MessageExtraActionItemPresenter;
public class MessageSelectionViewHolder extends RecyclerView.ViewHolder { public class MessageExtraActionItemViewHolder extends RecyclerView.ViewHolder {
private ImageView messageSpecIcon; private ImageView iconView;
private TextView messageSpecTitle; private TextView titleView;
public MessageSelectionViewHolder(View itemView) { public MessageExtraActionItemViewHolder(View itemView) {
super(itemView); super(itemView);
messageSpecIcon = (ImageView) itemView.findViewById(R.id.message_spec_icon); iconView = (ImageView) itemView.findViewById(R.id.icon);
messageSpecTitle = (TextView) itemView.findViewById(R.id.message_spec_title); titleView = (TextView) itemView.findViewById(R.id.title);
} }
public void onBind(AbstractMessageSpec abstractMessageSpec) { public void onBind(MessageExtraActionItemPresenter actionItem) {
itemView.setTag(abstractMessageSpec); itemView.setTag(actionItem.getItemId());
AbstractMessageSpec.ViewData viewData = abstractMessageSpec.getViewData(); setIconBackgroundColorTint(actionItem.getBackgroundTint());
setIconBackgroundColorTint(viewData.getBackgroundTint()); setIcon(actionItem.getIcon());
setIcon(viewData.getIcon()); setTitle(actionItem.getTitle());
setTitle(viewData.getTitle());
} }
public void setIconBackgroundColorTint(@ColorRes int color) { public void setIconBackgroundColorTint(@ColorRes int color) {
// Drawable background = DrawableCompat.wrap(messageSpecIcon.getBackground()); // Drawable background = DrawableCompat.wrap(messageSpecIcon.getBackground());
DrawableCompat.setTint(messageSpecIcon.getBackground(), DrawableCompat.setTint(iconView.getBackground(),
ContextCompat.getColor(messageSpecIcon.getContext(), color)); ContextCompat.getColor(iconView.getContext(), color));
} }
public void setIcon(@DrawableRes int icon) { public void setIcon(@DrawableRes int icon) {
messageSpecIcon.setImageResource(icon); iconView.setImageResource(icon);
} }
public void setTitle(@StringRes int title) { 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();
}
...@@ -15,7 +15,12 @@ import android.widget.EditText; ...@@ -15,7 +15,12 @@ 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 {
...@@ -24,6 +29,7 @@ public class MessageFormLayout extends LinearLayout { ...@@ -24,6 +29,7 @@ public class MessageFormLayout extends LinearLayout {
private View btnExtra; private View btnExtra;
private View btnSubmit; private View btnSubmit;
private List<MessageExtraActionItemPresenter> extraActionItems;
public MessageFormLayout(Context context) { public MessageFormLayout(Context context) {
super(context); super(context);
...@@ -51,6 +57,7 @@ public class MessageFormLayout extends LinearLayout { ...@@ -51,6 +57,7 @@ public class MessageFormLayout extends LinearLayout {
} }
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);
...@@ -58,10 +65,8 @@ public class MessageFormLayout extends LinearLayout { ...@@ -58,10 +65,8 @@ public class MessageFormLayout extends LinearLayout {
btnExtra.setOnClickListener(new OnClickListener() { btnExtra.setOnClickListener(new OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View view) {
if (actionListener != null) { showExtraActionSelectionDialog();
actionListener.onExtra();
}
} }
}); });
...@@ -72,7 +77,7 @@ public class MessageFormLayout extends LinearLayout { ...@@ -72,7 +77,7 @@ public class MessageFormLayout extends LinearLayout {
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 && actionListener != null) {
actionListener.onSubmit(messageText); actionListener.onSubmitText(messageText);
} }
} }
}); });
...@@ -105,6 +110,20 @@ public class MessageFormLayout extends LinearLayout { ...@@ -105,6 +110,20 @@ public class MessageFormLayout extends LinearLayout {
addView(composer); 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() { private TextView getEditor() {
return (TextView) composer.findViewById(R.id.editor); return (TextView) composer.findViewById(R.id.editor);
} }
...@@ -140,11 +159,23 @@ public class MessageFormLayout extends LinearLayout { ...@@ -140,11 +159,23 @@ public class MessageFormLayout extends LinearLayout {
}); });
} }
public interface ActionListener { private void showExtraActionSelectionDialog() {
void onSubmit(String message); 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"?> <?xml version="1.0" encoding="utf-8"?>
<chat.rocket.android.widget.AutofitRecyclerView xmlns:android="http://schemas.android.com/apk/res/android" <chat.rocket.android.widget.AutofitRecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" 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_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:columnWidth="80dp" android:columnWidth="80dp"
tools:listitem="@layout/dialog_message_selection_item" tools:listitem="@layout/message_extra_action_item"
android:clipToPadding="false"> android:clipToPadding="false">
</chat.rocket.android.widget.AutofitRecyclerView> </chat.rocket.android.widget.AutofitRecyclerView>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical" android:orientation="vertical"
android:layout_width="80dp" android:layout_width="80dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:padding="@dimen/margin_8"> android:padding="8dp">
<ImageView <ImageView
android:id="@+id/message_spec_icon" android:id="@+id/icon"
android:layout_width="64dp" android:layout_width="64dp"
android:layout_height="64dp" android:layout_height="64dp"
android:padding="@dimen/margin_16" android:padding="16dp"
tools:src="@drawable/ic_insert_photo_white_24dp"
android:layout_marginBottom="4dp" android:layout_marginBottom="4dp"
android:background="@drawable/circle_black" /> android:background="@drawable/circle_black" />
<TextView <TextView
android:id="@+id/message_spec_title" android:id="@+id/title"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:gravity="center_horizontal" android:gravity="center_horizontal"/>
tools:text="Attach file" />
</LinearLayout> </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