2019-11-16 20:25:55 +01:00
|
|
|
//
|
2024-11-02 19:08:58 +01:00
|
|
|
// AddFeedDefaultContainer.swift
|
2019-11-16 20:25:55 +01:00
|
|
|
// NetNewsWire-iOS
|
|
|
|
//
|
|
|
|
// Created by Maurice Parker on 11/16/19.
|
|
|
|
// Copyright © 2019 Ranchero Software. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import Account
|
|
|
|
|
2024-11-02 05:58:47 +01:00
|
|
|
struct AddFeedDefaultContainer {
|
2019-11-16 20:25:55 +01:00
|
|
|
|
|
|
|
static var defaultContainer: Container? {
|
|
|
|
|
2024-11-02 05:58:47 +01:00
|
|
|
if let accountID = AppDefaults.shared.addFeedAccountID, let account = AccountManager.shared.activeAccounts.first(where: { $0.accountID == accountID }) {
|
|
|
|
if let folderName = AppDefaults.shared.addFeedFolderName, let folder = account.existingFolder(withDisplayName: folderName) {
|
2019-11-16 20:25:55 +01:00
|
|
|
return folder
|
|
|
|
} else {
|
|
|
|
return substituteContainerIfNeeded(account: account)
|
|
|
|
}
|
|
|
|
} else if let account = AccountManager.shared.sortedActiveAccounts.first {
|
|
|
|
return substituteContainerIfNeeded(account: account)
|
|
|
|
} else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-11-17 02:44:01 +01:00
|
|
|
static func saveDefaultContainer(_ container: Container) {
|
2024-11-02 05:58:47 +01:00
|
|
|
AppDefaults.shared.addFeedAccountID = container.account?.accountID
|
2019-11-16 20:25:55 +01:00
|
|
|
if let folder = container as? Folder {
|
2024-11-02 05:58:47 +01:00
|
|
|
AppDefaults.shared.addFeedFolderName = folder.nameForDisplay
|
2019-11-16 20:25:55 +01:00
|
|
|
} else {
|
2024-11-02 05:58:47 +01:00
|
|
|
AppDefaults.shared.addFeedFolderName = nil
|
2019-11-16 20:25:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static func substituteContainerIfNeeded(account: Account) -> Container? {
|
|
|
|
if !account.behaviors.contains(.disallowFeedInRootFolder) {
|
|
|
|
return account
|
|
|
|
} else {
|
|
|
|
if let folder = account.sortedFolders?.first {
|
|
|
|
return folder
|
|
|
|
} else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|