iOS: Add theme override setting

This commit is contained in:
Shadowfacts 2020-09-29 23:44:44 -04:00
parent b000f1c2b3
commit 83dad76b82
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
3 changed files with 34 additions and 1 deletions

View File

@ -5,7 +5,7 @@
// Created by Shadowfacts on 9/27/20. // Created by Shadowfacts on 9/27/20.
// //
import Foundation import UIKit
class Preferences: Codable, ObservableObject { class Preferences: Codable, ObservableObject {
@ -34,6 +34,8 @@ class Preferences: Codable, ObservableObject {
required init(from decoder: Decoder) throws { required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self) let container = try decoder.container(keyedBy: CodingKeys.self)
theme = try container.decode(UIUserInterfaceStyle.self, forKey: .theme)
useInAppSafari = try container.decode(Bool.self, forKey: .useInAppSafari) useInAppSafari = try container.decode(Bool.self, forKey: .useInAppSafari)
useReaderMode = try container.decode(Bool.self, forKey: .useReaderMode) useReaderMode = try container.decode(Bool.self, forKey: .useReaderMode)
} }
@ -41,16 +43,25 @@ class Preferences: Codable, ObservableObject {
func encode(to encoder: Encoder) throws { func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self) var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(theme, forKey: .theme)
try container.encode(useInAppSafari, forKey: .useInAppSafari) try container.encode(useInAppSafari, forKey: .useInAppSafari)
try container.encode(useReaderMode, forKey: .useReaderMode) try container.encode(useReaderMode, forKey: .useReaderMode)
} }
@Published var theme = UIUserInterfaceStyle.unspecified
@Published var useInAppSafari = false @Published var useInAppSafari = false
@Published var useReaderMode = false @Published var useReaderMode = false
enum CodingKeys: String, CodingKey { enum CodingKeys: String, CodingKey {
case theme
case useInAppSafari case useInAppSafari
case useReaderMode case useReaderMode
} }
} }
extension UIUserInterfaceStyle: Codable {}

View File

@ -15,6 +15,8 @@ struct PreferencesView: View {
var body: some View { var body: some View {
NavigationView { NavigationView {
List { List {
appearanceSection
safariSection safariSection
} }
.navigationBarTitle("Preferences") .navigationBarTitle("Preferences")
@ -34,6 +36,16 @@ struct PreferencesView: View {
}) })
} }
private var appearanceSection: some View {
Section(header: Text("Appearance")) {
Picker(selection: $preferences.theme, label: Text("Theme")) {
Text("Use System Theme").tag(UIUserInterfaceStyle.unspecified)
Text("Always Light").tag(UIUserInterfaceStyle.light)
Text("Always Dark").tag(UIUserInterfaceStyle.dark)
}
}
}
private var safariSection: some View { private var safariSection: some View {
Section(header: Text("Safari")) { Section(header: Text("Safari")) {
Toggle("Use In-App Safari", isOn: $preferences.useInAppSafari) Toggle("Use In-App Safari", isOn: $preferences.useInAppSafari)

View File

@ -9,6 +9,7 @@ import UIKit
import SwiftUI import SwiftUI
import BrowserCore import BrowserCore
import SafariServices import SafariServices
import Combine
class SceneDelegate: UIResponder, UIWindowSceneDelegate { class SceneDelegate: UIResponder, UIWindowSceneDelegate {
@ -16,6 +17,8 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var navigationManager: NavigationManager! var navigationManager: NavigationManager!
private var cancellables = [AnyCancellable]()
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) { func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`. // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene. // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
@ -42,11 +45,18 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
// Use a UIHostingController as window root view controller. // Use a UIHostingController as window root view controller.
if let windowScene = scene as? UIWindowScene { if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene) let window = UIWindow(windowScene: windowScene)
window.overrideUserInterfaceStyle = Preferences.shared.theme
window.rootViewController = UIHostingController(rootView: contentView) window.rootViewController = UIHostingController(rootView: contentView)
// window.rootViewController = BrowserViewController(navigator: navigationManager) // window.rootViewController = BrowserViewController(navigator: navigationManager)
self.window = window self.window = window
window.makeKeyAndVisible() window.makeKeyAndVisible()
} }
Preferences.shared.$theme
.sink { (newStyle) in
self.window!.overrideUserInterfaceStyle = newStyle
}
.store(in: &cancellables)
} }
func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) { func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {