Cleanup merge issues/API changes

This commit is contained in:
Jeremy Beker 2019-06-01 06:54:10 -04:00
parent 98c32b9987
commit 76d1daf122
No known key found for this signature in database
GPG Key ID: CD5EE767A4A34FD0
1 changed files with 169 additions and 188 deletions

View File

@ -31,6 +31,8 @@ final class GoogleReaderCompatibleAccountDelegate: AccountDelegate {
private var log = OSLog(subsystem: Bundle.main.bundleIdentifier!, category: "GoogleReaderCompatible") private var log = OSLog(subsystem: Bundle.main.bundleIdentifier!, category: "GoogleReaderCompatible")
let supportsSubFolders = false let supportsSubFolders = false
let usesTags = true
var server: String? { var server: String? {
get { get {
return caller.server return caller.server
@ -239,6 +241,14 @@ final class GoogleReaderCompatibleAccountDelegate: AccountDelegate {
} }
func addFolder(for account: Account, name: String, completion: @escaping (Result<Folder, Error>) -> Void) {
if let folder = account.ensureFolder(with: name) {
completion(.success(folder))
} else {
completion(.failure(FeedbinAccountDelegateError.invalidParameter))
}
}
func renameFolder(for account: Account, with folder: Folder, to name: String, completion: @escaping (Result<Void, Error>) -> Void) { func renameFolder(for account: Account, with folder: Folder, to name: String, completion: @escaping (Result<Void, Error>) -> Void) {
caller.renameTag(oldName: folder.name ?? "", newName: name) { result in caller.renameTag(oldName: folder.name ?? "", newName: name) { result in
@ -258,51 +268,46 @@ final class GoogleReaderCompatibleAccountDelegate: AccountDelegate {
} }
func deleteFolder(for account: Account, with folder: Folder, completion: @escaping (Result<Void, Error>) -> Void) { func removeFolder(for account: Account, with folder: Folder, completion: @escaping (Result<Void, Error>) -> Void) {
// GoogleReaderCompatible uses tags and if at least one feed isn't tagged, then the folder doesn't exist on their system // Feedbin uses tags and if at least one feed isn't tagged, then the folder doesn't exist on their system
guard folder.hasAtLeastOneFeed() else { guard folder.hasAtLeastOneFeed() else {
account.deleteFolder(folder) account.removeFolder(folder)
return return
} }
// After we successfully delete at GoogleReaderCompatible, we add all the feeds to the account to save them. We then let group = DispatchGroup()
// delete the folder. We then sync the taggings we received on the delete to remove any feeds from
// the account that might be in another folder.
caller.deleteTag(name: folder.name ?? "") { result in
switch result {
case .success(let taggings):
DispatchQueue.main.sync {
BatchUpdate.shared.perform {
for feed in folder.topLevelFeeds { for feed in folder.topLevelFeeds {
account.addFeed(feed) group.enter()
self.clearFolderRelationship(for: feed, withFolderName: folder.name ?? "") removeFeed(for: account, with: feed, from: folder) { result in
group.leave()
switch result {
case .success:
break
case .failure(let error):
os_log(.error, log: self.log, "Remove feed error: %@.", error.localizedDescription)
} }
account.deleteFolder(folder)
} }
}
group.notify(queue: DispatchQueue.main) {
account.removeFolder(folder)
completion(.success(())) completion(.success(()))
} }
self.syncTaggings(account, taggings)
case .failure(let error):
DispatchQueue.main.async {
let wrappedError = AccountError.wrappedError(error: error, account: account)
completion(.failure(wrappedError))
}
}
}
} }
func createFeed(for account: Account, url: String, completion: @escaping (Result<Feed, Error>) -> Void) { func createFeed(for account: Account, url: String, name: String?, container: Container, completion: @escaping (Result<Feed, Error>) -> Void) {
caller.createSubscription(url: url) { result in caller.createSubscription(url: url) { result in
switch result { switch result {
case .success(let subResult): case .success(let subResult):
switch subResult { switch subResult {
case .created(let subscription): case .created(let subscription):
self.createFeed(account: account, subscription: subscription, completion: completion) self.createFeed(account: account, subscription: subscription, name: name, container: container, completion: completion)
case .multipleChoice(let choices): case .multipleChoice(let choices):
self.decideBestFeedChoice(account: account, url: url, choices: choices, completion: completion) self.decideBestFeedChoice(account: account, url: url, name: name, container: container, choices: choices, completion: completion)
case .alreadySubscribed: case .alreadySubscribed:
DispatchQueue.main.async { DispatchQueue.main.async {
completion(.failure(AccountError.createErrorAlreadySubscribed)) completion(.failure(AccountError.createErrorAlreadySubscribed))
@ -327,7 +332,7 @@ final class GoogleReaderCompatibleAccountDelegate: AccountDelegate {
// This error should never happen // This error should never happen
guard let subscriptionID = feed.subscriptionID else { guard let subscriptionID = feed.subscriptionID else {
completion(.failure(GoogleReaderCompatibleAccountDelegateError.invalidParameter)) completion(.failure(FeedbinAccountDelegateError.invalidParameter))
return return
} }
@ -348,37 +353,31 @@ final class GoogleReaderCompatibleAccountDelegate: AccountDelegate {
} }
func deleteFeed(for account: Account, with feed: Feed, completion: @escaping (Result<Void, Error>) -> Void) { func removeFeed(for account: Account, with feed: Feed, from container: Container?, completion: @escaping (Result<Void, Error>) -> Void) {
if feed.folderRelationship?.count ?? 0 > 1 {
// This error should never happen deleteTagging(for: account, with: feed, from: container, completion: completion)
guard let subscriptionID = feed.subscriptionID else { } else {
completion(.failure(GoogleReaderCompatibleAccountDelegateError.invalidParameter)) account.clearFeedMetadata(feed)
return deleteSubscription(for: account, with: feed, from: container, completion: completion)
}
} }
caller.deleteSubscription(subscriptionID: subscriptionID) { result in func moveFeed(for account: Account, with feed: Feed, from: Container, to: Container, completion: @escaping (Result<Void, Error>) -> Void) {
if from is Account {
addFeed(for: account, with: feed, to: to, completion: completion)
} else {
deleteTagging(for: account, with: feed, from: from) { result in
switch result { switch result {
case .success: case .success:
DispatchQueue.main.async { self.addFeed(for: account, with: feed, to: to, completion: completion)
account.removeFeed(feed)
if let folders = account.folders {
for folder in folders {
folder.removeFeed(feed)
}
}
completion(.success(()))
}
case .failure(let error): case .failure(let error):
DispatchQueue.main.async { completion(.failure(error))
let wrappedError = AccountError.wrappedError(error: error, account: account) }
completion(.failure(wrappedError))
} }
} }
} }
} func addFeed(for account: Account, with feed: Feed, to container: Container, completion: @escaping (Result<Void, Error>) -> Void) {
func addFeed(for account: Account, to container: Container, with feed: Feed, completion: @escaping (Result<Void, Error>) -> Void) {
if let folder = container as? Folder, let feedID = Int(feed.feedID) { if let folder = container as? Folder, let feedID = Int(feed.feedID) {
caller.createTagging(feedID: feedID, name: folder.name ?? "") { result in caller.createTagging(feedID: feedID, name: folder.name ?? "") { result in
@ -386,6 +385,7 @@ final class GoogleReaderCompatibleAccountDelegate: AccountDelegate {
case .success(let taggingID): case .success(let taggingID):
DispatchQueue.main.async { DispatchQueue.main.async {
self.saveFolderRelationship(for: feed, withFolderName: folder.name ?? "", id: String(taggingID)) self.saveFolderRelationship(for: feed, withFolderName: folder.name ?? "", id: String(taggingID))
account.removeFeed(feed)
folder.addFeed(feed) folder.addFeed(feed)
completion(.success(())) completion(.success(()))
} }
@ -397,55 +397,24 @@ final class GoogleReaderCompatibleAccountDelegate: AccountDelegate {
} }
} }
} else { } else {
if let account = container as? Account {
account.addFeed(feed)
}
DispatchQueue.main.async { DispatchQueue.main.async {
if let account = container as? Account {
account.addFeedIfNotInAnyFolder(feed)
}
completion(.success(())) completion(.success(()))
} }
} }
} }
func removeFeed(for account: Account, from container: Container, with feed: Feed, completion: @escaping (Result<Void, Error>) -> Void) { func restoreFeed(for account: Account, feed: Feed, container: Container, completion: @escaping (Result<Void, Error>) -> Void) {
if let folder = container as? Folder, let feedTaggingID = feed.folderRelationship?[folder.name ?? ""] { createFeed(for: account, url: feed.url, name: feed.editedName, container: container) { result in
caller.deleteTagging(taggingID: feedTaggingID) { result in
switch result { switch result {
case .success: case .success:
DispatchQueue.main.async {
folder.removeFeed(feed)
completion(.success(())) completion(.success(()))
}
case .failure(let error): case .failure(let error):
DispatchQueue.main.async { completion(.failure(error))
let wrappedError = AccountError.wrappedError(error: error, account: account)
completion(.failure(wrappedError))
}
}
}
} else {
if let account = container as? Account {
account.removeFeed(feed)
}
completion(.success(()))
}
}
func restoreFeed(for account: Account, feed: Feed, folder: Folder?, completion: @escaping (Result<Void, Error>) -> Void) {
let editedName = feed.editedName
createFeed(for: account, url: feed.url) { result in
switch result {
case .success(let feed):
self.processRestoredFeed(for: account, feed: feed, editedName: editedName, folder: folder, completion: completion)
case .failure(let error):
DispatchQueue.main.async {
let wrappedError = AccountError.wrappedError(error: error, account: account)
completion(.failure(wrappedError))
}
} }
} }
@ -459,7 +428,7 @@ final class GoogleReaderCompatibleAccountDelegate: AccountDelegate {
for feed in folder.topLevelFeeds { for feed in folder.topLevelFeeds {
group.enter() group.enter()
addFeed(for: account, to: folder, with: feed) { result in addFeed(for: account, with: feed, to: folder) { result in
if account.topLevelFeeds.contains(feed) { if account.topLevelFeeds.contains(feed) {
account.removeFeed(feed) account.removeFeed(feed)
} }
@ -488,7 +457,6 @@ final class GoogleReaderCompatibleAccountDelegate: AccountDelegate {
return account.update(articles, statusKey: statusKey, flag: flag) return account.update(articles, statusKey: statusKey, flag: flag)
} }
func accountDidInitialize(_ account: Account) { func accountDidInitialize(_ account: Account) {
accountMetadata = account.metadata accountMetadata = account.metadata
credentials = try? account.retrieveGoogleAuthCredentials() credentials = try? account.retrieveGoogleAuthCredentials()
@ -587,7 +555,7 @@ private extension GoogleReaderCompatibleAccountDelegate {
account.addFeed(feed) account.addFeed(feed)
clearFolderRelationship(for: feed, withFolderName: folder.name ?? "") clearFolderRelationship(for: feed, withFolderName: folder.name ?? "")
} }
account.deleteFolder(folder) account.removeFolder(folder)
} }
} }
} }
@ -839,73 +807,7 @@ private extension GoogleReaderCompatibleAccountDelegate {
} }
func processRestoredFeed(for account: Account, feed: Feed, editedName: String?, folder: Folder?, completion: @escaping (Result<Void, Error>) -> Void) {
if let folder = folder {
addFeed(for: account, to: folder, with: feed) { result in
switch result {
case .success:
if editedName != nil {
DispatchQueue.main.async {
account.removeFeed(feed)
folder.addFeed(feed)
}
self.processRestoredFeedName(for: account, feed: feed, editedName: editedName!, completion: completion)
} else {
DispatchQueue.main.async {
account.removeFeed(feed)
folder.addFeed(feed)
completion(.success(()))
}
}
case .failure(let error):
DispatchQueue.main.async {
completion(.failure(error))
}
}
}
} else {
DispatchQueue.main.async {
account.addFeed(feed)
}
if editedName != nil {
processRestoredFeedName(for: account, feed: feed, editedName: editedName!, completion: completion)
} else {
DispatchQueue.main.async {
completion(.success(()))
}
}
}
}
func processRestoredFeedName(for account: Account, feed: Feed, editedName: String, completion: @escaping (Result<Void, Error>) -> Void) {
renameFeed(for: account, with: feed, to: editedName) { result in
switch result {
case .success:
DispatchQueue.main.async {
feed.editedName = editedName
completion(.success(()))
}
case .failure(let error):
DispatchQueue.main.async {
completion(.failure(error))
}
}
}
}
func clearFolderRelationship(for feed: Feed, withFolderName folderName: String) { func clearFolderRelationship(for feed: Feed, withFolderName folderName: String) {
if var folderRelationship = feed.folderRelationship { if var folderRelationship = feed.folderRelationship {
@ -923,7 +825,7 @@ private extension GoogleReaderCompatibleAccountDelegate {
} }
} }
func decideBestFeedChoice(account: Account, url: String, choices: [GoogleReaderCompatibleSubscriptionChoice], completion: @escaping (Result<Feed, Error>) -> Void) { func decideBestFeedChoice(account: Account, url: String, name: String?, container: Container, choices: [GoogleReaderCompatibleSubscriptionChoice], completion: @escaping (Result<Feed, Error>) -> Void) {
let feedSpecifiers: [FeedSpecifier] = choices.map { choice in let feedSpecifiers: [FeedSpecifier] = choices.map { choice in
let source = url == choice.url ? FeedSpecifier.Source.UserEntered : FeedSpecifier.Source.HTMLLink let source = url == choice.url ? FeedSpecifier.Source.UserEntered : FeedSpecifier.Source.HTMLLink
@ -933,7 +835,7 @@ private extension GoogleReaderCompatibleAccountDelegate {
if let bestSpecifier = FeedSpecifier.bestFeed(in: Set(feedSpecifiers)) { if let bestSpecifier = FeedSpecifier.bestFeed(in: Set(feedSpecifiers)) {
if let bestSubscription = choices.filter({ bestSpecifier.urlString == $0.url }).first { if let bestSubscription = choices.filter({ bestSpecifier.urlString == $0.url }).first {
createFeed(for: account, url: bestSubscription.url, completion: completion) createFeed(for: account, url: bestSubscription.url, name: name, container: container, completion: completion)
} else { } else {
DispatchQueue.main.async { DispatchQueue.main.async {
completion(.failure(GoogleReaderCompatibleAccountDelegateError.invalidParameter)) completion(.failure(GoogleReaderCompatibleAccountDelegateError.invalidParameter))
@ -947,13 +849,39 @@ private extension GoogleReaderCompatibleAccountDelegate {
} }
func createFeed( account: Account, subscription sub: GoogleReaderCompatibleSubscription, completion: @escaping (Result<Feed, Error>) -> Void) { func createFeed( account: Account, subscription sub: GoogleReaderCompatibleSubscription, name: String?, container: Container, completion: @escaping (Result<Feed, Error>) -> Void) {
DispatchQueue.main.async { DispatchQueue.main.async {
let feed = account.createFeed(with: sub.name, url: sub.url, feedID: String(sub.feedID), homePageURL: sub.homePageURL) let feed = account.createFeed(with: sub.name, url: sub.url, feedID: String(sub.feedID), homePageURL: sub.homePageURL)
feed.subscriptionID = String(sub.subscriptionID) feed.subscriptionID = String(sub.subscriptionID)
account.addFeed(feed, to: container) { result in
switch result {
case .success:
if let name = name {
account.renameFeed(feed, to: name) { result in
switch result {
case .success:
self.initialFeedDownload(account: account, feed: feed, completion: completion)
case .failure(let error):
completion(.failure(error))
}
}
} else {
self.initialFeedDownload(account: account, feed: feed, completion: completion)
}
case .failure(let error):
completion(.failure(error))
}
}
}
}
func initialFeedDownload( account: Account, feed: Feed, completion: @escaping (Result<Feed, Error>) -> Void) {
// Download the initial articles // Download the initial articles
self.caller.retrieveEntries(feedID: feed.feedID) { result in self.caller.retrieveEntries(feedID: feed.feedID) { result in
@ -973,12 +901,7 @@ private extension GoogleReaderCompatibleAccountDelegate {
} }
case .failure(let error): case .failure(let error):
os_log(.error, log: self.log, "Initial articles download failed: %@.", error.localizedDescription) completion(.failure(error))
DispatchQueue.main.async {
completion(.success(feed))
}
}
} }
} }
@ -1211,4 +1134,62 @@ private extension GoogleReaderCompatibleAccountDelegate {
} }
func deleteTagging(for account: Account, with feed: Feed, from container: Container?, completion: @escaping (Result<Void, Error>) -> Void) {
if let folder = container as? Folder, let feedTaggingID = feed.folderRelationship?[folder.name ?? ""] {
caller.deleteTagging(taggingID: feedTaggingID) { result in
switch result {
case .success:
DispatchQueue.main.async {
self.clearFolderRelationship(for: feed, withFolderName: folder.name ?? "")
folder.removeFeed(feed)
account.addFeedIfNotInAnyFolder(feed)
completion(.success(()))
}
case .failure(let error):
DispatchQueue.main.async {
let wrappedError = AccountError.wrappedError(error: error, account: account)
completion(.failure(wrappedError))
}
}
}
} else {
if let account = container as? Account {
account.removeFeed(feed)
}
completion(.success(()))
}
}
func deleteSubscription(for account: Account, with feed: Feed, from container: Container?, completion: @escaping (Result<Void, Error>) -> Void) {
// This error should never happen
guard let subscriptionID = feed.subscriptionID else {
completion(.failure(FeedbinAccountDelegateError.invalidParameter))
return
}
caller.deleteSubscription(subscriptionID: subscriptionID) { result in
switch result {
case .success:
DispatchQueue.main.async {
account.removeFeed(feed)
if let folders = account.folders {
for folder in folders {
folder.removeFeed(feed)
}
}
completion(.success(()))
}
case .failure(let error):
DispatchQueue.main.async {
let wrappedError = AccountError.wrappedError(error: error, account: account)
completion(.failure(wrappedError))
}
}
}
}
} }