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.homePageURL = homePageURL
addFeed(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) {
let since: Date = {

View File

@ -839,12 +839,39 @@ private extension FeedbinAccountDelegate {
}
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)
feed.subscriptionID = String(sub.subscriptionID)
// 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)) {
@ -866,6 +893,7 @@ private extension FeedbinAccountDelegate {
case .failure(let error):
guard let self = self else { return }
os_log(.error, log: self.log, "Refresh articles failed: %@.", error.localizedDescription)
completion()
}
}
@ -914,6 +942,8 @@ private extension FeedbinAccountDelegate {
group.leave()
}
}
} else {
group.leave()
}
}