Skip to content

Commit bd0de22

Browse files
committed
Add doc comment to extension functions
1 parent 8f7fab3 commit bd0de22

4 files changed

Lines changed: 55 additions & 6 deletions

File tree

base/src/main/java/com/enginebai/base/extensions/EditTextExt.kt

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ import io.reactivex.Observable
1010
import io.reactivex.Single
1111
import io.reactivex.subjects.PublishSubject
1212

13+
/**
14+
* Invoke the listener when afterTextChanged() method is called.
15+
*
16+
* @return TextWatcher remember to remove when no longer use it anymore.
17+
*/
1318
fun EditText.textChanged(listener: (String) -> Unit): TextWatcher {
1419
val textWatcher = object : TextWatcher {
1520
override fun afterTextChanged(s: Editable?) {
@@ -48,7 +53,12 @@ private class TextChangeWatcher(private var publisher: PublishSubject<String>? =
4853
}
4954
}
5055

51-
56+
/**
57+
* Validate the input text and display error when it's invalid.
58+
* @param [errorMessage] error message to display
59+
* @param [validator] the predicate to validate input.
60+
* @return TextWatcher remember to remove when no longer use it anymore.
61+
*/
5262
fun EditText.validate(errorMessage: String, validator: (String) -> Boolean): TextWatcher {
5363
fun showErrorIfInvalid(s: String) {
5464
this.error = if (validator(s)) null else errorMessage
@@ -59,10 +69,28 @@ fun EditText.validate(errorMessage: String, validator: (String) -> Boolean): Tex
5969
return this.textChanged { showErrorIfInvalid(it) }
6070
}
6171

72+
/**
73+
* Validate if input is valid email, and invoke either valid or invalid listener.
74+
* @return TextWatcher remember to remove when no longer use it anymore.
75+
*/
76+
fun EditText.validateEmail(validListener: (String) -> Unit, invalidListener: (String) -> Unit = {}): TextWatcher {
77+
return this.textChanged {
78+
if (it.isValidEmail()) validListener(it)
79+
else invalidListener(it)
80+
}
81+
}
82+
83+
/**
84+
* Validate if input is valid email, and display error if invalid. * @return TextWatcher remember to remove when no longer use it anymore.
85+
* @return TextWatcher remember to remove when no longer use it anymore.
86+
*/
6287
fun EditText.validateEmail(errorMessage: String): TextWatcher {
6388
return validate(errorMessage) { it.isValidEmail() }
6489
}
6590

91+
/**
92+
* Validate if input is valid email and return result as stream.
93+
*/
6694
fun EditText.validateEmail(): Single<Boolean> {
6795
return Single.fromObservable(this.textChanged()).flatMap {
6896
Single.just(it.toString().isValidEmail())

base/src/main/java/com/enginebai/base/extensions/StringExt.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ package com.enginebai.base.extensions
33
import android.util.Patterns
44
import androidx.core.util.PatternsCompat
55

6-
fun String.isValidEmail(): Boolean {
7-
return PatternsCompat.EMAIL_ADDRESS.matcher(this).matches()
6+
fun String?.isValidEmail(): Boolean {
7+
return PatternsCompat.EMAIL_ADDRESS.matcher(this ?: "").matches()
88
}

base/src/main/java/com/enginebai/base/extensions/TextViewExt.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,17 @@ package com.enginebai.base.extensions
33
import android.view.View
44
import android.widget.TextView
55

6+
/**
7+
* Set view visible when text is not null or blank, else set view gone.
8+
*/
69
fun TextView.setTextWithExistence(s: String?) {
710
s?.let { this.text = it }
811
setExistence(!s.isNullOrBlank())
912
}
1013

14+
/**
15+
* Set view visible when text is not null or blank, else set view invisible.
16+
*/
1117
fun TextView.setTextWithVisibility(s: String?) {
1218
s?.let { this.text = it }
1319
setVisible(!s.isNullOrBlank())

base/src/main/java/com/enginebai/base/extensions/ViewExt.kt

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ import androidx.core.content.ContextCompat
1010

1111
/**
1212
* Prevent multiple click in a short period of time. Default interval is 1500 milli-second.
13-
* @param listener: The click listener
1413
* @param intervalInMillis: The time interval to trigger next click events.
14+
* @param listener: The click listener
1515
*/
16-
inline fun View.debounceClick(crossinline listener: (view: View) -> Unit, intervalInMillis: Int = 1500) {
16+
inline fun View.debounceClick(intervalInMillis: Int = 1500, crossinline listener: (view: View) -> Unit) {
1717
var lastClick = 0L
1818
setOnClickListener {
1919
val diff = System.currentTimeMillis() - lastClick
@@ -27,28 +27,43 @@ inline fun View.debounceClick(crossinline listener: (view: View) -> Unit, interv
2727
//
2828
// visibility
2929
//
30+
/**
31+
* Set visible or invisible of view.
32+
*/
3033
fun View.setVisible(visible: Boolean) {
3134
visibility = if (visible) View.VISIBLE else View.INVISIBLE
3235
}
3336

37+
/**
38+
* Set visible or gone of view.
39+
*/
3440
fun View.setExistence(exist: Boolean) {
3541
visibility = if (exist) View.VISIBLE else View.GONE
3642
}
3743

44+
/**
45+
* Set view visible when matching the given [predicate]
46+
*/
3847
inline fun View.showIf(predicate: () -> Boolean): View {
3948
if (visibility != View.VISIBLE && predicate()) {
4049
visibility = View.VISIBLE
4150
}
4251
return this
4352
}
4453

54+
/**
55+
* Set view invisible when matching the given [predicate]
56+
*/
4557
inline fun View.hideIf(predicate: () -> Boolean): View {
4658
if (visibility != View.INVISIBLE && predicate()) {
47-
visibility = View.VISIBLE
59+
visibility = View.INVISIBLE
4860
}
4961
return this
5062
}
5163

64+
/**
65+
* Set view gone when matching the given [predicate]
66+
*/
5267
inline fun View.goneIf(predicate: () -> Boolean): View {
5368
if (visibility != View.GONE && predicate()) {
5469
visibility = View.GONE

0 commit comments

Comments
 (0)