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