Skip to content

Commit 78bf8e2

Browse files
use window inset listener
1 parent adffdb9 commit 78bf8e2

1 file changed

Lines changed: 33 additions & 6 deletions

File tree

app/src/main/kotlin/org/fossify/notes/fragments/TextFragment.kt

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ import android.view.ViewGroup
1818
import android.view.inputmethod.InputMethodManager
1919
import android.widget.ImageView
2020
import android.widget.TextView
21+
import androidx.core.view.ViewCompat
22+
import androidx.core.view.WindowInsetsAnimationCompat
23+
import androidx.core.view.WindowInsetsCompat
2124
import androidx.viewbinding.ViewBinding
2225
import org.fossify.commons.extensions.*
2326
import org.fossify.commons.views.MyEditText
@@ -170,15 +173,10 @@ class TextFragment : NoteFragment() {
170173
if (config.showKeyboard && isMenuVisible && (!note!!.isLocked() || shouldShowLockedContent)) {
171174
onGlobalLayout {
172175
if (activity?.isDestroyed == false) {
176+
setupKeyboardListener()
173177
requestFocus()
174178
val inputManager = requireActivity().getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
175179
inputManager.showSoftInput(this, InputMethodManager.SHOW_IMPLICIT)
176-
177-
val rect = Rect()
178-
noteEditText.getFocusedRect(rect)
179-
binding.notesScrollview.postDelayed({
180-
binding.notesScrollview.smoothScrollTo(0, rect.bottom)
181-
}, 1000)
182180
}
183181
}
184182
}
@@ -207,6 +205,35 @@ class TextFragment : NoteFragment() {
207205
setTextWatcher()
208206
}
209207

208+
private fun setupKeyboardListener() {
209+
requireActivity().window.decorView.setOnApplyWindowInsetsListener { view, insets ->
210+
scrollToEndOfNote()
211+
view.onApplyWindowInsets(insets)
212+
}
213+
214+
val callback =
215+
object : WindowInsetsAnimationCompat.Callback(DISPATCH_MODE_CONTINUE_ON_SUBTREE) {
216+
override fun onPrepare(animation: WindowInsetsAnimationCompat) {
217+
super.onPrepare(animation)
218+
scrollToEndOfNote()
219+
}
220+
221+
override fun onProgress(
222+
insets: WindowInsetsCompat,
223+
runningAnimations: MutableList<WindowInsetsAnimationCompat>,
224+
) = insets
225+
}
226+
ViewCompat.setWindowInsetsAnimationCallback(requireActivity().window.decorView, callback)
227+
}
228+
229+
private fun scrollToEndOfNote() {
230+
val rect = Rect()
231+
noteEditText.getFocusedRect(rect)
232+
binding.notesScrollview.post {
233+
binding.notesScrollview.scrollTo(0, rect.bottom)
234+
}
235+
}
236+
210237
fun setTextWatcher() {
211238
noteEditText.apply {
212239
removeTextChangedListener(textWatcher)

0 commit comments

Comments
 (0)