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

View File

@ -15,6 +15,8 @@ struct PreferencesView: View {
var body: some View {
NavigationView {
List {
appearanceSection
safariSection
}
.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 {
Section(header: Text("Safari")) {
Toggle("Use In-App Safari", isOn: $preferences.useInAppSafari)

View File

@ -9,12 +9,15 @@ import UIKit
import SwiftUI
import BrowserCore
import SafariServices
import Combine
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
var navigationManager: NavigationManager!
private var cancellables = [AnyCancellable]()
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`.
@ -42,11 +45,18 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
// Use a UIHostingController as window root view controller.
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
window.overrideUserInterfaceStyle = Preferences.shared.theme
window.rootViewController = UIHostingController(rootView: contentView)
// window.rootViewController = BrowserViewController(navigator: navigationManager)
self.window = window
window.makeKeyAndVisible()
}
Preferences.shared.$theme
.sink { (newStyle) in
self.window!.overrideUserInterfaceStyle = newStyle
}
.store(in: &cancellables)
}
func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {