ReplyDescription.swift (2048B)
1 // 2 // ReplyDescription.swift 3 // damus 4 // 5 // Created by William Casarin on 2023-01-23. 6 // 7 8 import SwiftUI 9 10 // jb55 - TODO: this could be a lot better 11 struct ReplyDescription: View { 12 let event: NostrEvent 13 let profiles: Profiles 14 15 var body: some View { 16 Text(verbatim: "\(reply_desc(profiles: profiles, event: event))") 17 .font(.footnote) 18 .foregroundColor(.gray) 19 .frame(maxWidth: .infinity, alignment: .leading) 20 } 21 } 22 23 struct ReplyDescription_Previews: PreviewProvider { 24 static var previews: some View { 25 ReplyDescription(event: test_event, profiles: test_damus_state().profiles) 26 } 27 } 28 29 func reply_desc(profiles: Profiles, event: NostrEvent, locale: Locale = Locale.current) -> String { 30 let desc = make_reply_description(event.tags) 31 let pubkeys = desc.pubkeys 32 let n = desc.others 33 34 let bundle = bundleForLocale(locale: locale) 35 36 if desc.pubkeys.count == 0 { 37 return NSLocalizedString("Replying to self", bundle: bundle, comment: "Label to indicate that the user is replying to themself.") 38 } 39 40 let names: [String] = pubkeys.map { 41 let prof = profiles.lookup(id: $0) 42 return Profile.displayName(profile: prof, pubkey: $0).username.truncate(maxLength: 50) 43 } 44 45 let uniqueNames = NSOrderedSet(array: names).array as! [String] 46 47 if uniqueNames.count > 1 { 48 let othersCount = n - pubkeys.count 49 if othersCount == 0 { 50 return String(format: NSLocalizedString("Replying to %@ & %@", bundle: bundle, comment: "Label to indicate that the user is replying to 2 users."), locale: locale, uniqueNames[0], uniqueNames[1]) 51 } else { 52 return String(format: localizedStringFormat(key: "replying_to_two_and_others", locale: locale), locale: locale, othersCount, uniqueNames[0], uniqueNames[1]) 53 } 54 } 55 56 return String(format: NSLocalizedString("Replying to %@", bundle: bundle, comment: "Label to indicate that the user is replying to 1 user."), locale: locale, uniqueNames[0]) 57 } 58 59