Skip to content

Commit aa79e59

Browse files
use window inset listener
1 parent 2c03abc commit aa79e59

File tree

1 file changed

+33
-6
lines changed

1 file changed

+33
-6
lines changed

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

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ import android.view.inputmethod.EditorInfo
1919
import android.view.inputmethod.InputMethodManager
2020
import android.widget.ImageView
2121
import android.widget.TextView
22+
import androidx.core.view.ViewCompat
23+
import androidx.core.view.WindowInsetsAnimationCompat
24+
import androidx.core.view.WindowInsetsCompat
2225
import androidx.viewbinding.ViewBinding
2326
import org.fossify.commons.extensions.*
2427
import org.fossify.commons.views.MyEditText
@@ -167,15 +170,10 @@ class TextFragment : NoteFragment() {
167170
if (config.showKeyboard && isMenuVisible && (!note!!.isLocked() || shouldShowLockedContent)) {
168171
onGlobalLayout {
169172
if (activity?.isDestroyed == false) {
173+
setupKeyboardListener()
170174
requestFocus()
171175
val inputManager = requireActivity().getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
172176
inputManager.showSoftInput(this, InputMethodManager.SHOW_IMPLICIT)
173-
174-
val rect = Rect()
175-
noteEditText.getFocusedRect(rect)
176-
binding.notesScrollview.postDelayed({
177-
binding.notesScrollview.smoothScrollTo(0, rect.bottom)
178-
}, 1000)
179177
}
180178
}
181179
}
@@ -209,6 +207,35 @@ class TextFragment : NoteFragment() {
209207
setTextWatcher()
210208
}
211209

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

0 commit comments

Comments
 (0)