@@ -62,77 +62,59 @@ import SwiftyAttributes
6262 self . removeTimer? . invalidate ( )
6363 }
6464
65- private func getUsersTypingString( ) -> NSAttributedString {
66- // Array keep the order of the elements, no need to sort here manually
67- if self . typingUsers. count == 1 {
68- // Alice
69- return self . typingUsers [ 0 ] . displayName. withTextColor ( . secondaryLabel)
70-
71- } else {
72- let separator = " , " . withTextColor ( . tertiaryLabel)
73- let separatorSpace = NSAttributedString ( string: " " )
74- let separatorLast = NSLocalizedString ( " and " , comment: " Alice and Bob " ) . withTextColor ( . tertiaryLabel)
75-
76- if self . typingUsers. count == 2 {
77- // Alice and Bob
78- let user1 = self . typingUsers [ 0 ] . displayName. withTextColor ( . secondaryLabel)
79- let user2 = self . typingUsers [ 1 ] . displayName. withTextColor ( . secondaryLabel)
80-
81- return user1 + separatorSpace + separatorLast + separatorSpace + user2
82-
83- } else if self . typingUsers. count == 3 {
84- // Alice, Bob and Charlie
85- let user1 = self . typingUsers [ 0 ] . displayName. withTextColor ( . secondaryLabel)
86- let user2 = self . typingUsers [ 1 ] . displayName. withTextColor ( . secondaryLabel)
87- let user3 = self . typingUsers [ 2 ] . displayName. withTextColor ( . secondaryLabel)
88-
89- return user1 + separator + user2 + separatorSpace + separatorLast + separatorSpace + user3
90-
91- } else {
92- // Alice, Bob, Charlie
93- let user1 = self . typingUsers [ 0 ] . displayName. withTextColor ( . secondaryLabel)
94- let user2 = self . typingUsers [ 1 ] . displayName. withTextColor ( . secondaryLabel)
95- let user3 = self . typingUsers [ 2 ] . displayName. withTextColor ( . secondaryLabel)
96-
97- return user1 + separator + user2 + separator + user3
98- }
99- }
100- }
101-
102- private func updateTypingIndicator( ) {
65+ internal func updateTypingIndicator( ) {
10366 if self . typingUsers. isEmpty {
10467 // Just hide the label to have a nice animation. Otherwise we would animate an empty label/space
10568 self . isVisible = false
10669 } else {
107- let attributedSpace = NSAttributedString ( string: " " )
108- var localizedSuffix : NSAttributedString
70+ var localizedAttributedString : NSAttributedString ?
10971
11072 if self . typingUsers. count == 1 {
111- localizedSuffix = NSLocalizedString ( " is typing… " , comment: " Alice is typing… " ) . withTextColor ( . tertiaryLabel)
73+ let unformattedAttributedString = NSLocalizedString ( " %@ is typing… " , comment: " Alice is typing… " ) . withTextColor ( . tertiaryLabel)
74+ localizedAttributedString = NSAttributedString ( format: unformattedAttributedString,
75+ self . typingUsers [ 0 ] . displayName. withTextColor ( . secondaryLabel) )
11276
113- } else if self . typingUsers. count == 2 || self . typingUsers. count == 3 {
114- localizedSuffix = NSLocalizedString ( " are typing… " , comment: " Alice and Bob are typing… " ) . withTextColor ( . tertiaryLabel)
77+ } else if self . typingUsers. count == 2 {
78+ let unformattedAttributedString = NSLocalizedString ( " %1$@ and %2$@ are typing… " , comment: " Alice and Bob are typing… " ) . withTextColor ( . tertiaryLabel)
79+ localizedAttributedString = NSAttributedString ( format: unformattedAttributedString,
80+ self . typingUsers [ 0 ] . displayName. withTextColor ( . secondaryLabel) ,
81+ self . typingUsers [ 1 ] . displayName. withTextColor ( . secondaryLabel) )
11582
116- } else if self . typingUsers. count == 4 {
117- localizedSuffix = NSLocalizedString ( " and 1 other is typing… " , comment: " Alice, Bob, Charlie and 1 other is typing… " ) . withTextColor ( . tertiaryLabel)
83+ } else if self . typingUsers. count == 3 {
84+ let unformattedAttributedString = NSLocalizedString ( " %1$@, %2$@ and %3$@ are typing… " , comment: " Alice, Bob and Charlie are typing… " ) . withTextColor ( . tertiaryLabel)
85+ localizedAttributedString = NSAttributedString ( format: unformattedAttributedString,
86+ self . typingUsers [ 0 ] . displayName. withTextColor ( . secondaryLabel) ,
87+ self . typingUsers [ 1 ] . displayName. withTextColor ( . secondaryLabel) ,
88+ self . typingUsers [ 2 ] . displayName. withTextColor ( . secondaryLabel) )
11889
90+ } else if self . typingUsers. count == 4 {
91+ let unformattedAttributedString = NSLocalizedString ( " %1$@, %2$@, %3$@ and 1 other is typing… " , comment: " Alice, Bob, Charlie and 1 other is typing… " ) . withTextColor ( . tertiaryLabel)
92+ localizedAttributedString = NSAttributedString ( format: unformattedAttributedString,
93+ self . typingUsers [ 0 ] . displayName. withTextColor ( . secondaryLabel) ,
94+ self . typingUsers [ 1 ] . displayName. withTextColor ( . secondaryLabel) ,
95+ self . typingUsers [ 2 ] . displayName. withTextColor ( . secondaryLabel) )
11996 } else {
120- let localizedString = NSLocalizedString ( " and %ld others are typing… " , comment: " Alice, Bob, Charlie and 3 others are typing… " )
121- let formattedString = String ( format: localizedString, self . typingUsers. count - 3 )
122- localizedSuffix = formattedString. withTextColor ( . tertiaryLabel)
97+ let othersCount = self . typingUsers. count - 3
98+ let unformattedAttributedString = NSLocalizedString ( " %1$@, %2$@, %3$@ and %4$@ others are typing… " , comment: " Alice, Bob, Charlie and 3 others are typing… " ) . withTextColor ( . tertiaryLabel)
99+ localizedAttributedString = NSAttributedString ( format: unformattedAttributedString,
100+ self . typingUsers [ 0 ] . displayName. withTextColor ( . secondaryLabel) ,
101+ self . typingUsers [ 1 ] . displayName. withTextColor ( . secondaryLabel) ,
102+ self . typingUsers [ 2 ] . displayName. withTextColor ( . secondaryLabel) ,
103+ othersCount)
123104 }
124105
125- UIView . transition ( with: self . typingLabel,
126- duration: 0.2 ,
127- options: . transitionCrossDissolve,
128- animations: {
129-
130- let newTypingText = self . getUsersTypingString ( ) + attributedSpace + localizedSuffix
131-
132- self . typingLabel. attributedText = newTypingText. withFont ( . preferredFont( forTextStyle: . body) )
133- } , completion: nil )
106+ if let localizedAttributedString {
107+ UIView . transition ( with: self . typingLabel,
108+ duration: 0.2 ,
109+ options: . transitionCrossDissolve,
110+ animations: {
111+ self . typingLabel. attributedText = localizedAttributedString. withFont ( . preferredFont( forTextStyle: . body) )
112+ } , completion: nil )
134113
135- self . isVisible = true
114+ self . isVisible = true
115+ } else {
116+ self . isVisible = false
117+ }
136118 }
137119
138120 self . previousUpdateTimestamp = Date ( ) . timeIntervalSinceReferenceDate
0 commit comments