Gemini/Gemini-iOS/PreferencesView.swift

108 lines
2.9 KiB
Swift

//
// PreferencesView.swift
// Gemini-iOS
//
// Created by Shadowfacts on 9/27/20.
//
import SwiftUI
struct PreferencesView: View {
let dismiss: () -> Void
@ObservedObject private var preferences: Preferences = .shared
// todo: this should really be a @StateObject on ToolbarPrefView
@State private var toolbarViewModel = CustomizeToolbarViewModel()
var body: some View {
NavigationView {
List {
untitledSection
appearanceSection
safariSection
behaviorSection
}
.navigationBarTitle("Preferences")
.insetOrGroupedListStyle()
.navigationBarItems(trailing: doneButton)
}
.navigationViewStyle(.stack)
.onDisappear {
Preferences.save()
}
}
private var doneButton: some View {
Button(action: {
dismiss()
}, label: {
Text("Done")
})
.hoverEffect(.highlight)
}
private var untitledSection: some View {
Section {
NavigationLink(destination: HomepagePrefView()) {
Text("Homepage")
}
}
}
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)
}
Toggle("Show Link Icons", isOn: $preferences.showLinkIcons)
}
}
private var safariSection: some View {
Section(header: Text("Safari")) {
Toggle("Use In-App Safari", isOn: $preferences.useInAppSafari)
Toggle("Use Reader Mode", isOn: $preferences.useReaderMode)
.disabled(!preferences.useInAppSafari)
}
}
private var behaviorSection: some View {
Section(header: Text("Behavior")) {
Toggle("Pull to Refresh", isOn: $preferences.pullToRefreshEnabled)
Toggle("Hide Toolbars When Scrolling", isOn: $preferences.hideToolbarsWhenScrolling)
NavigationLink {
ToolbarPrefView(model: toolbarViewModel)
} label: {
Text("Customize Toolbar")
}
}
}
}
fileprivate extension View {
@ViewBuilder
func insetOrGroupedListStyle() -> some View {
if #available(iOS 14.0, *) {
self.listStyle(.insetGrouped)
} else {
self.listStyle(.grouped)
}
}
}
struct PreferencesView_Previews: PreviewProvider {
@State static var presented = true
static var previews: some View {
PreferencesView(dismiss: {})
}
}