Commit 1880ace4 authored by Tiago Cunha's avatar Tiago Cunha

Merge branch 'develop' into feature/unread-mode

parents 4cd1a173 f91a845d
...@@ -164,7 +164,7 @@ public class MainActivity extends AbstractAuthedActivity { ...@@ -164,7 +164,7 @@ public class MainActivity extends AbstractAuthedActivity {
protected void onRoomIdUpdated() { protected void onRoomIdUpdated() {
super.onRoomIdUpdated(); super.onRoomIdUpdated();
if (roomId != null) { if (roomId != null && RoomFragment.canCreate(RealmStore.get(serverConfigId))) {
showFragment(RoomFragment.create(serverConfigId, roomId)); showFragment(RoomFragment.create(serverConfigId, roomId));
closeSidebarIfNeeded(); closeSidebarIfNeeded();
} else { } else {
......
...@@ -67,6 +67,14 @@ public class RoomFragment extends AbstractChatRoomFragment ...@@ -67,6 +67,14 @@ public class RoomFragment extends AbstractChatRoomFragment
public RoomFragment() { public RoomFragment() {
} }
public static boolean canCreate(RealmHelper realmHelper) {
User currentUser = realmHelper.executeTransactionForRead(realm ->
User.queryCurrentUser(realm).findFirst());
Session session = realmHelper.executeTransactionForRead(realm ->
Session.queryDefaultSession(realm).findFirst());
return currentUser != null && session != null;
}
/** /**
* create fragment with roomId. * create fragment with roomId.
*/ */
......
...@@ -13,21 +13,21 @@ public enum MessageType { ...@@ -13,21 +13,21 @@ public enum MessageType {
@Override @Override
public String getString(Context context, Message message) { public String getString(Context context, Message message) {
return context.getString(R.string.message_room_name_changed, return context.getString(R.string.message_room_name_changed,
message.getMessage(), message.getUser().getUsername()); message.getMessage(), getUsername(message));
} }
}, },
USER_ADDED("au") { USER_ADDED("au") {
@Override @Override
public String getString(Context context, Message message) { public String getString(Context context, Message message) {
return context.getString(R.string.message_user_added_by, return context.getString(R.string.message_user_added_by,
message.getMessage(), message.getUser().getUsername()); message.getMessage(), getUsername(message));
} }
}, },
USER_REMOVED("ru") { USER_REMOVED("ru") {
@Override @Override
public String getString(Context context, Message message) { public String getString(Context context, Message message) {
return context.getString(R.string.message_user_removed_by, return context.getString(R.string.message_user_removed_by,
message.getMessage(), message.getUser().getUsername()); message.getMessage(), getUsername(message));
} }
}, },
USER_JOINED("uj") { USER_JOINED("uj") {
...@@ -45,7 +45,7 @@ public enum MessageType { ...@@ -45,7 +45,7 @@ public enum MessageType {
WELCOME("wm") { WELCOME("wm") {
@Override @Override
public String getString(Context context, Message message) { public String getString(Context context, Message message) {
return context.getString(R.string.message_welcome, message.getUser().getUsername()); return context.getString(R.string.message_welcome, getUsername(message));
} }
}, },
MESSAGE_REMOVED("rm") { MESSAGE_REMOVED("rm") {
...@@ -77,4 +77,12 @@ public enum MessageType { ...@@ -77,4 +77,12 @@ public enum MessageType {
public String getString(Context context, Message message) { public String getString(Context context, Message message) {
return ""; return "";
} }
private static String getUsername(Message message) {
if (message != null && message.getUser() != null) {
return message.getUser().getUsername();
} else {
return "";
}
}
} }
...@@ -28,7 +28,7 @@ public class Linkify { ...@@ -28,7 +28,7 @@ public class Linkify {
public static void markup(TextView textview) { public static void markup(TextView textview) {
textview.setMovementMethod(LinkMovementMethodCompat.getInstance()); textview.setMovementMethod(LinkMovementMethodCompat.getInstance());
final CharSequence text = textview.getText().toString(); final CharSequence text = textview.getText();
textview.setText(markupInner(text)); textview.setText(markupInner(text));
} }
......
package chat.rocket.android.widget.helper;
import android.graphics.Typeface;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.StrikethroughSpan;
import android.text.style.StyleSpan;
import android.widget.TextView;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* supports only bold, italic, strike.
*/
public class MarkDown {
/**
* transform MarkDown text into Spans.
*/
public static void apply(TextView textView) {
SpannableString text = new SpannableString(textView.getText());
bold(text);
italic(text);
strike(text);
textView.setText(text);
}
private static void bold(SpannableString inputText) {
Pattern boldPattern = Pattern.compile(
"(^|&gt;|[ >_~`])(\\*{1,2})[^\\*\\r\\n]+(\\*{1,2})([<_~`]|\\B|\\b|$)", Pattern.MULTILINE);
Matcher matcher = boldPattern.matcher(inputText);
while (matcher.find()) {
setSpan(new StyleSpan(Typeface.BOLD), inputText,
matcher.start() + matcher.group(1).length(),
matcher.end() - matcher.group(4).length(),
matcher.group(2).length(),
matcher.group(3).length());
}
}
private static void italic(SpannableString inputText) {
Pattern italicPattern = Pattern.compile(
"(^|&gt;|[ >*~`])(\\_)[^\\_\\r\\n]+(\\_)([<*~`]|\\B|\\b|$)", Pattern.MULTILINE);
Matcher matcher = italicPattern.matcher(inputText);
while (matcher.find()) {
setSpan(new StyleSpan(Typeface.ITALIC), inputText,
matcher.start() + matcher.group(1).length(),
matcher.end() - matcher.group(4).length(),
matcher.group(2).length(),
matcher.group(3).length());
}
}
private static void strike(SpannableString inputText) {
Pattern strikePattern = Pattern.compile(
"(^|&gt;|[ >_*`])(\\~{1,2})[^~\\r\\n]+(\\~{1,2})([<_*`]|\\B|\\b|$)", Pattern.MULTILINE);
Matcher matcher = strikePattern.matcher(inputText);
while (matcher.find()) {
setSpan(new StrikethroughSpan(), inputText,
matcher.start() + matcher.group(1).length(),
matcher.end() - matcher.group(4).length(),
matcher.group(2).length(),
matcher.group(3).length());
}
}
private static void setSpan(Object span, SpannableString inputText,
int start, int end, int markStartLen, int markEndLen) {
if (markStartLen > 0) {
inputText.setSpan(new AbsoluteSizeSpan(0),
start, start + markStartLen,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
inputText.setSpan(span,
start + markStartLen, end - markEndLen,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
if (markEndLen > 0) {
inputText.setSpan(new AbsoluteSizeSpan(0),
end - markEndLen, end,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
}
...@@ -13,6 +13,7 @@ import com.emojione.Emojione; ...@@ -13,6 +13,7 @@ import com.emojione.Emojione;
import chat.rocket.android.widget.R; import chat.rocket.android.widget.R;
import chat.rocket.android.widget.helper.InlineHightlighter; import chat.rocket.android.widget.helper.InlineHightlighter;
import chat.rocket.android.widget.helper.Linkify; import chat.rocket.android.widget.helper.Linkify;
import chat.rocket.android.widget.helper.MarkDown;
/** /**
*/ */
...@@ -77,6 +78,7 @@ public class RocketChatMessageLayout extends LinearLayout { ...@@ -77,6 +78,7 @@ public class RocketChatMessageLayout extends LinearLayout {
TextView textView = (TextView) inflater.inflate(R.layout.message_body, this, false); TextView textView = (TextView) inflater.inflate(R.layout.message_body, this, false);
textView.setText(Emojione.shortnameToUnicode(text, false)); textView.setText(Emojione.shortnameToUnicode(text, false));
MarkDown.apply(textView);
Linkify.markup(textView); Linkify.markup(textView);
InlineHightlighter.highlight(textView); InlineHightlighter.highlight(textView);
......
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