From 96f5ea8af19a1842468b9b4046c7a71b5713ad4a Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Thu, 6 Feb 2025 13:33:42 -0500 Subject: [PATCH] Put compose text view attributed string behavior behind feature flag --- .../ComposeUI/Views/NewMainTextView.swift | 25 +++++++++++++------ .../Supporting Types/FeatureFlag.swift | 1 + 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/Packages/ComposeUI/Sources/ComposeUI/Views/NewMainTextView.swift b/Packages/ComposeUI/Sources/ComposeUI/Views/NewMainTextView.swift index 80a9be5e..c1efc81b 100644 --- a/Packages/ComposeUI/Sources/ComposeUI/Views/NewMainTextView.swift +++ b/Packages/ComposeUI/Sources/ComposeUI/Views/NewMainTextView.swift @@ -139,31 +139,40 @@ private final class WrappedTextViewCoordinator: NSObject { attributedText.string.replacingOccurrences(of: "\u{FFFC}", with: "") } + @MainActor private func attributedTextFromPlain(_ text: String) -> NSAttributedString { let str = NSMutableAttributedString(string: text) str.addAttributes([ .foregroundColor: UIColor.label, .font: UIFontMetrics.default.scaledFont(for: .systemFont(ofSize: 20)), ], range: NSRange(location: 0, length: str.length)) - let mentionMatches = CharacterCounter.mention.matches(in: text, range: NSRange(location: 0, length: str.length)) - for match in mentionMatches.reversed() { - str.insert(NSAttributedString(attachment: Self.attachment), at: match.range.location) - let range = NSRange(location: match.range.location, length: match.range.length + 1) - str.addAttributes([ - .mention: true, - .foregroundColor: UIColor.tintColor, - ], range: range) + if Preferences.shared.hasFeatureFlag(.composeTextAttributes) { + let mentionMatches = CharacterCounter.mention.matches(in: text, range: NSRange(location: 0, length: str.length)) + for match in mentionMatches.reversed() { + str.insert(NSAttributedString(attachment: Self.attachment), at: match.range.location) + let range = NSRange(location: match.range.location, length: match.range.length + 1) + str.addAttributes([ + .mention: true, + .foregroundColor: UIColor.tintColor, + ], range: range) + } } return str } + @MainActor func updateTextViewTextIfNecessary(_ text: String, textView: UITextView) { if text != plainTextFromAttributed(textView.attributedText) { textView.attributedText = attributedTextFromPlain(text) } } + @MainActor private func updateAttributes(in textView: UITextView) { + guard Preferences.shared.hasFeatureFlag(.composeTextAttributes) else { + return + } + let str = NSMutableAttributedString(attributedString: textView.attributedText!) var changed = false var cursorOffset = 0 diff --git a/Packages/TuskerPreferences/Sources/TuskerPreferences/Supporting Types/FeatureFlag.swift b/Packages/TuskerPreferences/Sources/TuskerPreferences/Supporting Types/FeatureFlag.swift index fd7845d5..51e32fcd 100644 --- a/Packages/TuskerPreferences/Sources/TuskerPreferences/Supporting Types/FeatureFlag.swift +++ b/Packages/TuskerPreferences/Sources/TuskerPreferences/Supporting Types/FeatureFlag.swift @@ -10,5 +10,6 @@ import Foundation public enum FeatureFlag: String, Codable { case iPadBrowserNavigation = "ipad-browser-navigation" case composeRewrite = "compose-rewrite" + case composeTextAttributes = "compose-text-attributes" case pushNotifCustomEmoji = "push-notif-custom-emoji" }