improved compose text mention style

This commit is contained in:
Mariotaku Lee 2017-04-04 14:15:37 +08:00
parent 10942cdc86
commit 033d30bf3f
No known key found for this signature in database
GPG Key ID: 15C10F89D7C33535
5 changed files with 44 additions and 5 deletions

View File

@ -0,0 +1,30 @@
/*
* Twidere - Twitter client for Android
*
* Copyright (C) 2012-2017 Mariotaku Lee <mariotaku.lee@gmail.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
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) }
}

View File

@ -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

View File

@ -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<Extractor.Entity>, val index: Int)
data class ReplyTextAndMentions(
val replyStartIndex: Int,
val replyText: String,
val extraMentions: List<Extractor.Entity>,
val excludedMentions: List<ParcelableUserMention>,

View File

@ -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)
}

View File

@ -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()