Convert renameFeed and renameFolder to async await.

This commit is contained in:
Brent Simmons
2024-03-27 17:49:09 -07:00
parent a7ba7e3b4a
commit 6ad90583a4
18 changed files with 263 additions and 107 deletions

View File

@@ -1139,59 +1139,51 @@ private extension SidebarViewController {
func rename(indexPath: IndexPath) {
guard let feed = coordinator.nodeFor(indexPath)?.representedObject as? SidebarItem else { return }
guard let sidebarItem = coordinator.nodeFor(indexPath)?.representedObject as? SidebarItem else {
return
}
let formatString = NSLocalizedString("Rename “%@”", comment: "Rename feed")
let title = NSString.localizedStringWithFormat(formatString as NSString, feed.nameForDisplay) as String
let title = NSString.localizedStringWithFormat(formatString as NSString, sidebarItem.nameForDisplay) as String
let alertController = UIAlertController(title: title, message: nil, preferredStyle: .alert)
let cancelTitle = NSLocalizedString("Cancel", comment: "Cancel")
alertController.addAction(UIAlertAction(title: cancelTitle, style: .cancel))
let renameTitle = NSLocalizedString("Rename", comment: "Rename")
let renameAction = UIAlertAction(title: renameTitle, style: .default) { [weak self] action in
guard let name = alertController.textFields?[0].text, !name.isEmpty else {
return
}
if let feed = feed as? Feed {
feed.rename(to: name) { result in
switch result {
case .success:
break
case .failure(let error):
self?.presentError(error)
}
}
} else if let folder = feed as? Folder {
folder.rename(to: name) { result in
switch result {
case .success:
break
case .failure(let error):
Task { @MainActor in
if let renamableItem = sidebarItem as? Renamable {
do {
try await renamableItem.rename(to: name)
} catch {
self?.presentError(error)
}
}
}
}
alertController.addAction(renameAction)
alertController.preferredAction = renameAction
alertController.addTextField() { textField in
textField.text = feed.nameForDisplay
textField.text = sidebarItem.nameForDisplay
textField.placeholder = NSLocalizedString("Name", comment: "Name")
}
self.present(alertController, animated: true) {
}
}
func delete(indexPath: IndexPath) {
guard let feed = coordinator.nodeFor(indexPath)?.representedObject as? SidebarItem else { return }

View File

@@ -62,7 +62,10 @@ class FeedInspectorViewController: UITableViewController {
if nameTextField.text != feed.nameForDisplay {
let nameText = nameTextField.text ?? ""
let newName = nameText.isEmpty ? (feed.name ?? NSLocalizedString("Untitled", comment: "Feed name")) : nameText
feed.rename(to: newName) { _ in }
Task { @MainActor in
try? await feed.rename(to: newName)
}
}
}