diff --git a/Multiplatform/Shared/AppDefaults.swift b/Multiplatform/Shared/AppDefaults.swift index 1031c7ef0..270841646 100644 --- a/Multiplatform/Shared/AppDefaults.swift +++ b/Multiplatform/Shared/AppDefaults.swift @@ -26,13 +26,6 @@ enum UserInterfaceColorPalette: Int, CustomStringConvertible, CaseIterable { } } -enum FontSize: Int { - case small = 0 - case medium = 1 - case large = 2 - case veryLarge = 3 -} - final class AppDefaults: ObservableObject { #if os(macOS) @@ -62,26 +55,21 @@ final class AppDefaults: ObservableObject { static let addWebFeedAccountID = "addWebFeedAccountID" static let addWebFeedFolderName = "addWebFeedFolderName" static let addFolderAccountID = "addFolderAccountID" - static let timelineSortDirection = "timelineSortDirection" - - // iOS Defaults + static let userInterfaceColorPalette = "userInterfaceColorPalette" + static let timelineSortDirection = "timelineSortDirection" static let timelineGroupByFeed = "timelineGroupByFeed" - static let refreshClearsReadArticles = "refreshClearsReadArticles" - static let timelineNumberOfLines = "timelineNumberOfLines" - static let timelineIconSize = "timelineIconSize" - static let articleFullscreenAvailable = "articleFullscreenAvailable" + static let timelineIconDimensions = "timelineIconDimensions" + static let timelineNumberOfLines = "timelineNumberOfLines" + + // iOS Defaults + static let refreshClearsReadArticles = "refreshClearsReadArticles" + static let articleFullscreenAvailable = "articleFullscreenAvailable" static let articleFullscreenEnabled = "articleFullscreenEnabled" static let confirmMarkAllAsRead = "confirmMarkAllAsRead" // macOS Defaults - static let windowState = "windowState" - static let sidebarFontSize = "sidebarFontSize" - static let timelineFontSize = "timelineFontSize" - static let detailFontSize = "detailFontSize" static let openInBrowserInBackground = "openInBrowserInBackground" - static let importOPMLAccountID = "importOPMLAccountID" - static let exportOPMLAccountID = "exportOPMLAccountID" static let defaultBrowserID = "defaultBrowserID" static let checkForUpdatesAutomatically = "checkForUpdatesAutomatically" static let downloadTestBuilds = "downloadTestBuild" @@ -99,9 +87,6 @@ final class AppDefaults: ObservableObject { } - private static let smallestFontSizeRawValue = FontSize.small.rawValue - private static let largestFontSizeRawValue = FontSize.veryLarge.rawValue - // MARK: Development Builds let isDeveloperBuild: Bool = { if let dev = Bundle.main.object(forInfoDictionaryKey: "DeveloperEntitlements") as? String, dev == "-dev" { @@ -193,7 +178,7 @@ final class AppDefaults: ObservableObject { } } - @AppStorage(wrappedValue: 40.0, Key.timelineIconSize, store: store) var timelineIconSize: Double { + @AppStorage(wrappedValue: 40.0, Key.timelineIconDimensions, store: store) var timelineIconDimensions: Double { didSet { objectWillChange.send() } @@ -220,64 +205,12 @@ final class AppDefaults: ObservableObject { } // MARK: Window State - var windowState: [AnyHashable : Any]? { - get { - return AppDefaults.store.object(forKey: Key.windowState) as? [AnyHashable : Any] - } - set { - UserDefaults.standard.set(newValue, forKey: Key.windowState) - objectWillChange.send() - } - } - @AppStorage(wrappedValue: false, Key.openInBrowserInBackground, store: store) var openInBrowserInBackground: Bool { didSet { objectWillChange.send() } } - var sidebarFontSize: FontSize { - get { - return fontSize(for: Key.sidebarFontSize) - } - set { - AppDefaults.store.set(newValue.rawValue, forKey: Key.sidebarFontSize) - objectWillChange.send() - } - } - - var timelineFontSize: FontSize { - get { - return fontSize(for: Key.timelineFontSize) - } - set { - AppDefaults.store.set(newValue.rawValue, forKey: Key.timelineFontSize) - objectWillChange.send() - } - } - - var detailFontSize: FontSize { - get { - return fontSize(for: Key.detailFontSize) - } - set { - AppDefaults.store.set(newValue.rawValue, forKey: Key.detailFontSize) - objectWillChange.send() - } - } - - @AppStorage(Key.importOPMLAccountID, store: store) var importOPMLAccountID: String? { - didSet { - objectWillChange.send() - } - } - - @AppStorage(Key.exportOPMLAccountID, store: store) var exportOPMLAccountID: String? { - didSet { - objectWillChange.send() - } - } - @AppStorage(Key.defaultBrowserID, store: store) var defaultBrowserID: String? { didSet { objectWillChange.send() @@ -333,6 +266,21 @@ final class AppDefaults: ObservableObject { objectWillChange.send() } } + + static func registerDefaults() { + let defaults: [String : Any] = [Key.userInterfaceColorPalette: UserInterfaceColorPalette.automatic.rawValue, + Key.timelineGroupByFeed: false, + Key.refreshClearsReadArticles: false, + Key.timelineNumberOfLines: 2, + Key.timelineIconDimensions: 40, + Key.timelineSortDirection: ComparisonResult.orderedDescending.rawValue, + Key.articleFullscreenAvailable: false, + Key.articleFullscreenEnabled: false, + Key.confirmMarkAllAsRead: true, + "NSScrollViewShouldScrollUnderTitlebar": false, + Key.refreshInterval: RefreshInterval.everyHour.rawValue] + AppDefaults.store.register(defaults: defaults) + } } @@ -346,8 +294,4 @@ extension AppDefaults { return true } - func fontSize(for key: String) -> FontSize { - // Punted till after 1.0. - return .medium - } } diff --git a/Multiplatform/Shared/MainApp.swift b/Multiplatform/Shared/MainApp.swift index ce26ce6cf..3bd88b9ac 100644 --- a/Multiplatform/Shared/MainApp.swift +++ b/Multiplatform/Shared/MainApp.swift @@ -150,7 +150,9 @@ struct MainApp: App { SceneNavigationView() .environmentObject(sceneModel) .environmentObject(defaults) - }.commands { + .modifier(PreferredColorSchemeModifier(preferredColorScheme: defaults.userInterfaceColorPalette)) + } + .commands { CommandGroup(after: .newItem, addition: { Button("New Feed", action: {}) .keyboardShortcut("N") @@ -197,3 +199,20 @@ struct MainApp: App { #endif } } + +struct PreferredColorSchemeModifier: ViewModifier { + + var preferredColorScheme: UserInterfaceColorPalette + + @ViewBuilder + func body(content: Content) -> some View { + switch preferredColorScheme { + case .automatic: + content + case .dark: + content.preferredColorScheme(.dark) + case .light: + content.preferredColorScheme(.light) + } + } +} diff --git a/Multiplatform/Shared/Sidebar/SidebarToolbar.swift b/Multiplatform/Shared/Sidebar/SidebarToolbar.swift index 2777e4c3e..48705d4bb 100644 --- a/Multiplatform/Shared/Sidebar/SidebarToolbar.swift +++ b/Multiplatform/Shared/Sidebar/SidebarToolbar.swift @@ -9,6 +9,7 @@ import SwiftUI struct SidebarToolbar: View { +<<<<<<< HEAD enum ToolbarSheets { case none, web, twitter, reddit, folder, settings @@ -21,6 +22,12 @@ struct SidebarToolbar: View { } } @State private var showActionSheet: Bool = false +======= + + @EnvironmentObject private var appSettings: AppDefaults + @State private var showSettings: Bool = false + @State private var showAddSheet: Bool = false +>>>>>>> pr/7 var addActionSheetButtons = [ Button(action: {}, label: { Text("Add Feed") }) @@ -66,6 +73,7 @@ struct SidebarToolbar: View { .padding(.top, 4) } .background(VisualEffectBlur(blurStyle: .systemChromeMaterial).edgesIgnoringSafeArea(.bottom)) +<<<<<<< HEAD .sheet(isPresented: $showSheet, onDismiss: { sheetToShow = .none }) { switch sheetToShow { case .web: @@ -73,6 +81,10 @@ struct SidebarToolbar: View { default: EmptyView() } +======= + .sheet(isPresented: $showSettings, onDismiss: { showSettings = false }) { + SettingsView().modifier(PreferredColorSchemeModifier(preferredColorScheme: appSettings.userInterfaceColorPalette)) +>>>>>>> pr/7 } } diff --git a/Multiplatform/Shared/Timeline/TimelineItemView.swift b/Multiplatform/Shared/Timeline/TimelineItemView.swift index d4f4f172a..1e1a25ae8 100644 --- a/Multiplatform/Shared/Timeline/TimelineItemView.swift +++ b/Multiplatform/Shared/Timeline/TimelineItemView.swift @@ -21,7 +21,7 @@ struct TimelineItemView: View { TimelineItemStatusView(status: timelineItem.status) if let image = articleIconImageLoader.image { IconImageView(iconImage: image) - .frame(width: CGFloat(defaults.timelineIconSize), height: CGFloat(defaults.timelineIconSize), alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/) + .frame(width: CGFloat(defaults.timelineIconDimensions), height: CGFloat(defaults.timelineIconDimensions), alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/) } VStack { Text(verbatim: timelineItem.article.title ?? "N/A") diff --git a/Multiplatform/iOS/AppDelegate.swift b/Multiplatform/iOS/AppDelegate.swift index 3df75f60d..f97bef0c6 100644 --- a/Multiplatform/iOS/AppDelegate.swift +++ b/Multiplatform/iOS/AppDelegate.swift @@ -71,7 +71,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD } func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { - //AppDefaults.registerDefaults() + AppDefaults.registerDefaults() let isFirstRun = AppDefaults.shared.isFirstRun() if isFirstRun { @@ -103,6 +103,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD UNUserNotificationCenter.current().delegate = self userNotificationManager = UserNotificationManager() + NotificationCenter.default.addObserver(self, selector: #selector(userDefaultsDidChange), name: UserDefaults.didChangeNotification, object: nil) + + // extensionContainersFile = ExtensionContainersFile() // extensionFeedAddRequestFile = ExtensionFeedAddRequestFile() @@ -388,3 +391,29 @@ private extension AppDelegate { } } + +private extension AppDelegate { + @objc func userDefaultsDidChange() { + updateUserInterfaceStyle() + } + + var window: UIWindow? { + guard let scene = UIApplication.shared.connectedScenes.first, + let windowSceneDelegate = scene.delegate as? UIWindowSceneDelegate, + let window = windowSceneDelegate.window else { + return nil + } + return window + } + + func updateUserInterfaceStyle() { +// switch AppDefaults.shared.userInterfaceColorPalette { +// case .automatic: +// window?.overrideUserInterfaceStyle = .unspecified +// case .light: +// window?.overrideUserInterfaceStyle = .light +// case .dark: +// window?.overrideUserInterfaceStyle = .dark +// } + } +} diff --git a/Multiplatform/iOS/Settings/SettingsView.swift b/Multiplatform/iOS/Settings/SettingsView.swift index b08cd249b..9aca56100 100644 --- a/Multiplatform/iOS/Settings/SettingsView.swift +++ b/Multiplatform/iOS/Settings/SettingsView.swift @@ -155,12 +155,12 @@ struct SettingsView: View { var appearance: some View { Section(header: Text("Appearance"), content: { NavigationLink( - destination: EmptyView(), + destination: ColorPaletteContainerView().environmentObject(settings), label: { HStack { - Text("Color Pallete") + Text("Color Palette") Spacer() - Text("Automatic") + Text(settings.userInterfaceColorPalette.description) .foregroundColor(.secondary) } }) diff --git a/Multiplatform/iOS/Settings/Submenus/ColorPaletteContainerView.swift b/Multiplatform/iOS/Settings/Submenus/ColorPaletteContainerView.swift new file mode 100644 index 000000000..3cf4ee25a --- /dev/null +++ b/Multiplatform/iOS/Settings/Submenus/ColorPaletteContainerView.swift @@ -0,0 +1,60 @@ +// +// ColorPaletteContainerView.swift +// Multiplatform iOS +// +// Created by Rizwan on 02/07/20. +// Copyright © 2020 Ranchero Software. All rights reserved. +// + +import SwiftUI + +struct ColorPaletteContainerView: View { + private let colorPalettes = UserInterfaceColorPalette.allCases + @EnvironmentObject private var appSettings: AppDefaults + @Environment(\.presentationMode) var presentationMode + + var body: some View { + List { + ForEach.init(0 ..< colorPalettes.count) { index in + Button(action: { + onTapColorPalette(at:index) + }) { + ColorPaletteView(colorPalette: colorPalettes[index]) + } + } + } + .listStyle(InsetGroupedListStyle()) + .navigationBarTitle("Color Palette", displayMode: .inline) + } + + func onTapColorPalette(at index: Int) { + if let colorPalette = UserInterfaceColorPalette(rawValue: index) { + appSettings.userInterfaceColorPalette = colorPalette + } + self.presentationMode.wrappedValue.dismiss() + } +} + +struct ColorPaletteView: View { + var colorPalette: UserInterfaceColorPalette + @EnvironmentObject private var appSettings: AppDefaults + + var body: some View { + HStack { + Text(colorPalette.description).foregroundColor(.primary) + Spacer() + if colorPalette == appSettings.userInterfaceColorPalette { + Image(systemName: "checkmark") + .foregroundColor(.blue) + } + } + } +} + +struct ColorPaletteContainerView_Previews: PreviewProvider { + static var previews: some View { + NavigationView { + ColorPaletteContainerView() + } + } +} diff --git a/Multiplatform/iOS/Settings/Submenus/TimelineLayoutView.swift b/Multiplatform/iOS/Settings/Submenus/TimelineLayoutView.swift index 2b064917c..679d65e4a 100644 --- a/Multiplatform/iOS/Settings/Submenus/TimelineLayoutView.swift +++ b/Multiplatform/iOS/Settings/Submenus/TimelineLayoutView.swift @@ -33,8 +33,8 @@ struct TimelineLayoutView: View { } var iconSize: some View { - Slider(value: $appSettings.timelineIconSize, in: 20...60, step: 10, minimumValueLabel: Text("Small"), maximumValueLabel: Text("Large"), label: { - Text(String(appSettings.timelineIconSize)) + Slider(value: $appSettings.timelineIconDimensions, in: 20...60, step: 10, minimumValueLabel: Text("Small"), maximumValueLabel: Text("Large"), label: { + Text(String(appSettings.timelineIconDimensions)) }) } @@ -54,7 +54,7 @@ struct TimelineLayoutView: View { Image(systemName: "paperplane.circle") .resizable() - .frame(width: CGFloat(appSettings.timelineIconSize), height: CGFloat(appSettings.timelineIconSize), alignment: .top) + .frame(width: CGFloat(appSettings.timelineIconDimensions), height: CGFloat(appSettings.timelineIconDimensions), alignment: .top) .foregroundColor(.accentColor) VStack(alignment: .leading, spacing: 4) { diff --git a/Multiplatform/macOS/AppDelegate.swift b/Multiplatform/macOS/AppDelegate.swift index e01a4f244..2ad541197 100644 --- a/Multiplatform/macOS/AppDelegate.swift +++ b/Multiplatform/macOS/AppDelegate.swift @@ -133,7 +133,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCenterDele } #endif - //AppDefaults.registerDefaults() + AppDefaults.registerDefaults() let isFirstRun = AppDefaults.shared.isFirstRun() if isFirstRun { os_log(.debug, log: log, "Is first run.") diff --git a/NetNewsWire.xcodeproj/project.pbxproj b/NetNewsWire.xcodeproj/project.pbxproj index e7f67b954..afea53483 100644 --- a/NetNewsWire.xcodeproj/project.pbxproj +++ b/NetNewsWire.xcodeproj/project.pbxproj @@ -631,6 +631,8 @@ 6581C73D20CED60100F4AD34 /* SafariExtensionViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 6581C73B20CED60100F4AD34 /* SafariExtensionViewController.xib */; }; 6581C74020CED60100F4AD34 /* netnewswire-subscribe-to-feed.js in Resources */ = {isa = PBXBuildFile; fileRef = 6581C73F20CED60100F4AD34 /* netnewswire-subscribe-to-feed.js */; }; 6581C74220CED60100F4AD34 /* ToolbarItemIcon.pdf in Resources */ = {isa = PBXBuildFile; fileRef = 6581C74120CED60100F4AD34 /* ToolbarItemIcon.pdf */; }; + 65CBAD3624AE02D50006DD91 /* TimelineLayoutView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17B223DB24AC24D2001E4592 /* TimelineLayoutView.swift */; }; + 65CBAD5A24AE03C20006DD91 /* ColorPaletteContainerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 65CBAD5924AE03C20006DD91 /* ColorPaletteContainerView.swift */; }; 65ED3FB7235DEF6C0081F399 /* ArticleArray.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84F204DF1FAACBB30076E152 /* ArticleArray.swift */; }; 65ED3FB8235DEF6C0081F399 /* CrashReporter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 848B937121C8C5540038DC0D /* CrashReporter.swift */; }; 65ED3FB9235DEF6C0081F399 /* IconView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 847CD6C9232F4CBF00FAC46D /* IconView.swift */; }; @@ -2000,6 +2002,7 @@ 6581C73F20CED60100F4AD34 /* netnewswire-subscribe-to-feed.js */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.javascript; path = "netnewswire-subscribe-to-feed.js"; sourceTree = ""; }; 6581C74120CED60100F4AD34 /* ToolbarItemIcon.pdf */ = {isa = PBXFileReference; lastKnownFileType = image.pdf; path = ToolbarItemIcon.pdf; sourceTree = ""; }; 6581C74320CED60100F4AD34 /* Subscribe_to_Feed.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = Subscribe_to_Feed.entitlements; sourceTree = ""; }; + 65CBAD5924AE03C20006DD91 /* ColorPaletteContainerView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ColorPaletteContainerView.swift; sourceTree = ""; }; 65ED4083235DEF6C0081F399 /* NetNewsWire.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = NetNewsWire.app; sourceTree = BUILT_PRODUCTS_DIR; }; 65ED409D235DEF770081F399 /* Subscribe to Feed.appex */ = {isa = PBXFileReference; explicitFileType = "wrapper.app-extension"; includeInIndex = 0; path = "Subscribe to Feed.appex"; sourceTree = BUILT_PRODUCTS_DIR; }; 65ED409F235DEFF00081F399 /* container-migration.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "container-migration.plist"; sourceTree = ""; }; @@ -2412,6 +2415,7 @@ isa = PBXGroup; children = ( 17B223DB24AC24D2001E4592 /* TimelineLayoutView.swift */, + 65CBAD5924AE03C20006DD91 /* ColorPaletteContainerView.swift */, ); path = Submenus; sourceTree = ""; @@ -4810,6 +4814,7 @@ 51E498F524A8085D00B667CB /* TodayFeedDelegate.swift in Sources */, 172952B024AA287100D65E66 /* CompactSidebarContainerView.swift in Sources */, 172199F124AB716900A31D04 /* SidebarToolbar.swift in Sources */, + 65CBAD5A24AE03C20006DD91 /* ColorPaletteContainerView.swift in Sources */, 51E4990B24A808C500B667CB /* ImageDownloader.swift in Sources */, 51E498F424A8085D00B667CB /* SmartFeedDelegate.swift in Sources */, 514E6BFF24AD255D00AC6F6E /* PreviewArticles.swift in Sources */, diff --git a/NetNewsWire.xcodeproj/xcshareddata/xcschemes/Multiplatform iOS.xcscheme b/NetNewsWire.xcodeproj/xcshareddata/xcschemes/Multiplatform iOS.xcscheme index eafbe066c..e06266bca 100644 --- a/NetNewsWire.xcodeproj/xcshareddata/xcschemes/Multiplatform iOS.xcscheme +++ b/NetNewsWire.xcodeproj/xcshareddata/xcschemes/Multiplatform iOS.xcscheme @@ -1,10 +1,28 @@ + version = "1.7"> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/iOS/AppDefaults.swift b/iOS/AppDefaults.swift index a9ad904d0..d1e7ec909 100644 --- a/iOS/AppDefaults.swift +++ b/iOS/AppDefaults.swift @@ -45,7 +45,7 @@ final class AppDefaults { static let timelineGroupByFeed = "timelineGroupByFeed" static let refreshClearsReadArticles = "refreshClearsReadArticles" static let timelineNumberOfLines = "timelineNumberOfLines" - static let timelineIconSize = "timelineIconSize" + static let timelineIconDimension = "timelineIconSize" static let timelineSortDirection = "timelineSortDirection" static let articleFullscreenAvailable = "articleFullscreenAvailable" static let articleFullscreenEnabled = "articleFullscreenEnabled" @@ -202,11 +202,11 @@ final class AppDefaults { var timelineIconSize: IconSize { get { - let rawValue = AppDefaults.store.integer(forKey: Key.timelineIconSize) + let rawValue = AppDefaults.store.integer(forKey: Key.timelineIconDimension) return IconSize(rawValue: rawValue) ?? IconSize.medium } set { - AppDefaults.store.set(newValue.rawValue, forKey: Key.timelineIconSize) + AppDefaults.store.set(newValue.rawValue, forKey: Key.timelineIconDimension) } } @@ -215,7 +215,7 @@ final class AppDefaults { Key.timelineGroupByFeed: false, Key.refreshClearsReadArticles: false, Key.timelineNumberOfLines: 2, - Key.timelineIconSize: IconSize.medium.rawValue, + Key.timelineIconDimension: IconSize.medium.rawValue, Key.timelineSortDirection: ComparisonResult.orderedDescending.rawValue, Key.articleFullscreenAvailable: false, Key.articleFullscreenEnabled: false,