Add initial feed download for Feedbin.

This commit is contained in:
Maurice Parker 2019-05-13 11:18:54 -05:00
parent ccac456d77
commit eede1cc7eb
3 changed files with 59 additions and 2 deletions

View File

@ -382,6 +382,8 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container,
feed.name = name feed.name = name
feed.homePageURL = homePageURL feed.homePageURL = homePageURL
addFeed(feed)
return feed return feed
} }

View File

@ -299,6 +299,31 @@ final class FeedbinAPICaller: NSObject {
} }
func retrieveEntries(feedID: String, completion: @escaping (Result<([FeedbinEntry]?, String?), Error>) -> Void) {
let since = Calendar.current.date(byAdding: .month, value: -3, to: Date()) ?? Date()
let sinceString = FeedbinDate.formatter.string(from: since)
var callURL = URLComponents(url: feedbinBaseURL.appendingPathComponent("/feeds/\(feedID)/entries.json"), resolvingAgainstBaseURL: false)!
callURL.queryItems = [URLQueryItem(name: "since", value: sinceString), URLQueryItem(name: "per_page", value: "100")]
let request = URLRequest(url: callURL.url!, credentials: credentials)
transport.send(request: request, resultType: [FeedbinEntry].self) { result in
switch result {
case .success(let (response, entries)):
let pagingInfo = HTTPLinkPagingInfo(urlResponse: response)
completion(.success((entries, pagingInfo.nextPage)))
case .failure(let error):
completion(.failure(error))
}
}
}
func retrieveEntries(completion: @escaping (Result<([FeedbinEntry]?, String?), Error>) -> Void) { func retrieveEntries(completion: @escaping (Result<([FeedbinEntry]?, String?), Error>) -> Void) {
let since: Date = { let since: Date = {

View File

@ -839,11 +839,38 @@ private extension FeedbinAccountDelegate {
} }
func createFeed( account: Account, subscription sub: FeedbinSubscription, completion: @escaping (Result<Feed, Error>) -> Void) { func createFeed( account: Account, subscription sub: FeedbinSubscription, completion: @escaping (Result<Feed, Error>) -> Void) {
DispatchQueue.main.async {
DispatchQueue.main.async { [weak self] in
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)
completion(.success(feed))
// Download the initial articles
self?.caller.retrieveEntries(feedID: feed.feedID) { [weak self] result in
switch result {
case .success(let (entries, page)):
self?.processEntries(account: account, entries: entries) {
self?.refreshArticles(account, page: page) {
DispatchQueue.main.async {
completion(.success(feed))
}
}
}
case .failure(let error):
guard let self = self else { return }
os_log(.error, log: self.log, "Initial articles download failed: %@.", error.localizedDescription)
DispatchQueue.main.async {
completion(.success(feed))
}
}
}
} }
} }
func refreshArticles(_ account: Account, completion: @escaping (() -> Void)) { func refreshArticles(_ account: Account, completion: @escaping (() -> Void)) {
@ -866,6 +893,7 @@ private extension FeedbinAccountDelegate {
case .failure(let error): case .failure(let error):
guard let self = self else { return } guard let self = self else { return }
os_log(.error, log: self.log, "Refresh articles failed: %@.", error.localizedDescription) os_log(.error, log: self.log, "Refresh articles failed: %@.", error.localizedDescription)
completion()
} }
} }
@ -914,6 +942,8 @@ private extension FeedbinAccountDelegate {
group.leave() group.leave()
} }
} }
} else {
group.leave()
} }
} }