Rename, delete web feed, fix adding feed directly under account not working

This commit is contained in:
Anh Do 2020-03-21 18:29:20 -04:00
parent 70302a425c
commit 20ff053335
No known key found for this signature in database
GPG Key ID: 451E3092F917B62D
4 changed files with 110 additions and 10 deletions

View File

@ -471,4 +471,39 @@ extension NewsBlurAccountDelegate {
}
}
}
func deleteFeed(for account: Account, with feed: WebFeed, from container: Container?, completion: @escaping (Result<Void, Error>) -> Void) {
// This error should never happen
guard let feedID = feed.subscriptionID else {
completion(.failure(NewsBlurError.invalidParameter))
return
}
refreshProgress.addToNumberOfTasksAndRemaining(1)
let folderName = (container as? Folder)?.name
caller.deleteFeed(feedID: feedID, folder: folderName) { result in
self.refreshProgress.completeTask()
switch result {
case .success:
DispatchQueue.main.async {
account.clearWebFeedMetadata(feed)
account.removeWebFeed(feed)
if let folders = account.folders {
for folder in folders where folderName != nil && folder.name == folderName {
folder.removeWebFeed(feed)
}
}
completion(.success(()))
}
case .failure(let error):
DispatchQueue.main.async {
let wrappedError = AccountError.wrappedError(error: error, account: account)
completion(.failure(wrappedError))
}
}
}
}
}

View File

@ -9,7 +9,9 @@
import Foundation
enum NewsBlurFeedChange {
case add(String)
case add(String, String?)
case rename(String, String)
case delete(String, String?)
}
extension NewsBlurFeedChange: NewsBlurDataConvertible {
@ -17,11 +19,21 @@ extension NewsBlurFeedChange: NewsBlurDataConvertible {
var postData = URLComponents()
postData.queryItems = {
switch self {
case .add(let url):
case .add(let url, let folder):
return [
URLQueryItem(name: "url", value: url),
URLQueryItem(name: "folder", value: ""), // root folder
folder != nil ? URLQueryItem(name: "folder", value: folder) : nil
].compactMap { $0 }
case .rename(let feedID, let newName):
return [
URLQueryItem(name: "feed_id", value: feedID),
URLQueryItem(name: "feed_title", value: newName),
]
case .delete(let feedID, let folder):
return [
URLQueryItem(name: "feed_id", value: feedID),
folder != nil ? URLQueryItem(name: "in_folder", value: folder) : nil,
].compactMap { $0 }
}
}()

View File

@ -220,10 +220,10 @@ final class NewsBlurAPICaller: NSObject {
)
}
func addURL(_ url: String, completion: @escaping (Result<NewsBlurFeed?, Error>) -> Void) {
func addURL(_ url: String, folder: String?, completion: @escaping (Result<NewsBlurFeed?, Error>) -> Void) {
sendUpdates(
endpoint: "reader/add_url",
payload: NewsBlurFeedChange.add(url),
payload: NewsBlurFeedChange.add(url, folder),
resultType: NewsBlurAddURLResponse.self
) { result in
switch result {
@ -234,4 +234,32 @@ final class NewsBlurAPICaller: NSObject {
}
}
}
func renameFeed(feedID: String, newName: String, completion: @escaping (Result<Void, Error>) -> Void) {
sendUpdates(
endpoint: "reader/rename_feed",
payload: NewsBlurFeedChange.rename(feedID, newName)
) { result in
switch result {
case .success:
completion(.success(()))
case .failure(let error):
completion(.failure(error))
}
}
}
func deleteFeed(feedID: String, folder: String? = nil, completion: @escaping (Result<Void, Error>) -> Void) {
sendUpdates(
endpoint: "reader/delete_feed",
payload: NewsBlurFeedChange.delete(feedID, folder)
) { result in
switch result {
case .success:
completion(.success(()))
case .failure(let error):
completion(.failure(error))
}
}
}
}

View File

@ -387,7 +387,7 @@ final class NewsBlurAccountDelegate: AccountDelegate {
var feedIDs: [String] = []
for feed in folder.topLevelWebFeeds {
if feed.folderRelationship?.count ?? 0 > 1 {
if (feed.folderRelationship?.count ?? 0) > 1 {
clearFolderRelationship(for: feed, withFolderName: folderToRemove)
} else if let subscriptionID = feed.subscriptionID {
feedIDs.append(subscriptionID)
@ -412,7 +412,8 @@ final class NewsBlurAccountDelegate: AccountDelegate {
func createWebFeed(for account: Account, url: String, name: String?, container: Container, completion: @escaping (Result<WebFeed, Error>) -> ()) {
refreshProgress.addToNumberOfTasksAndRemaining(1)
caller.addURL(url) { result in
let folderName = (container as? Folder)?.name
caller.addURL(url, folder: folderName) { result in
self.refreshProgress.completeTask()
switch result {
@ -428,14 +429,38 @@ final class NewsBlurAccountDelegate: AccountDelegate {
}
func renameWebFeed(for account: Account, with feed: WebFeed, to name: String, completion: @escaping (Result<Void, Error>) -> ()) {
completion(.success(()))
// This error should never happen
guard let feedID = feed.subscriptionID else {
completion(.failure(NewsBlurError.invalidParameter))
return
}
refreshProgress.addToNumberOfTasksAndRemaining(1)
caller.renameFeed(feedID: 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 addWebFeed(for account: Account, with feed: WebFeed, to container: Container, completion: @escaping (Result<Void, Error>) -> ()) {
guard let folder = container as? Folder else {
DispatchQueue.main.async {
if let account = container as? Account {
account.addFeedIfNotInAnyFolder(feed)
account.addWebFeed(feed)
}
completion(.success(()))
}
@ -451,7 +476,7 @@ final class NewsBlurAccountDelegate: AccountDelegate {
}
func removeWebFeed(for account: Account, with feed: WebFeed, from container: Container, completion: @escaping (Result<Void, Error>) -> ()) {
completion(.success(()))
deleteFeed(for: account, with: feed, from: container, completion: completion)
}
func moveWebFeed(for account: Account, with feed: WebFeed, from: Container, to: Container, completion: @escaping (Result<Void, Error>) -> ()) {