From 7fec53f5d079f326c3a7aedfb3223bbf1ea914fb Mon Sep 17 00:00:00 2001 From: Jake-B Date: Fri, 28 Feb 2025 08:16:56 -0500 Subject: [PATCH] Filter variation selectors from shortName field --- Meshtastic/Extensions/String.swift | 11 +++++++++++ Meshtastic/Views/Settings/UserConfig.swift | 9 +++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/Meshtastic/Extensions/String.swift b/Meshtastic/Extensions/String.swift index b97ad1c5..c7c6385b 100644 --- a/Meshtastic/Extensions/String.swift +++ b/Meshtastic/Extensions/String.swift @@ -90,4 +90,15 @@ extension String { let end = index(start, offsetBy: range.upperBound - range.lowerBound) return String(self[start ..< end]) } + + // Filter out variation selectors from the string + var withoutVariationSelectors: String { + return self.unicodeScalars + .filter { scalar in + return !scalar.properties.isVariationSelector + } + .compactMap { UnicodeScalar($0) } + .map { String($0) } + .joined() + } } diff --git a/Meshtastic/Views/Settings/UserConfig.swift b/Meshtastic/Views/Settings/UserConfig.swift index 5b388ca7..ea64e36e 100644 --- a/Meshtastic/Views/Settings/UserConfig.swift +++ b/Meshtastic/Views/Settings/UserConfig.swift @@ -75,11 +75,16 @@ struct UserConfig: View { TextField("Short Name", text: $shortName) .foregroundColor(.gray) .onChange(of: shortName) { - var totalBytes = shortName.utf8.count + let newValue = shortName.withoutVariationSelectors + let totalBytes = newValue.utf8.count // Only mess with the value if it is too big if totalBytes > 4 { + // If too long, drop the last thing entered shortName = String(shortName.dropLast()) - totalBytes = shortName.utf8.count + } else if shortName != newValue { + // If not too long, make sure the stripped + // variant is placed back in text field if necessary + shortName = newValue } } .foregroundColor(.gray)