Skip to content

Commit 1f7a50a

Browse files
authored
Remove timer execution in order to prevent crash (#5)
Remove timer execution in order to prevent crash
2 parents 2f49b5c + 09d1c93 commit 1f7a50a

2 files changed

Lines changed: 7 additions & 18 deletions

File tree

codeeditor/src/main/java/me/testica/codeeditor/EditorText.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class EditorText(context: Context, attrs: AttributeSet): EditText(context, attrs
5050
// TODO: getColor is deprecated
5151
setBackgroundColor(resources.getColor(android.R.color.transparent))
5252

53-
delayedTextChanged { applySyntaxHighlight() }
53+
addTextChangedListener(afterTextChanged { applySyntaxHighlight() })
5454
}
5555

5656
/**

codeeditor/src/main/java/me/testica/codeeditor/Util.kt

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import android.content.Context
44
import android.text.Editable
55
import android.text.TextWatcher
66
import android.text.style.ForegroundColorSpan
7-
import java.util.*
87

98

109
/**
@@ -15,26 +14,16 @@ fun Int.toDp(context: Context): Int {
1514
}
1615

1716
/**
18-
* Extension TextChangedListener but with delay (1 second)
19-
* @param predicate editable emitted after text changed
17+
* Return TextWatcher object with listener on afterTextChanged
18+
* @param predicate void
2019
*/
21-
fun EditorText.delayedTextChanged(predicate: ((Editable?) -> Unit)) {
22-
val ms: Long = 1000
23-
var timer : Timer? = null
20+
fun afterTextChanged(predicate: (() -> Unit)): TextWatcher {
2421

25-
this.addTextChangedListener(object : TextWatcher {
22+
return object : TextWatcher {
2623
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
2724
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {}
28-
override fun afterTextChanged(s: Editable?) {
29-
if (timer != null) timer!!.cancel()
30-
timer = Timer()
31-
timer!!.schedule(object : TimerTask() {
32-
override fun run() {
33-
predicate.invoke(s)
34-
}
35-
}, ms)
36-
}
37-
})
25+
override fun afterTextChanged(s: Editable?) { predicate.invoke() }
26+
}
3827
}
3928

4029
/**

0 commit comments

Comments
 (0)