|
| 1 | +import SwiftUI |
| 2 | +import WordPressUI |
| 3 | +import WordPressKit |
| 4 | +import WordPressData |
| 5 | +import SVProgressHUD |
| 6 | + |
| 7 | +struct EditTagView: View { |
| 8 | + @Environment(\.dismiss) private var dismiss |
| 9 | + @StateObject private var viewModel: EditTagViewModel |
| 10 | + |
| 11 | + init(tag: RemotePostTag?, tagsService: TagsService) { |
| 12 | + self._viewModel = StateObject(wrappedValue: EditTagViewModel(tag: tag, tagsService: tagsService)) |
| 13 | + } |
| 14 | + |
| 15 | + var body: some View { |
| 16 | + Form { |
| 17 | + Section(Strings.tagSectionHeader) { |
| 18 | + HStack { |
| 19 | + TextField(Strings.tagNamePlaceholder, text: $viewModel.tagName) |
| 20 | + .textFieldStyle(.plain) |
| 21 | + .autocorrectionDisabled() |
| 22 | + .textInputAutocapitalization(.never) |
| 23 | + .keyboardType(.default) |
| 24 | + |
| 25 | + if !viewModel.tagName.isEmpty { |
| 26 | + Button(action: { |
| 27 | + viewModel.tagName = "" |
| 28 | + }) { |
| 29 | + Image(systemName: "xmark.circle.fill") |
| 30 | + .foregroundColor(.secondary) |
| 31 | + } |
| 32 | + } |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + Section(Strings.descriptionSectionHeader) { |
| 37 | + TextField(Strings.descriptionPlaceholder, text: $viewModel.tagDescription, axis: .vertical) |
| 38 | + .textFieldStyle(.plain) |
| 39 | + .lineLimit(5...15) |
| 40 | + } |
| 41 | + |
| 42 | + if viewModel.isExistingTag { |
| 43 | + Section { |
| 44 | + Button(action: { |
| 45 | + viewModel.showDeleteConfirmation = true |
| 46 | + }) { |
| 47 | + Text(SharedStrings.Button.delete) |
| 48 | + .foregroundColor(.red) |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + .navigationTitle(viewModel.navigationTitle) |
| 54 | + .navigationBarTitleDisplayMode(.inline) |
| 55 | + .toolbar { |
| 56 | + ToolbarItem(placement: .navigationBarTrailing) { |
| 57 | + Button(SharedStrings.Button.save) { |
| 58 | + Task { |
| 59 | + let success = await viewModel.saveTag() |
| 60 | + if success { |
| 61 | + dismiss() |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + .disabled(viewModel.tagName.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty) |
| 66 | + } |
| 67 | + } |
| 68 | + .confirmationDialog( |
| 69 | + Strings.deleteConfirmationTitle, |
| 70 | + isPresented: $viewModel.showDeleteConfirmation, |
| 71 | + titleVisibility: .visible |
| 72 | + ) { |
| 73 | + Button(SharedStrings.Button.delete, role: .destructive) { |
| 74 | + Task { |
| 75 | + let success = await viewModel.deleteTag() |
| 76 | + if success { |
| 77 | + dismiss() |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + Button(SharedStrings.Button.cancel, role: .cancel) { } |
| 82 | + } message: { |
| 83 | + Text(Strings.deleteConfirmationMessage) |
| 84 | + } |
| 85 | + .alert(SharedStrings.Error.generic, isPresented: $viewModel.showError) { |
| 86 | + Button(SharedStrings.Button.ok) { } |
| 87 | + } message: { |
| 88 | + Text(viewModel.errorMessage) |
| 89 | + } |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +@MainActor |
| 94 | +class EditTagViewModel: ObservableObject { |
| 95 | + @Published var tagName: String |
| 96 | + @Published var tagDescription: String |
| 97 | + @Published var showDeleteConfirmation = false |
| 98 | + @Published var showError = false |
| 99 | + @Published var errorMessage = "" |
| 100 | + |
| 101 | + private let originalTag: RemotePostTag? |
| 102 | + private let tagsService: TagsService |
| 103 | + |
| 104 | + var isExistingTag: Bool { |
| 105 | + originalTag != nil |
| 106 | + } |
| 107 | + |
| 108 | + var navigationTitle: String { |
| 109 | + originalTag?.name ?? Strings.newTagTitle |
| 110 | + } |
| 111 | + |
| 112 | + init(tag: RemotePostTag?, tagsService: TagsService) { |
| 113 | + self.originalTag = tag |
| 114 | + self.tagsService = tagsService |
| 115 | + self.tagName = tag?.name ?? "" |
| 116 | + self.tagDescription = tag?.tagDescription ?? "" |
| 117 | + } |
| 118 | + |
| 119 | + func deleteTag() async -> Bool { |
| 120 | + guard let tag = originalTag else { return false } |
| 121 | + |
| 122 | + SVProgressHUD.show() |
| 123 | + defer { SVProgressHUD.dismiss() } |
| 124 | + |
| 125 | + do { |
| 126 | + try await tagsService.deleteTag(tag) |
| 127 | + |
| 128 | + // Post notification to update the UI |
| 129 | + NotificationCenter.default.post( |
| 130 | + name: .tagDeleted, |
| 131 | + object: nil, |
| 132 | + userInfo: [TagNotificationUserInfoKeys.tagID: tag.tagID ?? 0] |
| 133 | + ) |
| 134 | + return true |
| 135 | + } catch { |
| 136 | + errorMessage = error.localizedDescription |
| 137 | + showError = true |
| 138 | + return false |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + func saveTag() async -> Bool { |
| 143 | + SVProgressHUD.show() |
| 144 | + defer { SVProgressHUD.dismiss() } |
| 145 | + |
| 146 | + let tagToSave: RemotePostTag |
| 147 | + if let existingTag = originalTag { |
| 148 | + tagToSave = existingTag |
| 149 | + } else { |
| 150 | + tagToSave = RemotePostTag() |
| 151 | + } |
| 152 | + |
| 153 | + tagToSave.name = tagName.trimmingCharacters(in: .whitespacesAndNewlines) |
| 154 | + tagToSave.tagDescription = tagDescription.trimmingCharacters(in: .whitespacesAndNewlines) |
| 155 | + |
| 156 | + do { |
| 157 | + let savedTag = try await tagsService.saveTag(tagToSave) |
| 158 | + |
| 159 | + NotificationCenter.default.post( |
| 160 | + name: originalTag == nil ? .tagCreated : .tagUpdated, |
| 161 | + object: nil, |
| 162 | + userInfo: [TagNotificationUserInfoKeys.tag: savedTag] |
| 163 | + ) |
| 164 | + return true |
| 165 | + } catch { |
| 166 | + errorMessage = error.localizedDescription |
| 167 | + showError = true |
| 168 | + return false |
| 169 | + } |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +private enum Strings { |
| 174 | + static let tagSectionHeader = NSLocalizedString( |
| 175 | + "edit.tag.section.tag", |
| 176 | + value: "Tag", |
| 177 | + comment: "Section header for tag name in edit tag view" |
| 178 | + ) |
| 179 | + |
| 180 | + static let descriptionSectionHeader = NSLocalizedString( |
| 181 | + "edit.tag.section.description", |
| 182 | + value: "Description", |
| 183 | + comment: "Section header for tag description in edit tag view" |
| 184 | + ) |
| 185 | + |
| 186 | + static let tagNamePlaceholder = NSLocalizedString( |
| 187 | + "edit.tag.name.placeholder", |
| 188 | + value: "Tag name", |
| 189 | + comment: "Placeholder text for tag name field" |
| 190 | + ) |
| 191 | + |
| 192 | + static let descriptionPlaceholder = NSLocalizedString( |
| 193 | + "edit.tag.description.placeholder", |
| 194 | + value: "Add a description...", |
| 195 | + comment: "Placeholder text for tag description field" |
| 196 | + ) |
| 197 | + |
| 198 | + static let newTagTitle = NSLocalizedString( |
| 199 | + "edit.tag.new.title", |
| 200 | + value: "New Tag", |
| 201 | + comment: "Navigation title for new tag creation" |
| 202 | + ) |
| 203 | + |
| 204 | + static let deleteConfirmationTitle = NSLocalizedString( |
| 205 | + "edit.tag.delete.confirmation.title", |
| 206 | + value: "Delete Tag", |
| 207 | + comment: "Title for delete tag confirmation dialog" |
| 208 | + ) |
| 209 | + |
| 210 | + static let deleteConfirmationMessage = NSLocalizedString( |
| 211 | + "edit.tag.delete.confirmation.message", |
| 212 | + value: "Are you sure you want to delete this tag?", |
| 213 | + comment: "Message for delete tag confirmation dialog" |
| 214 | + ) |
| 215 | +} |
0 commit comments