From 57ed93f2a55775265721d09da54ad9bdee5516f1 Mon Sep 17 00:00:00 2001
From: Taras Smakula <tarassmakula@gmail.com>
Date: Fri, 1 Dec 2023 14:20:13 +0200
Subject: [PATCH] Fix cursor position after append

---
 .../java/org/futo/circles/view/PreviewPostView.kt  | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/app/src/main/java/org/futo/circles/view/PreviewPostView.kt b/app/src/main/java/org/futo/circles/view/PreviewPostView.kt
index f46cff0cf..bb07937ba 100644
--- a/app/src/main/java/org/futo/circles/view/PreviewPostView.kt
+++ b/app/src/main/java/org/futo/circles/view/PreviewPostView.kt
@@ -124,19 +124,27 @@ class PreviewPostView(
     }
 
     fun insertEmoji(unicode: String) {
+        val selection = binding.etTextPost.selectionStart
         binding.etTextPost.append(unicode)
+        binding.etTextPost.setSelection(selection + unicode.length)
     }
 
     private fun insertMentionMark() {
         with(binding.etTextPost) {
-            val previousChar = text?.getOrNull(selectionStart - 1)
+            val selection = selectionStart
+            val previousChar = text?.getOrNull(selection - 1)
             val noNeedToAddSpace = previousChar?.isWhitespace() == true || previousChar == null
-            append(if (noNeedToAddSpace) "@" else " @")
+            val textToInsert = if (noNeedToAddSpace) "@" else " @"
+            append(textToInsert)
+            setSelection(selection + textToInsert.length)
         }
     }
 
     fun insertLink(title: String?, link: String) {
-        binding.etTextPost.insertLink(link, title ?: link)
+        val selection = binding.etTextPost.selectionStart
+        val text = title ?: link
+        binding.etTextPost.insertLink(link, text)
+        binding.etTextPost.setSelection(selection + text.length)
     }
 
     fun setMediaFromExistingPost(mediaContent: MediaContent) {
-- 
GitLab