From 43ea59ab2fdf5ee37af71d2da9b3edb75534abb4 Mon Sep 17 00:00:00 2001 From: Goooler Date: Sat, 11 Mar 2023 03:27:24 +0800 Subject: [PATCH] Replace DefaultTextWatcher with extensions in core-ktx (#3401) * Replace DefaultTextWatcher with extensions in core-ktx * Fix positiveButton.isEnabled * editable!! for highlightSpans * Fix style * Put noteWatcher back --- .../com/keylesspalace/tusky/ListsActivity.kt | 6 ++-- .../tusky/TabPreferenceActivity.kt | 4 +-- .../tusky/adapter/AccountFieldEditAdapter.kt | 27 ++++----------- .../components/account/AccountActivity.kt | 16 ++++----- .../components/compose/ComposeActivity.kt | 10 +++--- .../compose/dialog/AddPollOptionsAdapter.kt | 4 +-- .../tusky/util/ViewExtensions.kt | 34 ------------------- 7 files changed, 26 insertions(+), 75 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/ListsActivity.kt b/app/src/main/java/com/keylesspalace/tusky/ListsActivity.kt index b02728cc..ec241c20 100644 --- a/app/src/main/java/com/keylesspalace/tusky/ListsActivity.kt +++ b/app/src/main/java/com/keylesspalace/tusky/ListsActivity.kt @@ -31,6 +31,7 @@ import android.widget.TextView import androidx.activity.viewModels import androidx.annotation.StringRes import androidx.appcompat.app.AlertDialog +import androidx.core.widget.doOnTextChanged import androidx.lifecycle.lifecycleScope import androidx.recyclerview.widget.DiffUtil import androidx.recyclerview.widget.DividerItemDecoration @@ -45,7 +46,6 @@ import com.keylesspalace.tusky.di.Injectable import com.keylesspalace.tusky.di.ViewModelFactory import com.keylesspalace.tusky.entity.MastoList import com.keylesspalace.tusky.util.hide -import com.keylesspalace.tusky.util.onTextChanged import com.keylesspalace.tusky.util.show import com.keylesspalace.tusky.util.viewBinding import com.keylesspalace.tusky.util.visible @@ -143,8 +143,8 @@ class ListsActivity : BaseActivity(), Injectable, HasAndroidInjector { .show() val positiveButton = dialog.getButton(Dialog.BUTTON_POSITIVE) - editText.onTextChanged { s, _, _, _ -> - positiveButton.isEnabled = s.isNotBlank() + editText.doOnTextChanged { s, _, _, _ -> + positiveButton.isEnabled = s?.isNotBlank() == true } editText.setText(list?.title) editText.text?.let { editText.setSelection(it.length) } diff --git a/app/src/main/java/com/keylesspalace/tusky/TabPreferenceActivity.kt b/app/src/main/java/com/keylesspalace/tusky/TabPreferenceActivity.kt index cb492424..ad4ade73 100644 --- a/app/src/main/java/com/keylesspalace/tusky/TabPreferenceActivity.kt +++ b/app/src/main/java/com/keylesspalace/tusky/TabPreferenceActivity.kt @@ -24,6 +24,7 @@ import androidx.activity.OnBackPressedCallback import androidx.appcompat.app.AlertDialog import androidx.appcompat.widget.AppCompatEditText import androidx.core.view.updatePadding +import androidx.core.widget.doOnTextChanged import androidx.lifecycle.Lifecycle import androidx.lifecycle.lifecycleScope import androidx.recyclerview.widget.DividerItemDecoration @@ -45,7 +46,6 @@ import com.keylesspalace.tusky.appstore.MainTabsChangedEvent import com.keylesspalace.tusky.databinding.ActivityTabPreferenceBinding import com.keylesspalace.tusky.di.Injectable import com.keylesspalace.tusky.network.MastodonApi -import com.keylesspalace.tusky.util.onTextChanged import com.keylesspalace.tusky.util.unsafeLazy import com.keylesspalace.tusky.util.viewBinding import com.keylesspalace.tusky.util.visible @@ -255,7 +255,7 @@ class TabPreferenceActivity : BaseActivity(), Injectable, ItemInteractionListene } .create() - editText.onTextChanged { s, _, _, _ -> + editText.doOnTextChanged { s, _, _, _ -> dialog.getButton(AlertDialog.BUTTON_POSITIVE).isEnabled = validateHashtag(s) } diff --git a/app/src/main/java/com/keylesspalace/tusky/adapter/AccountFieldEditAdapter.kt b/app/src/main/java/com/keylesspalace/tusky/adapter/AccountFieldEditAdapter.kt index 30cf6309..fc258f08 100644 --- a/app/src/main/java/com/keylesspalace/tusky/adapter/AccountFieldEditAdapter.kt +++ b/app/src/main/java/com/keylesspalace/tusky/adapter/AccountFieldEditAdapter.kt @@ -15,10 +15,9 @@ package com.keylesspalace.tusky.adapter -import android.text.Editable -import android.text.TextWatcher import android.view.LayoutInflater import android.view.ViewGroup +import androidx.core.widget.doAfterTextChanged import androidx.recyclerview.widget.RecyclerView import com.keylesspalace.tusky.databinding.ItemEditFieldBinding import com.keylesspalace.tusky.entity.StringField @@ -81,25 +80,13 @@ class AccountFieldEditAdapter : RecyclerView.Adapter + fieldData[holder.bindingAdapterPosition].first = newText.toString() + } - override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {} - - override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {} - }) - - holder.binding.accountFieldValueText.addTextChangedListener(object : TextWatcher { - override fun afterTextChanged(newText: Editable) { - fieldData[holder.bindingAdapterPosition].second = newText.toString() - } - - override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {} - - override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {} - }) + holder.binding.accountFieldValueText.doAfterTextChanged { newText -> + fieldData[holder.bindingAdapterPosition].second = newText.toString() + } } class MutableStringPair(var first: String, var second: String) diff --git a/app/src/main/java/com/keylesspalace/tusky/components/account/AccountActivity.kt b/app/src/main/java/com/keylesspalace/tusky/components/account/AccountActivity.kt index cdb30608..561e5fc0 100644 --- a/app/src/main/java/com/keylesspalace/tusky/components/account/AccountActivity.kt +++ b/app/src/main/java/com/keylesspalace/tusky/components/account/AccountActivity.kt @@ -24,7 +24,7 @@ import android.content.res.ColorStateList import android.graphics.Color import android.graphics.drawable.LayerDrawable import android.os.Bundle -import android.text.Editable +import android.text.TextWatcher import android.view.Menu import android.view.MenuInflater import android.view.MenuItem @@ -42,6 +42,7 @@ import androidx.core.view.WindowCompat import androidx.core.view.WindowInsetsCompat import androidx.core.view.WindowInsetsCompat.Type.systemBars import androidx.core.view.updatePadding +import androidx.core.widget.doAfterTextChanged import androidx.preference.PreferenceManager import androidx.recyclerview.widget.LinearLayoutManager import androidx.viewpager2.widget.MarginPageTransformer @@ -74,7 +75,6 @@ import com.keylesspalace.tusky.interfaces.ActionButtonActivity import com.keylesspalace.tusky.interfaces.LinkListener import com.keylesspalace.tusky.interfaces.ReselectableFragment import com.keylesspalace.tusky.settings.PrefKeys -import com.keylesspalace.tusky.util.DefaultTextWatcher import com.keylesspalace.tusky.util.Error import com.keylesspalace.tusky.util.Loading import com.keylesspalace.tusky.util.Success @@ -154,6 +154,8 @@ class AccountActivity : BottomSheetActivity(), ActionButtonActivity, MenuProvide private lateinit var adapter: AccountPagerAdapter + private var noteWatcher: TextWatcher? = null + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) loadResources() @@ -660,15 +662,11 @@ class AccountActivity : BottomSheetActivity(), ActionButtonActivity, MenuProvide binding.accountNoteTextInputLayout.visible(relation.note != null) binding.accountNoteTextInputLayout.editText?.setText(relation.note) - binding.accountNoteTextInputLayout.editText?.addTextChangedListener(noteWatcher) - - updateButtons() - } - - private val noteWatcher = object : DefaultTextWatcher() { - override fun afterTextChanged(s: Editable) { + noteWatcher = binding.accountNoteTextInputLayout.editText?.doAfterTextChanged { s -> viewModel.noteChanged(s.toString()) } + + updateButtons() } private fun updateFollowButton() { diff --git a/app/src/main/java/com/keylesspalace/tusky/components/compose/ComposeActivity.kt b/app/src/main/java/com/keylesspalace/tusky/components/compose/ComposeActivity.kt index db606a31..55ea6b15 100644 --- a/app/src/main/java/com/keylesspalace/tusky/components/compose/ComposeActivity.kt +++ b/app/src/main/java/com/keylesspalace/tusky/components/compose/ComposeActivity.kt @@ -56,6 +56,8 @@ import androidx.core.view.ContentInfoCompat import androidx.core.view.OnReceiveContentListener import androidx.core.view.isGone import androidx.core.view.isVisible +import androidx.core.widget.doAfterTextChanged +import androidx.core.widget.doOnTextChanged import androidx.lifecycle.lifecycleScope import androidx.preference.PreferenceManager import androidx.recyclerview.widget.LinearLayoutManager @@ -90,7 +92,6 @@ import com.keylesspalace.tusky.entity.Status import com.keylesspalace.tusky.settings.PrefKeys import com.keylesspalace.tusky.util.APP_THEME_DEFAULT import com.keylesspalace.tusky.util.PickMediaFiles -import com.keylesspalace.tusky.util.afterTextChanged import com.keylesspalace.tusky.util.getInitialLanguages import com.keylesspalace.tusky.util.getLocaleList import com.keylesspalace.tusky.util.getMediaSize @@ -98,7 +99,6 @@ import com.keylesspalace.tusky.util.hide import com.keylesspalace.tusky.util.highlightSpans import com.keylesspalace.tusky.util.loadAvatar import com.keylesspalace.tusky.util.modernLanguageCode -import com.keylesspalace.tusky.util.onTextChanged import com.keylesspalace.tusky.util.setDrawableTint import com.keylesspalace.tusky.util.show import com.keylesspalace.tusky.util.unsafeLazy @@ -370,7 +370,7 @@ class ComposeActivity : if (startingContentWarning != null) { binding.composeContentWarningField.setText(startingContentWarning) } - binding.composeContentWarningField.onTextChanged { _, _, _, _ -> updateVisibleCharactersLeft() } + binding.composeContentWarningField.doOnTextChanged { _, _, _, _ -> updateVisibleCharactersLeft() } } private fun setupComposeField(preferences: SharedPreferences, startingText: String?) { @@ -393,8 +393,8 @@ class ComposeActivity : val mentionColour = binding.composeEditField.linkTextColors.defaultColor highlightSpans(binding.composeEditField.text, mentionColour) - binding.composeEditField.afterTextChanged { editable -> - highlightSpans(editable, mentionColour) + binding.composeEditField.doAfterTextChanged { editable -> + highlightSpans(editable!!, mentionColour) updateVisibleCharactersLeft() } diff --git a/app/src/main/java/com/keylesspalace/tusky/components/compose/dialog/AddPollOptionsAdapter.kt b/app/src/main/java/com/keylesspalace/tusky/components/compose/dialog/AddPollOptionsAdapter.kt index 3640ffa9..ded1b7cf 100644 --- a/app/src/main/java/com/keylesspalace/tusky/components/compose/dialog/AddPollOptionsAdapter.kt +++ b/app/src/main/java/com/keylesspalace/tusky/components/compose/dialog/AddPollOptionsAdapter.kt @@ -19,11 +19,11 @@ import android.text.InputFilter import android.view.LayoutInflater import android.view.View import android.view.ViewGroup +import androidx.core.widget.doOnTextChanged import androidx.recyclerview.widget.RecyclerView import com.keylesspalace.tusky.R import com.keylesspalace.tusky.databinding.ItemAddPollOptionBinding import com.keylesspalace.tusky.util.BindingHolder -import com.keylesspalace.tusky.util.onTextChanged import com.keylesspalace.tusky.util.visible class AddPollOptionsAdapter( @@ -46,7 +46,7 @@ class AddPollOptionsAdapter( val holder = BindingHolder(binding) binding.optionEditText.filters = arrayOf(InputFilter.LengthFilter(maxOptionLength)) - binding.optionEditText.onTextChanged { s, _, _, _ -> + binding.optionEditText.doOnTextChanged { s, _, _, _ -> val pos = holder.bindingAdapterPosition if (pos != RecyclerView.NO_POSITION) { options[pos] = s.toString() diff --git a/app/src/main/java/com/keylesspalace/tusky/util/ViewExtensions.kt b/app/src/main/java/com/keylesspalace/tusky/util/ViewExtensions.kt index 3e56c68b..dc55ea52 100644 --- a/app/src/main/java/com/keylesspalace/tusky/util/ViewExtensions.kt +++ b/app/src/main/java/com/keylesspalace/tusky/util/ViewExtensions.kt @@ -16,11 +16,8 @@ package com.keylesspalace.tusky.util -import android.text.Editable -import android.text.TextWatcher import android.util.Log import android.view.View -import android.widget.EditText import androidx.recyclerview.widget.RecyclerView import androidx.viewpager2.widget.ViewPager2 @@ -36,37 +33,6 @@ fun View.visible(visible: Boolean, or: Int = View.GONE) { this.visibility = if (visible) View.VISIBLE else or } -open class DefaultTextWatcher : TextWatcher { - override fun afterTextChanged(s: Editable) { - } - - override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) { - } - - override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) { - } -} - -inline fun EditText.onTextChanged( - crossinline callback: (s: CharSequence, start: Int, before: Int, count: Int) -> Unit -) { - addTextChangedListener(object : DefaultTextWatcher() { - override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) { - callback(s, start, before, count) - } - }) -} - -inline fun EditText.afterTextChanged( - crossinline callback: (s: Editable) -> Unit -) { - addTextChangedListener(object : DefaultTextWatcher() { - override fun afterTextChanged(s: Editable) { - callback(s) - } - }) -} - /** * Reduce ViewPager2's sensitivity to horizontal swipes. */