fix shouldTrimStatus and add tests (#1404)

This commit is contained in:
Konrad Pozniak 2019-07-25 12:15:54 +02:00 committed by GitHub
commit f975522e63
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 96 additions and 41 deletions

View file

@ -176,7 +176,7 @@ fun Status.toEntity() =
spoilerText, attachments, mentions,
false,
false,
!shouldTrimStatus(content),
shouldTrimStatus(content),
true,
poll
)

View file

@ -79,7 +79,7 @@ class StatusViewHolder(itemView: View,
private fun updateTextView() {
status()?.let { status ->
setupCollapsedState(status.isCollapsible(), viewState.isCollapsed(status.id, true),
setupCollapsedState(shouldTrimStatus(status.content), viewState.isCollapsed(status.id, true),
viewState.isContentShow(status.id, status.sensitive), status.spoilerText)
if (status.spoilerText.isBlank()) {

View file

@ -48,7 +48,6 @@ import com.keylesspalace.tusky.interfaces.StatusActionListener;
import com.keylesspalace.tusky.network.MastodonApi;
import com.keylesspalace.tusky.util.ListStatusAccessibilityDelegate;
import com.keylesspalace.tusky.util.PairedList;
import com.keylesspalace.tusky.util.SmartLengthInputFilterKt;
import com.keylesspalace.tusky.util.ThemeUtils;
import com.keylesspalace.tusky.util.ViewDataUtils;
import com.keylesspalace.tusky.view.ConversationLineItemDecoration;
@ -360,7 +359,6 @@ public final class ViewThreadFragment extends SFragment implements
}
StatusViewData.Concrete updatedStatus = new StatusViewData.Builder(status)
.setCollapsible(!SmartLengthInputFilterKt.shouldTrimStatus(status.getContent()))
.setCollapsed(isCollapsed)
.createStatusViewData();
statuses.setPairedItem(position, updatedStatus);

View file

@ -38,8 +38,7 @@ private const val LENGTH_DEFAULT = 500
* @return Whether the message should be trimmed or not.
*/
fun shouldTrimStatus(message: Spanned): Boolean {
// Check for emptiness so that we don't divide by zero
return message.isNotEmpty() && LENGTH_DEFAULT.toFloat() / message.length > 0.75
return message.isNotEmpty() && LENGTH_DEFAULT.toFloat() / message.length < 0.75
}
/**
@ -60,7 +59,7 @@ object SmartLengthInputFilter : InputFilter {
// https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/text/InputFilter.java#175
val sourceLength = source.length
var keep = LENGTH_DEFAULT - dest.length - dend - dstart
var keep = LENGTH_DEFAULT - (dest.length - (dend - dstart))
if (keep <= 0) return ""
if (keep >= end - start) return null // Keep original
@ -78,17 +77,17 @@ object SmartLengthInputFilter : InputFilter {
// those without having to add the ICU4J library at a minimum Api trade-off.
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
val iterator = android.icu.text.BreakIterator.getWordInstance()
iterator.setText(source.toString())
boundary = iterator.following(keep)
if (keep - boundary > RUNWAY) boundary = iterator.preceding(keep)
} else {
val iterator = java.text.BreakIterator.getWordInstance()
iterator.setText(source.toString())
boundary = iterator.following(keep)
if (keep - boundary > RUNWAY) boundary = iterator.preceding(keep)
}
iterator.setText(source.toString())
boundary = iterator.following(keep)
if (keep - boundary > RUNWAY) boundary = iterator.preceding(keep)
} else {
val iterator = java.text.BreakIterator.getWordInstance()
iterator.setText(source.toString())
boundary = iterator.following(keep)
if (keep - boundary > RUNWAY) boundary = iterator.preceding(keep)
}
keep = boundary
keep = boundary
} else {
// If no runway is allowed simply remove whitespaces if present

View file

@ -1,23 +0,0 @@
/* Copyright 2019 Joel Pyska
*
* This file is a part of Tusky.
*
* 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.
*
* Tusky 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 Tusky; if not,
* see <http://www.gnu.org/licenses>. */
package com.keylesspalace.tusky.util
import com.keylesspalace.tusky.entity.Status
fun Status.isCollapsible(): Boolean {
return !shouldTrimStatus(content)
}

View file

@ -58,7 +58,7 @@ public final class ViewDataUtils {
.setApplication(visibleStatus.getApplication())
.setStatusEmojis(visibleStatus.getEmojis())
.setAccountEmojis(visibleStatus.getAccount().getEmojis())
.setCollapsible(!SmartLengthInputFilterKt.shouldTrimStatus(visibleStatus.getContent()))
.setCollapsible(SmartLengthInputFilterKt.shouldTrimStatus(visibleStatus.getContent()))
.setCollapsed(true)
.setPoll(visibleStatus.getPoll())
.setCard(visibleStatus.getCard())