mirror of
https://github.com/sudoxnym/habitica-android.git
synced 2026-05-19 12:18:59 +00:00
Improve chat display
This commit is contained in:
parent
6d3bb9c10b
commit
7003a4ff4f
5 changed files with 99 additions and 7 deletions
|
|
@ -99,7 +99,8 @@
|
|||
android:layout_height="wrap_content"
|
||||
android:layout_margin="8dp"
|
||||
android:lineSpacingMultiplier="1.0"
|
||||
tools:text="This is the chat message"/>
|
||||
tools:text="This is the chat message"
|
||||
android:textColor="@color/gray_10"/>
|
||||
<com.nex3z.flowlayout.FlowLayout
|
||||
android:id="@+id/buttons_wrapper"
|
||||
android:layout_width="match_parent"
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<color name="contributor.0">#aaa</color>
|
||||
<color name="contributor.0">#4e4a57</color>
|
||||
<color name="contributor.1">#c42871</color>
|
||||
<color name="contributor.2">#b01515</color>
|
||||
<color name="contributor.3">#d70e14</color>
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
package com.habitrpg.android.habitica.ui.helpers
|
||||
|
||||
import android.graphics.Color
|
||||
import android.text.Html
|
||||
|
||||
import com.commonsware.cwac.anddown.AndDown
|
||||
|
|
@ -7,11 +8,16 @@ import com.commonsware.cwac.anddown.AndDown
|
|||
import net.pherth.android.emoji_library.EmojiParser
|
||||
|
||||
import android.text.Html.FROM_HTML_MODE_LEGACY
|
||||
import android.text.SpannableStringBuilder
|
||||
import android.text.Spanned
|
||||
import android.text.style.ForegroundColorSpan
|
||||
import android.text.style.StyleSpan
|
||||
import com.habitrpg.android.habitica.helpers.RxErrorHandler
|
||||
import io.reactivex.Single
|
||||
import io.reactivex.android.schedulers.AndroidSchedulers
|
||||
import io.reactivex.functions.Consumer
|
||||
import io.reactivex.schedulers.Schedulers
|
||||
import java.util.regex.Pattern
|
||||
|
||||
/**
|
||||
* @author data5tream
|
||||
|
|
@ -20,6 +26,9 @@ object MarkdownParser {
|
|||
|
||||
private val processor = AndDown()
|
||||
|
||||
private val regex = Pattern.compile("@(?:\\w+)")
|
||||
private val colorSpan = UsernameSpan()
|
||||
|
||||
/**
|
||||
* Parses formatted markdown and returns it as styled CharSequence
|
||||
*
|
||||
|
|
@ -30,14 +39,19 @@ object MarkdownParser {
|
|||
if (input == null) {
|
||||
return ""
|
||||
}
|
||||
var output: CharSequence = if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
|
||||
Html.fromHtml(processor.markdownToHtml(EmojiParser.parseEmojis(input.trim { it <= ' ' })), FROM_HTML_MODE_LEGACY)
|
||||
val output: SpannableStringBuilder = if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
|
||||
Html.fromHtml(processor.markdownToHtml(EmojiParser.parseEmojis(input.trim { it <= ' ' })), FROM_HTML_MODE_LEGACY) as? SpannableStringBuilder
|
||||
} else {
|
||||
@Suppress("DEPRECATION")
|
||||
Html.fromHtml(processor.markdownToHtml(EmojiParser.parseEmojis(input.trim { it <= ' ' })))
|
||||
(Html.fromHtml(processor.markdownToHtml(EmojiParser.parseEmojis(input.trim { it <= ' ' }))) as? SpannableStringBuilder)
|
||||
} ?: SpannableStringBuilder()
|
||||
|
||||
val matcher = regex.matcher(output)
|
||||
while (matcher.find()) {
|
||||
output.setSpan(colorSpan, matcher.start(), matcher.end(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
|
||||
}
|
||||
if (output.length >= 2) output = output.subSequence(0, output.length - 2)
|
||||
return output
|
||||
|
||||
return if (output.length >= 2) output.subSequence(0, output.length - 2) else output
|
||||
}
|
||||
|
||||
fun parseMarkdownAsync(input: String?, onSuccess: Consumer<CharSequence>) {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,76 @@
|
|||
package com.habitrpg.android.habitica.ui.helpers;
|
||||
|
||||
import android.graphics.Color;
|
||||
import android.graphics.Paint;
|
||||
import android.graphics.Typeface;
|
||||
import android.os.Parcel;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.text.ParcelableSpan;
|
||||
import android.text.TextPaint;
|
||||
import android.text.style.MetricAffectingSpan;
|
||||
|
||||
public class UsernameSpan extends MetricAffectingSpan implements ParcelableSpan {
|
||||
|
||||
private final int color = Color.parseColor("#6133b4");
|
||||
|
||||
@Override
|
||||
public int describeContents() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeToParcel(@NonNull Parcel dest, int flags) {
|
||||
writeToParcelInternal(dest, flags);
|
||||
}
|
||||
|
||||
private void writeToParcelInternal(@NonNull Parcel dest, int flags) {
|
||||
dest.writeInt(color);
|
||||
}
|
||||
|
||||
public void updateDrawState(@NonNull TextPaint textPaint) {
|
||||
textPaint.setColor(color);
|
||||
apply(textPaint);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getSpanTypeId() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateMeasureState(@NonNull TextPaint textPaint) {
|
||||
apply(textPaint);
|
||||
}
|
||||
|
||||
private static void apply(Paint paint) {
|
||||
int oldStyle;
|
||||
|
||||
Typeface old = paint.getTypeface();
|
||||
if (old == null) {
|
||||
oldStyle = 0;
|
||||
} else {
|
||||
oldStyle = old.getStyle();
|
||||
}
|
||||
|
||||
int want = oldStyle | Typeface.BOLD;
|
||||
|
||||
Typeface tf;
|
||||
if (old == null) {
|
||||
tf = Typeface.defaultFromStyle(want);
|
||||
} else {
|
||||
tf = Typeface.create(old, want);
|
||||
}
|
||||
|
||||
int fake = want & ~tf.getStyle();
|
||||
|
||||
if ((fake & Typeface.BOLD) != 0) {
|
||||
paint.setFakeBoldText(true);
|
||||
}
|
||||
|
||||
if ((fake & Typeface.ITALIC) != 0) {
|
||||
paint.setTextSkewX(-0.25f);
|
||||
}
|
||||
|
||||
paint.setTypeface(tf);
|
||||
}
|
||||
}
|
||||
|
|
@ -43,6 +43,7 @@ class UsernameLabel(context: Context?, attrs: AttributeSet?) : LinearLayout(cont
|
|||
addView(textView, params)
|
||||
val padding = context?.resources?.getDimension(R.dimen.spacing_small)?.toInt() ?: 0
|
||||
textView.setPadding(0, 0, padding, 0)
|
||||
textView.
|
||||
addView(tierIconView, params)
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue