From 033d30bf3f6c87162579ad3b3d6ab750ee9a9145 Mon Sep 17 00:00:00 2001 From: Mariotaku Lee Date: Tue, 4 Apr 2017 14:15:37 +0800 Subject: [PATCH] improved compose text mention style --- .../ktextension/EditableExtensions.kt | 30 +++++++++++++++++++ .../twidere/activity/ComposeActivity.kt | 10 ++++++- .../text/twitter/ExtractorExtensions.kt | 5 ++-- .../text/twitter/ValidatorExtensions.kt | 2 +- .../twidere/task/twitter/UpdateStatusTask.kt | 2 +- 5 files changed, 44 insertions(+), 5 deletions(-) create mode 100644 twidere/src/main/kotlin/org/mariotaku/ktextension/EditableExtensions.kt diff --git a/twidere/src/main/kotlin/org/mariotaku/ktextension/EditableExtensions.kt b/twidere/src/main/kotlin/org/mariotaku/ktextension/EditableExtensions.kt new file mode 100644 index 000000000..b8445ce72 --- /dev/null +++ b/twidere/src/main/kotlin/org/mariotaku/ktextension/EditableExtensions.kt @@ -0,0 +1,30 @@ +/* + * Twidere - Twitter client for Android + * + * Copyright (C) 2012-2017 Mariotaku Lee + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.mariotaku.ktextension + +import android.text.Editable + +/** + * Created by mariotaku on 2017/4/4. + */ + +fun Editable.clearSpans(type: Class<*>) { + getSpans(0, length, type).forEach { removeSpan(it) } +} diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/activity/ComposeActivity.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/activity/ComposeActivity.kt index 47444783a..4731a044d 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/activity/ComposeActivity.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/activity/ComposeActivity.kt @@ -1509,16 +1509,20 @@ class ComposeActivity : BaseActivity(), OnMenuItemClickListener, OnClickListener private fun updateTextCount() { val am = AccountManager.get(this) - val text = editText.text?.toString() ?: return + val editable = editText.editableText ?: return val inReplyTo = inReplyToStatus val ignoreMentions = accountsAdapter.selectedAccountKeys.all { val account = AccountUtils.findByAccountKey(am, it) ?: return@all false return@all account.getAccountType(am) == AccountType.TWITTER } + val text = editable.toString() + val mentionColor = ThemeUtils.getColorFromAttribute(this, android.R.attr.textColorSecondary, 0) if (inReplyTo != null && ignoreMentions) { val textAndMentions = extractor.extractReplyTextAndMentions(text, inReplyTo) if (textAndMentions.replyToOriginalUser) { hintLabel.visibility = View.GONE + editable.setSpan(MentionColorSpan(mentionColor), 0, textAndMentions.replyStartIndex, + Spannable.SPAN_EXCLUSIVE_EXCLUSIVE) } else { hintLabel.visibility = View.VISIBLE hintLabel.text = HtmlSpanBuilder.fromHtml(getString(R.string.hint_status_reply_to_user_removed)).apply { @@ -1538,11 +1542,13 @@ class ComposeActivity : BaseActivity(), OnMenuItemClickListener, OnClickListener }, spanStart, spanEnd, Spanned.SPAN_INCLUSIVE_INCLUSIVE) } } + editable.clearSpans(MentionColorSpan::class.java) } statusTextCount.textCount = validator.getTweetLength(textAndMentions.replyText) } else { statusTextCount.textCount = validator.getTweetLength(text) hintLabel.visibility = View.GONE + editable.clearSpans(MentionColorSpan::class.java) } } @@ -2024,6 +2030,8 @@ class ComposeActivity : BaseActivity(), OnMenuItemClickListener, OnClickListener } } + private class MentionColorSpan(color: Int) : ForegroundColorSpan(color) + companion object { // Constants diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/extension/text/twitter/ExtractorExtensions.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/extension/text/twitter/ExtractorExtensions.kt index 8f4488015..cc4d7c088 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/extension/text/twitter/ExtractorExtensions.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/extension/text/twitter/ExtractorExtensions.kt @@ -78,7 +78,7 @@ fun Extractor.extractReplyTextAndMentions(text: String, inReplyTo: ParcelableSta * then this status should be treated at a mention referring to `inReplyTo`, all other mentions * counts. */ - return ReplyTextAndMentions(text, emptyList(), emptyList(), mentioningUser) + return ReplyTextAndMentions(index, text, emptyList(), emptyList(), mentioningUser) } val overrideText = run { val sb = StringBuilder() @@ -91,12 +91,13 @@ fun Extractor.extractReplyTextAndMentions(text: String, inReplyTo: ParcelableSta sb.append(text, index, text.length) return@run sb.toString() } - return ReplyTextAndMentions(overrideText, extraMentions, excludedMentions, mentioningUser) + return ReplyTextAndMentions(index, overrideText, extraMentions, excludedMentions, mentioningUser) } data class MentionsAndNonMentionStartIndex(val mentions: List, val index: Int) data class ReplyTextAndMentions( + val replyStartIndex: Int, val replyText: String, val extraMentions: List, val excludedMentions: List, diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/extension/text/twitter/ValidatorExtensions.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/extension/text/twitter/ValidatorExtensions.kt index f2efc6d3f..72ff3c991 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/extension/text/twitter/ValidatorExtensions.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/extension/text/twitter/ValidatorExtensions.kt @@ -33,7 +33,7 @@ fun Validator.getTweetLength(text: String, ignoreMentions: Boolean, inReplyTo: P return getTweetLength(text) } - val (replyText, _, _, _) = InternalExtractor.extractReplyTextAndMentions(text, inReplyTo) + val (_, replyText, _, _, _) = InternalExtractor.extractReplyTextAndMentions(text, inReplyTo) return getTweetLength(replyText) } diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/task/twitter/UpdateStatusTask.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/task/twitter/UpdateStatusTask.kt index 5d42bf905..e25957cc0 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/task/twitter/UpdateStatusTask.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/task/twitter/UpdateStatusTask.kt @@ -151,7 +151,7 @@ class UpdateStatusTask( val inReplyTo = update.in_reply_to_status ?: return for (i in 0 until pending.length) { if (update.accounts[i].type != AccountType.TWITTER) continue - val (replyText, _, excludedMentions, replyToOriginalUser) = + val (_, replyText, _, excludedMentions, replyToOriginalUser) = extractor.extractReplyTextAndMentions(pending.overrideTexts[i], inReplyTo) pending.overrideTexts[i] = replyText pending.excludeReplyUserIds[i] = excludedMentions.map { it.key.id }.toTypedArray()