damus

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

MutelistView.swift (5627B)


      1 //
      2 //  MutelistView.swift
      3 //  damus
      4 //
      5 //  Created by William Casarin on 2023-01-25.
      6 //
      7 
      8 import SwiftUI
      9 
     10 struct MutelistView: View {
     11     let damus_state: DamusState
     12     @State var show_add_muteitem: Bool = false
     13 
     14     @State var users: [MuteItem] = []
     15     @State var hashtags: [MuteItem] = []
     16     @State var threads: [MuteItem] = []
     17     @State var words: [MuteItem] = []
     18     
     19     @State var new_text: String = ""
     20 
     21     func RemoveAction(item: MuteItem) -> some View {
     22         Button {
     23             guard let mutelist = damus_state.mutelist_manager.event,
     24                   let keypair = damus_state.keypair.to_full(),
     25                   let new_ev = remove_from_mutelist(keypair: keypair,
     26                                                     prev: mutelist,
     27                                                     to_remove: item)
     28             else {
     29                 return
     30             }
     31 
     32             damus_state.mutelist_manager.set_mutelist(new_ev)
     33             damus_state.postbox.send(new_ev)
     34             updateMuteItems()
     35         } label: {
     36             Label(NSLocalizedString("Delete", comment: "Button to remove a user from their mutelist."), image: "delete")
     37         }
     38         .tint(.red)
     39     }
     40 
     41     func updateMuteItems() {
     42         users = Array(damus_state.mutelist_manager.users)
     43         hashtags = Array(damus_state.mutelist_manager.hashtags)
     44         threads = Array(damus_state.mutelist_manager.threads)
     45         words = Array(damus_state.mutelist_manager.words)
     46     }
     47 
     48     var body: some View {
     49         List {
     50             Section(NSLocalizedString("Users", comment: "Section header title for a list of muted users.")) {
     51                 ForEach(users, id: \.self) { user in
     52                     if case let MuteItem.user(pubkey, _) = user {
     53                         UserViewRow(damus_state: damus_state, pubkey: pubkey)
     54                          .id(pubkey)
     55                          .swipeActions {
     56                              RemoveAction(item: .user(pubkey, nil))
     57                          }
     58                          .onTapGesture {
     59                              damus_state.nav.push(route: Route.ProfileByKey(pubkey: pubkey))
     60                          }
     61                     }
     62                 }
     63             }
     64             Section(NSLocalizedString("Hashtags", comment: "Section header title for a list of hashtags that are muted.")) {
     65                 ForEach(hashtags, id: \.self) { item in
     66                     if case let MuteItem.hashtag(hashtag, _) = item {
     67                         Text(verbatim: "#\(hashtag.hashtag)")
     68                             .id(hashtag.hashtag)
     69                             .swipeActions {
     70                                 RemoveAction(item: .hashtag(hashtag, nil))
     71                             }
     72                             .onTapGesture {
     73                                 damus_state.nav.push(route: Route.Search(search: SearchModel.init(state: damus_state, search: NostrFilter(hashtag: [hashtag.hashtag]))))
     74                             }
     75                     }
     76                 }
     77             }
     78             Section(NSLocalizedString("Words", comment: "Section header title for a list of words that are muted.")) {
     79                 ForEach(words, id: \.self) { item in
     80                     if case let MuteItem.word(word, _) = item {
     81                         Text(word)
     82                             .id(word)
     83                             .swipeActions {
     84                                 RemoveAction(item: .word(word, nil))
     85                             }
     86                     }
     87                 }
     88             }
     89             Section(
     90                 header: Text(NSLocalizedString("Threads", comment: "Section header title for a list of threads that are muted.")),
     91                 footer: Text("").padding(.bottom, 10 + tabHeight + getSafeAreaBottom())
     92             ) {
     93                 ForEach(threads, id: \.self) { item in
     94                     if case let MuteItem.thread(note_id, _) = item {
     95                         if let event = damus_state.events.lookup(note_id) {
     96                             EventView(damus: damus_state, event: event)
     97                                 .id(note_id.hex())
     98                                 .swipeActions {
     99                                     RemoveAction(item: .thread(note_id, nil))
    100                                 }
    101                         } else {
    102                             Text("Error retrieving muted event", comment: "Text for an item that application failed to retrieve the muted event for.")
    103                         }
    104                     }
    105                 }
    106             }
    107         }
    108         .navigationTitle(NSLocalizedString("Muted", comment: "Navigation title of view to see list of muted users & phrases."))
    109         .onAppear {
    110             updateMuteItems()
    111         }
    112         .onReceive(handle_notify(.new_mutes)) { new_mutes in
    113             updateMuteItems()
    114         }
    115         .onReceive(handle_notify(.new_unmutes)) { new_unmutes in
    116             updateMuteItems()
    117         }
    118         .toolbar {
    119             ToolbarItem(placement: .topBarTrailing) {
    120                 Button {
    121                     self.show_add_muteitem = true
    122                 } label: {
    123                     Image(systemName: "plus")
    124                 }
    125             }
    126         }
    127         .sheet(isPresented: $show_add_muteitem, onDismiss: { self.show_add_muteitem = false }) {
    128             AddMuteItemView(state: damus_state, new_text: $new_text)
    129                 .presentationDetents([.height(300)])
    130                 .presentationDragIndicator(.visible)
    131         }
    132     }
    133 }
    134 
    135 struct MutelistView_Previews: PreviewProvider {
    136     static var previews: some View {
    137         MutelistView(damus_state: test_damus_state)
    138     }
    139 }