NetNewsWire/Shared/Extensions/AddFeedDefaultContainer.swift

52 lines
1.4 KiB
Swift
Raw Normal View History

//
2024-11-02 19:08:58 +01:00
// AddFeedDefaultContainer.swift
// 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 {
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) {
return folder
} else {
return substituteContainerIfNeeded(account: account)
}
} else if let account = AccountManager.shared.sortedActiveAccounts.first {
return substituteContainerIfNeeded(account: account)
} else {
return nil
}
}
static func saveDefaultContainer(_ container: Container) {
2024-11-02 05:58:47 +01:00
AppDefaults.shared.addFeedAccountID = container.account?.accountID
if let folder = container as? Folder {
2024-11-02 05:58:47 +01:00
AppDefaults.shared.addFeedFolderName = folder.nameForDisplay
} else {
2024-11-02 05:58:47 +01:00
AppDefaults.shared.addFeedFolderName = nil
}
}
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
}
}
}
}