1
0
mirror of https://github.com/TwidereProject/Twidere-Android synced 2025-02-17 04:00:48 +01:00

moved wrong source file path

updated tweet count mechanism
This commit is contained in:
Mariotaku Lee 2017-03-31 10:39:09 +08:00
parent da6268b35a
commit 31543f1d25
No known key found for this signature in database
GPG Key ID: 15C10F89D7C33535
9 changed files with 195 additions and 23 deletions

View File

@ -1 +1 @@
9461c14f4025f990812a8d1130a450a710b60e6f
c32e149416ab15cc31b378adfcf29dc7db7b4711

View File

@ -0,0 +1,49 @@
/*
* 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.twidere.extension
import com.twitter.Validator
import org.junit.Assert
import org.junit.Test
/**
* Created by mariotaku on 2017/3/31.
*/
class ValidatorExtensionsKtTest {
val validator = Validator()
@Test
fun getTweetLength() {
Assert.assertEquals(0, validator.getTweetLength("@user1 ", true))
Assert.assertEquals(0, validator.getTweetLength("@user1 ", true))
Assert.assertEquals(4, validator.getTweetLength("@user1 @user2 test", true))
Assert.assertEquals(4, validator.getTweetLength("@user1 @user2 test", true))
Assert.assertEquals(4, validator.getTweetLength("@user1 @user2 test", true))
Assert.assertEquals(9, validator.getTweetLength("@user1 @user2 test ", true))
Assert.assertEquals(11, validator.getTweetLength("@user1 test @user2", true))
Assert.assertEquals(4, validator.getTweetLength("test", true))
val long50Mentions = Array(50) { "@user${it + 1}" }.joinToString(" ")
Assert.assertEquals(4, validator.getTweetLength("$long50Mentions test", true))
Assert.assertEquals(12, validator.getTweetLength("$long50Mentions @user51 test", true))
}
}

View File

