From 417c116ce00342c8b5da746e8d757ac93c318a36 Mon Sep 17 00:00:00 2001 From: maltaisn Date: Fri, 30 Dec 2022 17:34:30 -0500 Subject: [PATCH] Bullet text watcher fixes & enhancement - Fix auto bullet not working with auto-suggesting keyboards: they underline the last word, and on enter the text changes includes the whole word because the underline is removed, instead of just the newline character as previously thought. - Allow bullet indentation. --- CHANGELOG.md | 1 + .../notes/ui/edit/BulletTextWatcher.kt | 36 ++++++++----------- 2 files changed, 16 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 695dc9a2..c5460fee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ - Change default focus to title when creating a new note. - Fix animated swipe icons not working on API <23. - Allow zero lines in note preview (#83). +- Fix auto bullet feature not working with auto-suggesting keyboards. ## v1.4.5 (2022-09-25) - Fixed cursor moving to first line when scrolling (#60, thanks to @nhoeher) diff --git a/app/src/main/kotlin/com/maltaisn/notes/ui/edit/BulletTextWatcher.kt b/app/src/main/kotlin/com/maltaisn/notes/ui/edit/BulletTextWatcher.kt index d41a56ff..67c8637c 100644 --- a/app/src/main/kotlin/com/maltaisn/notes/ui/edit/BulletTextWatcher.kt +++ b/app/src/main/kotlin/com/maltaisn/notes/ui/edit/BulletTextWatcher.kt @@ -1,5 +1,5 @@ /* - * Copyright 2020 Nicolas Maltais + * Copyright 2022 Nicolas Maltais * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -30,7 +30,8 @@ class BulletTextWatcher : TextWatcher { override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) = Unit override fun onTextChanged(text: CharSequence?, start: Int, before: Int, count: Int) { - if (text != null && count == 1 && text[start] == '\n') { + val end = start + count - 1 + if (text != null && count - before == 1 && text[end] == '\n') { // User inserted a single char, a line break. // Find start position of current line. var pos = start - 1 @@ -43,27 +44,16 @@ class BulletTextWatcher : TextWatcher { val lineStart = pos + 1 // Check if current line used a list bullet. - val bullet = text[lineStart] - if (bullet in Note.BULLET_CHARS) { - // Get the bullet text, i.e the bullet with all following whitespaces. - val bulletText = buildString { - append(bullet) - pos = lineStart + 1 - while (pos < start) { - val c = text[pos] - if (!c.isWhitespace()) { - break - } - append(text[pos]) - pos++ - } - } - bulletChange = if (pos == start) { - // Last item was blank, create no new bullet and delete current one. - { it.delete(lineStart, start + 1) } + val lineText = text.substring(lineStart, end) + val bulletMatch = BULLET_REGEX.find(lineText) + if (bulletMatch != null) { + val bulletText = bulletMatch.value + bulletChange = if (lineStart + bulletText.length == end) { + // Last item was blank, create no new bullet and delete current one (also delete newline). + { it.delete(lineStart, end + 1) } } else { // Last item wasn't blank, insert bullet text on new line. - { it.insert(start + 1, bulletText, 0, bulletText.length) } + { it.insert(end + 1, bulletText, 0, bulletText.length) } } } } @@ -76,4 +66,8 @@ class BulletTextWatcher : TextWatcher { bulletChange = null change?.invoke(text ?: return) } + + companion object { + val BULLET_REGEX = """\s*[${Note.BULLET_CHARS}]\s*""".toRegex() + } }