diff options
author | Stefan Niedermann <info@niedermann.it> | 2020-11-25 22:06:50 +0300 |
---|---|---|
committer | Stefan Niedermann <info@niedermann.it> | 2020-11-25 22:06:50 +0300 |
commit | 5a162167612fb98ec5bc8dd614e1ca7f0269610f (patch) | |
tree | aeff922af67003096b11256675e214caf6ea90ef /app/src/main | |
parent | 8110ebb0724c7e414223be651e9dcf6a95bcde4b (diff) |
Implement support for @mentions in RxMarkdownViewer and MarkwonMarkdownViewer
Signed-off-by: Stefan Niedermann <info@niedermann.it>
Diffstat (limited to 'app/src/main')
-rw-r--r-- | app/src/main/java/it/niedermann/nextcloud/deck/ui/card/comments/ItemCommentViewHolder.java | 11 | ||||
-rw-r--r-- | app/src/main/java/it/niedermann/nextcloud/deck/util/ViewUtil.java | 62 |
2 files changed, 8 insertions, 65 deletions
diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/ui/card/comments/ItemCommentViewHolder.java b/app/src/main/java/it/niedermann/nextcloud/deck/ui/card/comments/ItemCommentViewHolder.java index 56230d3c3..42431ae0c 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/ui/card/comments/ItemCommentViewHolder.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/ui/card/comments/ItemCommentViewHolder.java @@ -14,6 +14,8 @@ import androidx.recyclerview.widget.RecyclerView; import java.time.ZoneId; import java.time.format.DateTimeFormatter; import java.time.format.FormatStyle; +import java.util.HashMap; +import java.util.Map; import it.niedermann.android.util.ClipboardUtil; import it.niedermann.android.util.DimensionUtil; @@ -21,12 +23,11 @@ import it.niedermann.nextcloud.deck.R; import it.niedermann.nextcloud.deck.databinding.ItemCommentBinding; import it.niedermann.nextcloud.deck.model.Account; import it.niedermann.nextcloud.deck.model.enums.DBStatus; +import it.niedermann.nextcloud.deck.model.ocs.comment.Mention; import it.niedermann.nextcloud.deck.model.ocs.comment.full.FullDeckComment; import it.niedermann.nextcloud.deck.util.DateUtil; import it.niedermann.nextcloud.deck.util.ViewUtil; -import static it.niedermann.nextcloud.deck.util.ViewUtil.setupMentions; - public class ItemCommentViewHolder extends RecyclerView.ViewHolder { private final ItemCommentBinding binding; private final DateTimeFormatter dateFormatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM); @@ -76,7 +77,11 @@ public class ItemCommentViewHolder extends RecyclerView.ViewHolder { binding.notSyncedYet.setVisibility(DBStatus.LOCAL_EDITED.equals(comment.getStatusEnum()) ? View.VISIBLE : View.GONE); TooltipCompat.setTooltipText(binding.creationDateTime, comment.getComment().getCreationDateTime().atZone(ZoneId.systemDefault()).format(dateFormatter)); - setupMentions(account, binding.message.getContext(), comment.getComment().getMessage(), comment.getComment().getMentions(), this.binding.message); + final Map<String, String> mentions = new HashMap<>(comment.getComment().getMentions().size()); + for (Mention mention : comment.getComment().getMentions()) { + mentions.put(mention.getMentionId(), mention.getMentionDisplayName()); + } + binding.message.setMentions(mentions); if (comment.getParent() == null) { binding.parentContainer.setVisibility(View.GONE); diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/util/ViewUtil.java b/app/src/main/java/it/niedermann/nextcloud/deck/util/ViewUtil.java index 7319f7793..013a3d334 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/util/ViewUtil.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/util/ViewUtil.java @@ -2,12 +2,8 @@ package it.niedermann.nextcloud.deck.util; import android.content.Context; import android.content.res.ColorStateList; -import android.graphics.Bitmap; import android.graphics.drawable.Drawable; import android.net.Uri; -import android.text.Spannable; -import android.text.SpannableStringBuilder; -import android.text.style.ImageSpan; import android.widget.ImageView; import android.widget.TextView; @@ -15,7 +11,6 @@ import androidx.annotation.ColorInt; import androidx.annotation.ColorRes; import androidx.annotation.DrawableRes; import androidx.annotation.NonNull; -import androidx.annotation.Nullable; import androidx.annotation.Px; import androidx.core.content.ContextCompat; import androidx.core.graphics.drawable.DrawableCompat; @@ -23,17 +18,11 @@ import androidx.core.widget.TextViewCompat; import com.bumptech.glide.Glide; import com.bumptech.glide.request.RequestOptions; -import com.bumptech.glide.request.target.CustomTarget; -import com.bumptech.glide.request.transition.Transition; import java.time.LocalDate; -import java.util.List; -import it.niedermann.android.markdown.MarkdownEditor; import it.niedermann.android.util.DimensionUtil; import it.niedermann.nextcloud.deck.R; -import it.niedermann.nextcloud.deck.model.Account; -import it.niedermann.nextcloud.deck.model.ocs.comment.Mention; import static android.os.Build.VERSION.SDK_INT; import static android.os.Build.VERSION_CODES.LOLLIPOP; @@ -89,57 +78,6 @@ public final class ViewUtil { return drawable; } - /** - * Replaces all mentions in the textView with an avatar and the display name - * - * @param account {@link Account} where the users of those mentions belong to - * @param mentions {@link List} of all mentions that should be substituted - * @param target target {@link TextView} - */ - public static void setupMentions(@NonNull Account account, @NonNull Context context, String text, @NonNull List<Mention> mentions, MarkdownEditor target) { - SpannableStringBuilder messageBuilder = new SpannableStringBuilder(text); - - // Step 1 - // Add avatar icons and display names - for (Mention m : mentions) { - final String mentionId = "@" + m.getMentionId(); - final String mentionDisplayName = " " + m.getMentionDisplayName(); - int index = messageBuilder.toString().lastIndexOf(mentionId); - while (index >= 0) { - messageBuilder.setSpan(new ImageSpan(context, R.drawable.ic_person_grey600_24dp), index, index + mentionId.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); - messageBuilder.insert(index + mentionId.length(), mentionDisplayName); - index = messageBuilder.toString().substring(0, index).lastIndexOf(mentionId); - } - } - target.setMarkdownString(messageBuilder); - - // Step 2 - // Replace avatar icons with real avatars - final ImageSpan[] list = messageBuilder.getSpans(0, messageBuilder.length(), ImageSpan.class); - for (ImageSpan span : list) { - final int spanStart = messageBuilder.getSpanStart(span); - final int spanEnd = messageBuilder.getSpanEnd(span); - Glide.with(context) - .asBitmap() - .placeholder(R.drawable.ic_person_grey600_24dp) - .load(account.getUrl() + "/index.php/avatar/" + messageBuilder.subSequence(spanStart + 1, spanEnd).toString() + "/" + DimensionUtil.INSTANCE.dpToPx(context, R.dimen.icon_size_details)) - .apply(RequestOptions.circleCropTransform()) - .into(new CustomTarget<Bitmap>() { - @Override - public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) { - messageBuilder.removeSpan(span); - messageBuilder.setSpan(new ImageSpan(context, resource), spanStart, spanEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); - } - - @Override - public void onLoadCleared(@Nullable Drawable placeholder) { - // silence is gold - } - }); - } - target.setMarkdownString(messageBuilder); - } - public static void setImageColor(@NonNull Context context, @NonNull ImageView imageView, @ColorRes int colorRes) { if (SDK_INT >= LOLLIPOP) { imageView.setImageTintList(ColorStateList.valueOf(ContextCompat.getColor(context, colorRes))); |