@ -1,5 +1,7 @@
package org.mariotaku.twidere.util.filter
import android.content.Context
import android.net.ConnectivityManager
import android.support.test.InstrumentationRegistry
import android.support.test.runner.AndroidJUnit4
import org.junit.Test
@ -13,6 +15,10 @@ class UrlFiltersSubscriptionProviderTest {
@Test
fun testFetchXml() {
val context = InstrumentationRegistry.getTargetContext()
val cm = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
if (!(cm.activeNetworkInfo?.isConnected ?: false)) return
val url = "https://raw.githubusercontent.com/mariotaku/wtb/master/twidere/bots.xml"
val arguments = UrlFiltersSubscriptionProvider.Arguments().apply {
this.url = url

View File

@ -74,10 +74,13 @@ import org.mariotaku.twidere.R
import org.mariotaku.twidere.TwidereConstants
import org.mariotaku.twidere.adapter.BaseRecyclerViewAdapter
import org.mariotaku.twidere.adapter.MediaPreviewAdapter
import org.mariotaku.twidere.annotation.AccountType
import org.mariotaku.twidere.constant.*
import org.mariotaku.twidere.constant.IntentConstants.EXTRA_SCREEN_NAME
import org.mariotaku.twidere.extension.applyTheme
import org.mariotaku.twidere.extension.getTweetLength
import org.mariotaku.twidere.extension.loadProfileImage
import org.mariotaku.twidere.extension.model.getAccountType
import org.mariotaku.twidere.extension.model.getAccountUser
import org.mariotaku.twidere.extension.model.textLimit
import org.mariotaku.twidere.extension.model.unique_id_non_null
@ -1428,7 +1431,10 @@ class ComposeActivity : BaseActivity(), OnMenuItemClickListener, OnClickListener
if (isFinishing || editText == null) return
val hasMedia = hasMedia()
val text = editText.text.toString()
val tweetLength = validator.getTweetLength(text)
val accountKeys = accountsAdapter.selectedAccountKeys
val accounts = AccountUtils.getAllAccountDetails(AccountManager.get(this), accountKeys, true)
val ignoreMentions = accounts.all { it.type == AccountType.TWITTER }
val tweetLength = validator.getTweetLength(text, ignoreMentions)
val maxLength = statusTextCount.maxLength
if (accountsAdapter.isSelectionEmpty) {
editText.error = getString(R.string.message_toast_no_account_selected)
@ -1444,11 +1450,10 @@ class ComposeActivity : BaseActivity(), OnMenuItemClickListener, OnClickListener
}
val attachLocation = kPreferences[attachLocationKey]
val attachPreciseLocation = kPreferences[attachPreciseLocationKey]
val accountKeys = accountsAdapter.selectedAccountKeys
val isPossiblySensitive = hasMedia && possiblySensitive
val update = ParcelableStatusUpdate()
@Draft.Action val action = draft?.action_type ?: getDraftAction(intent.action)
update.accounts = AccountUtils.getAllAccountDetails(AccountManager.get(this), accountKeys, true)
update.accounts = accounts
update.text = text
if (attachLocation) {
update.location = recentLocation
@ -1484,8 +1489,13 @@ class ComposeActivity : BaseActivity(), OnMenuItemClickListener, OnClickListener
}
private fun updateTextCount() {
val am = AccountManager.get(this)
val text = editText.text?.toString() ?: return
statusTextCount.textCount = validator.getTweetLength(text)
val ignoreMentions = accountsAdapter.selectedAccountKeys.all {
val account = AccountUtils.findByAccountKey(am, it) ?: return@all false
return@all account.getAccountType(am) == AccountType.TWITTER
}
statusTextCount.textCount = validator.getTweetLength(text, ignoreMentions)
}
private fun updateUpdateStatusIcon() {

View File

@ -0,0 +1,60 @@
/*
* 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.twidere.extension
import com.twitter.Extractor
import com.twitter.Validator
import java.text.Normalizer
/**
* Created by mariotaku on 2017/3/31.
*/
fun Validator.getTweetLength(text: String, ignoreMentions: Boolean): Int {
var temp = text
temp = Normalizer.normalize(temp, Normalizer.Form.NFC)
var length = temp.codePointCount(0, temp.length)
if (ignoreMentions) {
var nextExpectedPos = 0
run {
val mentions = InternalExtractor.extractMentionedScreennamesWithIndices(temp)
mentions.forEachIndexed { index, entity ->
// Limit to 50 mentions https://dev.twitter.com/overview/api/upcoming-changes-to-tweets
if (index >= 50) return@run
if (entity.start != nextExpectedPos) return@run
nextExpectedPos = (entity.end..temp.indices.endInclusive).firstOrNull {
!temp[it].isWhitespace()
} ?: temp.indices.endInclusive + 1
}
}
length -= temp.codePointCount(0, nextExpectedPos)
}
for (urlEntity in InternalExtractor.extractURLsWithIndices(temp)) {
length += urlEntity.start - urlEntity.end
length += if (urlEntity.value.startsWith("https://", ignoreCase = true)) shortUrlLengthHttps else shortUrlLength
}
return length
}
private object InternalExtractor : Extractor()

View File

@ -0,0 +1,45 @@
/*
* 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.twidere.extensions
import android.support.v7.widget.RecyclerView
import android.support.v7.widget.StaggeredGridLayoutManager
/**
* Created by mariotaku on 2017/3/30.
*/
val StaggeredGridLayoutManager.reachingStart: Boolean
get() {
var visiblePos = findFirstCompletelyVisibleItemPositions(null)
if (visiblePos.all { it == RecyclerView.NO_POSITION }) {
visiblePos = findFirstVisibleItemPositions(null)
}
return visiblePos.contains(0)
}
val StaggeredGridLayoutManager.reachingEnd: Boolean
get() {
var visiblePos = findLastCompletelyVisibleItemPositions(null)
if (visiblePos.all { it == RecyclerView.NO_POSITION }) {
visiblePos = findLastVisibleItemPositions(null)
}
return visiblePos.contains(itemCount - 1)
}

View File

@ -48,6 +48,8 @@ import org.mariotaku.twidere.annotation.AccountType
import org.mariotaku.twidere.constant.IntentConstants.*
import org.mariotaku.twidere.constant.SharedPreferenceConstants.KEY_QUICK_SEND
import org.mariotaku.twidere.extension.applyTheme
import org.mariotaku.twidere.extension.getTweetLength
import org.mariotaku.twidere.extension.model.getAccountType
import org.mariotaku.twidere.extension.model.textLimit
import org.mariotaku.twidere.model.*
import org.mariotaku.twidere.model.util.AccountUtils
@ -179,7 +181,8 @@ class RetweetQuoteDialogFragment : BaseDialogFragment() {
return dialog
}
private fun updateTextCount(dialog: DialogInterface, s: CharSequence, status: ParcelableStatus, credentials: AccountDetails) {
private fun updateTextCount(dialog: DialogInterface, s: CharSequence, status: ParcelableStatus,
credentials: AccountDetails) {
if (dialog !is AlertDialog) return
val positiveButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE) ?: return
if (s.isNotEmpty()) {
@ -195,8 +198,11 @@ class RetweetQuoteDialogFragment : BaseDialogFragment() {
positiveButton.setText(R.string.action_retweet)
positiveButton.isEnabled = !status.user_is_protected
}
val textCountView = (dialog.findViewById(R.id.commentTextCount) as StatusTextCountView?)!!
textCountView.textCount = validator.getTweetLength(s.toString())
val textCountView = dialog.findViewById(R.id.commentTextCount) as StatusTextCountView
val am = AccountManager.get(context)
val ignoreMentions = AccountUtils.findByAccountKey(am, accountKey)?.getAccountType(am) ==
AccountType.TWITTER
textCountView.textCount = validator.getTweetLength(s.toString(), ignoreMentions)
}
private val status: ParcelableStatus

View File

@ -185,7 +185,7 @@ class UserProfileEditorFragment : BaseFragment(), OnSizeChangedListener, TextWat
return
}
val lengthChecker = TwitterValidatorMETLengthChecker(Validator())
val lengthChecker = TwitterValidatorMETLengthChecker(Validator(), false)
editName.addTextChangedListener(this)
editDescription.addTextChangedListener(this)
editLocation.addTextChangedListener(this)

View File

@ -17,26 +17,22 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.mariotaku.twidere.util;
package org.mariotaku.twidere.util
import android.support.annotation.NonNull;
import com.rengwuxian.materialedittext.validation.METLengthChecker;
import com.twitter.Validator;
import com.rengwuxian.materialedittext.validation.METLengthChecker
import com.twitter.Validator
import org.mariotaku.twidere.extension.getTweetLength
/**
* Created by mariotaku on 15/4/29.
*/
public class TwitterValidatorMETLengthChecker extends METLengthChecker {
private final Validator mValidator;
class TwitterValidatorMETLengthChecker(
private val validator: Validator,
private val ignoreMentions: Boolean
) : METLengthChecker() {
public TwitterValidatorMETLengthChecker(@NonNull Validator validator) {
mValidator = validator;
}
@Override
public int getLength(CharSequence charSequence) {
return mValidator.getTweetLength(String.valueOf(charSequence));
override fun getLength(charSequence: CharSequence): Int {
return validator.getTweetLength(charSequence.toString(), ignoreMentions)
}
}