mirror of
https://github.com/yattee/yattee.git
synced 2025-08-05 02:04:07 +00:00
Settings for iOS/macOS
This commit is contained in:
117
Shared/Settings/AccountFormView.swift
Normal file
117
Shared/Settings/AccountFormView.swift
Normal file
@@ -0,0 +1,117 @@
|
||||
import Defaults
|
||||
import SwiftUI
|
||||
|
||||
struct AccountFormView: View {
|
||||
let instance: Instance
|
||||
var selectedAccount: Binding<Instance.Account?>?
|
||||
|
||||
@State private var name = ""
|
||||
@State private var sid = ""
|
||||
|
||||
@State private var valid = false
|
||||
@State private var validated = false
|
||||
|
||||
@FocusState private var focused: Bool
|
||||
|
||||
@Environment(\.dismiss) private var dismiss
|
||||
|
||||
@EnvironmentObject<InstancesModel> private var instances
|
||||
|
||||
var body: some View {
|
||||
VStack {
|
||||
HStack(alignment: .center) {
|
||||
Text("Add Account")
|
||||
.font(.title2.bold())
|
||||
|
||||
Spacer()
|
||||
|
||||
Button("Cancel") {
|
||||
dismiss()
|
||||
}
|
||||
#if !os(tvOS)
|
||||
.keyboardShortcut(.cancelAction)
|
||||
#endif
|
||||
}
|
||||
.padding(.horizontal)
|
||||
|
||||
Form {
|
||||
TextField("Name", text: $name, prompt: Text("Account Name (optional)"))
|
||||
.focused($focused)
|
||||
|
||||
TextField("SID", text: $sid, prompt: Text("Invidious SID Cookie"))
|
||||
}
|
||||
.onAppear(perform: initializeForm)
|
||||
.onChange(of: sid) { _ in validate() }
|
||||
|
||||
#if os(macOS)
|
||||
.padding(.horizontal)
|
||||
#endif
|
||||
|
||||
HStack {
|
||||
HStack(spacing: 4) {
|
||||
Image(systemName: valid ? "checkmark.circle.fill" : "xmark.circle.fill")
|
||||
.foregroundColor(valid ? .green : .red)
|
||||
VStack(alignment: .leading) {
|
||||
Text(valid ? "Account found" : "Invalid account details")
|
||||
}
|
||||
}
|
||||
.opacity(validated ? 1 : 0)
|
||||
Spacer()
|
||||
|
||||
Button("Save", action: submitForm)
|
||||
.disabled(!valid)
|
||||
#if !os(tvOS)
|
||||
.keyboardShortcut(.defaultAction)
|
||||
#endif
|
||||
}
|
||||
.frame(minHeight: 35)
|
||||
.padding(.horizontal)
|
||||
}
|
||||
|
||||
#if os(iOS)
|
||||
.padding(.vertical)
|
||||
#else
|
||||
.frame(width: 400, height: 145)
|
||||
#endif
|
||||
}
|
||||
|
||||
func initializeForm() {
|
||||
focused = true
|
||||
}
|
||||
|
||||
func validate() {
|
||||
guard !sid.isEmpty else {
|
||||
validator.reset()
|
||||
return
|
||||
}
|
||||
|
||||
validator.validateAccount()
|
||||
}
|
||||
|
||||
func submitForm() {
|
||||
guard valid else {
|
||||
return
|
||||
}
|
||||
|
||||
let account = instances.addAccount(instance: instance, name: name, sid: sid)
|
||||
selectedAccount?.wrappedValue = account
|
||||
|
||||
dismiss()
|
||||
}
|
||||
|
||||
private var validator: InstanceAccountValidator {
|
||||
InstanceAccountValidator(
|
||||
url: instance.url,
|
||||
account: Instance.Account(url: instance.url, sid: sid),
|
||||
formObjectID: $sid,
|
||||
valid: $valid,
|
||||
validated: $validated
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
struct AccountFormView_Previews: PreviewProvider {
|
||||
static var previews: some View {
|
||||
AccountFormView(instance: Instance.fixture)
|
||||
}
|
||||
}
|
36
Shared/Settings/AccountSettingsView.swift
Normal file
36
Shared/Settings/AccountSettingsView.swift
Normal file
@@ -0,0 +1,36 @@
|
||||
import SwiftUI
|
||||
|
||||
struct AccountSettingsView: View {
|
||||
let instance: Instance
|
||||
let account: Instance.Account
|
||||
@Binding var selectedAccount: Instance.Account?
|
||||
|
||||
@State private var presentingRemovalConfirmationDialog = false
|
||||
|
||||
@EnvironmentObject<InstancesModel> private var instances
|
||||
|
||||
var body: some View {
|
||||
HStack {
|
||||
Text(account.description)
|
||||
Spacer()
|
||||
|
||||
HStack {
|
||||
Button("Remove", role: .destructive) {
|
||||
presentingRemovalConfirmationDialog = true
|
||||
}
|
||||
.confirmationDialog(
|
||||
"Are you sure you want to remove \(account.description) account?",
|
||||
isPresented: $presentingRemovalConfirmationDialog
|
||||
) {
|
||||
Button("Remove", role: .destructive) {
|
||||
instances.removeAccount(instance: instance, account: account)
|
||||
}
|
||||
}
|
||||
#if os(macOS)
|
||||
.foregroundColor(.red)
|
||||
#endif
|
||||
}
|
||||
.opacity(account == selectedAccount ? 1 : 0)
|
||||
}
|
||||
}
|
||||
}
|
45
Shared/Settings/InstanceDetailsSettingsView.swift
Normal file
45
Shared/Settings/InstanceDetailsSettingsView.swift
Normal file
@@ -0,0 +1,45 @@
|
||||
import SwiftUI
|
||||
|
||||
struct InstanceDetailsSettingsView: View {
|
||||
let instanceID: Instance.ID?
|
||||
|
||||
@State private var accountsChanged = false
|
||||
@State private var presentingAccountForm = false
|
||||
|
||||
@EnvironmentObject<InstancesModel> private var instances
|
||||
|
||||
var instance: Instance! {
|
||||
instances.find(instanceID)
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
List {
|
||||
Section(header: Text("Accounts")) {
|
||||
ForEach(instance.accounts, id: \.self) { account in
|
||||
Text(account.description)
|
||||
#if !os(tvOS)
|
||||
.swipeActions(edge: .trailing, allowsFullSwipe: false) {
|
||||
Button("Remove", role: .destructive) {
|
||||
instances.removeAccount(instance: instance, account: account)
|
||||
accountsChanged.toggle()
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
.redrawOn(change: accountsChanged)
|
||||
|
||||
Button("Add account...") {
|
||||
presentingAccountForm = true
|
||||
}
|
||||
}
|
||||
}
|
||||
#if os(iOS)
|
||||
.listStyle(.insetGrouped)
|
||||
#endif
|
||||
|
||||
.navigationTitle(instance.shortDescription)
|
||||
.sheet(isPresented: $presentingAccountForm, onDismiss: { accountsChanged.toggle() }) {
|
||||
AccountFormView(instance: instance)
|
||||
}
|
||||
}
|
||||
}
|
126
Shared/Settings/InstanceFormView.swift
Normal file
126
Shared/Settings/InstanceFormView.swift
Normal file
@@ -0,0 +1,126 @@
|
||||
import SwiftUI
|
||||
|
||||
struct InstanceFormView: View {
|
||||
@Binding var savedInstanceID: Instance.ID?
|
||||
|
||||
@State private var name = ""
|
||||
@State private var url = ""
|
||||
|
||||
@State private var valid = false
|
||||
@State private var validated = false
|
||||
@State private var validationError: String?
|
||||
|
||||
@FocusState private var nameFieldFocused: Bool
|
||||
|
||||
@Environment(\.dismiss) private var dismiss
|
||||
|
||||
@EnvironmentObject<InstancesModel> private var instancesModel
|
||||
|
||||
var body: some View {
|
||||
VStack(alignment: .leading) {
|
||||
HStack(alignment: .center) {
|
||||
Text("Add Instance")
|
||||
.font(.title2.bold())
|
||||
|
||||
Spacer()
|
||||
|
||||
Button("Cancel") {
|
||||
dismiss()
|
||||
}
|
||||
#if !os(tvOS)
|
||||
.keyboardShortcut(.cancelAction)
|
||||
#endif
|
||||
}
|
||||
.padding(.horizontal)
|
||||
|
||||
Form {
|
||||
TextField("Name", text: $name, prompt: Text("Instance Name (optional)"))
|
||||
.frame(maxWidth: 450)
|
||||
.focused($nameFieldFocused)
|
||||
|
||||
TextField("URL", text: $url, prompt: Text("https://invidious.home.net"))
|
||||
.frame(maxWidth: 450)
|
||||
}
|
||||
#if os(macOS)
|
||||
.padding(.horizontal)
|
||||
#endif
|
||||
|
||||
HStack {
|
||||
HStack(spacing: 4) {
|
||||
Image(systemName: valid ? "checkmark.circle.fill" : "xmark.circle.fill")
|
||||
.foregroundColor(valid ? .green : .red)
|
||||
VStack(alignment: .leading) {
|
||||
Text(valid ? "Connected successfully" : "Connection failed")
|
||||
if !valid {
|
||||
Text(validationError ?? "Unknown Error")
|
||||
.font(.caption2)
|
||||
.foregroundColor(.secondary)
|
||||
.truncationMode(.tail)
|
||||
.lineLimit(1)
|
||||
}
|
||||
}
|
||||
.frame(minHeight: 40)
|
||||
}
|
||||
.opacity(validated ? 1 : 0)
|
||||
Spacer()
|
||||
|
||||
Button("Save", action: submitForm)
|
||||
.disabled(!valid)
|
||||
#if !os(tvOS)
|
||||
.keyboardShortcut(.defaultAction)
|
||||
#endif
|
||||
}
|
||||
.padding(.horizontal)
|
||||
}
|
||||
.onChange(of: url) { _ in validate() }
|
||||
.onAppear(perform: initializeForm)
|
||||
#if os(iOS)
|
||||
.padding(.vertical)
|
||||
#else
|
||||
.frame(width: 400, height: 150)
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
var validator: InstanceAccountValidator {
|
||||
InstanceAccountValidator(
|
||||
url: url,
|
||||
formObjectID: $url,
|
||||
valid: $valid,
|
||||
validated: $validated,
|
||||
error: $validationError
|
||||
)
|
||||
}
|
||||
|
||||
func validate() {
|
||||
valid = false
|
||||
validated = false
|
||||
validationError = nil
|
||||
|
||||
guard !url.isEmpty else {
|
||||
return
|
||||
}
|
||||
|
||||
validator.validateInstance()
|
||||
}
|
||||
|
||||
func initializeForm() {
|
||||
nameFieldFocused = true
|
||||
}
|
||||
|
||||
func submitForm() {
|
||||
guard valid else {
|
||||
return
|
||||
}
|
||||
|
||||
savedInstanceID = instancesModel.add(name: name, url: url).id
|
||||
|
||||
dismiss()
|
||||
}
|
||||
}
|
||||
|
||||
struct InstanceFormView_Previews: PreviewProvider {
|
||||
static var previews: some View {
|
||||
InstanceFormView(savedInstanceID: .constant(nil))
|
||||
}
|
||||
}
|
168
Shared/Settings/InstancesSettingsView.swift
Normal file
168
Shared/Settings/InstancesSettingsView.swift
Normal file
@@ -0,0 +1,168 @@
|
||||
import Defaults
|
||||
import SwiftUI
|
||||
|
||||
struct InstancesSettingsView: View {
|
||||
@Default(.instances) private var instances
|
||||
@EnvironmentObject<InstancesModel> private var instancesModel
|
||||
|
||||
@EnvironmentObject<InvidiousAPI> private var api
|
||||
@EnvironmentObject<SubscriptionsModel> private var subscriptions
|
||||
@EnvironmentObject<PlaylistsModel> private var playlists
|
||||
|
||||
@State private var selectedInstanceID: Instance.ID?
|
||||
@State private var selectedAccount: Instance.Account?
|
||||
|
||||
@State private var presentingAccountForm = false
|
||||
@State private var presentingInstanceForm = false
|
||||
@State private var savedFormInstanceID: Instance.ID?
|
||||
|
||||
@State private var presentingConfirmationDialog = false
|
||||
@State private var presentingInstanceDetails = false
|
||||
|
||||
var selectedInstance: Instance! {
|
||||
instancesModel.find(selectedInstanceID)
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
Group {
|
||||
#if os(iOS)
|
||||
Section(header: instancesHeader) {
|
||||
ForEach(instances, id: \.self) { instance in
|
||||
Button(action: {
|
||||
self.selectedInstanceID = instance.id
|
||||
self.presentingInstanceDetails = true
|
||||
}) {
|
||||
HStack {
|
||||
Text(instance.description)
|
||||
Spacer()
|
||||
NavigationLink(
|
||||
isActive: .constant(false),
|
||||
destination: { EmptyView() },
|
||||
label: { EmptyView() }
|
||||
)
|
||||
.frame(maxWidth: 100)
|
||||
}
|
||||
}
|
||||
.swipeActions(edge: .trailing, allowsFullSwipe: false) {
|
||||
Button("Remove", role: .destructive) {
|
||||
instancesModel.remove(instance)
|
||||
}
|
||||
}
|
||||
.buttonStyle(.plain)
|
||||
}
|
||||
|
||||
Button("Add Instance...") {
|
||||
presentingInstanceForm = true
|
||||
}
|
||||
}
|
||||
.listStyle(.insetGrouped)
|
||||
#else
|
||||
Section {
|
||||
Text("Instance")
|
||||
|
||||
if !instances.isEmpty {
|
||||
Picker("Instance", selection: $selectedInstanceID) {
|
||||
ForEach(instances, id: \.url) { instance in
|
||||
Text(instance.description).tag(Optional(instance.id))
|
||||
}
|
||||
}
|
||||
.labelsHidden()
|
||||
} else {
|
||||
Text("You have no instances configured")
|
||||
.font(.caption)
|
||||
.foregroundColor(.secondary)
|
||||
}
|
||||
|
||||
if let instance = selectedInstance {
|
||||
if instance.accounts.isEmpty {
|
||||
Text("You have no accounts for this instance")
|
||||
.font(.caption)
|
||||
.foregroundColor(.secondary)
|
||||
} else {
|
||||
Text("Accounts")
|
||||
List(selection: $selectedAccount) {
|
||||
ForEach(instance.accounts, id: \.self) { account in
|
||||
AccountSettingsView(instance: instance, account: account,
|
||||
selectedAccount: $selectedAccount)
|
||||
}
|
||||
}
|
||||
#if os(macOS)
|
||||
.listStyle(.inset(alternatesRowBackgrounds: true))
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
if selectedInstance != nil {
|
||||
HStack {
|
||||
Button("Add Account...") {
|
||||
selectedAccount = nil
|
||||
presentingAccountForm = true
|
||||
}
|
||||
Spacer()
|
||||
|
||||
Button("Remove Instance", role: .destructive) {
|
||||
presentingConfirmationDialog = true
|
||||
}
|
||||
.confirmationDialog(
|
||||
"Are you sure you want to remove \(selectedInstance!.description) instance?",
|
||||
isPresented: $presentingConfirmationDialog
|
||||
) {
|
||||
Button("Remove Instance", role: .destructive) {
|
||||
instancesModel.remove(selectedInstance!)
|
||||
selectedInstanceID = instances.last?.id
|
||||
}
|
||||
}
|
||||
|
||||
#if os(macOS)
|
||||
.foregroundColor(.red)
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
Button("Add Instance...") {
|
||||
presentingInstanceForm = true
|
||||
}
|
||||
}
|
||||
.frame(minWidth: 0, maxWidth: .infinity, alignment: .leading)
|
||||
|
||||
.onAppear {
|
||||
selectedInstanceID = instances.first?.id
|
||||
}
|
||||
.sheet(isPresented: $presentingAccountForm) {
|
||||
AccountFormView(instance: selectedInstance, selectedAccount: $selectedAccount)
|
||||
}
|
||||
|
||||
Spacer()
|
||||
|
||||
#endif
|
||||
}
|
||||
.sheet(isPresented: $presentingInstanceForm, onDismiss: setSelectedInstanceToFormInstance) {
|
||||
InstanceFormView(savedInstanceID: $savedFormInstanceID)
|
||||
}
|
||||
}
|
||||
|
||||
var instancesHeader: some View {
|
||||
Text("Instances").background(instanceDetailsNavigationLink)
|
||||
}
|
||||
|
||||
var instanceDetailsNavigationLink: some View {
|
||||
NavigationLink(
|
||||
isActive: $presentingInstanceDetails,
|
||||
destination: { InstanceDetailsSettingsView(instanceID: selectedInstanceID) },
|
||||
label: { EmptyView() }
|
||||
)
|
||||
}
|
||||
|
||||
func setSelectedInstanceToFormInstance() {
|
||||
if let id = savedFormInstanceID {
|
||||
selectedInstanceID = id
|
||||
savedFormInstanceID = nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct InstancesSettingsView_Previews: PreviewProvider {
|
||||
static var previews: some View {
|
||||
InstancesSettingsView()
|
||||
}
|
||||
}
|
25
Shared/Settings/PlaybackSettingsView.swift
Normal file
25
Shared/Settings/PlaybackSettingsView.swift
Normal file
@@ -0,0 +1,25 @@
|
||||
import Defaults
|
||||
import SwiftUI
|
||||
|
||||
struct PlaybackSettingsView: View {
|
||||
@Default(.quality) private var quality
|
||||
|
||||
var body: some View {
|
||||
Section(header: Text("Quality")) {
|
||||
Picker("Quality", selection: $quality) {
|
||||
ForEach(Stream.ResolutionSetting.allCases, id: \.self) { resolution in
|
||||
Text(resolution.description).tag(resolution)
|
||||
}
|
||||
}
|
||||
.labelsHidden()
|
||||
|
||||
#if os(iOS)
|
||||
.pickerStyle(.automatic)
|
||||
#endif
|
||||
|
||||
#if os(macOS)
|
||||
Spacer()
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
66
Shared/Settings/SettingsView.swift
Normal file
66
Shared/Settings/SettingsView.swift
Normal file
@@ -0,0 +1,66 @@
|
||||
import Defaults
|
||||
import Foundation
|
||||
import SwiftUI
|
||||
|
||||
struct SettingsView: View {
|
||||
private enum Tabs: Hashable {
|
||||
case playback, instances
|
||||
}
|
||||
|
||||
@Environment(\.dismiss) private var dismiss
|
||||
|
||||
var body: some View {
|
||||
#if os(macOS)
|
||||
TabView {
|
||||
Form {
|
||||
InstancesSettingsView()
|
||||
}
|
||||
.tabItem {
|
||||
Label("Instances", systemImage: "server.rack")
|
||||
}
|
||||
.tag(Tabs.instances)
|
||||
|
||||
Form {
|
||||
PlaybackSettingsView()
|
||||
}
|
||||
.tabItem {
|
||||
Label("Playback", systemImage: "play.rectangle.on.rectangle.fill")
|
||||
}
|
||||
.tag(Tabs.playback)
|
||||
}
|
||||
.padding(20)
|
||||
.frame(width: 400, height: 270)
|
||||
#else
|
||||
NavigationView {
|
||||
List {
|
||||
InstancesSettingsView()
|
||||
PlaybackSettingsView()
|
||||
}
|
||||
#if os(iOS)
|
||||
.listStyle(.insetGrouped)
|
||||
#endif
|
||||
|
||||
.navigationTitle("Settings")
|
||||
.toolbar {
|
||||
ToolbarItem(placement: .navigationBarTrailing) {
|
||||
Button("Done") {
|
||||
dismiss()
|
||||
}
|
||||
#if !os(tvOS)
|
||||
.keyboardShortcut(.cancelAction)
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
struct SettingsView_Previews: PreviewProvider {
|
||||
static var previews: some View {
|
||||
SettingsView()
|
||||
#if os(macOS)
|
||||
.frame(width: 600, height: 300)
|
||||
#endif
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user