damus

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

MutedEventView.swift (2516B)


      1 //
      2 //  MutedEventView.swift
      3 //  damus
      4 //
      5 //  Created by William Casarin on 2023-01-27.
      6 //
      7 
      8 import SwiftUI
      9 
     10 struct MutedEventView: View {
     11     let damus_state: DamusState
     12     let event: NostrEvent
     13     
     14     let selected: Bool
     15     @State var shown: Bool
     16     
     17     init(damus_state: DamusState, event: NostrEvent, selected: Bool) {
     18         self.damus_state = damus_state
     19         self.event = event
     20         self.selected = selected
     21         self._shown = State(initialValue: should_show_event(keypair: damus_state.keypair, hellthreads: damus_state.muted_threads, contacts: damus_state.contacts, ev: event))
     22     }
     23     
     24     var should_mute: Bool {
     25         return !should_show_event(keypair: damus_state.keypair, hellthreads: damus_state.muted_threads, contacts: damus_state.contacts, ev: event)
     26     }
     27     
     28     var MutedBox: some View {
     29         ZStack {
     30             RoundedRectangle(cornerRadius: 20)
     31                 .foregroundColor(DamusColors.adaptableGrey)
     32             
     33             HStack {
     34                 Text("Note from a user you've muted", comment: "Text to indicate that what is being shown is a note from a user who has been muted.")
     35                 Spacer()
     36                 Button(shown ? NSLocalizedString("Hide", comment: "Button to hide a note from a user who has been muted.") : NSLocalizedString("Show", comment: "Button to show a note from a user who has been muted.")) {
     37                     shown.toggle()
     38                 }
     39             }
     40             .padding(10)
     41         }
     42     }
     43     
     44     var Event: some View {
     45         Group {
     46             if selected {
     47                 SelectedEventView(damus: damus_state, event: event, size: .selected)
     48             } else {
     49                 EventView(damus: damus_state, event: event)
     50             }
     51         }
     52     }
     53     
     54     var body: some View {
     55         Group {
     56             if should_mute {
     57                 MutedBox
     58             }
     59             if shown {
     60                 Event
     61             }
     62         }
     63         .onReceive(handle_notify(.new_mutes)) { mutes in
     64             if mutes.contains(event.pubkey) {
     65                 shown = false
     66             }
     67         }
     68         .onReceive(handle_notify(.new_unmutes)) { unmutes in
     69             if unmutes.contains(event.pubkey) {
     70                 shown = true
     71             }
     72         }
     73     }
     74 }
     75 
     76 struct MutedEventView_Previews: PreviewProvider {
     77     
     78     static var previews: some View {
     79         
     80         MutedEventView(damus_state: test_damus_state, event: test_note, selected: false)
     81             .frame(width: .infinity, height: 50)
     82     }
     83 }