package net.squanchy.tweets.util;

import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.style.URLSpan;

import com.twitter.sdk.android.core.models.Tweet;
import com.twitter.sdk.android.core.models.UrlEntity;

import java.util.List;

public class TweetFormatter {

    public static Spannable format(Tweet tweet) {
        Spannable spannable = new SpannableString(tweet.text);
        addUrlSpan(spannable, tweet.entities.urls);
        return spannable;
    }

    private static void addUrlSpan(Spannable spannable, List<UrlEntity> urlEntities) {
        for (UrlEntity urlEntity : urlEntities) {
            spannable.setSpan(new URLSpan(urlEntity.url), urlEntity.getStart(), urlEntity.getEnd(), 0);
        }
    }
}

@@ -120,67 +120,139 @@
 ext.
-SpannableStringBuilder;%0Aimport android.text.style.URLSpan;%0A
+style.URLSpan;%0A%0Aimport com.twitter.sdk.android.core.models.HashtagEntity;%0Aimport com.twitter.sdk.android.core.models.MentionEntity;
 %0Aimp
@@ -408,16 +408,153 @@
 tter %7B%0A%0A
+    private static final String BASE_TWITTER_URL = %22https://twitter.com/%22;%0A    private static final String TWITTER_QUERY = %22search?q=%22;%0A%0A
     publ
@@ -658,16 +658,17 @@
 .text);%0A
+%0A
         
@@ -711,16 +711,141 @@
 .urls);%0A
+        addMentionSpan(spannable, tweet.entities.userMentions);%0A        addHashtagSpan(spannable, tweet.entities.hashtags);%0A%0A
         
@@ -1125,11 +1125,605 @@
  %7D%0A    %7D
+%0A%0A    private static void addMentionSpan(Spannable spannable, List%3CMentionEntity%3E mentionEntities) %7B%0A        for (MentionEntity entity : mentionEntities) %7B%0A            spannable.setSpan(new URLSpan(BASE_TWITTER_URL + entity.screenName), entity.getStart(), entity.getEnd(), 0);%0A        %7D%0A    %7D%0A%0A    private static void addHashtagSpan(Spannable spannable, List%3CHashtagEntity%3E hashtagEntities) %7B%0A        for (HashtagEntity entity : hashtagEntities) %7B%0A            spannable.setSpan(new URLSpan(BASE_TWITTER_URL + TWITTER_QUERY + entity.text), entity.getStart(), entity.getEnd(), 0);%0A        %7D%0A    %7D
 %0A%7D%0A

add mentions and hashtags spans