Commit c77a90ae authored by Yusuke Iwaki's avatar Yusuke Iwaki

FIX #86 render system message.

parent 9b1ba0df
...@@ -10,41 +10,27 @@ import chat.rocket.android.helper.DateTime; ...@@ -10,41 +10,27 @@ import chat.rocket.android.helper.DateTime;
import chat.rocket.android.helper.TextUtils; import chat.rocket.android.helper.TextUtils;
import chat.rocket.android.model.SyncState; import chat.rocket.android.model.SyncState;
import chat.rocket.android.realm_helper.RealmModelViewHolder; import chat.rocket.android.realm_helper.RealmModelViewHolder;
import chat.rocket.android.renderer.MessageRenderer;
import chat.rocket.android.widget.message.RocketChatMessageAttachmentsLayout;
import chat.rocket.android.widget.message.RocketChatMessageLayout;
import chat.rocket.android.widget.message.RocketChatMessageUrlsLayout;
/** public abstract class AbstractMessageViewHolder extends RealmModelViewHolder<PairedMessage> {
* View holder of NORMAL chat message. protected final ImageView avatar;
*/ protected final TextView username;
public class MessageViewHolder extends RealmModelViewHolder<PairedMessage> { protected final TextView timestamp;
private final ImageView avatar; protected final View userAndTimeContainer;
private final TextView username; protected final String hostname;
private final TextView timestamp; protected final String userId;
private final View userAndTimeContainer; protected final String token;
private final String hostname; protected final View newDayContainer;
private final String userId; protected final TextView newDayText;
private final String token;
private final RocketChatMessageLayout body;
private final RocketChatMessageUrlsLayout urls;
private final RocketChatMessageAttachmentsLayout attachments;
private final View newDayContainer;
private final TextView newDayText;
/** /**
* constructor WITH hostname. * constructor WITH hostname.
*/ */
public MessageViewHolder(View itemView, String hostname, String userId, String token) { public AbstractMessageViewHolder(View itemView, String hostname, String userId, String token) {
super(itemView); super(itemView);
avatar = (ImageView) itemView.findViewById(R.id.user_avatar); avatar = (ImageView) itemView.findViewById(R.id.user_avatar);
username = (TextView) itemView.findViewById(R.id.username); username = (TextView) itemView.findViewById(R.id.username);
timestamp = (TextView) itemView.findViewById(R.id.timestamp); timestamp = (TextView) itemView.findViewById(R.id.timestamp);
userAndTimeContainer = itemView.findViewById(R.id.user_and_timestamp_container); userAndTimeContainer = itemView.findViewById(R.id.user_and_timestamp_container);
body = (RocketChatMessageLayout) itemView.findViewById(R.id.message_body);
urls = (RocketChatMessageUrlsLayout) itemView.findViewById(R.id.message_urls);
attachments =
(RocketChatMessageAttachmentsLayout) itemView.findViewById(R.id.message_attachments);
newDayContainer = itemView.findViewById(R.id.newday_container); newDayContainer = itemView.findViewById(R.id.newday_container);
newDayText = (TextView) itemView.findViewById(R.id.newday_text); newDayText = (TextView) itemView.findViewById(R.id.newday_text);
this.hostname = hostname; this.hostname = hostname;
...@@ -55,14 +41,8 @@ public class MessageViewHolder extends RealmModelViewHolder<PairedMessage> { ...@@ -55,14 +41,8 @@ public class MessageViewHolder extends RealmModelViewHolder<PairedMessage> {
/** /**
* bind the view model. * bind the view model.
*/ */
public void bind(PairedMessage pairedMessage) { public final void bind(PairedMessage pairedMessage) {
new MessageRenderer(itemView.getContext(), pairedMessage.target) bindMessage(pairedMessage);
.avatarInto(avatar, hostname)
.usernameInto(username)
.timestampInto(timestamp)
.bodyInto(body)
.urlsInto(urls)
.attachmentsInto(attachments, hostname, userId, token);
if (pairedMessage.target != null) { if (pairedMessage.target != null) {
int syncstate = pairedMessage.target.getSyncState(); int syncstate = pairedMessage.target.getSyncState();
...@@ -76,6 +56,8 @@ public class MessageViewHolder extends RealmModelViewHolder<PairedMessage> { ...@@ -76,6 +56,8 @@ public class MessageViewHolder extends RealmModelViewHolder<PairedMessage> {
renderNewDayAndSequential(pairedMessage); renderNewDayAndSequential(pairedMessage);
} }
protected abstract void bindMessage(PairedMessage pairedMessage);
private void renderNewDayAndSequential(PairedMessage pairedMessage) { private void renderNewDayAndSequential(PairedMessage pairedMessage) {
//see Rocket.Chat:packages/rocketchat-livechat/app/client/views/message.coffee //see Rocket.Chat:packages/rocketchat-livechat/app/client/views/message.coffee
if (!pairedMessage.hasSameDate()) { if (!pairedMessage.hasSameDate()) {
...@@ -92,16 +74,25 @@ public class MessageViewHolder extends RealmModelViewHolder<PairedMessage> { ...@@ -92,16 +74,25 @@ public class MessageViewHolder extends RealmModelViewHolder<PairedMessage> {
} }
private void setSequential(boolean sequential) { private void setSequential(boolean sequential) {
if (avatar != null) {
if (sequential) { if (sequential) {
avatar.setVisibility(View.INVISIBLE); avatar.setVisibility(View.INVISIBLE);
userAndTimeContainer.setVisibility(View.GONE);
} else { } else {
avatar.setVisibility(View.VISIBLE); avatar.setVisibility(View.VISIBLE);
}
}
if (userAndTimeContainer != null) {
if (sequential) {
userAndTimeContainer.setVisibility(View.GONE);
} else {
userAndTimeContainer.setVisibility(View.VISIBLE); userAndTimeContainer.setVisibility(View.VISIBLE);
} }
} }
}
private void setNewDay(@Nullable String text) { private void setNewDay(@Nullable String text) {
if (newDayContainer != null) {
if (TextUtils.isEmpty(text)) { if (TextUtils.isEmpty(text)) {
newDayContainer.setVisibility(View.GONE); newDayContainer.setVisibility(View.GONE);
} else { } else {
...@@ -109,4 +100,5 @@ public class MessageViewHolder extends RealmModelViewHolder<PairedMessage> { ...@@ -109,4 +100,5 @@ public class MessageViewHolder extends RealmModelViewHolder<PairedMessage> {
newDayContainer.setVisibility(View.VISIBLE); newDayContainer.setVisibility(View.VISIBLE);
} }
} }
}
} }
...@@ -7,6 +7,7 @@ import java.util.ArrayList; ...@@ -7,6 +7,7 @@ import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import chat.rocket.android.R; import chat.rocket.android.R;
import chat.rocket.android.helper.TextUtils;
import chat.rocket.android.layouthelper.ExtRealmModelListAdapter; import chat.rocket.android.layouthelper.ExtRealmModelListAdapter;
import chat.rocket.android.model.ddp.Message; import chat.rocket.android.model.ddp.Message;
...@@ -14,7 +15,12 @@ import chat.rocket.android.model.ddp.Message; ...@@ -14,7 +15,12 @@ import chat.rocket.android.model.ddp.Message;
* target list adapter for chat room. * target list adapter for chat room.
*/ */
public class MessageListAdapter public class MessageListAdapter
extends ExtRealmModelListAdapter<Message, PairedMessage, MessageViewHolder> { extends ExtRealmModelListAdapter<Message, PairedMessage, AbstractMessageViewHolder> {
private static final int VIEW_TYPE_UNKNOWN = 0;
private static final int VIEW_TYPE_NORMAL_MESSAGE = 1;
private static final int VIEW_TYPE_SYSTEM_MESSAGE = 2;
private final String hostname; private final String hostname;
private final String userId; private final String userId;
private final String token; private final String token;
...@@ -53,17 +59,43 @@ public class MessageListAdapter ...@@ -53,17 +59,43 @@ public class MessageListAdapter
@Override @Override
protected int getRealmModelViewType(PairedMessage model) { protected int getRealmModelViewType(PairedMessage model) {
return 0; if (model.target != null) {
if (TextUtils.isEmpty(model.target.getType())) {
return VIEW_TYPE_NORMAL_MESSAGE;
} else {
return VIEW_TYPE_SYSTEM_MESSAGE;
}
}
return VIEW_TYPE_UNKNOWN;
} }
@Override @Override
protected int getRealmModelLayout(int viewType) { protected int getRealmModelLayout(int viewType) {
return R.layout.list_item_message; switch (viewType) {
case VIEW_TYPE_NORMAL_MESSAGE:
return R.layout.list_item_normal_message;
case VIEW_TYPE_SYSTEM_MESSAGE:
return R.layout.list_item_system_message;
default:
return R.layout.simple_screen;
}
} }
@Override @Override
protected MessageViewHolder onCreateRealmModelViewHolder(int viewType, View itemView) { protected AbstractMessageViewHolder onCreateRealmModelViewHolder(int viewType, View itemView) {
return new MessageViewHolder(itemView, hostname, userId, token); switch (viewType) {
case VIEW_TYPE_NORMAL_MESSAGE:
return new MessageNormalViewHolder(itemView, hostname, userId, token);
case VIEW_TYPE_SYSTEM_MESSAGE:
return new MessageSystemViewHolder(itemView, hostname, userId, token);
default:
return new AbstractMessageViewHolder(itemView, hostname, userId, token) {
@Override
protected void bindMessage(PairedMessage pairedMessage) {
}
};
}
} }
@Override @Override
......
package chat.rocket.android.layouthelper.chatroom;
import android.view.View;
import chat.rocket.android.R;
import chat.rocket.android.renderer.MessageRenderer;
import chat.rocket.android.widget.message.RocketChatMessageAttachmentsLayout;
import chat.rocket.android.widget.message.RocketChatMessageLayout;
import chat.rocket.android.widget.message.RocketChatMessageUrlsLayout;
/**
* View holder of NORMAL chat message.
*/
public class MessageNormalViewHolder extends AbstractMessageViewHolder {
private final RocketChatMessageLayout body;
private final RocketChatMessageUrlsLayout urls;
private final RocketChatMessageAttachmentsLayout attachments;
/**
* constructor WITH hostname.
*/
public MessageNormalViewHolder(View itemView, String hostname, String userId, String token) {
super(itemView, hostname, userId, token);
body = (RocketChatMessageLayout) itemView.findViewById(R.id.message_body);
urls = (RocketChatMessageUrlsLayout) itemView.findViewById(R.id.message_urls);
attachments =
(RocketChatMessageAttachmentsLayout) itemView.findViewById(R.id.message_attachments);
}
@Override
protected void bindMessage(PairedMessage pairedMessage) {
new MessageRenderer(itemView.getContext(), pairedMessage.target)
.avatarInto(avatar, hostname)
.usernameInto(username)
.timestampInto(timestamp)
.bodyInto(body)
.urlsInto(urls)
.attachmentsInto(attachments, hostname, userId, token);
}
}
package chat.rocket.android.layouthelper.chatroom;
import android.view.View;
import android.widget.TextView;
import chat.rocket.android.R;
import chat.rocket.android.renderer.MessageRenderer;
/**
* View holder of NORMAL chat message.
*/
public class MessageSystemViewHolder extends AbstractMessageViewHolder {
private final TextView body;
/**
* constructor WITH hostname.
*/
public MessageSystemViewHolder(View itemView, String hostname, String userId, String token) {
super(itemView, hostname, userId, token);
body = (TextView) itemView.findViewById(R.id.message_body);
}
@Override
protected void bindMessage(PairedMessage pairedMessage) {
new MessageRenderer(itemView.getContext(), pairedMessage.target)
.avatarInto(avatar, hostname)
.usernameInto(username)
.timestampInto(timestamp);
if (pairedMessage.target != null) {
body.setText(MessageType.parse(pairedMessage.target.getType())
.getString(body.getContext(), pairedMessage.target));
}
}
}
package chat.rocket.android.layouthelper.chatroom;
import android.content.Context;
import chat.rocket.android.R;
import chat.rocket.android.model.ddp.Message;
/**
* message type.
*/
public enum MessageType {
ROOM_NAME_CHANGED("r") {
@Override
public String getString(Context context, Message message) {
return context.getString(R.string.message_room_name_changed,
message.getMessage(), message.getUser().getUsername());
}
},
USER_ADDED("au") {
@Override
public String getString(Context context, Message message) {
return context.getString(R.string.message_user_added_by,
message.getMessage(), message.getUser().getUsername());
}
},
USER_REMOVED("ru") {
@Override
public String getString(Context context, Message message) {
return context.getString(R.string.message_user_removed_by,
message.getMessage(), message.getUser().getUsername());
}
},
USER_JOINED("uj") {
@Override
public String getString(Context context, Message message) {
return context.getString(R.string.message_user_joined_channel);
}
},
USER_LEFT("ul") {
@Override
public String getString(Context context, Message message) {
return context.getString(R.string.message_user_left);
}
},
WELCOME("wm") {
@Override
public String getString(Context context, Message message) {
return context.getString(R.string.message_welcome, message.getUser().getUsername());
}
},
MESSAGE_REMOVED("rm") {
@Override
public String getString(Context context, Message message) {
return context.getString(R.string.message_removed);
}
},
UNSPECIFIED("");
//------------
private final String value;
MessageType(String value) {
this.value = value;
}
public String getValue() {
return value;
}
public static MessageType parse(String value) {
for (MessageType type : MessageType.values()) {
if (type.value.equals(value)) return type;
}
return UNSPECIFIED;
}
public String getString(Context context, Message message) {
return "";
}
}
<?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:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:theme="@style/AppTheme"
>
<include layout="@layout/list_item_message_newday"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<ImageView
android:id="@+id/user_avatar"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_margin="8dp"
tools:src="@drawable/ic_default_avatar"
/>
<LinearLayout
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_weight="1"
android:orientation="vertical"
>
<LinearLayout
android:id="@+id/user_and_timestamp_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:id="@+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
tools:text="John Doe"
/>
<Space
android:layout_width="@dimen/margin_8"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/timestamp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="false"
tools:text="12:34"
/>
</LinearLayout>
<TextView
android:id="@+id/message_body"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
android:textStyle="italic"
android:enabled="false"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="message_room_name_changed">Room name changed to: %1$s by %2$s</string>
<string name="message_user_added_by">User %1$s added by %2$s</string>
<string name="message_user_removed_by">User %1$s removed by %2$s</string>
<string name="message_user_left">Has left the channel.</string>
<string name="message_user_joined_channel">Has joined the channel.</string>
<string name="message_welcome">Welcome %s</string>
<string name="message_removed">Message removed</string>
</resources>
\ 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