2017-11-04 20:19:34 +01:00
|
|
|
//
|
|
|
|
// FeedListFeed.swift
|
|
|
|
// Evergreen
|
|
|
|
//
|
|
|
|
// Created by Brent Simmons on 11/4/17.
|
|
|
|
// Copyright © 2017 Ranchero Software. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
2017-11-04 22:53:21 +01:00
|
|
|
import RSCore
|
2018-01-25 07:20:22 +01:00
|
|
|
import RSParser
|
|
|
|
import RSWeb
|
|
|
|
|
|
|
|
extension Notification.Name {
|
|
|
|
|
|
|
|
public static let FeedListFeedDidBecomeAvailable = Notification.Name(rawValue: "FeedListFeedDidBecomeAvailable")
|
|
|
|
}
|
2017-11-04 20:19:34 +01:00
|
|
|
|
2017-11-05 07:05:20 +01:00
|
|
|
final class FeedListFeed: Hashable, DisplayNameProvider {
|
2017-11-04 20:19:34 +01:00
|
|
|
|
|
|
|
let name: String
|
|
|
|
let url: String
|
|
|
|
let homePageURL: String
|
|
|
|
let hashValue: Int
|
2018-01-25 07:20:22 +01:00
|
|
|
var lastDownloadAttemptDate: Date? = nil
|
|
|
|
|
|
|
|
var parsedFeed: ParsedFeed? = nil {
|
|
|
|
didSet {
|
|
|
|
postFeedListFeedDidBecomeAvailableNotification()
|
|
|
|
}
|
|
|
|
}
|
2017-11-04 20:19:34 +01:00
|
|
|
|
2017-11-04 22:53:21 +01:00
|
|
|
var nameForDisplay: String { // DisplayNameProvider
|
|
|
|
get {
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-04 20:19:34 +01:00
|
|
|
init(name: String, url: String, homePageURL: String) {
|
|
|
|
|
|
|
|
self.name = name
|
|
|
|
self.url = url
|
|
|
|
self.homePageURL = homePageURL
|
|
|
|
self.hashValue = url.hashValue
|
|
|
|
}
|
|
|
|
|
|
|
|
private struct Key {
|
|
|
|
static let name = "name"
|
|
|
|
static let editedName = "editedName" // Used in DefaultFeeds.plist
|
|
|
|
static let url = "url"
|
|
|
|
static let homePageURL = "homePageURL"
|
|
|
|
}
|
|
|
|
|
2017-11-05 07:05:20 +01:00
|
|
|
convenience init(dictionary: [String: String]) {
|
2017-11-04 20:19:34 +01:00
|
|
|
|
|
|
|
let name = (dictionary[Key.name] ?? dictionary[Key.editedName])!
|
|
|
|
let url = dictionary[Key.url]!
|
|
|
|
let homePageURL = dictionary[Key.homePageURL]!
|
|
|
|
|
|
|
|
self.init(name: name, url: url, homePageURL: homePageURL)
|
|
|
|
}
|
|
|
|
|
2018-01-25 07:20:22 +01:00
|
|
|
func downloadIfNeeded() {
|
|
|
|
|
|
|
|
guard let lastDownloadAttemptDate = lastDownloadAttemptDate else {
|
|
|
|
downloadFeed()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let cutoffDate = Date().addingTimeInterval(-(30 * 60)) // 30 minutes in the past
|
|
|
|
if lastDownloadAttemptDate < cutoffDate {
|
|
|
|
downloadFeed()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-04 20:19:34 +01:00
|
|
|
static func ==(lhs: FeedListFeed, rhs: FeedListFeed) -> Bool {
|
|
|
|
|
|
|
|
return lhs.hashValue == rhs.hashValue && lhs.url == rhs.url && lhs.name == rhs.name && lhs.homePageURL == rhs.homePageURL
|
|
|
|
}
|
|
|
|
}
|
2018-01-25 07:20:22 +01:00
|
|
|
|
|
|
|
private extension FeedListFeed {
|
|
|
|
|
|
|
|
func postFeedListFeedDidBecomeAvailableNotification() {
|
|
|
|
|
|
|
|
NotificationCenter.default.post(name: .FeedListFeedDidBecomeAvailable, object: self, userInfo: nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func downloadFeed() {
|
|
|
|
|
|
|
|
lastDownloadAttemptDate = Date()
|
|
|
|
guard let feedURL = URL(string: url) else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
downloadUsingCache(feedURL) { (data, response, error) in
|
|
|
|
|
|
|
|
guard let data = data, error == nil else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let parserData = ParserData(url: self.url, data: data)
|
|
|
|
FeedParser.parse(parserData) { (parsedFeed, error) in
|
|
|
|
|
|
|
|
if let parsedFeed = parsedFeed, parsedFeed.items.count > 0 {
|
|
|
|
self.parsedFeed = parsedFeed
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|