damus

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

Reposted.swift (2899B)


      1 //
      2 //  Reposted.swift
      3 //  damus
      4 //
      5 //  Created by William Casarin on 2023-01-11.
      6 //
      7 
      8 import SwiftUI
      9 
     10 struct Reposted: View {
     11     let damus: DamusState
     12     let pubkey: Pubkey
     13     let target: NostrEvent
     14     @State var reposts: Int
     15 
     16     init(damus: DamusState, pubkey: Pubkey, target: NostrEvent) {
     17         self.damus = damus
     18         self.pubkey = pubkey
     19         self.target = target
     20         self.reposts = damus.boosts.counts[target.id] ?? 1
     21     }
     22 
     23     var body: some View {
     24         HStack(alignment: .center) {
     25             Image("repost")
     26                 .foregroundColor(Color.gray)
     27 
     28             // Show profile picture of the reposter only if the reposter is not the author of the reposted note.
     29             if pubkey != target.pubkey {
     30                 ProfilePicView(pubkey: pubkey, size: eventview_pfp_size(.small), highlight: .none, profiles: damus.profiles, disable_animation: damus.settings.disable_animation)
     31                     .onTapGesture {
     32                         show_profile_action_sheet_if_enabled(damus_state: damus, pubkey: pubkey)
     33                     }
     34                     .onLongPressGesture(minimumDuration: 0.1) {
     35                         UIImpactFeedbackGenerator(style: .medium).impactOccurred()
     36                         damus.nav.push(route: Route.ProfileByKey(pubkey: pubkey))
     37                     }
     38             }
     39 
     40             NavigationLink(value: Route.Reposts(reposts: .reposts(state: damus, target: target.id))) {
     41                 Text(people_reposted_text(profiles: damus.profiles, pubkey: pubkey, reposts: reposts))
     42                     .font(.subheadline)
     43                     .foregroundColor(.gray)
     44             }
     45         }
     46         .onReceive(handle_notify(.update_stats), perform: { note_id in
     47             guard note_id == target.id else { return }
     48             let repost_count = damus.boosts.counts[target.id]
     49             if let repost_count, reposts != repost_count {
     50                 reposts = repost_count
     51             }
     52         })
     53     }
     54 }
     55 
     56 func people_reposted_text(profiles: Profiles, pubkey: Pubkey, reposts: Int, locale: Locale = Locale.current) -> String {
     57     guard reposts > 0 else {
     58         return ""
     59     }
     60 
     61     let bundle = bundleForLocale(locale: locale)
     62     let other_reposts = reposts - 1
     63     let display_name = event_author_name(profiles: profiles, pubkey: pubkey)
     64 
     65     if other_reposts == 0 {
     66         return String(format: NSLocalizedString("%@ reposted", bundle: bundle, comment: "Text indicating that the note was reposted (i.e. re-shared)."), locale: locale, display_name)
     67     } else {
     68         return String(format: localizedStringFormat(key: "people_reposted_count", locale: locale), locale: locale, other_reposts, display_name)
     69     }
     70 }
     71 
     72 struct Reposted_Previews: PreviewProvider {
     73     static var previews: some View {
     74         let test_state = test_damus_state
     75         Reposted(damus: test_state, pubkey: test_state.pubkey, target: test_note)
     76     }
     77 }