damus

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

AddMuteItemView.swift (3954B)


      1 //
      2 //  AddMuteItemView.swift
      3 //  damus
      4 //
      5 //  Created by Charlie Fish on 1/10/24.
      6 //
      7 import SwiftUI
      8 
      9 struct AddMuteItemView: View {
     10     let state: DamusState
     11     @State var new_text: String = ""
     12     @State var expiration: DamusDuration = .indefinite
     13 
     14     @Environment(\.dismiss) var dismiss
     15 
     16     var body: some View {
     17         VStack {
     18             Text("Add mute item", comment: "Title text to indicate user to an add an item to their mutelist.")
     19                 .font(.system(size: 20, weight: .bold))
     20                 .padding(.vertical)
     21 
     22             Divider()
     23                 .padding(.bottom)
     24 
     25             Picker(selection: $expiration) {
     26                 ForEach(DamusDuration.allCases, id: \.self) { duration in
     27                     Text(duration.title).tag(duration)
     28                 }
     29             } label: {
     30                 Text("Duration", comment: "The duration in which to mute the given item.")
     31             }
     32 
     33 
     34             HStack {
     35                 Label("", image: "copy2")
     36                     .onTapGesture {
     37                     if let pasted_text = UIPasteboard.general.string {
     38                         self.new_text = pasted_text
     39                     }
     40                 }
     41                 TextField(NSLocalizedString("npub, #hashtag, phrase", comment: "Placeholder example for relay server address."), text: $new_text)
     42                     .autocorrectionDisabled(true)
     43                     .textInputAutocapitalization(.never)
     44 
     45                 Label("", image: "close-circle")
     46                     .foregroundColor(.accentColor)
     47                     .opacity((new_text == "") ? 0.0 : 1.0)
     48                     .onTapGesture {
     49                         self.new_text = ""
     50                     }
     51             }
     52             .padding(10)
     53             .background(.secondary.opacity(0.2))
     54             .cornerRadius(10)
     55 
     56             Button(action: {
     57                 let expiration_date: Date? = self.expiration.date_from_now
     58                 let mute_item: MuteItem? = {
     59                     if new_text.starts(with: "npub") {
     60                         if let pubkey: Pubkey = bech32_pubkey_decode(new_text) {
     61                             return .user(pubkey, expiration_date)
     62                         } else {
     63                             return nil
     64                         }
     65                     } else if new_text.starts(with: "#") {
     66                         // Remove the starting `#` character
     67                         return .hashtag(Hashtag(hashtag: String("\(new_text)".dropFirst())), expiration_date)
     68                     } else {
     69                         return .word(new_text, expiration_date)
     70                     }
     71                 }()
     72 
     73                 // Actually update & relay the new mute list
     74                 if let mute_item {
     75                     let existing_mutelist = state.mutelist_manager.event
     76 
     77                     guard
     78                         let full_keypair = state.keypair.to_full(),
     79                         let mutelist = create_or_update_mutelist(keypair: full_keypair, mprev: existing_mutelist, to_add: mute_item)
     80                     else {
     81                         return
     82                     }
     83 
     84                     state.mutelist_manager.set_mutelist(mutelist)
     85                     state.postbox.send(mutelist)
     86                 }
     87 
     88                 new_text = ""
     89 
     90                 UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
     91 
     92                 dismiss()
     93             }) {
     94                 HStack {
     95                     Text(verbatim: "Add mute item")
     96                         .bold()
     97                 }
     98                 .frame(minWidth: 300, maxWidth: .infinity, alignment: .center)
     99             }
    100             .buttonStyle(GradientButtonStyle(padding: 10))
    101             .padding(.vertical)
    102 
    103             Spacer()
    104         }
    105         .padding()
    106     }
    107 }
    108 
    109 struct AddMuteItemView_Previews: PreviewProvider {
    110     static var previews: some View {
    111         AddMuteItemView(state: test_damus_state)
    112     }
    113 }