damus

nostr ios client
git clone git://jb55.com/damus
Log | Files | Refs | README | LICENSE

ReplyDescription.swift (2251B)


      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 replying_to: NostrEvent?
     14     let ndb: Ndb
     15 
     16     var body: some View {
     17         Text(verbatim: "\(reply_desc(ndb: ndb, event: event, replying_to: replying_to))")
     18             .font(.footnote)
     19             .foregroundColor(.gray)
     20             .frame(maxWidth: .infinity, alignment: .leading)
     21     }
     22 }
     23 
     24 struct ReplyDescription_Previews: PreviewProvider {
     25     static var previews: some View {
     26         ReplyDescription(event: test_note, replying_to: test_note, ndb: test_damus_state.ndb)
     27     }
     28 }
     29 
     30 func reply_desc(ndb: Ndb, event: NostrEvent, replying_to: NostrEvent?, locale: Locale = Locale.current) -> String {
     31     let desc = make_reply_description(event, replying_to: replying_to)
     32     let pubkeys = desc.pubkeys
     33     let n = desc.others
     34 
     35     let bundle = bundleForLocale(locale: locale)
     36 
     37     if desc.pubkeys.count == 0 {
     38         return NSLocalizedString("Replying to self", bundle: bundle, comment: "Label to indicate that the user is replying to themself.")
     39     }
     40 
     41     guard let profile_txn = NdbTxn(ndb: ndb) else  {
     42         return ""
     43     }
     44 
     45     let names: [String] = pubkeys.map { pk in
     46         let prof = ndb.lookup_profile_with_txn(pk, txn: profile_txn)
     47 
     48         return Profile.displayName(profile: prof?.profile, pubkey: pk).username.truncate(maxLength: 50)
     49     }
     50     
     51     let uniqueNames = NSOrderedSet(array: names).array as! [String]
     52 
     53     if uniqueNames.count > 1 {
     54         let othersCount = n - pubkeys.count
     55         if othersCount <= 0 {
     56             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])
     57         } else {
     58             return String(format: localizedStringFormat(key: "replying_to_two_and_others", locale: locale), locale: locale, othersCount, uniqueNames[0], uniqueNames[1])
     59         }
     60     }
     61 
     62     return String(format: NSLocalizedString("Replying to %@", bundle: bundle, comment: "Label to indicate that the user is replying to 1 user."), locale: locale, uniqueNames[0])
     63 }
     64 
     65