Skip to content

Commit 5f1f108

Browse files
committed
Match system behavior for nicknames
1 parent 880cd2c commit 5f1f108

File tree

1 file changed

+10
-16
lines changed

1 file changed

+10
-16
lines changed

SignalServiceKit/Contacts/SignalAccount.swift

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,6 @@ import GRDB
77
import LibSignalClient
88
import SignalCoreKit
99

10-
/// We need to query the system preferences to achieve the behaviour at Messages on iOS.
11-
///
12-
/// If we ask NSPersonNameComponentsFormatter for "short" we will get the nickname if it
13-
/// exists but if it _doesn't_ exit we'll just get the first name. (Or the name pattern
14-
/// the user has selected in their system preferences. This means that in the conversation
15-
/// list in the left, where Messages displays the full name of a contact if they don't
16-
/// have a nickname, we'd just display the Short Name. To match the behaviour we ask
17-
/// UserDefaults for the value of this key and prefer to use the nickname, if available,
18-
/// in the conversation list.
19-
private let kSignalPreferNicknamesPreference = "NSPersonNameDefaultShouldPreferNicknamesPreference"
20-
2110
@objc(SignalAccount)
2211
public final class SignalAccount: NSObject, SDSCodableModel, Decodable {
2312
public static let databaseTableName = "model_SignalAccount"
@@ -271,11 +260,16 @@ extension SignalAccount {
271260
return components
272261
}
273262

274-
/// Whether nicknames should be used.
275-
///
276-
/// - SeeAlso: docs on``kSignalPreferNicknamesPreference``.
277-
public static func shouldUseNicknames(userDefaults: UserDefaults = .standard) -> Bool {
278-
userDefaults.bool(forKey: kSignalPreferNicknamesPreference)
263+
/// If we ask PersonNameComponentsFormatter for `.short`, we will get the
264+
/// nickname (if it exists). To match the system behavior for the chat list,
265+
/// we use the nickname in lieu of the full name as well.
266+
public static func shouldUseNicknames() -> Bool {
267+
var nameComponents = PersonNameComponents()
268+
nameComponents.givenName = "givenName"
269+
nameComponents.nickname = "nickname"
270+
let nameFormatter = PersonNameComponentsFormatter()
271+
nameFormatter.style = .short
272+
return nameFormatter.string(from: nameComponents) == "nickname"
279273
}
280274
}
281275

0 commit comments

Comments
 (0)