Commit d80e6b52 authored by Yusuke Iwaki's avatar Yusuke Iwaki

introduce RocketChatMessageLayout (just stub...)

parent 2aeaa74a
......@@ -7,6 +7,7 @@ import android.support.v7.widget.RecyclerView;
import chat.rocket.android.R;
import chat.rocket.android.helper.LogcatIfError;
import chat.rocket.android.layouthelper.chatroom.MessageListAdapter;
import chat.rocket.android.model.ServerConfig;
import chat.rocket.android.model.SyncState;
import chat.rocket.android.model.ddp.Message;
import chat.rocket.android.model.ddp.RoomSubscription;
......@@ -28,6 +29,7 @@ public class RoomFragment extends AbstractChatRoomFragment {
private RealmHelper realmHelper;
private String roomId;
private RealmObjectObserver<RoomSubscription> roomObserver;
private String hostname;
/**
* create fragment with roomId.
......@@ -48,8 +50,14 @@ public class RoomFragment extends AbstractChatRoomFragment {
super.onCreate(savedInstanceState);
Bundle args = getArguments();
realmHelper = RealmStore.get(args.getString("serverConfigId"));
String serverConfigId = args.getString("serverConfigId");
realmHelper = RealmStore.get(serverConfigId);
roomId = args.getString("roomId");
hostname = RealmStore.getDefault().executeTransactionForRead(realm ->
realm.where(ServerConfig.class)
.equalTo("serverConfigId", serverConfigId)
.isNotNull("hostname")
.findFirst()).getHostname();
roomObserver = realmHelper
.createObjectObserver(realm -> realm.where(RoomSubscription.class).equalTo("rid", roomId))
.setOnUpdateListener(this::onRenderRoom);
......@@ -68,7 +76,7 @@ public class RoomFragment extends AbstractChatRoomFragment {
realm -> realm.where(Message.class)
.equalTo("rid", roomId)
.findAllSorted("ts", Sort.DESCENDING),
MessageListAdapter::new);
context -> new MessageListAdapter(context, hostname));
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext(),
LinearLayoutManager.VERTICAL, true);
......
package chat.rocket.android.helper;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.TimeZone;
import timber.log.Timber;
public class DateTime {
private static final String TAG = DateTime.class.getName();
private static final SimpleDateFormat sSimpleTimeFormat = new SimpleDateFormat("HH:mm");
private static final SimpleDateFormat sSimpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");
private static final SimpleDateFormat sSimpleDayFormat = new SimpleDateFormat("MM/dd");
private static final SimpleDateFormat sSimpleDayTimeFormat = new SimpleDateFormat("MM/dd HH:mm");
private static final SimpleDateFormat sSimpleDateTimeFormat =
new SimpleDateFormat("yyyy/MM/dd HH:mm");
/**
* convert datetime ms to String.
*/
public static String fromEpocMs(long epocMs, Format format) {
Calendar cal = new GregorianCalendar();
cal.setTimeInMillis(epocMs);
switch (format) {
case DAY:
return sSimpleDayFormat.format(cal.getTime());
case DATE:
return sSimpleDateFormat.format(cal.getTime());
case TIME:
return sSimpleTimeFormat.format(cal.getTime());
case DATE_TIME:
return sSimpleDateTimeFormat.format(cal.getTime());
case DAY_TIME:
return sSimpleDayTimeFormat.format(cal.getTime());
case AUTO_DAY_TIME: {
final long curTimeMs = System.currentTimeMillis();
Calendar cal2 = Calendar.getInstance(TimeZone.getTimeZone("JST"));
cal2.setTimeInMillis(curTimeMs);
if (cal.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) && cal.get(Calendar.DAY_OF_YEAR) == cal2
.get(Calendar.DAY_OF_YEAR)) {
//same day.
return sSimpleDayTimeFormat.format(cal.getTime());
} else {
return sSimpleDayFormat.format(cal.getTime());
}
}
default:
throw new IllegalArgumentException();
}
}
/**
* parse datetime string to ms.
*/
public static long fromDateToEpocMs(String dateString) {
try {
Calendar cal = new GregorianCalendar();
cal.setTime(sSimpleDateFormat.parse(dateString));
return cal.getTimeInMillis();
} catch (ParseException exception) {
Timber.w(exception, "failed to parse date: %s", dateString);
}
return 0;
}
public enum Format {
DATE, DAY, TIME, DATE_TIME, DAY_TIME, AUTO_DAY_TIME
}
}
\ No newline at end of file
......@@ -2,6 +2,7 @@ package chat.rocket.android.layouthelper.chatroom;
import android.content.Context;
import android.view.View;
import chat.rocket.android.R;
import chat.rocket.android.model.ddp.Message;
import chat.rocket.android.realm_adapter.RealmModelListAdapter;
......@@ -10,8 +11,11 @@ import chat.rocket.android.realm_adapter.RealmModelListAdapter;
*/
public class MessageListAdapter extends RealmModelListAdapter<Message, MessageViewHolder> {
public MessageListAdapter(Context context) {
private final String hostname;
public MessageListAdapter(Context context, String hostname) {
super(context);
this.hostname = hostname;
}
@Override protected int getItemViewType(Message model) {
......@@ -19,10 +23,10 @@ public class MessageListAdapter extends RealmModelListAdapter<Message, MessageVi
}
@Override protected int getLayout(int viewType) {
return android.R.layout.simple_list_item_1;
return R.layout.list_item_message;
}
@Override protected MessageViewHolder onCreateRealmModelViewHolder(int viewType, View itemView) {
return new MessageViewHolder(itemView);
return new MessageViewHolder(itemView, hostname);
}
}
package chat.rocket.android.layouthelper.chatroom;
import android.graphics.Color;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import chat.rocket.android.R;
import chat.rocket.android.model.ddp.Message;
import chat.rocket.android.realm_adapter.RealmModelViewHolder;
import chat.rocket.android.renderer.MessageRenderer;
import chat.rocket.android.widget.message.RocketChatMessageLayout;
/**
*/
public class MessageViewHolder extends RealmModelViewHolder<Message> {
private TextView text;
public MessageViewHolder(View itemView) {
private final ImageView avatar;
private final TextView username;
private final TextView timestamp;
private final String hostname;
private final RocketChatMessageLayout body;
public MessageViewHolder(View itemView, String hostname) {
super(itemView);
text = (TextView) itemView.findViewById(android.R.id.text1);
avatar = (ImageView) itemView.findViewById(R.id.user_avatar);
username = (TextView) itemView.findViewById(R.id.username);
timestamp = (TextView) itemView.findViewById(R.id.timestamp);
body = (RocketChatMessageLayout) itemView.findViewById(R.id.message_body);
this.hostname = hostname;
}
public void bind(Message message) {
text.setText(message.getMsg());
text.setTextColor(Color.BLACK);
new MessageRenderer(itemView.getContext(), message)
.avatarInto(avatar, hostname)
.usernameInto(username)
.timestampInto(timestamp)
.bodyInto(body);
}
}
package chat.rocket.android.renderer;
import android.content.Context;
import android.widget.ImageView;
import android.widget.TextView;
import chat.rocket.android.helper.DateTime;
import chat.rocket.android.model.ddp.Message;
import chat.rocket.android.widget.message.RocketChatMessageLayout;
/**
* Renderer for Message model.
*/
public class MessageRenderer extends AbstractRenderer<Message> {
private UserRenderer userRenderer;
public MessageRenderer(Context context, Message message) {
super(context, message);
userRenderer = new UserRenderer(context, message.getU());
}
/**
* show Avatar image.
*/
public MessageRenderer avatarInto(ImageView imageView, String hostname) {
userRenderer.avatarInto(imageView, hostname);
return this;
}
/**
* show Username in textView.
*/
public MessageRenderer usernameInto(TextView textView) {
userRenderer.usernameInto(textView);
return this;
}
/**
* show timestamp in textView.
*/
public MessageRenderer timestampInto(TextView textView) {
if (!shouldHandle(textView)) {
return this;
}
textView.setText(DateTime.fromEpocMs(object.getTs(), DateTime.Format.TIME));
return this;
}
/**
* show body in RocketChatMessageLayout.
*/
public MessageRenderer bodyInto(RocketChatMessageLayout rocketChatMessageLayout) {
if (!shouldHandle(rocketChatMessageLayout)) {
return this;
}
rocketChatMessageLayout.setText(object.getMsg());
return this;
}
}
<?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="match_parent"
android:layout_height="wrap_content"
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_weight="1"
android:orientation="vertical"
android:layout_marginEnd="8dp">
<LinearLayout
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>
<chat.rocket.android.widget.message.RocketChatMessageLayout
android:id="@+id/message_body"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
\ 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:id="@+id/newday_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
android:layout_margin="16dp"
>
<View
android:layout_width="0px"
android:layout_height="1dp"
android:layout_weight="1"
android:background="@color/newday_color"/>
<TextView
android:id="@+id/list_item_message_newday_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/newday_color"
android:textSize="8sp"
android:textStyle="bold"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
tools:text="2016/01/23"/>
<View
android:layout_width="0px"
android:layout_height="1dp"
android:layout_weight="1"
android:background="@color/newday_color"/>
</LinearLayout>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="newday_color">#FF7F7F7F</color>
</resources>
\ No newline at end of file
......@@ -33,13 +33,16 @@ public class RealmModelListView extends RecyclerView {
@Override public void onViewAttachedToWindow(View view) {
realm = realmHelper.instance();
RealmResults<T> results = query.queryItems(realm);
if (getAdapter() instanceof RealmModelListAdapter) {
((RealmModelListAdapter<T, VH>) getAdapter()).updateData(results);
} else {
RealmModelListAdapter<T, VH> adapter = constructor.getNewInstance(view.getContext());
adapter.updateData(results);
setAdapter(adapter);
}
}
@Override public void onViewDetachedFromWindow(View view) {
setAdapter(null);
if (realm != null && !realm.isClosed()) {
realm.close();
}
......
......@@ -32,4 +32,5 @@ dependencies {
compile rootProject.ext.supportAnnotations
compile rootProject.ext.supportAppCompat
compile rootProject.ext.supportDesign
compile 'org.nibor.autolink:autolink:0.5.0'
}
package chat.rocket.android.widget.helper;
import android.text.Layout;
import android.text.Selection;
import android.text.Spannable;
import android.text.method.LinkMovementMethod;
import android.text.method.MovementMethod;
import android.text.method.Touch;
import android.text.style.ClickableSpan;
import android.view.MotionEvent;
import android.widget.TextView;
public class LinkMovementMethodCompat extends LinkMovementMethod {
private static LinkMovementMethodCompat sInstance;
public static MovementMethod getInstance() {
if (sInstance == null) {
sInstance = new LinkMovementMethodCompat();
}
return sInstance;
}
@Override public boolean canSelectArbitrarily() {
return true;
}
// http://stackoverflow.com/a/30572151/2104686
@Override public boolean onTouchEvent(TextView widget, Spannable buffer, MotionEvent event) {
int action = event.getAction();
if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_DOWN) {
int eventX = (int) event.getX();
int eventY = (int) event.getY();
eventX -= widget.getTotalPaddingLeft();
eventY -= widget.getTotalPaddingTop();
eventX += widget.getScrollX();
eventY += widget.getScrollY();
Layout layout = widget.getLayout();
int line = layout.getLineForVertical(eventY);
int off = layout.getOffsetForHorizontal(line, eventX);
ClickableSpan[] link = buffer.getSpans(off, off, ClickableSpan.class);
if (link.length != 0) {
if (action == MotionEvent.ACTION_UP) {
link[0].onClick(widget);
} else {
Selection.setSelection(buffer, buffer.getSpanStart(link[0]), buffer.getSpanEnd(link[0]));
}
return true;
}
}
return Touch.onTouchEvent(widget, buffer, event);
}
}
package chat.rocket.android.widget.helper;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ClickableSpan;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import java.util.HashSet;
import org.nibor.autolink.LinkExtractor;
import org.nibor.autolink.LinkSpan;
import org.nibor.autolink.LinkType;
public class Linkify {
private static HashSet<LinkType> sTargetType = new HashSet<>();
static {
sTargetType.add(LinkType.URL);
sTargetType.add(LinkType.EMAIL);
}
public static void markup(TextView textview) {
textview.setMovementMethod(LinkMovementMethodCompat.getInstance());
final CharSequence text = textview.getText().toString();
textview.setText(markupInner(text));
}
private static SpannableString markupInner(final CharSequence text) {
LinkExtractor linkExtractor = LinkExtractor.builder().linkTypes(sTargetType).build();
SpannableString spannableString = new SpannableString(text);
for (LinkSpan link : linkExtractor.extractLinks(text)) {
final int idx1 = link.getBeginIndex();
final int idx2 = link.getEndIndex();
final String url = text.subSequence(idx1, idx2).toString();
spannableString.setSpan(createLinkSpan(url), idx1, idx2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
return spannableString;
}
private static ClickableSpan createLinkSpan(final String url) {
return new ClickableSpan() {
@Override public void onClick(View view) {
final Context context = view.getContext();
try {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
return;
} catch (Exception exception) {
}
try {
ClipboardManager clipboardManager =
(ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
clipboardManager.setPrimaryClip(ClipData.newPlainText("linkURL", url));
Toast.makeText(context, "Copied to clipboard", Toast.LENGTH_SHORT).show();
} catch (Exception exception) {
}
}
};
}
}
\ No newline at end of file
package chat.rocket.android.widget.message;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.support.v4.content.ContextCompat;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.BackgroundColorSpan;
import android.text.style.CharacterStyle;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.text.style.TypefaceSpan;
import android.widget.TextView;
import chat.rocket.android.widget.R;
public class InlineHightlighter {
//TODO: not completely implemented...
// original implementation is RocketChat:packages/rocketchat-markdown/markdown.coffee
// regex pattern with '/(^|&gt;|[ >_*~])\`([^`\r\n]+)\`([<_*~]|\B|\b|$)/gm'
public static void highlight(TextView textview) {
final CharSequence text = textview.getText();
textview.setText(highlightInner(textview.getContext(), text));
}
private static CharacterStyle[] createCharStyles(final Context context) {
return new CharacterStyle[] {
new ForegroundColorSpan(ContextCompat.getColor(context, R.color.highlight_text_color)),
new BackgroundColorSpan(
ContextCompat.getColor(context, R.color.highlight_text_background_color)),
new StyleSpan(Typeface.BOLD), new TypefaceSpan("monospace")
};
}
private static ForegroundColorSpan createTransparentSpan() {
return new ForegroundColorSpan(Color.TRANSPARENT);
}
private static CharSequence highlightInner(final Context context, final CharSequence text) {
final SpannableString s = new SpannableString(text);
final int length = text.length();
int highlightStart = length;
for (int i = 0; i < length; i++) {
char chr = text.charAt(i);
if (chr == '`') {
if (i > highlightStart) {
final int highlightEnd = i;
if (highlightStart + 1 < highlightEnd) {
s.setSpan(createTransparentSpan(), highlightStart, highlightStart + 1,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
for (CharacterStyle span : createCharStyles(context)) {
s.setSpan(span, highlightStart + 1, highlightEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
s.setSpan(createTransparentSpan(), highlightEnd, highlightEnd + 1,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
highlightStart = length;
} else {
highlightStart = i;
}
}
}
return s;
}
}
package chat.rocket.android.widget.message;
import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.TextView;
import chat.rocket.android.widget.R;
import chat.rocket.android.widget.helper.Linkify;
/**
*/
public class RocketChatMessageLayout extends LinearLayout {
private LayoutInflater inflater;
public RocketChatMessageLayout(Context context) {
super(context);
initialize(context, null);
}
public RocketChatMessageLayout(Context context, AttributeSet attrs) {
super(context, attrs);
initialize(context, attrs);
}
public RocketChatMessageLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initialize(context, attrs);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public RocketChatMessageLayout(Context context, AttributeSet attrs, int defStyleAttr,
int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
initialize(context, attrs);
}
private void initialize(Context context, AttributeSet attrs) {
inflater = LayoutInflater.from(context);
setOrientation(VERTICAL);
}
public void setText(String messageBody) {
removeAllViews();
appendTextView(messageBody);
}
private void appendTextView(String text) {
TextView textView = (TextView) inflater.inflate(R.layout.message_body, this, false);
textView.setText(text);
Linkify.markup(textView);
InlineHightlighter.highlight(textView);
addView(textView);
}
}
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="@style/TextAppearance.RocketChat.MessageBody"
/>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="TextAppearance.RocketChat" parent="TextAppearance.AppCompat"/>
<style name="TextAppearance.RocketChat.MessageBody" parent="TextAppearance.AppCompat.Body2">
</style>
<color name="highlight_text_color">#333</color>
<color name="highlight_text_background_color">#f8f8f8</color>
<color name="highlight_text_border_color">#ccc</color>
</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