mirror of
https://github.com/sudoxnym/habitica-android.git
synced 2026-05-20 04:39:04 +00:00
Make TextWatcher more convenient to use
This commit is contained in:
parent
4a0b030022
commit
40d97c3382
8 changed files with 75 additions and 120 deletions
|
|
@ -0,0 +1,40 @@
|
|||
package com.habitrpg.android.habitica.extensions
|
||||
|
||||
import android.text.Editable
|
||||
import android.text.TextWatcher
|
||||
|
||||
class OnChangeTextWatcher(private var function: (CharSequence?, Int, Int, Int) -> Unit) : 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) {
|
||||
function(s, start, before, count)
|
||||
}
|
||||
}
|
||||
|
||||
class BeforeChangeTextWatcher(private var function: (CharSequence?, Int, Int, Int) -> Unit) : TextWatcher {
|
||||
override fun afterTextChanged(s: Editable?) {
|
||||
}
|
||||
|
||||
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
|
||||
function(s, start, count, after)
|
||||
}
|
||||
|
||||
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
|
||||
}
|
||||
}
|
||||
|
||||
class AfterChangeTextWatcher(private var function: (Editable?) -> Unit) : TextWatcher {
|
||||
override fun afterTextChanged(s: Editable?) {
|
||||
function(s)
|
||||
}
|
||||
|
||||
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
|
||||
}
|
||||
|
||||
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
|
||||
}
|
||||
}
|
||||
|
|
@ -3,18 +3,17 @@ package com.habitrpg.android.habitica.ui.activities
|
|||
import android.graphics.drawable.BitmapDrawable
|
||||
import android.graphics.drawable.Drawable
|
||||
import android.os.Bundle
|
||||
import androidx.core.content.ContextCompat
|
||||
import android.text.Editable
|
||||
import android.text.TextWatcher
|
||||
import android.text.method.LinkMovementMethod
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.Button
|
||||
import android.widget.EditText
|
||||
import android.widget.TextView
|
||||
import androidx.core.content.ContextCompat
|
||||
import com.habitrpg.android.habitica.R
|
||||
import com.habitrpg.android.habitica.components.AppComponent
|
||||
import com.habitrpg.android.habitica.data.UserRepository
|
||||
import com.habitrpg.android.habitica.extensions.OnChangeTextWatcher
|
||||
import com.habitrpg.android.habitica.extensions.runDelayed
|
||||
import com.habitrpg.android.habitica.helpers.RxErrorHandler
|
||||
import com.habitrpg.android.habitica.models.responses.VerifyUsernameResponse
|
||||
|
|
@ -22,9 +21,7 @@ import com.habitrpg.android.habitica.ui.helpers.bindView
|
|||
import com.habitrpg.android.habitica.ui.views.HabiticaIconsHelper
|
||||
import com.habitrpg.android.habitica.ui.views.HabiticaSnackbar
|
||||
import io.reactivex.BackpressureStrategy
|
||||
import io.reactivex.Completable
|
||||
import io.reactivex.Flowable
|
||||
import io.reactivex.Single
|
||||
import io.reactivex.functions.BiFunction
|
||||
import io.reactivex.functions.Consumer
|
||||
import io.reactivex.subjects.PublishSubject
|
||||
|
|
@ -69,28 +66,11 @@ class VerifyUsernameActivity: BaseActivity() {
|
|||
|
||||
confirmUsernameButton.setOnClickListener { confirmNames() }
|
||||
|
||||
displayNameEditText.addTextChangedListener(object: TextWatcher {
|
||||
override fun afterTextChanged(p0: Editable?) {
|
||||
}
|
||||
|
||||
override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
|
||||
}
|
||||
|
||||
override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
|
||||
displayNameVerificationEvents.onNext(p0.toString())
|
||||
}
|
||||
displayNameEditText.addTextChangedListener(OnChangeTextWatcher { p0, _, _, _ ->
|
||||
displayNameVerificationEvents.onNext(p0.toString())
|
||||
})
|
||||
|
||||
usernameEditText.addTextChangedListener(object: TextWatcher {
|
||||
override fun afterTextChanged(p0: Editable?) {
|
||||
}
|
||||
|
||||
override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
|
||||
}
|
||||
|
||||
override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
|
||||
usernameVerificationEvents.onNext(p0.toString())
|
||||
}
|
||||
usernameEditText.addTextChangedListener(OnChangeTextWatcher { p0, _, _, _ ->
|
||||
usernameVerificationEvents.onNext(p0.toString())
|
||||
})
|
||||
|
||||
compositeSubscription.add(Flowable.combineLatest(
|
||||
|
|
|
|||
|
|
@ -3,27 +3,24 @@ package com.habitrpg.android.habitica.ui.fragments.setup
|
|||
import android.graphics.drawable.BitmapDrawable
|
||||
import android.graphics.drawable.Drawable
|
||||
import android.os.Bundle
|
||||
import androidx.core.content.ContextCompat
|
||||
import android.text.Editable
|
||||
import android.text.TextWatcher
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.EditText
|
||||
import android.widget.TextView
|
||||
import androidx.core.content.ContextCompat
|
||||
import com.habitrpg.android.habitica.R
|
||||
import com.habitrpg.android.habitica.components.AppComponent
|
||||
import com.habitrpg.android.habitica.data.UserRepository
|
||||
import com.habitrpg.android.habitica.extensions.OnChangeTextWatcher
|
||||
import com.habitrpg.android.habitica.extensions.inflate
|
||||
import com.habitrpg.android.habitica.helpers.AmplitudeManager
|
||||
import com.habitrpg.android.habitica.helpers.RxErrorHandler
|
||||
import com.habitrpg.android.habitica.ui.SpeechBubbleView
|
||||
import com.habitrpg.android.habitica.ui.fragments.BaseFragment
|
||||
import com.habitrpg.android.habitica.ui.helpers.bindView
|
||||
import com.habitrpg.android.habitica.ui.helpers.resetViews
|
||||
import com.habitrpg.android.habitica.ui.views.HabiticaIconsHelper
|
||||
import io.reactivex.BackpressureStrategy
|
||||
import io.reactivex.functions.Consumer
|
||||
import io.reactivex.subjects.PublishSubject
|
||||
import java.util.*
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
|
@ -74,27 +71,11 @@ class WelcomeFragment : BaseFragment() {
|
|||
|
||||
super.onCreate(savedInstanceState)
|
||||
|
||||
displayNameEditText.addTextChangedListener(object: TextWatcher {
|
||||
override fun afterTextChanged(p0: Editable?) {
|
||||
}
|
||||
|
||||
override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
|
||||
}
|
||||
|
||||
override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
|
||||
displayNameEditText.addTextChangedListener(OnChangeTextWatcher { p0, _, _, _ ->
|
||||
displayNameVerificationEvents.onNext(p0.toString())
|
||||
}
|
||||
})
|
||||
usernameEditText.addTextChangedListener(object: TextWatcher {
|
||||
override fun afterTextChanged(p0: Editable?) {
|
||||
}
|
||||
|
||||
override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
|
||||
}
|
||||
|
||||
override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
|
||||
usernameEditText.addTextChangedListener(OnChangeTextWatcher { p0, _, _, _ ->
|
||||
usernameVerificationEvents.onNext(p0.toString())
|
||||
}
|
||||
})
|
||||
|
||||
compositeSubscription.add(displayNameVerificationEvents.toFlowable(BackpressureStrategy.DROP)
|
||||
|
|
|
|||
|
|
@ -2,8 +2,6 @@ package com.habitrpg.android.habitica.ui.views.social
|
|||
|
||||
import android.content.Context
|
||||
import android.os.Build
|
||||
import android.text.Editable
|
||||
import android.text.TextWatcher
|
||||
import android.util.AttributeSet
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
|
|
@ -13,6 +11,7 @@ import androidx.core.view.updateLayoutParams
|
|||
import com.habitrpg.android.habitica.HabiticaBaseApplication
|
||||
import com.habitrpg.android.habitica.R
|
||||
import com.habitrpg.android.habitica.data.SocialRepository
|
||||
import com.habitrpg.android.habitica.extensions.OnChangeTextWatcher
|
||||
import com.habitrpg.android.habitica.helpers.AppConfigManager
|
||||
import com.habitrpg.android.habitica.models.social.ChatMessage
|
||||
import com.habitrpg.android.habitica.ui.helpers.AutocompleteAdapter
|
||||
|
|
@ -74,15 +73,9 @@ class ChatBarView : FrameLayout {
|
|||
|
||||
HabiticaBaseApplication.component?.inject(this)
|
||||
|
||||
chatEditText.addTextChangedListener(object : 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) {
|
||||
chatEditText.addTextChangedListener(OnChangeTextWatcher { s, _, _, _ ->
|
||||
setSendButtonEnabled(chatEditText.text.isNotEmpty() && chatEditText.text.length <= maxChatLength)
|
||||
updateTextIndicator(chatEditText.text.toString())
|
||||
}
|
||||
})
|
||||
|
||||
sendButton.setOnClickListener { sendButtonPressed() }
|
||||
|
|
|
|||
|
|
@ -4,15 +4,14 @@ import android.content.Context
|
|||
import android.content.res.ColorStateList
|
||||
import android.graphics.PorterDuff
|
||||
import android.os.Build
|
||||
import androidx.core.content.ContextCompat
|
||||
import android.text.Editable
|
||||
import android.text.TextWatcher
|
||||
import android.util.AttributeSet
|
||||
import android.view.Gravity
|
||||
import android.view.View
|
||||
import android.widget.LinearLayout
|
||||
import android.widget.SeekBar
|
||||
import androidx.core.content.ContextCompat
|
||||
import com.habitrpg.android.habitica.R
|
||||
import com.habitrpg.android.habitica.extensions.AfterChangeTextWatcher
|
||||
import com.habitrpg.android.habitica.extensions.styledAttributes
|
||||
import kotlinx.android.synthetic.main.stats_slider_view.view.*
|
||||
|
||||
|
|
@ -62,12 +61,7 @@ class StatsSliderView(context: Context, attrs: AttributeSet?) : LinearLayout(con
|
|||
statsSeekBar.thumb = thumbDrawable
|
||||
}
|
||||
|
||||
valueEditText.addTextChangedListener(object: TextWatcher {
|
||||
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
|
||||
|
||||
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {}
|
||||
|
||||
override fun afterTextChanged(s: Editable?) {
|
||||
valueEditText.addTextChangedListener(AfterChangeTextWatcher {s ->
|
||||
val newValue = try {
|
||||
s.toString().toInt()
|
||||
} catch (e: NumberFormatException) {
|
||||
|
|
@ -80,11 +74,9 @@ class StatsSliderView(context: Context, attrs: AttributeSet?) : LinearLayout(con
|
|||
valueEditText.setText(currentValue.toString())
|
||||
valueEditText.setSelection(valueEditText.length())
|
||||
}
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
statsSeekBar.setOnSeekBarChangeListener(object: SeekBar.OnSeekBarChangeListener {
|
||||
statsSeekBar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
|
||||
override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
|
||||
currentValue = progress
|
||||
if (fromUser) {
|
||||
|
|
|
|||
|
|
@ -5,21 +5,20 @@ import android.content.res.ColorStateList
|
|||
import android.graphics.Color
|
||||
import android.graphics.drawable.Drawable
|
||||
import android.os.Bundle
|
||||
import androidx.annotation.IdRes
|
||||
import androidx.core.content.ContextCompat
|
||||
import androidx.core.widget.CompoundButtonCompat
|
||||
import androidx.appcompat.app.AlertDialog
|
||||
import androidx.appcompat.widget.AppCompatCheckBox
|
||||
import android.text.Editable
|
||||
import android.text.TextWatcher
|
||||
import android.util.TypedValue
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.WindowManager
|
||||
import android.widget.*
|
||||
import androidx.annotation.IdRes
|
||||
import androidx.appcompat.app.AlertDialog
|
||||
import androidx.appcompat.widget.AppCompatCheckBox
|
||||
import androidx.core.content.ContextCompat
|
||||
import androidx.core.widget.CompoundButtonCompat
|
||||
import com.habitrpg.android.habitica.R
|
||||
import com.habitrpg.android.habitica.components.AppComponent
|
||||
import com.habitrpg.android.habitica.data.TagRepository
|
||||
import com.habitrpg.android.habitica.extensions.OnChangeTextWatcher
|
||||
import com.habitrpg.android.habitica.helpers.RxErrorHandler
|
||||
import com.habitrpg.android.habitica.models.Tag
|
||||
import com.habitrpg.android.habitica.models.tasks.Task
|
||||
|
|
@ -199,14 +198,9 @@ class TaskFilterDialog(context: Context, component: AppComponent?) : AlertDialog
|
|||
val wrapper = inflater.inflate(R.layout.edit_tag_item, tagsList, false) as? LinearLayout
|
||||
val tagEditText = wrapper?.findViewById<View>(R.id.edit_text) as? EditText
|
||||
tagEditText?.setText(tag.name)
|
||||
tagEditText?.addTextChangedListener(object : TextWatcher {
|
||||
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
|
||||
|
||||
}
|
||||
|
||||
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
|
||||
tagEditText?.addTextChangedListener(OnChangeTextWatcher { s, _, _, _ ->
|
||||
if (index >= tags.size) {
|
||||
return
|
||||
return@OnChangeTextWatcher
|
||||
}
|
||||
val changedTag = tags[index]
|
||||
changedTag.name = s.toString()
|
||||
|
|
@ -216,11 +210,6 @@ class TaskFilterDialog(context: Context, component: AppComponent?) : AlertDialog
|
|||
editedTags[changedTag.getId()] = changedTag
|
||||
}
|
||||
tags[index] = changedTag
|
||||
}
|
||||
|
||||
override fun afterTextChanged(s: Editable) {
|
||||
|
||||
}
|
||||
})
|
||||
val deleteButton = wrapper?.findViewById<View>(R.id.delete_button) as? Button
|
||||
deleteButton?.setOnClickListener {
|
||||
|
|
|
|||
|
|
@ -1,20 +1,18 @@
|
|||
package com.habitrpg.android.habitica.ui.views.tasks.form
|
||||
|
||||
import android.content.Context
|
||||
import android.text.Editable
|
||||
import android.text.TextWatcher
|
||||
import android.util.AttributeSet
|
||||
import android.view.Gravity
|
||||
import android.view.ViewGroup
|
||||
import android.view.animation.Animation
|
||||
import android.view.animation.LinearInterpolator
|
||||
import android.view.animation.RotateAnimation
|
||||
import android.widget.EditText
|
||||
import android.widget.ImageButton
|
||||
import android.widget.LinearLayout
|
||||
import androidx.appcompat.widget.AppCompatEditText
|
||||
import androidx.core.content.ContextCompat
|
||||
import com.habitrpg.android.habitica.R
|
||||
import com.habitrpg.android.habitica.extensions.OnChangeTextWatcher
|
||||
import com.habitrpg.android.habitica.extensions.dpToPx
|
||||
import com.habitrpg.android.habitica.extensions.inflate
|
||||
import com.habitrpg.android.habitica.models.tasks.ChecklistItem
|
||||
|
|
@ -23,7 +21,7 @@ import com.habitrpg.android.habitica.ui.helpers.bindView
|
|||
|
||||
class ChecklistItemFormView @JvmOverloads constructor(
|
||||
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
|
||||
) : LinearLayout(context, attrs, defStyleAttr), TextWatcher {
|
||||
) : LinearLayout(context, attrs, defStyleAttr) {
|
||||
|
||||
|
||||
private val button: ImageButton by bindView(R.id.button)
|
||||
|
|
@ -71,18 +69,9 @@ class ChecklistItemFormView @JvmOverloads constructor(
|
|||
}
|
||||
button.drawable.mutate().setTint(tintColor)
|
||||
|
||||
editText.addTextChangedListener(this)
|
||||
editText.addTextChangedListener(OnChangeTextWatcher { s, _, _, _ ->
|
||||
item.text = s.toString()
|
||||
textChangedListener?.let { it(s.toString()) }
|
||||
})
|
||||
}
|
||||
|
||||
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) {
|
||||
item.text = s.toString()
|
||||
textChangedListener?.let { it(s.toString()) }
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,13 +1,12 @@
|
|||
package com.habitrpg.android.habitica.ui.views.tasks.form
|
||||
|
||||
import android.content.Context
|
||||
import android.text.Editable
|
||||
import android.text.TextWatcher
|
||||
import android.util.AttributeSet
|
||||
import android.widget.EditText
|
||||
import android.widget.ImageButton
|
||||
import android.widget.RelativeLayout
|
||||
import com.habitrpg.android.habitica.R
|
||||
import com.habitrpg.android.habitica.extensions.OnChangeTextWatcher
|
||||
import com.habitrpg.android.habitica.extensions.asDrawable
|
||||
import com.habitrpg.android.habitica.extensions.inflate
|
||||
import com.habitrpg.android.habitica.ui.helpers.bindView
|
||||
|
|
@ -16,7 +15,7 @@ import java.text.DecimalFormat
|
|||
|
||||
class RewardValueFormView @JvmOverloads constructor(
|
||||
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
|
||||
) : RelativeLayout(context, attrs, defStyleAttr), TextWatcher {
|
||||
) : RelativeLayout(context, attrs, defStyleAttr) {
|
||||
|
||||
private val editText: EditText by bindView(R.id.edit_text)
|
||||
private val upButton: ImageButton by bindView(R.id.up_button)
|
||||
|
|
@ -46,16 +45,8 @@ class RewardValueFormView @JvmOverloads constructor(
|
|||
value -= 1
|
||||
}
|
||||
|
||||
editText.addTextChangedListener(this)
|
||||
}
|
||||
|
||||
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) {
|
||||
value = s.toString().toDoubleOrNull() ?: 0.0
|
||||
editText.addTextChangedListener(OnChangeTextWatcher { s, _, _, _ ->
|
||||
value = s.toString().toDoubleOrNull() ?: 0.0
|
||||
})
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue