Retrieve credentials if necessary. On iOS credentials aren't always available when the delegate initializes.

This commit is contained in:
Maurice Parker 2019-06-23 13:56:11 -05:00
parent 6fada650da
commit 9653befb94
1 changed files with 31 additions and 10 deletions

View File

@ -75,6 +75,7 @@ final class FeedbinAccountDelegate: AccountDelegate {
var refreshProgress = DownloadProgress(numberOfTasks: 0)
func refreshAll(for account: Account, completion: @escaping (Result<Void, Error>) -> Void) {
retrieveCredentialsIfNecessary(account)
refreshProgress.addToNumberOfTasksAndRemaining(6)
@ -108,6 +109,7 @@ final class FeedbinAccountDelegate: AccountDelegate {
}
func sendArticleStatus(for account: Account, completion: @escaping (() -> Void)) {
retrieveCredentialsIfNecessary(account)
os_log(.debug, log: log, "Sending article statuses...")
@ -147,7 +149,8 @@ final class FeedbinAccountDelegate: AccountDelegate {
}
func refreshArticleStatus(for account: Account, completion: @escaping (() -> Void)) {
retrieveCredentialsIfNecessary(account)
os_log(.debug, log: log, "Refreshing article statuses...")
let group = DispatchGroup()
@ -186,7 +189,8 @@ final class FeedbinAccountDelegate: AccountDelegate {
}
func importOPML(for account:Account, opmlFile: URL, completion: @escaping (Result<Void, Error>) -> Void) {
retrieveCredentialsIfNecessary(account)
var fileData: Data?
do {
@ -232,6 +236,7 @@ final class FeedbinAccountDelegate: AccountDelegate {
}
func addFolder(for account: Account, name: String, completion: @escaping (Result<Folder, Error>) -> Void) {
retrieveCredentialsIfNecessary(account)
if let folder = account.ensureFolder(with: name) {
completion(.success(folder))
} else {
@ -240,7 +245,8 @@ final class FeedbinAccountDelegate: AccountDelegate {
}
func renameFolder(for account: Account, with folder: Folder, to name: String, completion: @escaping (Result<Void, Error>) -> Void) {
retrieveCredentialsIfNecessary(account)
guard folder.hasAtLeastOneFeed() else {
folder.name = name
return
@ -267,7 +273,8 @@ final class FeedbinAccountDelegate: AccountDelegate {
}
func removeFolder(for account: Account, with folder: Folder, completion: @escaping (Result<Void, Error>) -> Void) {
retrieveCredentialsIfNecessary(account)
// 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 {
account.removeFolder(folder)
@ -329,7 +336,8 @@ final class FeedbinAccountDelegate: AccountDelegate {
}
func createFeed(for account: Account, url: String, name: String?, container: Container, completion: @escaping (Result<Feed, Error>) -> Void) {
retrieveCredentialsIfNecessary(account)
refreshProgress.addToNumberOfTasksAndRemaining(1)
caller.createSubscription(url: url) { result in
self.refreshProgress.completeTask()
@ -361,7 +369,8 @@ final class FeedbinAccountDelegate: AccountDelegate {
}
func renameFeed(for account: Account, with feed: Feed, to name: String, completion: @escaping (Result<Void, Error>) -> Void) {
retrieveCredentialsIfNecessary(account)
// This error should never happen
guard let subscriptionID = feed.subscriptionID else {
completion(.failure(FeedbinAccountDelegateError.invalidParameter))
@ -388,6 +397,7 @@ final class FeedbinAccountDelegate: AccountDelegate {
}
func removeFeed(for account: Account, with feed: Feed, from container: Container?, completion: @escaping (Result<Void, Error>) -> Void) {
retrieveCredentialsIfNecessary(account)
if feed.folderRelationship?.count ?? 0 > 1 {
deleteTagging(for: account, with: feed, from: container, completion: completion)
} else {
@ -396,6 +406,7 @@ final class FeedbinAccountDelegate: AccountDelegate {
}
func moveFeed(for account: Account, with feed: Feed, from: Container, to: Container, completion: @escaping (Result<Void, Error>) -> Void) {
retrieveCredentialsIfNecessary(account)
if from is Account {
addFeed(for: account, with: feed, to: to, completion: completion)
} else {
@ -411,7 +422,8 @@ final class FeedbinAccountDelegate: AccountDelegate {
}
func addFeed(for account: Account, with feed: Feed, to container: Container, completion: @escaping (Result<Void, Error>) -> Void) {
retrieveCredentialsIfNecessary(account)
if let folder = container as? Folder, let feedID = Int(feed.feedID) {
refreshProgress.addToNumberOfTasksAndRemaining(1)
caller.createTagging(feedID: feedID, name: folder.name ?? "") { result in
@ -443,7 +455,8 @@ final class FeedbinAccountDelegate: AccountDelegate {
}
func restoreFeed(for account: Account, feed: Feed, container: Container, completion: @escaping (Result<Void, Error>) -> Void) {
retrieveCredentialsIfNecessary(account)
if let existingFeed = account.existingFeed(withURL: feed.url) {
account.addFeed(existingFeed, to: container) { result in
switch result {
@ -467,7 +480,8 @@ final class FeedbinAccountDelegate: AccountDelegate {
}
func restoreFolder(for account: Account, folder: Folder, completion: @escaping (Result<Void, Error>) -> Void) {
retrieveCredentialsIfNecessary(account)
let group = DispatchGroup()
for feed in folder.topLevelFeeds {
@ -495,7 +509,8 @@ final class FeedbinAccountDelegate: AccountDelegate {
}
func markArticles(for account: Account, articles: Set<Article>, statusKey: ArticleStatus.Key, flag: Bool) -> Set<Article>? {
retrieveCredentialsIfNecessary(account)
let syncStatuses = articles.map { article in
return SyncStatus(articleID: article.articleID, key: statusKey, flag: flag)
}
@ -1263,5 +1278,11 @@ private extension FeedbinAccountDelegate {
}
}
func retrieveCredentialsIfNecessary(_ account: Account) {
if credentials == nil {
credentials = try? account.retrieveCredentials()
}
}
}