2019-11-16 20:25:55 +01:00
|
|
|
//
|
2024-02-26 08:12:21 +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-03-26 05:10:37 +01:00
|
|
|
@MainActor struct AddFeedDefaultContainer {
|
2019-11-16 20:25:55 +01:00
|
|
|
|
2024-03-20 07:05:30 +01:00
|
|
|
@MainActor static var defaultContainer: Container? {
|
2019-11-16 20:25:55 +01:00
|
|
|
|
2024-02-26 08:12:21 +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-02-26 08:12:21 +01:00
|
|
|
AppDefaults.shared.addFeedAccountID = container.account?.accountID
|
2019-11-16 20:25:55 +01:00
|
|
|
if let folder = container as? Folder {
|
2024-02-26 08:12:21 +01:00
|
|
|
AppDefaults.shared.addFeedFolderName = folder.nameForDisplay
|
2019-11-16 20:25:55 +01:00
|
|
|
} else {
|
2024-02-26 08:12:21 +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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|