damus

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

EventMutingContainerView.swift (3872B)


      1 //
      2 //  EventMutingContainerView.swift
      3 //  damus
      4 //
      5 //  Created by William Casarin on 2023-01-27.
      6 //
      7 
      8 import SwiftUI
      9 
     10 /// A container view that shows or hides provided content based on whether the given event should be muted or not, with built-in user controls to show or hide content, and an option to customize the muted box
     11 struct EventMutingContainerView<Content: View>: View {
     12     typealias MuteBoxViewClosure = ((_ shown: Binding<Bool>, _ mutedReason: MuteItem?) -> AnyView)
     13 
     14     let damus_state: DamusState
     15     let event: NostrEvent
     16     let content: Content
     17     var customMuteBox: MuteBoxViewClosure?
     18     
     19     /// Represents if the note itself should be shown.
     20     ///
     21     /// By default this is the same as `should_show_event`. However, if the user taps the button to manually show a muted note, this can become out of sync with `should_show_event`.
     22     @State var shown: Bool
     23 
     24     @State var muted_reason: MuteItem?
     25 
     26     init(damus_state: DamusState, event: NostrEvent, @ViewBuilder content: () -> Content) {
     27         self.damus_state = damus_state
     28         self.event = event
     29         self.content = content()
     30         self._shown = State(initialValue: should_show_event(state: damus_state, ev: event))
     31     }
     32     
     33     init(damus_state: DamusState, event: NostrEvent, muteBox: @escaping MuteBoxViewClosure, @ViewBuilder content: () -> Content) {
     34         self.init(damus_state: damus_state, event: event, content: content)
     35         self.customMuteBox = muteBox
     36     }
     37     
     38     var should_mute: Bool {
     39         return !should_show_event(state: damus_state, ev: event)
     40     }
     41     
     42     var body: some View {
     43         Group {
     44             if should_mute {
     45                 if let customMuteBox {
     46                     customMuteBox($shown, muted_reason)
     47                 }
     48                 else {
     49                     EventMutedBoxView(shown: $shown, reason: muted_reason)
     50                 }
     51             }
     52             if shown {
     53                 self.content
     54             }
     55         }
     56         .onReceive(handle_notify(.new_mutes)) { mutes in
     57             let new_muted_event_reason = damus_state.mutelist_manager.event_muted_reason(event)
     58             if new_muted_event_reason != nil {
     59                 shown = false
     60                 muted_reason = new_muted_event_reason
     61             }
     62         }
     63         .onReceive(handle_notify(.new_unmutes)) { unmutes in
     64             if damus_state.mutelist_manager.event_muted_reason(event) != nil {
     65                 shown = true
     66                 muted_reason = nil
     67             }
     68         }
     69     }
     70 }
     71 
     72 /// A box that instructs the user about a content that has been muted.
     73 struct EventMutedBoxView: View {
     74     @Binding var shown: Bool
     75     var reason: MuteItem?
     76 
     77     var body: some View {
     78         ZStack {
     79             RoundedRectangle(cornerRadius: 20)
     80                 .foregroundColor(DamusColors.adaptableGrey)
     81             
     82             HStack {
     83                 if let reason {
     84                     Text("Note from a \(reason.title) you've muted", comment: "Text to indicate that what is being shown is a note which has been muted.")
     85                 } else {
     86                     Text("Note you've muted", comment: "Text to indicate that what is being shown is a note which has been muted.")
     87                 }
     88                 Spacer()
     89                 Button(shown ? NSLocalizedString("Hide", comment: "Button to hide a note which has been muted.") : NSLocalizedString("Show", comment: "Button to show a note which has been muted.")) {
     90                     shown.toggle()
     91                 }
     92             }
     93             .padding(10)
     94         }
     95     }
     96 }
     97 
     98 struct MutedEventView_Previews: PreviewProvider {
     99     
    100     static var previews: some View {
    101         
    102         EventMutingContainerView(damus_state: test_damus_state, event: test_note) {
    103             EventView(damus: test_damus_state, event: test_note)
    104         }
    105             .frame(width: .infinity, height: 50)
    106     }
    107 }