NetNewsWire/Frameworks/Account/Account.swift

203 lines
4.2 KiB
Swift
Raw Normal View History

//
// Account.swift
// DataModel
//
// Created by Brent Simmons on 7/1/17.
// Copyright © 2017 Ranchero Software, LLC. All rights reserved.
//
import Foundation
import RSCore
import Data
2017-09-17 21:08:50 +02:00
import RSParser
2017-09-18 02:03:58 +02:00
import Database
public enum AccountType: Int {
// Raw values should not change since theyre stored on disk.
case onMyMac = 1
case feedly = 16
case feedbin = 17
case feedWrangler = 18
case newsBlur = 19
// TODO: more
}
2017-09-17 20:32:58 +02:00
public final class Account: DisplayNameProvider, Hashable {
2017-09-17 21:08:50 +02:00
public let accountID: String
public let type: AccountType
2017-09-17 21:08:50 +02:00
public var nameForDisplay = ""
public let delegate: AccountDelegate
public let hashValue: Int
let settingsFile: String
let dataFolder: String
2017-09-18 02:03:58 +02:00
let database: Database
var topLevelObjects = [Any]()
var feedIDDictionary = [String: Feed]()
var username: String?
var refreshInProgress = false
var supportsSubFolders;
2017-09-17 21:08:50 +02:00
init?(dataFolder: String, settingsFile: String, type: AccountType, accountID: String) {
2017-09-17 20:32:58 +02:00
switch type {
2017-09-18 02:03:58 +02:00
2017-09-17 21:08:50 +02:00
case .onMyMac:
2017-09-17 20:32:58 +02:00
self.delegate = LocalAccountDelegate()
default:
return nil
}
2017-09-18 02:03:58 +02:00
self.accountID = accountID
self.type = type
self.settingsFile = settingsFile
self.dataFolder = dataFolder
self.hashValue = accountID.hashValue
let databaseFilePath = (dataFolder as NSString).appendingPathComponent("DB.sqlite3")
self.database = Database(databaseFilePath: databaseFilePath, accountID: accountID)
pullObjectsFromDisk()
}
2017-09-17 20:32:58 +02:00
// MARK: - API
public func refreshAll() {
delegate.refreshAll(for: self)
}
2017-09-17 21:08:50 +02:00
func update(_ feed: Feed, with parsedFeed: ParsedFeed, _ completion: RSVoidCompletionBlock) {
// TODO
}
2017-09-17 21:54:08 +02:00
public func markArticles(_ articles: Set<Article>, statusKey: String, flag: Bool) {
database.mark(articles, statusKey: statusKey, flag: flag)
2017-09-18 01:30:45 +02:00
}
2017-09-17 22:07:55 +02:00
public func ensureFolder(with name: String) -> Folder? {
return nil //TODO
}
public func canAddFeed(_ feed: Feed, to folder: Folder?) -> Bool {
// If folder is nil, then it should go at the top level.
// The same feed in multiple folders is allowed.
// But the same feed cant appear twice in the same folder
// (or at the top level).
return true // TODO
}
public func addFeed(_ feed: Feed, to folder: Folder?) -> Bool {
// Return false if it couldnt be added.
// If it already existed in that folder, return true.
return true // TODO
}
2017-09-27 06:43:40 +02:00
public func canAddFolder(_ folder: Folder, to containingFolder: Folder?) -> Bool {
return false // TODO
}
2017-09-27 06:43:40 +02:00
public func addFolder(_ folder: Folder, to containingFolder: Folder?) -> Bool {
return false // TODO
}
public func importOPML(_ opmlDocument: RSOPMLDocument) {
// TODO
}
2017-09-17 20:32:58 +02:00
// MARK: - Equatable
2017-09-17 20:32:58 +02:00
public class func ==(lhs: Account, rhs: Account) -> Bool {
2017-09-17 20:32:58 +02:00
return lhs === rhs
}
}
// MARK: - Disk
extension Account {
public func plist() -> AnyObject? {
return nil // TODO
}
private struct Key {
static let children = "children"
}
func pullObjectsFromDisk() {
guard let d = NSDictionary(contentsOf: settingsFile) as? [String: Any] else {
return
}
guard let childrenArray = d[Key.children] as? [Any] else {
return
}
topLevelObjects = objects(with: childrenArray)
updateFeedIDDictionary()
}
func objects(with diskObjects: [[String: Any]]) -> [Any] {
return diskObjects.flatMap { object(with: $0) }
}
func object(with diskObject: Any) -> Any {
guard let d = diskObject as? [String: Any] else {
return nil
}
if diskObjectIsFeed(diskObject) {
return Feed(accountID: accountID, dictionary: diskObject)
}
return Folder(accountID: accountID, dictionary: diskObject)
}
private func diskObjectIsFeed(_ diskObject: [String: Any]) -> Bool {
return d[Feed.Key.url] != nil
}
}
// Mark: - FeedIDDictionary
private extension Account {
func updateFeedIDDictionary() {
var d = [String: Feed]()
for feed in flattenedFeeds() {
d[feed.feedID] = feed
}
feedIDDictionary = d
}
}
// MARK: - OPMLRepresentable
extension Account: OPMLRepresentable {
public func OPMLString(indentLevel: Int) -> String {
var s = ""
for oneObject in topLevelObjects {
if let oneOPMLObject = oneObject as? OPMLRepresentable {
s += oneOPMLObject.OPMLString(indentLevel: indentLevel + 1)
}
}
return s
}
}