Do some cleanups on app code.

This commit is contained in:
Brent Simmons 2017-09-23 12:17:14 -07:00
parent 96bc93612e
commit 1ab282c23a
4 changed files with 34 additions and 34 deletions

View File

@ -23,6 +23,8 @@ final class AppDefaults {
static let openInBrowserInBackground = "openInBrowserInBackground"
}
let isFirstRun: Bool
var firstRunDate: Date? {
get {
return date(for: Key.firstRunDate)
@ -43,6 +45,14 @@ final class AppDefaults {
init() {
registerDefaults()
if self.firstRunDate == nil {
self.isFirstRun = true
self.firstRunDate = Date()
}
else {
self.isFirstRun = false
}
}
func registerDefaults() {

View File

@ -15,6 +15,7 @@ import RSParser
import RSWeb
import Account
let appName = "Evergreen"
var currentTheme: VSTheme!
@NSApplicationMain
@ -45,19 +46,13 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserInterfaceValidations {
func applicationDidFinishLaunching(_ note: Notification) {
registerDefaults()
let isFirstRun = AppDefaults.shared.isFirstRun
currentTheme = themeLoader.defaultTheme
let _ = AccountManager.sharedInstance
var isFirstRun = false
if UserDefaults.standard.object(forKey: AppDefaultsKey.firstRunDate) == nil {
isFirstRun = true
UserDefaults.standard.set(Date(), forKey: AppDefaultsKey.firstRunDate)
}
importDefaultFeedsIfNeeded(isFirstRun, account: AccountManager.sharedInstance.localAccount)
importDefaultFeedsIfNeeded(isFirstRun, account: AccountManager.shared.localAccount)
createAndShowMainWindow()
#if RELEASE
@ -104,7 +99,6 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserInterfaceValidations {
}
}
// MARK: Badge
private func updateBadgeCoalesced() {
@ -122,7 +116,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserInterfaceValidations {
func unreadCountDidChange(_ note: Notification) {
let updatedUnreadCount = AccountManager.sharedInstance.unreadCount
let updatedUnreadCount = AccountManager.shared.unreadCount
if updatedUnreadCount != unreadCount {
unreadCount = updatedUnreadCount
}
@ -150,10 +144,10 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserInterfaceValidations {
func validateUserInterfaceItem(_ item: NSValidatedUserInterfaceItem) -> Bool {
if item.action == #selector(refreshAll(_:)) {
return !AccountManager.sharedInstance.refreshInProgress
return !AccountManager.shared.refreshInProgress
}
if item.action == #selector(addAppNews(_:)) {
return !AccountManager.sharedInstance.anyAccountHasFeedWithURL(appNewsURLString)
return !AccountManager.shared.anyAccountHasFeedWithURL(appNewsURLString)
}
return true
}
@ -168,7 +162,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserInterfaceValidations {
addFeedController?.showAddFeedSheet(urlString, name)
}
// MARK: Actions
// MARK: - Actions
@IBAction func showPreferences(_ sender: AnyObject) {
@ -223,15 +217,13 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserInterfaceValidations {
panel.allowsOtherFileTypes = false
let result = panel.runModal()
if result == NSApplication.ModalResponse.OK {
if let url = panel.url {
DispatchQueue.main.async {
self.parseAndImportOPML(url, AccountManager.sharedInstance.localAccount)
}
if result == NSApplication.ModalResponse.OK, let url = panel.url {
DispatchQueue.main.async {
self.parseAndImportOPML(url, AccountManager.sharedInstance.localAccount)
}
}
}
@IBAction func importOPMLFromURL(_ sender: AnyObject) {
}
@ -249,21 +241,19 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserInterfaceValidations {
panel.nameFieldStringValue = "MySubscriptions.opml"
let result = panel.runModal()
if result.rawValue == NSFileHandlingPanelOKButton {
if let url = panel.url {
DispatchQueue.main.async {
let opmlString = AccountManager.sharedInstance.localAccount.OPMLString(indentLevel: 0)
do {
try opmlString.write(to: url, atomically: true, encoding: String.Encoding.utf8)
}
catch let error as NSError {
NSApplication.shared.presentError(error)
}
if result.rawValue == NSFileHandlingPanelOKButton, let url = panel.url {
DispatchQueue.main.async {
let opmlString = AccountManager.sharedInstance.localAccount.OPMLString(indentLevel: 0)
do {
try opmlString.write(to: url, atomically: true, encoding: String.Encoding.utf8)
}
catch let error as NSError {
NSApplication.shared.presentError(error)
}
}
}
}
@IBAction func emailSupport(_ sender: AnyObject) {
let escapedAppName = appName.replacingOccurrences(of: " ", with: "%20")
@ -276,7 +266,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserInterfaceValidations {
@IBAction func addAppNews(_ sender: AnyObject) {
if AccountManager.sharedInstance.anyAccountHasFeedWithURL(appNewsURLString) {
if AccountManager.shared.anyAccountHasFeedWithURL(appNewsURLString) {
return
}
addFeed(appNewsURLString, "Evergreen News")
@ -303,6 +293,8 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserInterfaceValidations {
}
}
// MARK: -
private extension AppDelegate {
func parseAndImportOPML(_ url: URL, _ account: Account) {

View File

@ -8,8 +8,6 @@
import Foundation
let appName = "Evergreen"
extension Notification.Name {
static let SidebarSelectionDidChange = Notification.Name("SidebarSelectionDidChangeNotification")

View File

@ -17,7 +17,7 @@ private let localAccountIdentifier = "OnMyMac"
public final class AccountManager: UnreadCountProvider {
public static let sharedInstance = AccountManager()
public static let shared = AccountManager()
public let localAccount: Account
private let accountsFolder = RSDataSubfolder(nil, "Accounts")!
private var accountsDictionary = [String: Account]()