Fix AddMuteItemView to trim leading and trailing whitespaces from mute text and disallow adding text with only whitespaces

Changelog-Fixed: Fixed AddMuteItemView to trim leading and trailing whitespaces from mute text and disallow adding text with only whitespaces

Signed-off-by: Terry Yiu <git@tyiu.xyz>
This commit is contained in:
2024-12-09 18:49:01 -05:00
committed by Daniel D’Aquino
parent b776788b38
commit 902e8c3950

View File

@@ -13,6 +13,10 @@ struct AddMuteItemView: View {
@Environment(\.dismiss) var dismiss @Environment(\.dismiss) var dismiss
var trimmedText: String {
new_text.trimmingCharacters(in: .whitespaces)
}
var body: some View { var body: some View {
VStack { VStack {
Text("Add mute item", comment: "Title text to indicate user to an add an item to their mutelist.") Text("Add mute item", comment: "Title text to indicate user to an add an item to their mutelist.")
@@ -30,12 +34,13 @@ struct AddMuteItemView: View {
Text("Duration", comment: "The duration in which to mute the given item.") Text("Duration", comment: "The duration in which to mute the given item.")
} }
let trimmedText = self.trimmedText
HStack { HStack {
Label("", image: "copy2") Label("", image: "copy2")
.onTapGesture { .onTapGesture {
if let pasted_text = UIPasteboard.general.string { if let pasted_text = UIPasteboard.general.string {
self.new_text = pasted_text self.new_text = pasted_text.trimmingCharacters(in: .whitespaces)
} }
} }
TextField(NSLocalizedString("npub, #hashtag, phrase", comment: "Placeholder example for relay server address."), text: $new_text) TextField(NSLocalizedString("npub, #hashtag, phrase", comment: "Placeholder example for relay server address."), text: $new_text)
@@ -44,7 +49,7 @@ struct AddMuteItemView: View {
Label("", image: "close-circle") Label("", image: "close-circle")
.foregroundColor(.accentColor) .foregroundColor(.accentColor)
.opacity((new_text == "") ? 0.0 : 1.0) .opacity(trimmedText.isEmpty ? 0.0 : 1.0)
.onTapGesture { .onTapGesture {
self.new_text = "" self.new_text = ""
} }
@@ -56,17 +61,17 @@ struct AddMuteItemView: View {
Button(action: { Button(action: {
let expiration_date: Date? = self.expiration.date_from_now let expiration_date: Date? = self.expiration.date_from_now
let mute_item: MuteItem? = { let mute_item: MuteItem? = {
if new_text.starts(with: "npub") { if trimmedText.starts(with: "npub") {
if let pubkey: Pubkey = bech32_pubkey_decode(new_text) { if let pubkey: Pubkey = bech32_pubkey_decode(trimmedText) {
return .user(pubkey, expiration_date) return .user(pubkey, expiration_date)
} else { } else {
return nil return nil
} }
} else if new_text.starts(with: "#") { } else if trimmedText.starts(with: "#") {
// Remove the starting `#` character // Remove the starting `#` character
return .hashtag(Hashtag(hashtag: String("\(new_text)".dropFirst())), expiration_date) return .hashtag(Hashtag(hashtag: String("\(trimmedText)".dropFirst())), expiration_date)
} else { } else {
return .word(new_text, expiration_date) return .word(trimmedText, expiration_date)
} }
}() }()
@@ -99,6 +104,8 @@ struct AddMuteItemView: View {
} }
.buttonStyle(GradientButtonStyle(padding: 10)) .buttonStyle(GradientButtonStyle(padding: 10))
.padding(.vertical) .padding(.vertical)
.opacity(trimmedText.isEmpty ? 0.5 : 1.0)
.disabled(trimmedText.isEmpty)
Spacer() Spacer()
} }