damus

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

MutelistView.swift (5585B)


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