mirror of
https://github.com/oxen-io/session-android.git
synced 2024-12-24 16:57:50 +00:00
Merge pull request #1680 from oxen-io/fix-mention-crash
Fix crashes when removing spaces in mention texts
This commit is contained in:
commit
439ec900f4
@ -202,13 +202,17 @@ class MentionViewModel(
|
||||
val sb = StringBuilder()
|
||||
var offset = 0
|
||||
for ((span, range) in spansWithRanges) {
|
||||
// Add content before the mention span
|
||||
sb.append(editable, offset, range.first)
|
||||
// Add content before the mention span. There's a possibility of overlapping spans so we need to
|
||||
// safe guard the start offset here to not go over our span's start.
|
||||
val thisMentionStart = range.first
|
||||
val lastMentionEnd = offset.coerceAtMost(thisMentionStart)
|
||||
sb.append(editable, lastMentionEnd, thisMentionStart)
|
||||
|
||||
// Replace the mention span with "@public key"
|
||||
sb.append('@').append(span.member.publicKey).append(' ')
|
||||
|
||||
offset = range.last + 1
|
||||
// Safe guard offset to not go over the end of the editable.
|
||||
offset = (range.last + 1).coerceAtMost(editable.length)
|
||||
}
|
||||
|
||||
// Add the remaining content
|
||||
|
@ -179,6 +179,11 @@ class MentionViewModelTest {
|
||||
// Should have normalised message with selected candidate
|
||||
assertThat(mentionViewModel.normalizeMessageBody())
|
||||
.isEqualTo("Hi @pubkey1 ")
|
||||
|
||||
// Should have correct normalised message even with the last space deleted
|
||||
editable.delete(editable.length - 1, editable.length)
|
||||
assertThat(mentionViewModel.normalizeMessageBody())
|
||||
.isEqualTo("Hi @pubkey1 ")
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user