Commit b2a993e7 authored by Yusuke Iwaki's avatar Yusuke Iwaki

improve the logic to render username and message alias

parent 44953579
...@@ -14,6 +14,7 @@ import chat.rocket.android.realm_helper.RealmModelViewHolder; ...@@ -14,6 +14,7 @@ import chat.rocket.android.realm_helper.RealmModelViewHolder;
public abstract class AbstractMessageViewHolder extends RealmModelViewHolder<PairedMessage> { public abstract class AbstractMessageViewHolder extends RealmModelViewHolder<PairedMessage> {
protected final ImageView avatar; protected final ImageView avatar;
protected final TextView username; protected final TextView username;
protected final TextView subUsername;
protected final TextView timestamp; protected final TextView timestamp;
protected final View userAndTimeContainer; protected final View userAndTimeContainer;
protected final String hostname; protected final String hostname;
...@@ -29,6 +30,7 @@ public abstract class AbstractMessageViewHolder extends RealmModelViewHolder<Pai ...@@ -29,6 +30,7 @@ public abstract class AbstractMessageViewHolder extends RealmModelViewHolder<Pai
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);
subUsername = (TextView) itemView.findViewById(R.id.sub_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);
newDayContainer = itemView.findViewById(R.id.newday_container); newDayContainer = itemView.findViewById(R.id.newday_container);
......
...@@ -31,7 +31,7 @@ public class MessageNormalViewHolder extends AbstractMessageViewHolder { ...@@ -31,7 +31,7 @@ public class MessageNormalViewHolder extends AbstractMessageViewHolder {
protected void bindMessage(PairedMessage pairedMessage) { protected void bindMessage(PairedMessage pairedMessage) {
new MessageRenderer(itemView.getContext(), pairedMessage.target) new MessageRenderer(itemView.getContext(), pairedMessage.target)
.avatarInto(avatar, hostname) .avatarInto(avatar, hostname)
.usernameInto(username) .usernameInto(username, subUsername)
.timestampInto(timestamp) .timestampInto(timestamp)
.bodyInto(body) .bodyInto(body)
.urlsInto(urls) .urlsInto(urls)
......
...@@ -24,7 +24,7 @@ public class MessageSystemViewHolder extends AbstractMessageViewHolder { ...@@ -24,7 +24,7 @@ public class MessageSystemViewHolder extends AbstractMessageViewHolder {
protected void bindMessage(PairedMessage pairedMessage) { protected void bindMessage(PairedMessage pairedMessage) {
new MessageRenderer(itemView.getContext(), pairedMessage.target) new MessageRenderer(itemView.getContext(), pairedMessage.target)
.avatarInto(avatar, hostname) .avatarInto(avatar, hostname)
.usernameInto(username) .usernameInto(username, subUsername)
.timestampInto(timestamp); .timestampInto(timestamp);
if (pairedMessage.target != null) { if (pairedMessage.target != null) {
......
package chat.rocket.android.renderer; package chat.rocket.android.renderer;
import android.content.Context; import android.content.Context;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.View; import android.view.View;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
import com.squareup.picasso.Picasso; import com.squareup.picasso.Picasso;
import chat.rocket.android.R; import chat.rocket.android.R;
...@@ -53,12 +48,14 @@ public class MessageRenderer extends AbstractRenderer<Message> { ...@@ -53,12 +48,14 @@ public class MessageRenderer extends AbstractRenderer<Message> {
/** /**
* show Username in textView. * show Username in textView.
*/ */
public MessageRenderer usernameInto(TextView textView) { public MessageRenderer usernameInto(TextView usernameTextView, TextView subUsernameTextView) {
if (TextUtils.isEmpty(object.getAlias())) { if (TextUtils.isEmpty(object.getAlias())) {
userRenderer.usernameInto(textView); userRenderer.usernameInto(usernameTextView);
if (subUsernameTextView != null) {
subUsernameTextView.setVisibility(View.GONE);
}
} else { } else {
final User user = object.getUser(); aliasAndUsernameInto(usernameTextView, subUsernameTextView);
setAliasInto(object.getAlias(), user == null ? null : user.getUsername(), textView);
} }
return this; return this;
} }
...@@ -146,21 +143,19 @@ public class MessageRenderer extends AbstractRenderer<Message> { ...@@ -146,21 +143,19 @@ public class MessageRenderer extends AbstractRenderer<Message> {
.into(imageView); .into(imageView);
} }
private void setAliasInto(String alias, String username, TextView textView) { private void aliasAndUsernameInto(TextView aliasTextView, TextView usernameTextView) {
final SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(); if (shouldHandle(aliasTextView)) {
final ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(Color.BLACK); aliasTextView.setText(object.getAlias());
spannableStringBuilder.append(alias);
if (username != null) {
spannableStringBuilder.append(" @");
spannableStringBuilder.append(username);
} }
spannableStringBuilder if (shouldHandle(usernameTextView)) {
.setSpan(foregroundColorSpan, 0, alias.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE); if (object.getUser() != null) {
usernameTextView.setText("@" + object.getUser().getUsername());
textView.setText(spannableStringBuilder); usernameTextView.setVisibility(View.VISIBLE);
} else {
usernameTextView.setVisibility(View.GONE);
}
}
} }
} }
package chat.rocket.android.renderer; package chat.rocket.android.renderer;
import android.content.Context; import android.content.Context;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
...@@ -44,14 +40,7 @@ public class UserRenderer extends AbstractRenderer<User> { ...@@ -44,14 +40,7 @@ public class UserRenderer extends AbstractRenderer<User> {
return this; return this;
} }
final SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(); textView.setText(object.getUsername());
final ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(Color.BLACK);
spannableStringBuilder.append(object.getUsername());
spannableStringBuilder.setSpan(foregroundColorSpan, 0, object.getUsername().length(),
Spannable.SPAN_INCLUSIVE_INCLUSIVE);
textView.setText(spannableStringBuilder);
return this; return this;
} }
......
...@@ -39,9 +39,20 @@ ...@@ -39,9 +39,20 @@
android:id="@+id/username" android:id="@+id/username"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:textStyle="bold" android:textAppearance="@style/TextAppearance.RocketChat.Message.Username"
tools:text="John Doe" /> tools:text="John Doe" />
<Space
android:layout_width="4dp"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/sub_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="@style/TextAppearance.RocketChat.Message.SubUsername"
tools:text="\@John Doe" />
<Space <Space
android:layout_width="@dimen/margin_8" android:layout_width="@dimen/margin_8"
android:layout_height="wrap_content" /> android:layout_height="wrap_content" />
......
...@@ -38,7 +38,7 @@ ...@@ -38,7 +38,7 @@
android:id="@+id/username" android:id="@+id/username"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:textStyle="bold" android:textAppearance="@style/TextAppearance.RocketChat.Message.Username"
tools:text="John Doe" /> tools:text="John Doe" />
<Space <Space
......
...@@ -2,4 +2,10 @@ ...@@ -2,4 +2,10 @@
<resources> <resources>
<color name="newday_color">#e0e0e0</color> <color name="newday_color">#e0e0e0</color>
<color name="newday_text_color">#444444</color> <color name="newday_text_color">#444444</color>
<style name="TextAppearance.RocketChat.Message.Username" parent="TextAppearance.AppCompat.Body2">
<item name="android:textColor">#bb000000</item>
</style>
<style name="TextAppearance.RocketChat.Message.SubUsername" parent="TextAppearance.AppCompat.Body1">
<item name="android:textColor">#5f000000</item>
</style>
</resources> </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