Add option to strip custom emoji from display names

This commit is contained in:
Shadowfacts 2018-08-28 21:18:58 -04:00
parent 310ebe588e
commit 7fda5ae9fe
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
11 changed files with 104 additions and 11 deletions

View File

@ -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:")

View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='5.0' target-platform='ios' executeOnSourceChanges='false'>
<timeline fileName='timeline.xctimeline'/>
</playground>

View File

@ -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 = "<group>"; };
D663626721360E2C00C9CBA2 /* PreferencesTableViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PreferencesTableViewController.swift; sourceTree = "<group>"; };
D66362692136163000C9CBA2 /* PreferencesAdaptive.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PreferencesAdaptive.swift; sourceTree = "<group>"; };
D663626B21361C6700C9CBA2 /* Account+Preferences.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Account+Preferences.swift"; sourceTree = "<group>"; };
D663626D213629B300C9CBA2 /* MyPlayground.playground */ = {isa = PBXFileReference; lastKnownFileType = file.playground; path = MyPlayground.playground; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.swift; };
D667E5E02134937B0057A976 /* StatusTableViewCell.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = StatusTableViewCell.xib; sourceTree = "<group>"; };
D667E5E2213499F70057A976 /* Profile.storyboard */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; path = Profile.storyboard; sourceTree = "<group>"; };
D667E5E621349D4C0057A976 /* ProfileTableViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProfileTableViewController.swift; sourceTree = "<group>"; };
@ -166,6 +169,7 @@
children = (
D667E5F02134D5050057A976 /* UIViewController+StatusTableViewCellDelegate.swift */,
D667E5F72135C3040057A976 /* Mastodon+Equatable.swift */,
D663626B21361C6700C9CBA2 /* Account+Preferences.swift */,
);
path = Extensions;
sourceTree = "<group>";
@ -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 */,

View File

@ -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: "")
}
}

View File

@ -34,4 +34,6 @@ class Preferences: Codable {
var avatarStyle = AvatarStyle.roundRect
var hideCustomEmojiInUsernames = false
}

View File

@ -47,12 +47,8 @@
</constraints>
</tableViewCellContentView>
</tableViewCell>
</cells>
</tableViewSection>
<tableViewSection id="RyJ-Y2-ypg">
<cells>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" preservesSuperviewLayoutMargins="YES" selectionStyle="default" indentationWidth="10" id="CFN-UV-Jyn">
<rect key="frame" x="0.0" y="115" width="375" height="44"/>
<rect key="frame" x="0.0" y="79" width="375" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" preservesSuperviewLayoutMargins="YES" insetsLayoutMarginsFromSafeArea="NO" tableViewCell="CFN-UV-Jyn" id="dcx-Vr-nHa">
<rect key="frame" x="0.0" y="0.0" width="375" height="43.5"/>
@ -79,6 +75,34 @@
</constraints>
</tableViewCellContentView>
</tableViewCell>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" preservesSuperviewLayoutMargins="YES" selectionStyle="default" indentationWidth="10" id="Kaj-yB-gMg">
<rect key="frame" x="0.0" y="123" width="375" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" preservesSuperviewLayoutMargins="YES" insetsLayoutMarginsFromSafeArea="NO" tableViewCell="Kaj-yB-gMg" id="oE9-YZ-9kb">
<rect key="frame" x="0.0" y="0.0" width="375" height="43.5"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Hide Custom Emoji in Usernames" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="4bl-xs-4DS">
<rect key="frame" x="16" y="11.5" width="255" height="21"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
<switch opaque="NO" contentMode="scaleToFill" horizontalHuggingPriority="750" verticalHuggingPriority="750" contentHorizontalAlignment="center" contentVerticalAlignment="center" on="YES" translatesAutoresizingMaskIntoConstraints="NO" id="vWi-fm-tjV">
<rect key="frame" x="310" y="6.5" width="51" height="31"/>
<connections>
<action selector="hideCustomEmojiInUsernamesChanged:" destination="ArB-e7-yko" eventType="valueChanged" id="1Tm-o6-ssu"/>
</connections>
</switch>
</subviews>
<constraints>
<constraint firstItem="vWi-fm-tjV" firstAttribute="centerY" secondItem="oE9-YZ-9kb" secondAttribute="centerY" id="64b-0G-5hd"/>
<constraint firstItem="4bl-xs-4DS" firstAttribute="centerY" secondItem="oE9-YZ-9kb" secondAttribute="centerY" id="9ws-m8-Hf6"/>
<constraint firstItem="4bl-xs-4DS" firstAttribute="leading" secondItem="oE9-YZ-9kb" secondAttribute="leadingMargin" id="nTB-KX-h8P"/>
<constraint firstAttribute="trailing" secondItem="vWi-fm-tjV" secondAttribute="trailing" constant="16" id="xM8-1M-HWj"/>
</constraints>
</tableViewCellContentView>
</tableViewCell>
</cells>
</tableViewSection>
</sections>
@ -90,6 +114,7 @@
<navigationItem key="navigationItem" title="Preferences" id="dN7-yl-voz"/>
<connections>
<outlet property="circularAvatarsSwitch" destination="EGF-Yh-zaU" id="jAS-4F-WwG"/>
<outlet property="hideCustomEmojiInUsernamesSwitch" destination="vWi-fm-tjV" id="8zA-bD-SE3"/>
<outlet property="showRepliesInProfilesSwitch" destination="iCG-F0-6ZK" id="p5E-oK-ZYl"/>
</connections>
</tableViewController>

View File

@ -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
}
}

View File

@ -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
}
/*

View File

@ -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) {

View File

@ -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

View File

@ -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) {