yattee/Shared/Settings/SettingsView.swift

367 lines
12 KiB
Swift
Raw Normal View History

2021-09-25 10:18:22 +02:00
import Defaults
import Foundation
import SwiftUI
2024-05-16 18:28:32 +02:00
2021-09-25 10:18:22 +02:00
struct SettingsView: View {
2022-07-04 00:13:06 +02:00
static let matrixURL = URL(string: "https://tinyurl.com/matrix-yattee")!
static let discordURL = URL(string: "https://yattee.stream/discord")!
#if os(macOS)
private enum Tabs: Hashable {
2024-02-01 23:54:16 +01:00
case browsing, player, controls, quality, history, sponsorBlock, locations, advanced, importExport, help
}
2022-01-06 16:02:53 +01:00
@State private var selection: Tabs = .browsing
#endif
2021-09-25 10:18:22 +02:00
@Environment(\.colorScheme) private var colorScheme
#if os(iOS)
2021-11-28 15:37:55 +01:00
@Environment(\.presentationMode) private var presentationMode
#endif
2021-09-25 10:18:22 +02:00
@ObservedObject private var accounts = AccountsModel.shared
@ObservedObject private var model = SettingsModel.shared
2021-11-28 15:37:55 +01:00
@Default(.instances) private var instances
2024-02-01 23:54:16 +01:00
@State private var filesToShare = []
@ObservedObject private var navigation = NavigationModel.shared
@ObservedObject private var settingsModel = SettingsModel.shared
2021-09-25 10:18:22 +02:00
var body: some View {
settings
.modifier(ImportSettingsSheetViewModifier(isPresented: $settingsModel.presentingSettingsImportSheet, settingsFile: $settingsModel.settingsImportURL))
2024-02-01 23:54:16 +01:00
#if !os(tvOS)
.modifier(ImportSettingsFileImporterViewModifier(isPresented: $navigation.presentingSettingsFileImporter))
2024-02-01 23:54:16 +01:00
#endif
2022-12-21 21:16:53 +01:00
#if os(iOS)
2024-02-01 23:54:16 +01:00
.backport
.scrollDismissesKeyboardInteractively()
2022-12-21 21:16:53 +01:00
#endif
2024-02-01 23:54:16 +01:00
.alert(isPresented: $model.presentingAlert) { model.alert }
}
var settings: some View {
2021-09-25 10:18:22 +02:00
#if os(macOS)
2022-01-06 16:02:53 +01:00
TabView(selection: $selection) {
2022-11-11 21:28:40 +01:00
Form {
BrowsingSettings()
}
.tabItem {
Label("Browsing", systemImage: "list.and.film")
2021-11-05 00:25:51 +01:00
}
2022-12-18 19:39:03 +01:00
.tag(Tabs.browsing)
2021-11-05 00:25:51 +01:00
Form {
2022-01-06 16:02:53 +01:00
PlayerSettings()
}
.tabItem {
2022-01-06 16:02:53 +01:00
Label("Player", systemImage: "play.rectangle")
}
2022-12-18 19:39:03 +01:00
.tag(Tabs.player)
2022-12-19 13:35:37 +01:00
Form {
PlayerControlsSettings()
}
.tabItem {
Label("Controls", systemImage: "hand.tap")
}
.tag(Tabs.controls)
2022-08-14 19:06:22 +02:00
Form {
QualitySettings()
}
.tabItem {
Label("Quality", systemImage: "4k.tv")
}
2022-12-18 19:39:03 +01:00
.tag(Tabs.quality)
2022-08-14 19:06:22 +02:00
2021-10-23 14:12:53 +02:00
Form {
2022-01-06 16:02:53 +01:00
HistorySettings()
2021-10-23 14:12:53 +02:00
}
.tabItem {
2022-01-06 16:02:53 +01:00
Label("History", systemImage: "clock.arrow.circlepath")
2021-10-23 14:12:53 +02:00
}
2022-12-18 19:39:03 +01:00
.tag(Tabs.history)
2021-10-23 14:12:53 +02:00
2022-11-11 20:34:20 +01:00
if !accounts.isEmpty {
Form {
SponsorBlockSettings()
}
.tabItem {
Label("SponsorBlock", systemImage: "dollarsign.circle")
}
2022-12-18 19:39:03 +01:00
.tag(Tabs.sponsorBlock)
2021-09-25 10:18:22 +02:00
}
2022-11-13 12:14:05 +01:00
Form {
LocationsSettings()
}
.tabItem {
Label("Locations", systemImage: "globe")
}
2022-12-18 19:39:03 +01:00
.tag(Tabs.locations)
2021-12-08 00:09:49 +01:00
Group {
AdvancedSettings()
}
.tabItem {
Label("Advanced", systemImage: "wrench.and.screwdriver")
}
2022-12-18 19:39:03 +01:00
.tag(Tabs.advanced)
2024-02-01 23:54:16 +01:00
Group {
ExportSettings()
}
.tabItem {
Label("Export", systemImage: "square.and.arrow.up")
}
.tag(Tabs.importExport)
2022-01-06 16:02:53 +01:00
Form {
Help()
}
.tabItem {
Label("Help", systemImage: "questionmark.circle")
}
2022-12-18 19:39:03 +01:00
.tag(Tabs.help)
2021-09-25 10:18:22 +02:00
}
.padding(20)
2024-02-01 23:54:16 +01:00
.frame(width: 700, height: windowHeight)
2021-09-25 10:18:22 +02:00
#else
2022-08-14 19:06:22 +02:00
NavigationView {
settingsList
2022-11-12 02:39:44 +01:00
.navigationTitle("Settings")
}
#endif
}
2021-11-28 15:37:55 +01:00
2023-09-10 20:08:42 +02:00
struct SettingsLabel: LabelStyle {
func makeBody(configuration: Configuration) -> some View {
#if os(tvOS)
Label {
configuration.title.padding(.leading, 10)
} icon: {
configuration.icon
}
#else
Label(configuration)
#endif
}
}
#if !os(macOS)
var settingsList: some View {
List {
#if os(tvOS)
2022-11-11 20:34:20 +01:00
if !accounts.isEmpty {
2022-12-11 23:15:56 +01:00
Section(header: Text("Current Location")) {
NavigationLink(destination: AccountsView()) {
if let account = accounts.current {
Text(account.isPublic ? account.description : "\(account.description)\(account.instance.shortDescription)")
} else {
Text("Not Selected")
}
}
}
2022-11-11 20:34:20 +01:00
Divider()
}
#endif
2022-01-06 16:02:53 +01:00
Section {
2022-11-11 21:28:40 +01:00
NavigationLink {
BrowsingSettings()
} label: {
2023-09-10 20:08:42 +02:00
Label("Browsing", systemImage: "list.and.film").labelStyle(SettingsLabel())
}
2022-01-06 16:02:53 +01:00
NavigationLink {
PlayerSettings()
} label: {
2023-09-10 20:08:42 +02:00
Label("Player", systemImage: "play.rectangle").labelStyle(SettingsLabel())
2022-01-06 16:02:53 +01:00
}
2022-12-19 13:35:37 +01:00
NavigationLink {
PlayerControlsSettings()
} label: {
2023-09-10 20:08:42 +02:00
Label("Controls", systemImage: "hand.tap").labelStyle(SettingsLabel())
2022-12-19 13:35:37 +01:00
}
2022-08-14 19:06:22 +02:00
NavigationLink {
QualitySettings()
} label: {
2023-09-10 20:08:42 +02:00
Label("Quality", systemImage: "4k.tv").labelStyle(SettingsLabel())
2022-08-14 19:06:22 +02:00
}
NavigationLink {
HistorySettings()
} label: {
2023-09-10 20:08:42 +02:00
Label("History", systemImage: "clock.arrow.circlepath").labelStyle(SettingsLabel())
}
2022-11-11 20:34:20 +01:00
if !accounts.isEmpty {
NavigationLink {
SponsorBlockSettings()
} label: {
2023-09-10 20:08:42 +02:00
Label("SponsorBlock", systemImage: "dollarsign.circle").labelStyle(SettingsLabel())
2022-11-11 20:34:20 +01:00
}
2022-01-06 16:02:53 +01:00
}
2022-11-13 12:14:05 +01:00
NavigationLink {
LocationsSettings()
} label: {
2023-09-10 20:08:42 +02:00
Label("Locations", systemImage: "globe").labelStyle(SettingsLabel())
2022-11-13 12:14:05 +01:00
}
NavigationLink {
AdvancedSettings()
} label: {
2023-09-10 20:08:42 +02:00
Label("Advanced", systemImage: "wrench.and.screwdriver").labelStyle(SettingsLabel())
}
2021-09-25 10:18:22 +02:00
}
2023-09-09 18:20:44 +02:00
#if os(tvOS)
.padding(.horizontal, 20)
#endif
2024-02-01 23:54:16 +01:00
importView
2022-07-04 00:39:05 +02:00
Section(footer: helpFooter) {
NavigationLink {
Help()
} label: {
2023-09-10 20:08:42 +02:00
Label("Help", systemImage: "questionmark.circle").labelStyle(SettingsLabel())
2021-09-25 10:18:22 +02:00
}
2021-09-27 00:03:33 +02:00
}
2023-09-09 18:20:44 +02:00
#if os(tvOS)
.padding(.horizontal, 20)
#endif
2022-07-04 00:13:06 +02:00
#if !os(tvOS)
Section(header: Text("Contact"), footer: versionString) {
Link(destination: Self.discordURL) {
HStack {
Image("Discord")
.resizable()
.renderingMode(.template)
.frame(maxWidth: 30, maxHeight: 30)
2023-09-10 20:07:24 +02:00
.padding(.trailing, 6)
2022-07-04 00:13:06 +02:00
Text("Discord Server")
}
}
Link(destination: Self.matrixURL) {
HStack {
Image("Matrix")
.resizable()
.renderingMode(.template)
.frame(maxWidth: 30, maxHeight: 30)
2023-09-10 20:07:24 +02:00
.padding(.trailing, 6)
2022-07-04 00:13:06 +02:00
Text("Matrix Chat")
}
}
}
#endif
2021-09-25 10:18:22 +02:00
}
.toolbar {
ToolbarItem(placement: .navigationBarLeading) {
#if !os(tvOS)
Button("Done") {
presentationMode.wrappedValue.dismiss()
}
.keyboardShortcut(.cancelAction)
#endif
}
2021-11-28 15:37:55 +01:00
}
.frame(maxWidth: 1000)
#if os(iOS)
.listStyle(.insetGrouped)
#endif
}
#endif
2021-11-28 15:37:55 +01:00
2024-02-01 23:54:16 +01:00
var importView: some View {
Section {
#if os(tvOS)
NavigationLink(destination: LazyView(ImportSettings())) {
Label("Import Settings", systemImage: "square.and.arrow.down")
.labelStyle(SettingsLabel())
}
.padding(.horizontal, 20)
#else
Button(action: importSettings) {
Label("Import Settings...", systemImage: "square.and.arrow.down")
.frame(maxWidth: .infinity, alignment: .leading)
.contentShape(Rectangle())
}
.foregroundColor(.accentColor)
.buttonStyle(.plain)
2024-02-01 23:54:16 +01:00
NavigationLink(destination: LazyView(ExportSettings())) {
Label("Export Settings", systemImage: "square.and.arrow.up")
.frame(maxWidth: .infinity, alignment: .leading)
.contentShape(Rectangle())
}
#endif
2024-02-01 23:54:16 +01:00
}
}
func importSettings() {
navigation.presentingSettingsFileImporter = true
}
2022-01-06 16:02:53 +01:00
#if os(macOS)
private var windowHeight: Double {
switch selection {
case .browsing:
return 800
2022-01-06 16:02:53 +01:00
case .player:
2024-02-02 00:17:06 +01:00
return 550
2022-12-19 13:35:37 +01:00
case .controls:
2023-04-22 19:22:13 +02:00
return 920
2022-08-14 19:06:22 +02:00
case .quality:
return 420
2022-01-06 16:02:53 +01:00
case .history:
2022-09-27 17:46:14 +02:00
return 500
2022-01-06 16:02:53 +01:00
case .sponsorBlock:
2022-11-11 19:55:18 +01:00
return 700
2022-11-13 12:14:05 +01:00
case .locations:
return 600
case .advanced:
2024-02-01 23:54:16 +01:00
return 500
case .importExport:
return 580
2022-01-06 16:02:53 +01:00
case .help:
return 650
2022-01-06 16:02:53 +01:00
}
}
#endif
2022-07-04 00:39:05 +02:00
var helpFooter: some View {
#if os(tvOS)
versionString
#else
EmptyView()
#endif
}
2022-01-06 16:02:53 +01:00
private var versionString: some View {
Text("Yattee \(YatteeApp.version) (build \(YatteeApp.build))")
#if os(tvOS)
.foregroundColor(.secondary)
#endif
}
2021-09-25 10:18:22 +02:00
}
struct SettingsView_Previews: PreviewProvider {
static var previews: some View {
SettingsView()
2021-09-29 13:45:00 +02:00
.injectFixtureEnvironmentObjects()
2021-09-25 10:18:22 +02:00
#if os(macOS)
.frame(width: 600, height: 300)
2022-11-11 16:19:14 +01:00
#else
.navigationViewStyle(.stack)
2021-09-25 10:18:22 +02:00
#endif
}
}