From 21801b8b6a798678d67eaf01feb4fddf791f322a Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Fri, 26 May 2017 13:22:31 -0700 Subject: [PATCH] Add Browser and Defaults. --- Evergreen.xcodeproj/project.pbxproj | 16 +++++++++ Evergreen/AppConstants.swift | 2 +- Evergreen/Browser.swift | 27 ++++++++++++++ Evergreen/Preferences/Defaults.swift | 54 ++++++++++++++++++++++++++++ 4 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 Evergreen/Browser.swift create mode 100644 Evergreen/Preferences/Defaults.swift diff --git a/Evergreen.xcodeproj/project.pbxproj b/Evergreen.xcodeproj/project.pbxproj index fc50357b3..d30bc77a2 100644 --- a/Evergreen.xcodeproj/project.pbxproj +++ b/Evergreen.xcodeproj/project.pbxproj @@ -8,6 +8,8 @@ /* Begin PBXBuildFile section */ 842E45CE1ED8C308000A8B52 /* AppConstants.swift in Sources */ = {isa = PBXBuildFile; fileRef = 842E45CD1ED8C308000A8B52 /* AppConstants.swift */; }; + 842E45DD1ED8C54B000A8B52 /* Browser.swift in Sources */ = {isa = PBXBuildFile; fileRef = 842E45DC1ED8C54B000A8B52 /* Browser.swift */; }; + 842E45DF1ED8C582000A8B52 /* Defaults.swift in Sources */ = {isa = PBXBuildFile; fileRef = 842E45DE1ED8C582000A8B52 /* Defaults.swift */; }; 8471A2C41ED4CEBF008F099E /* DataModel.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 8471A2B71ED4CEAD008F099E /* DataModel.framework */; }; 8471A2C51ED4CEBF008F099E /* DataModel.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 8471A2B71ED4CEAD008F099E /* DataModel.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; 8471A2F51ED4D062008F099E /* LocalAccount.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 8471A2F21ED4D04D008F099E /* LocalAccount.framework */; }; @@ -301,6 +303,8 @@ /* Begin PBXFileReference section */ 842E45CD1ED8C308000A8B52 /* AppConstants.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = AppConstants.swift; path = Evergreen/AppConstants.swift; sourceTree = ""; }; + 842E45DC1ED8C54B000A8B52 /* Browser.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Browser.swift; path = Evergreen/Browser.swift; sourceTree = ""; }; + 842E45DE1ED8C582000A8B52 /* Defaults.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Defaults.swift; path = Evergreen/Preferences/Defaults.swift; sourceTree = ""; }; 8471A2B21ED4CEAD008F099E /* DataModel.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = DataModel.xcodeproj; path = Frameworks/DataModel/DataModel.xcodeproj; sourceTree = ""; }; 8471A2EC1ED4D04D008F099E /* LocalAccount.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = LocalAccount.xcodeproj; path = Frameworks/LocalAccount/LocalAccount.xcodeproj; sourceTree = ""; }; 849C64601ED37A5D003D8FC0 /* Evergreen.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Evergreen.app; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -350,6 +354,14 @@ /* End PBXFrameworksBuildPhase section */ /* Begin PBXGroup section */ + 842E45E01ED8C587000A8B52 /* Preferences */ = { + isa = PBXGroup; + children = ( + 842E45DE1ED8C582000A8B52 /* Defaults.swift */, + ); + name = Preferences; + sourceTree = ""; + }; 8471A2B31ED4CEAD008F099E /* Products */ = { isa = PBXGroup; children = ( @@ -375,6 +387,8 @@ 842E45CD1ED8C308000A8B52 /* AppConstants.swift */, 849C64631ED37A5D003D8FC0 /* AppDelegate.swift */, 849C64651ED37A5D003D8FC0 /* ViewController.swift */, + 842E45DC1ED8C54B000A8B52 /* Browser.swift */, + 842E45E01ED8C587000A8B52 /* Preferences */, 849C646C1ED37A5D003D8FC0 /* Info.plist */, 849C64741ED37A5D003D8FC0 /* EvergreenTests */, 849C64611ED37A5D003D8FC0 /* Products */, @@ -806,7 +820,9 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 842E45DD1ED8C54B000A8B52 /* Browser.swift in Sources */, 849C64661ED37A5D003D8FC0 /* ViewController.swift in Sources */, + 842E45DF1ED8C582000A8B52 /* Defaults.swift in Sources */, 842E45CE1ED8C308000A8B52 /* AppConstants.swift in Sources */, 849C64641ED37A5D003D8FC0 /* AppDelegate.swift in Sources */, ); diff --git a/Evergreen/AppConstants.swift b/Evergreen/AppConstants.swift index 4009e1daa..00bb959b0 100644 --- a/Evergreen/AppConstants.swift +++ b/Evergreen/AppConstants.swift @@ -1,6 +1,6 @@ // // AppConstants.swift -// Rainier +// Evergreen // // Created by Brent Simmons on 8/30/15. // Copyright © 2015 Ranchero Software, LLC. All rights reserved. diff --git a/Evergreen/Browser.swift b/Evergreen/Browser.swift new file mode 100644 index 000000000..2a6593205 --- /dev/null +++ b/Evergreen/Browser.swift @@ -0,0 +1,27 @@ +// +// Browser.swift +// Evergren +// +// Created by Brent Simmons on 2/23/16. +// Copyright © 2016 Ranchero Software, LLC. All rights reserved. +// + +import Foundation +import CoreServices +import RSWeb + + +func openInBrowser(_ urlString: String) { + + // Opens according to prefs. + let openInBackground = UserDefaults.standard.bool(forKey: OpenInBrowserInBackgroundKey) + openInBrowser(urlString, inBackground: openInBackground) +} + +func openInBrowser(_ urlString: String, inBackground: Bool) { + + if let url = URL(string: urlString) { + let _ = MacWebBrowser.openURL(url, inBackground: inBackground) + } +} + diff --git a/Evergreen/Preferences/Defaults.swift b/Evergreen/Preferences/Defaults.swift new file mode 100644 index 000000000..471bae2b7 --- /dev/null +++ b/Evergreen/Preferences/Defaults.swift @@ -0,0 +1,54 @@ +// +// Defaults.swift +// Evergreen +// +// Created by Brent Simmons on 2/20/16. +// Copyright © 2016 Ranchero Software, LLC. All rights reserved. +// + +import Foundation + +let SidebarFontSizeKey = "sidebarFontSize" +let TimelineFontSizeKey = "timelineFontSize" +let ArticleFontSizeKey = "articleFontSize" + +let SidebarFontSizeKVOKey = "values." + SidebarFontSizeKey +let TimelineFontSizeKVOKey = "values." + TimelineFontSizeKey +let ArticleFontSizeKVOKey = "values." + ArticleFontSizeKey + +let OpenInBrowserInBackgroundKey = "openInBrowserInBackground" + +enum FontSize: Int { + case small = 0 + case medium = 1 + case large = 2 + case veryLarge = 3 +} + +private let smallestFontSizeRawValue = FontSize.small.rawValue +private let largestFontSizeRawValue = FontSize.veryLarge.rawValue + +func registerDefaults() { + + let defaults = [SidebarFontSizeKey: FontSize.medium.rawValue, TimelineFontSizeKey: FontSize.medium.rawValue, ArticleFontSizeKey: FontSize.medium.rawValue] + + UserDefaults.standard.register(defaults: defaults) +} + +func timelineFontSize() -> FontSize { + + return fontSizeForKey(TimelineFontSizeKey) +} + +private func fontSizeForKey(_ key: String) -> FontSize { + + var rawFontSize = UserDefaults.standard.integer(forKey: key) + if rawFontSize < smallestFontSizeRawValue { + rawFontSize = smallestFontSizeRawValue + } + if rawFontSize > largestFontSizeRawValue { + rawFontSize = largestFontSizeRawValue + } + return FontSize(rawValue: rawFontSize)! +} +