From a620a205886120eee4f3cd202e1f2a2d2fb06f0d Mon Sep 17 00:00:00 2001 From: Maurice Parker Date: Sun, 15 Mar 2020 04:25:25 -0500 Subject: [PATCH 1/7] Add the option to select the user interface color palette. Issue #1900 --- NetNewsWire.xcodeproj/project.pbxproj | 4 + iOS/AppDefaults.swift | 34 +- iOS/SceneDelegate.swift | 18 + .../ColorPaletteTableViewController.swift | 42 +++ iOS/Settings/Settings.storyboard | 334 +++++++++++------- iOS/Settings/SettingsViewController.swift | 11 +- .../VibrantTableViewCell.swift | 2 + 7 files changed, 310 insertions(+), 135 deletions(-) create mode 100644 iOS/Settings/ColorPaletteTableViewController.swift diff --git a/NetNewsWire.xcodeproj/project.pbxproj b/NetNewsWire.xcodeproj/project.pbxproj index 05212949e..7bbc3d0ad 100644 --- a/NetNewsWire.xcodeproj/project.pbxproj +++ b/NetNewsWire.xcodeproj/project.pbxproj @@ -95,6 +95,7 @@ 515D4FC123257A3200EE1167 /* FolderTreeControllerDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 849A97A11ED9F180007D329B /* FolderTreeControllerDelegate.swift */; }; 515D4FCA23257CB500EE1167 /* Node-Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 849A97971ED9EFAA007D329B /* Node-Extensions.swift */; }; 515D4FCC2325815A00EE1167 /* SafariExt.js in Resources */ = {isa = PBXBuildFile; fileRef = 515D4FCB2325815A00EE1167 /* SafariExt.js */; }; + 516244E3241E19F000B61C47 /* ColorPaletteTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 516244E2241E19F000B61C47 /* ColorPaletteTableViewController.swift */; }; 51627A6723861DA3007B3B4B /* MasterFeedViewController+Drag.swift in Sources */ = {isa = PBXBuildFile; fileRef = 51627A6623861DA3007B3B4B /* MasterFeedViewController+Drag.swift */; }; 51627A6923861DED007B3B4B /* MasterFeedViewController+Drop.swift in Sources */ = {isa = PBXBuildFile; fileRef = 51627A6823861DED007B3B4B /* MasterFeedViewController+Drop.swift */; }; 51627A6B238629D8007B3B4B /* MasterFeedDataSource.swift in Sources */ = {isa = PBXBuildFile; fileRef = 51627A6A238629D8007B3B4B /* MasterFeedDataSource.swift */; }; @@ -1303,6 +1304,7 @@ 515D4FCB2325815A00EE1167 /* SafariExt.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = SafariExt.js; sourceTree = ""; }; 515D4FCD2325909200EE1167 /* NetNewsWire_iOS_ShareExtension.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = NetNewsWire_iOS_ShareExtension.entitlements; sourceTree = ""; }; 515D4FCE2325B3D000EE1167 /* NetNewsWire_iOSshareextension_target.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = NetNewsWire_iOSshareextension_target.xcconfig; sourceTree = ""; }; + 516244E2241E19F000B61C47 /* ColorPaletteTableViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ColorPaletteTableViewController.swift; sourceTree = ""; }; 51627A6623861DA3007B3B4B /* MasterFeedViewController+Drag.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "MasterFeedViewController+Drag.swift"; sourceTree = ""; }; 51627A6823861DED007B3B4B /* MasterFeedViewController+Drop.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "MasterFeedViewController+Drop.swift"; sourceTree = ""; }; 51627A6A238629D8007B3B4B /* MasterFeedDataSource.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MasterFeedDataSource.swift; sourceTree = ""; }; @@ -1875,6 +1877,7 @@ 51A16990235E10D600EB091F /* Settings.storyboard */, 51A16995235E10D600EB091F /* AboutViewController.swift */, 51A16992235E10D600EB091F /* AddAccountViewController.swift */, + 516244E2241E19F000B61C47 /* ColorPaletteTableViewController.swift */, 516A09382360A2AE00EAE89B /* SettingsAccountTableViewCell.swift */, 516A091D23609A3600EAE89B /* SettingsAccountTableViewCell.xib */, 516A093A2360A4A000EAE89B /* SettingsTableViewCell.xib */, @@ -3957,6 +3960,7 @@ 5183CCE5226F4DFA0010922C /* RefreshInterval.swift in Sources */, 51C4529D22650A1000C03939 /* FaviconURLFinder.swift in Sources */, 5142192A23522B5500E07E2C /* ImageViewController.swift in Sources */, + 516244E3241E19F000B61C47 /* ColorPaletteTableViewController.swift in Sources */, 51C45258226508CF00C03939 /* AppAssets.swift in Sources */, 51FA73A82332BE880090D516 /* ExtractedArticle.swift in Sources */, 51C4527C2265091600C03939 /* MasterTimelineDefaultCellLayout.swift in Sources */, diff --git a/iOS/AppDefaults.swift b/iOS/AppDefaults.swift index 0e646a7e5..f790db2a6 100644 --- a/iOS/AppDefaults.swift +++ b/iOS/AppDefaults.swift @@ -8,6 +8,24 @@ import UIKit +enum UserInterfaceColorPalette: Int, CustomStringConvertible, CaseIterable { + case automatic = 0 + case light = 1 + case dark = 2 + + var description: String { + switch self { + case .automatic: + return NSLocalizedString("Automatic", comment: "Automatic") + case .light: + return NSLocalizedString("Light", comment: "Light") + case .dark: + return NSLocalizedString("Dark", comment: "Dark") + } + } + +} + struct AppDefaults { static var shared: UserDefaults = { @@ -17,6 +35,7 @@ struct AppDefaults { }() struct Key { + static let userInterfaceColorPalette = "userInterfaceColorPalette" static let lastImageCacheFlushDate = "lastImageCacheFlushDate" static let firstRunDate = "firstRunDate" static let timelineGroupByFeed = "timelineGroupByFeed" @@ -40,6 +59,18 @@ struct AppDefaults { firstRunDate = Date() return true }() + + static var userInterfaceColorPalette: UserInterfaceColorPalette { + get { + if let result = UserInterfaceColorPalette(rawValue: int(for: Key.userInterfaceColorPalette)) { + return result + } + return .automatic + } + set { + setInt(for: Key.userInterfaceColorPalette, newValue.rawValue) + } + } static var addWebFeedAccountID: String? { get { @@ -160,7 +191,8 @@ struct AppDefaults { } static func registerDefaults() { - let defaults: [String : Any] = [Key.timelineGroupByFeed: false, + let defaults: [String : Any] = [Key.userInterfaceColorPalette: UserInterfaceColorPalette.automatic.rawValue, + Key.timelineGroupByFeed: false, Key.refreshClearsReadArticles: false, Key.timelineNumberOfLines: 2, Key.timelineIconSize: IconSize.medium.rawValue, diff --git a/iOS/SceneDelegate.swift b/iOS/SceneDelegate.swift index af85dd81a..22d82cdde 100644 --- a/iOS/SceneDelegate.swift +++ b/iOS/SceneDelegate.swift @@ -21,10 +21,13 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate { window = UIWindow(windowScene: scene as! UIWindowScene) window!.tintColor = AppAssets.primaryAccentColor + updateUserInterfaceStyle() window!.rootViewController = coordinator.start(for: window!.frame.size) coordinator.restoreWindowState(session.stateRestorationActivity) + NotificationCenter.default.addObserver(self, selector: #selector(userDefaultsDidChange), name: UserDefaults.didChangeNotification, object: nil) + if let shortcutItem = connectionOptions.shortcutItem { window!.makeKeyAndVisible() handleShortcutItem(shortcutItem) @@ -102,4 +105,19 @@ private extension SceneDelegate { } } + @objc func userDefaultsDidChange() { + updateUserInterfaceStyle() + } + + func updateUserInterfaceStyle() { + switch AppDefaults.userInterfaceColorPalette { + case .automatic: + window!.overrideUserInterfaceStyle = .unspecified + case .light: + window!.overrideUserInterfaceStyle = .light + case .dark: + window!.overrideUserInterfaceStyle = .dark + } + } + } diff --git a/iOS/Settings/ColorPaletteTableViewController.swift b/iOS/Settings/ColorPaletteTableViewController.swift new file mode 100644 index 000000000..d64671c6f --- /dev/null +++ b/iOS/Settings/ColorPaletteTableViewController.swift @@ -0,0 +1,42 @@ +// +// ColorPaletteTableViewController.swift +// NetNewsWire-iOS +// +// Created by Maurice Parker on 3/15/20. +// Copyright © 2020 Ranchero Software. All rights reserved. +// + +import UIKit + +class ColorPaletteTableViewController: UITableViewController { + + // MARK: - Table view data source + + override func numberOfSections(in tableView: UITableView) -> Int { + return 1 + } + + override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { + return UserInterfaceColorPalette.allCases.count + } + + override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { + let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) + let rowColorPalette = UserInterfaceColorPalette.allCases[indexPath.row] + cell.textLabel?.text = String(describing: rowColorPalette) + if rowColorPalette == AppDefaults.userInterfaceColorPalette { + cell.accessoryType = .checkmark + } else { + cell.accessoryType = .none + } + return cell + } + + override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { + if let colorPalette = UserInterfaceColorPalette(rawValue: indexPath.row) { + AppDefaults.userInterfaceColorPalette = colorPalette + } + navigationController?.popViewController(animated: true) + } + +} diff --git a/iOS/Settings/Settings.storyboard b/iOS/Settings/Settings.storyboard index ed773045c..1ecb90a8e 100644 --- a/iOS/Settings/Settings.storyboard +++ b/iOS/Settings/Settings.storyboard @@ -1,8 +1,8 @@ - + - + @@ -19,7 +19,7 @@ - + @@ -40,7 +40,7 @@ - + @@ -61,7 +61,7 @@ - + @@ -317,136 +317,172 @@ - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +