Twidere-App-Android-Twitter.../twidere/src/main/kotlin/org/mariotaku/twidere/text/util/EmojiEditableFactory.kt

65 lines
2.2 KiB
Kotlin
Raw Normal View History

2016-08-28 06:24:48 +02:00
/*
* Twidere - Twitter client for Android
*
* Copyright (C) 2012-2015 Mariotaku Lee <mariotaku.lee@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.mariotaku.twidere.text.util
import android.text.Editable
import android.text.Spanned
import android.text.TextWatcher
import android.widget.TextView
2017-05-13 08:19:23 +02:00
import org.mariotaku.twidere.extension.applyTo
2017-02-13 17:44:56 +01:00
import org.mariotaku.twidere.text.SafeSpannableStringBuilder
2016-08-28 06:24:48 +02:00
import org.mariotaku.twidere.util.ExternalThemeManager
2017-04-16 15:09:56 +02:00
import org.mariotaku.twidere.util.dagger.GeneralComponent
2016-08-28 06:24:48 +02:00
import javax.inject.Inject
/**
* Created by mariotaku on 15/12/20.
*/
2017-02-13 17:44:56 +01:00
class EmojiEditableFactory(textView: TextView) : Editable.Factory() {
2016-08-28 06:24:48 +02:00
@Inject
lateinit internal var externalThemeManager: ExternalThemeManager
init {
2017-04-16 15:09:56 +02:00
GeneralComponent.get(textView.context).inject(this)
2016-08-28 06:24:48 +02:00
}
override fun newEditable(source: CharSequence): Editable {
2017-02-13 17:44:56 +01:00
val editable = SafeSpannableStringBuilder(source)
2017-05-13 08:19:23 +02:00
val emoji = externalThemeManager.emoji
emoji?.applyTo(editable)
2016-08-28 06:24:48 +02:00
editable.setSpan(object : TextWatcher {
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
}
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
if (count <= 0) return
2017-05-13 08:19:23 +02:00
emoji?.applyTo(editable)
2016-08-28 06:24:48 +02:00
}
override fun afterTextChanged(s: Editable) {
}
}, 0, editable.length, Spanned.SPAN_INCLUSIVE_INCLUSIVE)
return editable
}
}