From c814b1ccf288d06c3fc81285bbec401409361817 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Tue, 28 Aug 2018 21:18:58 -0400 Subject: [PATCH] Add option to strip custom emoji from display names --- MyPlayground.playground/Contents.swift | 14 ++++++++ MyPlayground.playground/contents.xcplayground | 4 +++ Tusker.xcodeproj/project.pbxproj | 6 ++++ Tusker/Extensions/Account+Preferences.swift | 29 +++++++++++++++ Tusker/Preferences/Preferences.swift | 2 ++ Tusker/Storyboards/Preferences.storyboard | 35 ++++++++++++++++--- .../PreferencesTableViewController.swift | 6 ++++ .../ProfileTableViewController.swift | 9 +++-- .../ConversationMainStatusTableViewCell.swift | 2 +- Tusker/Views/ProfileHeaderTableViewCell.swift | 2 +- Tusker/Views/StatusTableViewCell.swift | 6 ++-- 11 files changed, 104 insertions(+), 11 deletions(-) create mode 100644 MyPlayground.playground/Contents.swift create mode 100644 MyPlayground.playground/contents.xcplayground create mode 100644 Tusker/Extensions/Account+Preferences.swift diff --git a/MyPlayground.playground/Contents.swift b/MyPlayground.playground/Contents.swift new file mode 100644 index 00000000..37b43714 --- /dev/null +++ b/MyPlayground.playground/Contents.swift @@ -0,0 +1,14 @@ +import UIKit + +let regex = try! NSRegularExpression(pattern: ":[a-zA-Z0-9_]+:", options: []) + +func stripCustomEmoji(from string: String) -> String { + let range = NSRange(location: 0, length: string.utf16.count) + return regex.stringByReplacingMatches(in: string, options: [], range: range, withTemplate: "") +} + +let result = stripCustomEmoji(from: ":sparkles_nb: 🦑 :dizzy_trans:") + +stripCustomEmoji(from: ":dizzy_trans:") +stripCustomEmoji(from: " 🦑 :dizzy_trans:") +stripCustomEmoji(from: " :dizzy_trans:") diff --git a/MyPlayground.playground/contents.xcplayground b/MyPlayground.playground/contents.xcplayground new file mode 100644 index 00000000..9f5f2f40 --- /dev/null +++ b/MyPlayground.playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/Tusker.xcodeproj/project.pbxproj b/Tusker.xcodeproj/project.pbxproj index 71db6e2d..ee39bbfa 100644 --- a/Tusker.xcodeproj/project.pbxproj +++ b/Tusker.xcodeproj/project.pbxproj @@ -21,6 +21,7 @@ D663626621360DD700C9CBA2 /* Preferences.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = D663626521360DD700C9CBA2 /* Preferences.storyboard */; }; D663626821360E2C00C9CBA2 /* PreferencesTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D663626721360E2C00C9CBA2 /* PreferencesTableViewController.swift */; }; D663626A2136163000C9CBA2 /* PreferencesAdaptive.swift in Sources */ = {isa = PBXBuildFile; fileRef = D66362692136163000C9CBA2 /* PreferencesAdaptive.swift */; }; + D663626C21361C6700C9CBA2 /* Account+Preferences.swift in Sources */ = {isa = PBXBuildFile; fileRef = D663626B21361C6700C9CBA2 /* Account+Preferences.swift */; }; D667E5E12134937B0057A976 /* StatusTableViewCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = D667E5E02134937B0057A976 /* StatusTableViewCell.xib */; }; D667E5E3213499F70057A976 /* Profile.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = D667E5E2213499F70057A976 /* Profile.storyboard */; }; D667E5E721349D4C0057A976 /* ProfileTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D667E5E621349D4C0057A976 /* ProfileTableViewController.swift */; }; @@ -94,6 +95,8 @@ D663626521360DD700C9CBA2 /* Preferences.storyboard */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; path = Preferences.storyboard; sourceTree = ""; }; D663626721360E2C00C9CBA2 /* PreferencesTableViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PreferencesTableViewController.swift; sourceTree = ""; }; D66362692136163000C9CBA2 /* PreferencesAdaptive.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PreferencesAdaptive.swift; sourceTree = ""; }; + D663626B21361C6700C9CBA2 /* Account+Preferences.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Account+Preferences.swift"; sourceTree = ""; }; + D663626D213629B300C9CBA2 /* MyPlayground.playground */ = {isa = PBXFileReference; lastKnownFileType = file.playground; path = MyPlayground.playground; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.swift; }; D667E5E02134937B0057A976 /* StatusTableViewCell.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = StatusTableViewCell.xib; sourceTree = ""; }; D667E5E2213499F70057A976 /* Profile.storyboard */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; path = Profile.storyboard; sourceTree = ""; }; D667E5E621349D4C0057A976 /* ProfileTableViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProfileTableViewController.swift; sourceTree = ""; }; @@ -166,6 +169,7 @@ children = ( D667E5F02134D5050057A976 /* UIViewController+StatusTableViewCellDelegate.swift */, D667E5F72135C3040057A976 /* Mastodon+Equatable.swift */, + D663626B21361C6700C9CBA2 /* Account+Preferences.swift */, ); path = Extensions; sourceTree = ""; @@ -188,6 +192,7 @@ D6D4DDC3212518A000E1C4BB = { isa = PBXGroup; children = ( + D663626D213629B300C9CBA2 /* MyPlayground.playground */, D6BED16E212663DA00F02DA0 /* SwiftSoup.framework */, D6F953E6212519A400CF0F2B /* MastodonKit.framework */, D6D4DDCE212518A000E1C4BB /* Tusker */, @@ -429,6 +434,7 @@ D6BED174212667E900F02DA0 /* StatusTableViewCell.swift in Sources */, D64D0AAD2128D88B005A6F37 /* LocalData.swift in Sources */, D663626221360B1900C9CBA2 /* Preferences.swift in Sources */, + D663626C21361C6700C9CBA2 /* Account+Preferences.swift in Sources */, D667E5EF2134C39F0057A976 /* StatusContentLabel.swift in Sources */, D64D0AB12128D9AE005A6F37 /* OnboardingViewController.swift in Sources */, D663626821360E2C00C9CBA2 /* PreferencesTableViewController.swift in Sources */, diff --git a/Tusker/Extensions/Account+Preferences.swift b/Tusker/Extensions/Account+Preferences.swift new file mode 100644 index 00000000..69c533c0 --- /dev/null +++ b/Tusker/Extensions/Account+Preferences.swift @@ -0,0 +1,29 @@ +// +// Account+Preferences.swift +// Tusker +// +// Created by Shadowfacts on 8/28/18. +// Copyright © 2018 Shadowfacts. All rights reserved. +// + +import Foundation +import MastodonKit + +extension Account { + + var realDisplayName: String { + if Preferences.shared.hideCustomEmojiInUsernames { + return stripCustomEmoji(from: displayName) + } else { + return displayName + } + } + + private static let customEmojiRegex = try! NSRegularExpression(pattern: ":[a-zA-Z0-9_]+:", options: []) + + private func stripCustomEmoji(from string: String) -> String { + let range = NSRange(location: 0, length: string.utf16.count) + return Account.customEmojiRegex.stringByReplacingMatches(in: string, options: [], range: range, withTemplate: "") + } + +} diff --git a/Tusker/Preferences/Preferences.swift b/Tusker/Preferences/Preferences.swift index 65965d71..61fb1077 100644 --- a/Tusker/Preferences/Preferences.swift +++ b/Tusker/Preferences/Preferences.swift @@ -34,4 +34,6 @@ class Preferences: Codable { var avatarStyle = AvatarStyle.roundRect + var hideCustomEmojiInUsernames = false + } diff --git a/Tusker/Storyboards/Preferences.storyboard b/Tusker/Storyboards/Preferences.storyboard index 114459bc..90d41585 100644 --- a/Tusker/Storyboards/Preferences.storyboard +++ b/Tusker/Storyboards/Preferences.storyboard @@ -47,12 +47,8 @@ - - - - - + @@ -79,6 +75,34 @@ + + + + + + + + + + + + + + + + + + + + + + + @@ -90,6 +114,7 @@ + diff --git a/Tusker/View Controllers/PreferencesTableViewController.swift b/Tusker/View Controllers/PreferencesTableViewController.swift index dd8027fd..24e24d9f 100644 --- a/Tusker/View Controllers/PreferencesTableViewController.swift +++ b/Tusker/View Controllers/PreferencesTableViewController.swift @@ -17,12 +17,14 @@ class PreferencesTableViewController: UITableViewController { @IBOutlet weak var showRepliesInProfilesSwitch: UISwitch! @IBOutlet weak var circularAvatarsSwitch: UISwitch! + @IBOutlet weak var hideCustomEmojiInUsernamesSwitch: UISwitch! override func viewDidLoad() { super.viewDidLoad() showRepliesInProfilesSwitch.setOn(Preferences.shared.showRepliesInProfiles, animated: false) circularAvatarsSwitch.setOn(Preferences.shared.avatarStyle == .circle, animated: false) + hideCustomEmojiInUsernamesSwitch.setOn(Preferences.shared.hideCustomEmojiInUsernames, animated: false) } /* @@ -43,4 +45,8 @@ class PreferencesTableViewController: UITableViewController { Preferences.shared.avatarStyle = circularAvatarsSwitch.isOn ? .circle : .roundRect } + @IBAction func hideCustomEmojiInUsernamesChanged(_ sender: Any) { + Preferences.shared.hideCustomEmojiInUsernames = hideCustomEmojiInUsernamesSwitch.isOn + } + } diff --git a/Tusker/View Controllers/ProfileTableViewController.swift b/Tusker/View Controllers/ProfileTableViewController.swift index 7a098579..3208dd46 100644 --- a/Tusker/View Controllers/ProfileTableViewController.swift +++ b/Tusker/View Controllers/ProfileTableViewController.swift @@ -10,7 +10,7 @@ import UIKit import MastodonKit import SafariServices -class ProfileTableViewController: UITableViewController { +class ProfileTableViewController: UITableViewController, PreferencesAdaptive { static func create(for account: Account) -> UIViewController { guard let profileController = UIStoryboard(name: "Profile", bundle: nil).instantiateInitialViewController() as? ProfileTableViewController else { fatalError() } @@ -45,7 +45,7 @@ class ProfileTableViewController: UITableViewController { tableView.register(UINib(nibName: "StatusTableViewCell", bundle: nil), forCellReuseIdentifier: "statusCell") tableView.register(UINib(nibName: "ProfileHeaderTableViewCell", bundle: nil), forCellReuseIdentifier: "headerCell") - navigationItem.title = account.displayName + updateUIForPreferences() MastodonController.shared.client.run(request()) { result in guard case let .success(statuses, pagination) = result else { fatalError() } @@ -61,6 +61,11 @@ class ProfileTableViewController: UITableViewController { cell.updateUIForPreferences() } } + updateUIForPreferences() + } + + func updateUIForPreferences() { + navigationItem.title = account.realDisplayName } /* diff --git a/Tusker/Views/ConversationMainStatusTableViewCell.swift b/Tusker/Views/ConversationMainStatusTableViewCell.swift index 713e0f0c..f8d43985 100644 --- a/Tusker/Views/ConversationMainStatusTableViewCell.swift +++ b/Tusker/Views/ConversationMainStatusTableViewCell.swift @@ -35,6 +35,7 @@ class ConversationMainStatusTableViewCell: UITableViewCell, PreferencesAdaptive func updateUIForPreferences() { avatarImageView.layer.cornerRadius = Preferences.shared.avatarStyle.cornerRadius(for: avatarImageView) + displayNameLabel.text = account.realDisplayName } func updateUI(for status: Status) { @@ -50,7 +51,6 @@ class ConversationMainStatusTableViewCell: UITableViewCell, PreferencesAdaptive updateUIForPreferences() - displayNameLabel.text = account.displayName usernameLabel.text = "@\(account.acct)" avatarImageView.image = nil if let url = URL(string: account.avatar) { diff --git a/Tusker/Views/ProfileHeaderTableViewCell.swift b/Tusker/Views/ProfileHeaderTableViewCell.swift index 98c5ef05..fa41192e 100644 --- a/Tusker/Views/ProfileHeaderTableViewCell.swift +++ b/Tusker/Views/ProfileHeaderTableViewCell.swift @@ -40,6 +40,7 @@ class ProfileHeaderTableViewCell: UITableViewCell, PreferencesAdaptive { func updateUIForPreferences() { avatarContainerView.layer.cornerRadius = Preferences.shared.avatarStyle.cornerRadius(for: avatarContainerView) avatarImageView.layer.cornerRadius = Preferences.shared.avatarStyle.cornerRadius(for: avatarImageView) + displayNameLabel.text = account.realDisplayName } func updateUI(for account: Account) { @@ -47,7 +48,6 @@ class ProfileHeaderTableViewCell: UITableViewCell, PreferencesAdaptive { updateUIForPreferences() - displayNameLabel.text = account.displayName usernameLabel.text = "@\(account.acct)" avatarImageView.image = nil diff --git a/Tusker/Views/StatusTableViewCell.swift b/Tusker/Views/StatusTableViewCell.swift index 3b737498..48db7275 100644 --- a/Tusker/Views/StatusTableViewCell.swift +++ b/Tusker/Views/StatusTableViewCell.swift @@ -53,6 +53,10 @@ class StatusTableViewCell: UITableViewCell, PreferencesAdaptive { func updateUIForPreferences() { avatarImageView.layer.cornerRadius = Preferences.shared.avatarStyle.cornerRadius(for: avatarImageView) + if let reblogger = reblogger { + reblogLabel.text = "Reblogged by \(reblogger.realDisplayName)" + } + displayNameLabel.text = account.realDisplayName } func updateUI(for status: Status) { @@ -63,7 +67,6 @@ class StatusTableViewCell: UITableViewCell, PreferencesAdaptive { account = reblog.account reblogger = status.account reblogLabel.isHidden = false - reblogLabel.text = "Reblogged by \(reblogger!.displayName)" } else { account = status.account reblogLabel.isHidden = true @@ -74,7 +77,6 @@ class StatusTableViewCell: UITableViewCell, PreferencesAdaptive { updateUIForPreferences() - displayNameLabel.text = account.displayName usernameLabel.text = "@\(account.acct)" avatarImageView.image = nil if let url = URL(string: account.avatar) {