Skip to content

Commit e4afba9

Browse files
authored
(Input) Use CharSequence instead of String
Make use of CharSequence instead of String for text manipulation.
2 parents 3be828c + 430b664 commit e4afba9

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

input/src/main/java/com/maxkeppeler/sheets/input/type/InputEditText.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ import com.google.android.material.textfield.TextInputLayout
2727
import com.maxkeppeler.sheets.input.ValidationResult
2828

2929
/** Listener that returns the new value. */
30-
typealias EditTextInputListener = (value: String?) -> Unit
30+
typealias EditTextInputListener = (value: CharSequence?) -> Unit
3131

3232
/** Listener that is invoked when the value changes and to which a custom validation logic can be executed. */
33-
typealias EditTextInputValidationListener = (value: String /* Non-nullable, text entered can not be null. */) -> ValidationResult
33+
typealias EditTextInputValidationListener = (value: CharSequence /* Non-nullable, text entered can not be null. */) -> ValidationResult
3434

3535
/** Listener that is invoked after a custom validation. */
3636
internal typealias EditTextInputValidationResultListener = (validationResult: ValidationResult) -> Unit
@@ -86,13 +86,13 @@ class InputEditText(key: String? = null, func: InputEditText.() -> Unit) : Input
8686
internal var suffixRes: Int? = null
8787
private set
8888

89-
var value: String? = null
89+
var value: CharSequence? = null
9090
internal set(value) {
9191
invokeListeners(value)
9292
field = value
9393
}
9494

95-
private fun invokeListeners(value: String?) {
95+
private fun invokeListeners(value: CharSequence?) {
9696
changeListener?.invoke(value)
9797
value?.let { textValue ->
9898
validationListener?.invoke(textValue)?.let { result ->
@@ -107,7 +107,7 @@ class InputEditText(key: String? = null, func: InputEditText.() -> Unit) : Input
107107
}
108108

109109
/** Set the default value. */
110-
fun defaultValue(defaultText: String) {
110+
fun defaultValue(defaultText: CharSequence) {
111111
this.value = defaultText
112112
}
113113

@@ -201,6 +201,6 @@ class InputEditText(key: String? = null, func: InputEditText.() -> Unit) : Input
201201
resultListener?.invoke(value)
202202

203203
override fun putValue(bundle: Bundle, index: Int) {
204-
bundle.putString(getKeyOrIndex(index), value)
204+
bundle.putCharSequence(getKeyOrIndex(index), value)
205205
}
206206
}

sample/src/main/java/com/maxkeppeler/sample/MainActivity.kt

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ import androidx.appcompat.app.AppCompatActivity
3535
import androidx.appcompat.app.AppCompatDelegate
3636
import androidx.core.app.ActivityCompat
3737
import androidx.core.content.ContextCompat
38+
import androidx.core.text.bold
39+
import androidx.core.text.buildSpannedString
3840
import androidx.recyclerview.widget.RecyclerView
3941
import com.google.android.material.shape.CornerFamily
4042
import com.google.android.material.textfield.TextInputLayout
@@ -516,12 +518,18 @@ class MainActivity : AppCompatActivity() {
516518
title("Short survey")
517519
with(InputEditText {
518520
required()
521+
defaultValue(buildSpannedString {
522+
append("The ")
523+
bold{
524+
append("Office")
525+
}
526+
})
519527
startIconDrawable(R.drawable.ic_mail)
520528
label("Your favorite TV-Show")
521529
hint("The Mandalorian, ...")
522530
inputType(InputType.TYPE_CLASS_TEXT)
523-
changeListener { value -> showToast("Text change", value) }
524-
resultListener { value -> showToast("Text result", value) }
531+
changeListener { value -> showToast("Text change", value.toString()) }
532+
resultListener { value -> showToast("Text result", value.toString()) }
525533
})
526534
with(InputCheckBox("binge_watching") { // Read value later by index or custom key from bundle
527535
label("Binge Watching")
@@ -623,15 +631,15 @@ class MainActivity : AppCompatActivity() {
623631
endIconMode(TextInputLayout.END_ICON_PASSWORD_TOGGLE)
624632
passwordVisible(false /* Don't display password in clear text. */)
625633
validationListener { value ->
626-
password1 = value
634+
password1 = value.toString()
627635
val pattern = Pattern.compile(regex)
628636
val matcher = pattern.matcher(value)
629637
val valid = matcher.find()
630638
if (valid) Validation.success()
631639
else Validation.failed(errorText)
632640
}
633-
changeListener { value -> showToast("Text change", value) }
634-
resultListener { value -> showToast("Text result", value) }
641+
changeListener { value -> showToast("Text change", value.toString()) }
642+
resultListener { value -> showToast("Text result", value.toString()) }
635643
})
636644
with(InputEditText {
637645
required()
@@ -640,12 +648,12 @@ class MainActivity : AppCompatActivity() {
640648
endIconMode(TextInputLayout.END_ICON_PASSWORD_TOGGLE)
641649
passwordVisible(false)
642650
validationListener { value ->
643-
password2 = value
651+
password2 = value.toString()
644652
if (password1 != password2) {
645653
Validation.failed("Passwords don't match.")
646654
} else Validation.success()
647655
}
648-
resultListener { value -> showToast("Text result", value) }
656+
resultListener { value -> showToast("Text result", value.toString()) }
649657
})
650658
onNegative("cancel") { showToast("InputSheet cancelled", "No result") }
651659
onPositive("register") {

0 commit comments

Comments
 (0)