Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,7 @@ fun TextExpanderScreen(onClickBack: () -> Unit) {
mutableStateOf(TextExpanderUtils.getShortcuts(context))
}
// the stored order is the order of adding, so OLDEST is the list as before
var sortOrder by remember {
mutableStateOf(
UserDictSortOrder.entries.firstOrNull { it.name == context.prefs().getString(TextExpanderUtils.PREF_SORT_ORDER, null) }
?: UserDictSortOrder.OLDEST
)
}
var sortOrder by remember { mutableStateOf(readShortcutSortOrder(context)) }
var showSortDialog by remember { mutableStateOf(false) }
val sortedShortcuts = remember(shortcutsMap, sortOrder) { sortShortcuts(shortcutsMap, sortOrder) }

Expand Down Expand Up @@ -821,6 +816,20 @@ internal fun <V> sortShortcuts(shortcuts: Map<String, V>, order: UserDictSortOrd
UserDictSortOrder.OLDEST -> shortcuts.entries.toList()
}

/** The order chosen for the shortcut list and the bulk editor; OLDEST, the order of adding, by default */
internal fun readShortcutSortOrder(context: android.content.Context): UserDictSortOrder =
UserDictSortOrder.entries.firstOrNull { it.name == context.prefs().getString(TextExpanderUtils.PREF_SORT_ORDER, null) }
?: UserDictSortOrder.OLDEST

/**
* [edited] in the stored order of [stored]: kept shortcuts keep their place and new ones come last, so saving
* from a sorted bulk editor keeps the order of adding (for Oldest and Last added).
*/
internal fun <V> inStoredOrder(stored: Map<String, V>, edited: Map<String, V>): Map<String, V> = LinkedHashMap<String, V>().apply {
stored.keys.forEach { key -> edited[key]?.let { put(key, it) } }
edited.forEach { (key, value) -> if (key !in this) put(key, value) }
}

/** Placeholder tags, a tap inserts the tag at the cursor */
@Composable
internal fun PlaceholderTagsRow(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,11 @@ fun TextExpanderTextEditor(onClose: (Boolean) -> Unit) {
val scroll = rememberScrollState()

var current by remember { mutableStateOf(TextExpanderUtils.getShortcuts(context)) }
var original by remember { mutableStateOf(TextExpanderText.format(current)) }
// shown in the order chosen for the shortcut list (#962), saved in the stored order, see inStoredOrder
fun formatSorted(shortcuts: Map<String, TextExpanderUtils.ShortcutEntry>) = TextExpanderText.format(
sortShortcuts(shortcuts, readShortcutSortOrder(context)).associateTo(LinkedHashMap()) { it.key to it.value }
)
var original by remember { mutableStateOf(formatSorted(current)) }
var value by remember { mutableStateOf(TextFieldValue(original)) }
var layout by remember { mutableStateOf<TextLayoutResult?>(null) }
var changedSomething by remember { mutableStateOf(false) }
Expand All @@ -111,7 +115,7 @@ fun TextExpanderTextEditor(onClose: (Boolean) -> Unit) {
fun replaceText(text: String) { value = TextFieldValue(text.replace("\r\n", "\n")) }
fun reload() {
current = TextExpanderUtils.getShortcuts(context)
original = TextExpanderText.format(current)
original = formatSorted(current)
value = TextFieldValue(original)
hasPrevious = prefs.contains(TextExpanderUtils.PREF_DATA_PREVIOUS)
}
Expand Down Expand Up @@ -333,7 +337,7 @@ fun TextExpanderTextEditor(onClose: (Boolean) -> Unit) {
prefs.edit(commit = true) {
putString(TextExpanderUtils.PREF_DATA_PREVIOUS, prefs.getString(TextExpanderUtils.PREF_DATA, null) ?: "{}")
}
TextExpanderUtils.saveShortcuts(context, result.entries)
TextExpanderUtils.saveShortcuts(context, inStoredOrder(current, result.entries))
changedSomething = true
pending = null
reload()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package helium314.keyboard.settings

import helium314.keyboard.latin.utils.TextExpanderUtils
import helium314.keyboard.settings.screens.UserDictSortOrder
import helium314.keyboard.settings.screens.inStoredOrder
import helium314.keyboard.settings.screens.sortShortcuts
import org.junit.Assert.assertEquals
import org.junit.Test
Expand All @@ -19,6 +20,14 @@ class TextExpanderSortTest {

@Test fun `last added reverses it`() = assertEquals(listOf(5, 4, 3, 2, 1), order(UserDictSortOrder.LAST_ADDED))

@Test fun `saving a sorted bulk editor keeps the order of adding`() {
val stored = linkedMapOf("brb" to 1, "ack" to 2, "cya" to 3)
// edited in alphabetical order: "ack" changed, "brb" removed, "zz" and "aa" added
val edited = linkedMapOf("aa" to 9, "ack" to 20, "cya" to 3, "zz" to 8)
val saved = inStoredOrder(stored, edited)
assertEquals(listOf("ack" to 20, "cya" to 3, "aa" to 9, "zz" to 8), saved.toList())
}

@Test fun `alphabetical ignores case and sorts regex shortcuts by their label`() =
// ";addr" < "ack" < "brb" < "cya" < "regex: \d+eur"
assertEquals(listOf(2, 5, 1, 4, 3), order(UserDictSortOrder.ALPHABETICAL))
Expand Down
Loading