@@ -5,12 +5,19 @@ import android.os.Handler
55import android.os.Looper
66import android.view.View
77import android.view.inputmethod.InputMethodManager
8+ import android.widget.TextView
9+ import androidx.core.view.isVisible
810
911class AndroidManager (
1012 private val context : Context ,
1113 private val editText : FakeEditText ,
14+ private val previewContainer : View ,
15+ private val previewText : TextView ,
1216) {
1317 private val handler = Handler (Looper .getMainLooper())
18+ private var isImeVisible = false
19+ private var shouldShowPreview = false
20+ private var pendingPreviewText: String = " "
1421
1522 /*
1623 * Methods called from JNI
@@ -30,11 +37,90 @@ class AndroidManager(
3037 editText.visibility = View .INVISIBLE
3138 val imm = editText.context.getSystemService(Context .INPUT_METHOD_SERVICE ) as InputMethodManager
3239 imm.hideSoftInputFromWindow(editText.windowToken, 0 )
40+ hidePreviewInternal(clearText = false )
41+ }
42+ }
43+
44+ fun showInputPreview (text : String ) {
45+ handler.post {
46+ pendingPreviewText = text
47+ shouldShowPreview = true
48+ if (isImeVisible) showPreviewInternal()
49+ }
50+ }
51+
52+ fun updateInputPreview (text : String ) {
53+ handler.post {
54+ pendingPreviewText = text
55+ if (shouldShowPreview && isImeVisible) showPreviewInternal(animate = false )
56+ }
57+ }
58+
59+ fun hideInputPreview () {
60+ handler.post {
61+ shouldShowPreview = false
62+ pendingPreviewText = " "
63+ hidePreviewInternal(clearText = true )
64+ }
65+ }
66+
67+ fun onImeVisibilityChanged (visible : Boolean ) {
68+ handler.post {
69+ isImeVisible = visible
70+ if (visible) {
71+ if (shouldShowPreview) showPreviewInternal()
72+ } else {
73+ hidePreviewInternal(clearText = false )
74+ }
3375 }
3476 }
3577
3678 fun getDisplayDensity (): Float = context.resources.displayMetrics.density
3779
3880 external fun nativeInit ()
3981 external fun nativeSetAudioEnabled (enabled : Boolean )
82+
83+ private fun showPreviewInternal (animate : Boolean = true) {
84+ previewContainer.animate().cancel()
85+ previewText.text = pendingPreviewText
86+ if (! previewContainer.isVisible) {
87+ previewContainer.alpha = if (animate) 0f else 1f
88+ previewContainer.isVisible = true
89+ if (animate) {
90+ previewContainer.animate()
91+ .alpha(1f )
92+ .setDuration(120L )
93+ .start()
94+ }
95+ } else if (animate) {
96+ previewContainer.animate()
97+ .alpha(1f )
98+ .setDuration(120L )
99+ .start()
100+ }
101+ }
102+
103+ private fun hidePreviewInternal (animate : Boolean = true, clearText : Boolean ) {
104+ previewContainer.animate().cancel()
105+ if (! previewContainer.isVisible) {
106+ if (clearText) previewText.text = " "
107+ return
108+ }
109+
110+ if (animate) {
111+ previewContainer.animate()
112+ .alpha(0f )
113+ .setDuration(120L )
114+ .withEndAction {
115+ previewContainer.isVisible = false
116+ previewContainer.alpha = 1f
117+ if (clearText) previewText.text = " "
118+ }
119+ .start()
120+ } else {
121+ previewContainer.alpha = 1f
122+ previewContainer.isVisible = false
123+ if (clearText) previewText.text = " "
124+ }
125+ }
40126}
0 commit comments