2017-03-26 12:05:34 +02:00
|
|
|
package org.mariotaku.twidere.model
|
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
import android.content.res.Resources
|
|
|
|
import android.graphics.Typeface
|
2020-01-26 08:35:15 +01:00
|
|
|
import androidx.core.content.ContextCompat
|
2017-03-26 12:05:34 +02:00
|
|
|
import android.text.SpannableString
|
|
|
|
import android.text.SpannableStringBuilder
|
|
|
|
import android.text.Spanned
|
|
|
|
import android.text.style.StyleSpan
|
|
|
|
import org.mariotaku.microblog.library.twitter.model.Activity
|
|
|
|
import org.mariotaku.twidere.R
|
2017-05-03 15:50:29 +02:00
|
|
|
import org.mariotaku.twidere.extension.model.activityStatus
|
2017-03-26 12:05:34 +02:00
|
|
|
import org.mariotaku.twidere.text.style.NonBreakEllipseSpan
|
|
|
|
import org.mariotaku.twidere.util.UserColorNameManager
|
|
|
|
import org.oshkimaadziig.george.androidutils.SpanFormatter
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by mariotaku on 16/1/1.
|
|
|
|
*/
|
|
|
|
class ActivityTitleSummaryMessage private constructor(val icon: Int, val color: Int, val title: CharSequence, val summary: CharSequence?) {
|
|
|
|
companion object {
|
|
|
|
|
|
|
|
fun get(context: Context, manager: UserColorNameManager, activity: ParcelableActivity,
|
2017-04-28 15:44:45 +02:00
|
|
|
sources: Array<ParcelableLiteUser>, defaultColor: Int, shouldUseStarsForLikes: Boolean,
|
2017-03-26 12:05:34 +02:00
|
|
|
nameFirst: Boolean): ActivityTitleSummaryMessage? {
|
|
|
|
val resources = context.resources
|
|
|
|
when (activity.action) {
|
|
|
|
Activity.Action.FOLLOW -> {
|
|
|
|
val typeIcon = R.drawable.ic_activity_action_follow
|
|
|
|
val color = ContextCompat.getColor(context, R.color.highlight_follow)
|
|
|
|
val title = getTitleStringAboutMe(resources, manager, R.string.activity_about_me_follow,
|
|
|
|
R.string.activity_about_me_follow_multi, sources, nameFirst)
|
|
|
|
return ActivityTitleSummaryMessage(typeIcon, color, title, null)
|
|
|
|
}
|
|
|
|
Activity.Action.FAVORITE -> {
|
|
|
|
val typeIcon: Int
|
|
|
|
val color: Int
|
|
|
|
val title: CharSequence
|
|
|
|
if (shouldUseStarsForLikes) {
|
|
|
|
typeIcon = R.drawable.ic_activity_action_favorite
|
|
|
|
color = ContextCompat.getColor(context, R.color.highlight_favorite)
|
|
|
|
title = getTitleStringAboutMe(resources, manager, R.string.activity_about_me_favorite,
|
|
|
|
R.string.activity_about_me_favorite_multi, sources, nameFirst)
|
|
|
|
} else {
|
|
|
|
typeIcon = R.drawable.ic_activity_action_like
|
|
|
|
color = ContextCompat.getColor(context, R.color.highlight_like)
|
|
|
|
|
|
|
|
title = getTitleStringAboutMe(resources, manager, R.string.activity_about_me_like,
|
|
|
|
R.string.activity_about_me_like_multi, sources, nameFirst)
|
|
|
|
}
|
2017-04-28 15:44:45 +02:00
|
|
|
val summary = generateTextOnlySummary(activity.summary_line)
|
2017-03-26 12:05:34 +02:00
|
|
|
return ActivityTitleSummaryMessage(typeIcon, color, title, summary)
|
|
|
|
}
|
|
|
|
Activity.Action.RETWEET -> {
|
|
|
|
val typeIcon = R.drawable.ic_activity_action_retweet
|
|
|
|
val color = ContextCompat.getColor(context, R.color.highlight_retweet)
|
|
|
|
val title = getTitleStringAboutMe(resources, manager, R.string.activity_about_me_retweet,
|
|
|
|
R.string.activity_about_me_retweet_multi, sources, nameFirst)
|
2017-04-28 15:44:45 +02:00
|
|
|
val summary = generateTextOnlySummary(activity.summary_line)
|
2017-03-26 12:05:34 +02:00
|
|
|
return ActivityTitleSummaryMessage(typeIcon, color, title, summary)
|
|
|
|
}
|
|
|
|
Activity.Action.FAVORITED_RETWEET -> {
|
|
|
|
val typeIcon: Int
|
|
|
|
val color: Int
|
|
|
|
val title: CharSequence
|
|
|
|
if (shouldUseStarsForLikes) {
|
|
|
|
typeIcon = R.drawable.ic_activity_action_favorite
|
|
|
|
color = ContextCompat.getColor(context, R.color.highlight_favorite)
|
|
|
|
title = getTitleStringAboutMe(resources, manager, R.string.activity_about_me_favorited_retweet,
|
|
|
|
R.string.activity_about_me_favorited_retweet_multi, sources, nameFirst)
|
|
|
|
} else {
|
|
|
|
typeIcon = R.drawable.ic_activity_action_like
|
|
|
|
color = ContextCompat.getColor(context, R.color.highlight_like)
|
|
|
|
title = getTitleStringAboutMe(resources, manager, R.string.activity_about_me_liked_retweet,
|
|
|
|
R.string.activity_about_me_liked_retweet_multi, sources, nameFirst)
|
|
|
|
}
|
2017-04-28 15:44:45 +02:00
|
|
|
val summary = generateStatusTextSummary(context, manager, activity.summary_line,
|
2017-03-26 12:05:34 +02:00
|
|
|
nameFirst)
|
|
|
|
return ActivityTitleSummaryMessage(typeIcon, color, title, summary)
|
|
|
|
}
|
|
|
|
Activity.Action.RETWEETED_RETWEET -> {
|
|
|
|
val typeIcon = R.drawable.ic_activity_action_retweet
|
|
|
|
val color = ContextCompat.getColor(context, R.color.highlight_retweet)
|
|
|
|
val title = getTitleStringAboutMe(resources, manager, R.string.activity_about_me_retweeted_retweet,
|
|
|
|
R.string.activity_about_me_retweeted_retweet_multi, sources, nameFirst)
|
2017-04-28 15:44:45 +02:00
|
|
|
val summary = generateStatusTextSummary(context, manager, activity.summary_line,
|
2017-03-26 12:05:34 +02:00
|
|
|
nameFirst)
|
|
|
|
return ActivityTitleSummaryMessage(typeIcon, color, title, summary)
|
|
|
|
}
|
|
|
|
Activity.Action.RETWEETED_MENTION -> {
|
|
|
|
val typeIcon = R.drawable.ic_activity_action_retweet
|
|
|
|
val color = ContextCompat.getColor(context, R.color.highlight_retweet)
|
|
|
|
val title = getTitleStringAboutMe(resources, manager, R.string.activity_about_me_retweeted_mention,
|
|
|
|
R.string.activity_about_me_retweeted_mention_multi, sources, nameFirst)
|
2017-04-28 15:44:45 +02:00
|
|
|
val summary = generateStatusTextSummary(context, manager, activity.summary_line,
|
2017-03-26 12:05:34 +02:00
|
|
|
nameFirst)
|
|
|
|
return ActivityTitleSummaryMessage(typeIcon, color, title, summary)
|
|
|
|
}
|
|
|
|
Activity.Action.FAVORITED_MENTION -> {
|
|
|
|
val typeIcon: Int
|
|
|
|
val color: Int
|
|
|
|
val title: CharSequence
|
|
|
|
if (shouldUseStarsForLikes) {
|
|
|
|
typeIcon = R.drawable.ic_activity_action_favorite
|
|
|
|
color = ContextCompat.getColor(context, R.color.highlight_favorite)
|
|
|
|
title = getTitleStringAboutMe(resources, manager, R.string.activity_about_me_favorited_mention,
|
|
|
|
R.string.activity_about_me_favorited_mention_multi, sources, nameFirst)
|
|
|
|
} else {
|
|
|
|
typeIcon = R.drawable.ic_activity_action_like
|
|
|
|
color = ContextCompat.getColor(context, R.color.highlight_like)
|
|
|
|
title = getTitleStringAboutMe(resources, manager, R.string.activity_about_me_liked_mention,
|
|
|
|
R.string.activity_about_me_liked_mention_multi, sources, nameFirst)
|
|
|
|
}
|
2017-04-28 15:44:45 +02:00
|
|
|
val summary = generateStatusTextSummary(context, manager, activity.summary_line,
|
2017-03-26 12:05:34 +02:00
|
|
|
nameFirst)
|
|
|
|
return ActivityTitleSummaryMessage(typeIcon, color, title, summary)
|
|
|
|
}
|
|
|
|
Activity.Action.LIST_MEMBER_ADDED -> {
|
|
|
|
val title: CharSequence
|
|
|
|
val icon = R.drawable.ic_activity_action_list_added
|
2020-06-08 23:19:10 +02:00
|
|
|
title = if (sources.size == 1 && activity.summary_line?.size == 1) {
|
2017-03-26 12:05:34 +02:00
|
|
|
val firstDisplayName = SpannableString(manager.getDisplayName(
|
2020-06-08 23:19:10 +02:00
|
|
|
sources[0], nameFirst))
|
2017-04-28 15:44:45 +02:00
|
|
|
val listName = SpannableString(activity.summary_line[0].content)
|
2017-03-26 12:05:34 +02:00
|
|
|
firstDisplayName.setSpan(StyleSpan(Typeface.BOLD), 0, firstDisplayName.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
|
|
|
|
listName.setSpan(StyleSpan(Typeface.BOLD), 0, listName.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
|
|
|
|
val format = context.getString(R.string.activity_about_me_list_member_added_with_name)
|
2020-06-08 23:19:10 +02:00
|
|
|
SpanFormatter.format(format, firstDisplayName, listName)
|
2017-03-26 12:05:34 +02:00
|
|
|
} else {
|
2020-06-08 23:19:10 +02:00
|
|
|
getTitleStringAboutMe(resources, manager, R.string.activity_about_me_list_member_added,
|
|
|
|
R.string.activity_about_me_list_member_added_multi, sources, nameFirst)
|
2017-03-26 12:05:34 +02:00
|
|
|
}
|
|
|
|
return ActivityTitleSummaryMessage(icon, defaultColor, title, null)
|
|
|
|
}
|
|
|
|
Activity.Action.MENTION, Activity.Action.REPLY, Activity.Action.QUOTE -> {
|
2017-04-28 15:44:45 +02:00
|
|
|
val status = activity.activityStatus ?: return null
|
2017-03-26 12:05:34 +02:00
|
|
|
val title = SpannableString(manager.getDisplayName(status,
|
|
|
|
nameFirst))
|
|
|
|
title.setSpan(StyleSpan(Typeface.BOLD), 0, title.length,
|
|
|
|
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
|
|
|
|
return ActivityTitleSummaryMessage(0, 0, title, status.text_unescaped)
|
|
|
|
}
|
|
|
|
Activity.Action.JOINED_TWITTER -> {
|
|
|
|
val typeIcon = R.drawable.ic_activity_action_follow
|
|
|
|
val color = ContextCompat.getColor(context, R.color.highlight_follow)
|
|
|
|
val title = getTitleStringAboutMe(resources, manager,
|
|
|
|
R.string.activity_joined_twitter, R.string.activity_joined_twitter_multi,
|
|
|
|
sources, nameFirst)
|
|
|
|
return ActivityTitleSummaryMessage(typeIcon, color, title, null)
|
|
|
|
}
|
|
|
|
Activity.Action.MEDIA_TAGGED -> {
|
|
|
|
val typeIcon = R.drawable.ic_activity_action_media_tagged
|
|
|
|
val color = ContextCompat.getColor(context, R.color.highlight_tagged)
|
|
|
|
val title = getTitleStringAboutMe(resources, manager, R.string.activity_about_me_media_tagged,
|
|
|
|
R.string.activity_about_me_media_tagged_multi, sources, nameFirst)
|
2017-04-28 15:44:45 +02:00
|
|
|
val summary = generateStatusTextSummary(context, manager, activity.summary_line,
|
2017-03-26 12:05:34 +02:00
|
|
|
nameFirst)
|
|
|
|
return ActivityTitleSummaryMessage(typeIcon, color, title, summary)
|
|
|
|
}
|
|
|
|
Activity.Action.FAVORITED_MEDIA_TAGGED -> {
|
|
|
|
val typeIcon: Int
|
|
|
|
val color: Int
|
|
|
|
val title: CharSequence
|
|
|
|
if (shouldUseStarsForLikes) {
|
|
|
|
typeIcon = R.drawable.ic_activity_action_favorite
|
|
|
|
color = ContextCompat.getColor(context, R.color.highlight_favorite)
|
|
|
|
title = getTitleStringAboutMe(resources, manager, R.string.activity_about_me_favorited_media_tagged,
|
|
|
|
R.string.activity_about_me_favorited_media_tagged_multi, sources, nameFirst)
|
|
|
|
} else {
|
|
|
|
typeIcon = R.drawable.ic_activity_action_like
|
|
|
|
color = ContextCompat.getColor(context, R.color.highlight_like)
|
|
|
|
title = getTitleStringAboutMe(resources, manager, R.string.activity_about_me_liked_media_tagged,
|
|
|
|
R.string.activity_about_me_liked_media_tagged_multi, sources, nameFirst)
|
|
|
|
}
|
2017-04-28 15:44:45 +02:00
|
|
|
val summary = generateStatusTextSummary(context, manager, activity.summary_line,
|
2017-03-26 12:05:34 +02:00
|
|
|
nameFirst)
|
|
|
|
return ActivityTitleSummaryMessage(typeIcon, color, title, summary)
|
|
|
|
}
|
|
|
|
Activity.Action.RETWEETED_MEDIA_TAGGED -> {
|
|
|
|
val typeIcon = R.drawable.ic_activity_action_retweet
|
|
|
|
val color = ContextCompat.getColor(context, R.color.highlight_retweet)
|
|
|
|
val title = getTitleStringAboutMe(resources, manager, R.string.activity_about_me_retweeted_media_tagged,
|
|
|
|
R.string.activity_about_me_retweeted_media_tagged_multi, sources, nameFirst)
|
2017-04-28 15:44:45 +02:00
|
|
|
val summary = generateStatusTextSummary(context, manager, activity.summary_line,
|
2017-03-26 12:05:34 +02:00
|
|
|
nameFirst)
|
|
|
|
return ActivityTitleSummaryMessage(typeIcon, color, title, summary)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun generateStatusTextSummary(context: Context, manager: UserColorNameManager,
|
2017-04-28 15:44:45 +02:00
|
|
|
statuses: Array<ParcelableActivity.SummaryLine>?, nameFirst: Boolean): Spanned? {
|
2017-03-26 12:05:34 +02:00
|
|
|
return statuses?.joinTo(SpannableStringBuilder(), separator = "\n") { status ->
|
2017-04-28 15:44:45 +02:00
|
|
|
val displayName = SpannableString(manager.getDisplayName(status.key,
|
|
|
|
status.name, status.screen_name, nameFirst)).also {
|
2017-03-26 12:05:34 +02:00
|
|
|
it.setSpan(StyleSpan(Typeface.BOLD), 0, it.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
|
|
|
|
}
|
|
|
|
|
|
|
|
val statusText = if (statuses.size > 1) {
|
2017-04-28 15:44:45 +02:00
|
|
|
SpannableString(status.content.replace('\n', ' ')).also {
|
2017-03-26 12:05:34 +02:00
|
|
|
it.setSpan(NonBreakEllipseSpan(), 0, it.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
|
|
|
|
}
|
|
|
|
} else {
|
2017-04-28 15:44:45 +02:00
|
|
|
status.content
|
2017-03-26 12:05:34 +02:00
|
|
|
}
|
|
|
|
return@joinTo SpanFormatter.format(context.getString(R.string.title_summary_line_format),
|
|
|
|
displayName, statusText)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-28 15:44:45 +02:00
|
|
|
private fun generateTextOnlySummary(lines: Array<ParcelableActivity.SummaryLine>?): CharSequence? {
|
|
|
|
return lines?.joinTo(SpannableStringBuilder(), separator = "\n") { status ->
|
|
|
|
if (lines.size > 1) {
|
|
|
|
return@joinTo SpannableString(status.content.replace('\n', ' ')).also {
|
2017-03-26 12:05:34 +02:00
|
|
|
it.setSpan(NonBreakEllipseSpan(), 0, it.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
|
|
|
|
}
|
|
|
|
} else {
|
2017-04-28 15:44:45 +02:00
|
|
|
return@joinTo status.content
|
2017-03-26 12:05:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun getTitleStringAboutMe(resources: Resources, manager: UserColorNameManager,
|
2017-04-28 15:44:45 +02:00
|
|
|
stringRes: Int, stringResMulti: Int, sources: Array<ParcelableLiteUser>,
|
2017-03-26 12:05:34 +02:00
|
|
|
nameFirst: Boolean): CharSequence {
|
|
|
|
val firstDisplayName = SpannableString(manager.getDisplayName(sources[0],
|
|
|
|
nameFirst))
|
|
|
|
firstDisplayName.setSpan(StyleSpan(Typeface.BOLD), 0, firstDisplayName.length,
|
|
|
|
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
|
2020-06-09 02:21:48 +02:00
|
|
|
when (sources.size) {
|
|
|
|
1 -> {
|
|
|
|
val format = resources.getString(stringRes)
|
|
|
|
return SpanFormatter.format(format, firstDisplayName)
|
|
|
|
}
|
|
|
|
2 -> {
|
|
|
|
val format = resources.getString(stringResMulti)
|
|
|
|
val secondDisplayName = SpannableString(manager.getDisplayName(sources[1],
|
2017-03-26 12:05:34 +02:00
|
|
|
nameFirst))
|
2020-06-09 02:21:48 +02:00
|
|
|
secondDisplayName.setSpan(StyleSpan(Typeface.BOLD), 0, secondDisplayName.length,
|
2017-03-26 12:05:34 +02:00
|
|
|
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
|
2020-06-09 02:21:48 +02:00
|
|
|
return SpanFormatter.format(format, firstDisplayName,
|
2017-03-26 12:05:34 +02:00
|
|
|
secondDisplayName)
|
2020-06-09 02:21:48 +02:00
|
|
|
}
|
|
|
|
else -> {
|
|
|
|
val othersCount = sources.size - 1
|
|
|
|
val nOthers = resources.getQuantityString(R.plurals.N_others, othersCount, othersCount)
|
|
|
|
val format = resources.getString(stringResMulti)
|
|
|
|
return SpanFormatter.format(format, firstDisplayName, nOthers)
|
|
|
|
}
|
2017-03-26 12:05:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|