From 36861f2eb36904413092be4da8461fe3a384b98e Mon Sep 17 00:00:00 2001 From: Jonathan Bennett Date: Wed, 16 Oct 2019 11:32:22 -0400 Subject: [PATCH] allow renaming of feeds --- .../FeedWrangler/FeedWranglerAPICaller.swift | 27 +++++++++++++++++++ .../FeedWranglerAccountDelegate.swift | 24 ++++++++++++++++- 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/Frameworks/Account/FeedWrangler/FeedWranglerAPICaller.swift b/Frameworks/Account/FeedWrangler/FeedWranglerAPICaller.swift index 1aeabe525..577a8a4c0 100644 --- a/Frameworks/Account/FeedWrangler/FeedWranglerAPICaller.swift +++ b/Frameworks/Account/FeedWrangler/FeedWranglerAPICaller.swift @@ -71,4 +71,31 @@ final class FeedWranglerAPICaller: NSObject { } } + func renameSubscription(feedID: String, newName: String, completion: @escaping (Result) -> Void) { + guard var components = URLComponents(url: FeedWranglerConfig.clientURL.appendingPathComponent("subscriptions/rename_feed"), resolvingAgainstBaseURL: false) else { + completion(.failure(TransportError.noURL)) + return + } + components.queryItems = [ + URLQueryItem(name: "feed_id", value: feedID), + URLQueryItem(name: "feed_name", value: newName), + ] + + guard let url = components.url else { + completion(.failure(TransportError.noURL)) + return + } + + let request = URLRequest(url: url, credentials: credentials) + + transport.send(request: request, resultType: FeedWranglerSubscriptionsRequest.self) { result in + switch result { + case .success: + completion(.success(())) + + case .failure(let error): + completion(.failure(error)) + } + } + } } diff --git a/Frameworks/Account/FeedWrangler/FeedWranglerAccountDelegate.swift b/Frameworks/Account/FeedWrangler/FeedWranglerAccountDelegate.swift index 1f5a056da..0ded03802 100644 --- a/Frameworks/Account/FeedWrangler/FeedWranglerAccountDelegate.swift +++ b/Frameworks/Account/FeedWrangler/FeedWranglerAccountDelegate.swift @@ -141,7 +141,28 @@ final class FeedWranglerAccountDelegate: AccountDelegate { } func renameFeed(for account: Account, with feed: Feed, to name: String, completion: @escaping (Result) -> Void) { - fatalError() + refreshProgress.addToNumberOfTasksAndRemaining(2) + + self.refreshCredentials(for: account) { + self.refreshProgress.completeTask() + self.caller.renameSubscription(feedID: feed.feedID, newName: name) { result in + self.refreshProgress.completeTask() + + switch result { + case .success: + DispatchQueue.main.async { + feed.editedName = name + completion(.success(())) + } + + case .failure(let error): + DispatchQueue.main.async { + let wrappedError = AccountError.wrappedError(error: error, account: account) + completion(.failure(wrappedError)) + } + } + } + } } func addFeed(for account: Account, with: Feed, to container: Container, completion: @escaping (Result) -> Void) { @@ -199,6 +220,7 @@ private extension FeedWranglerAccountDelegate { if let feed = account.existingFeed(withFeedID: subscriptionId) { feed.name = subscription.title + feed.editedName = nil feed.homePageURL = subscription.siteURL feed.subscriptionID = nil // MARK: TODO What should this be? } else {