1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-13 22:00:31 +05:30
yattee/macOS/InstancesSettings.swift

191 lines
6.9 KiB
Swift
Raw Normal View History

2021-09-29 15:44:43 +05:30
import Defaults
import SwiftUI
2021-10-23 17:21:02 +05:30
struct InstancesSettings: View {
2021-09-29 15:44:43 +05:30
@State private var selectedInstanceID: Instance.ID?
2021-10-21 03:51:50 +05:30
@State private var selectedAccount: Account?
2021-09-29 15:44:43 +05:30
@State private var presentingAccountForm = false
@State private var presentingInstanceForm = false
@State private var savedFormInstanceID: Instance.ID?
@State private var presentingAccountRemovalConfirmation = false
@State private var presentingInstanceRemovalConfirmation = false
2021-09-29 15:44:43 +05:30
2021-10-28 02:41:38 +05:30
@State private var frontendURL = ""
2021-12-03 00:52:55 +05:30
@Environment(\.colorScheme) private var colorScheme
2021-10-18 04:36:00 +05:30
@EnvironmentObject<AccountsModel> private var accounts
@EnvironmentObject<SettingsModel> private var settings
2021-10-18 04:36:00 +05:30
@Default(.instances) private var instances
2021-09-29 15:44:43 +05:30
var body: some View {
VStack(alignment: .leading, spacing: 10) {
2021-09-29 15:44:43 +05:30
if !instances.isEmpty {
Picker("Instance", selection: $selectedInstanceID) {
ForEach(instances) { instance in
2021-10-17 04:18:58 +05:30
Text(instance.longDescription).tag(Optional(instance.id))
2021-09-29 15:44:43 +05:30
}
}
.labelsHidden()
} else {
Text("You have no custom locations configured")
2021-09-29 15:44:43 +05:30
.font(.caption)
.foregroundColor(.secondary)
2021-11-05 20:16:50 +05:30
Spacer()
2021-09-29 15:44:43 +05:30
}
2021-10-21 03:51:50 +05:30
if !selectedInstance.isNil, selectedInstance.app.supportsAccounts {
2021-11-05 03:31:27 +05:30
SettingsHeader(text: "Accounts")
2021-11-28 20:07:55 +05:30
let list = List(selection: $selectedAccount) {
2021-10-18 04:36:00 +05:30
if selectedInstanceAccounts.isEmpty {
Text("You have no accounts for this location")
2021-09-29 15:44:43 +05:30
.foregroundColor(.secondary)
}
2021-10-18 04:36:00 +05:30
ForEach(selectedInstanceAccounts) { account in
HStack {
Text(account.description)
Spacer()
2021-11-28 20:07:55 +05:30
Button("Remove") {
presentingAccountRemovalConfirmation = true
}
2021-12-03 00:52:55 +05:30
.foregroundColor(colorScheme == .dark ? .white : .red)
.opacity(account == selectedAccount ? 1 : 0)
}
.tag(account)
}
}
2021-11-28 20:07:55 +05:30
.alert(isPresented: $presentingAccountRemovalConfirmation) {
Alert(
title: Text(
"Are you sure you want to remove \(selectedAccount?.description ?? "") account?"
),
message: Text("This cannot be undone"),
primaryButton: .destructive(Text("Remove")) {
2021-11-28 20:07:55 +05:30
AccountsModel.remove(selectedAccount!)
},
secondaryButton: .cancel()
)
}
if #available(macOS 12.0, *) {
list
.listStyle(.inset(alternatesRowBackgrounds: true))
} else {
list
2021-09-29 15:44:43 +05:30
}
}
2021-10-28 02:41:38 +05:30
if selectedInstance != nil, selectedInstance.app.hasFrontendURL {
2021-11-05 03:31:27 +05:30
SettingsHeader(text: "Frontend URL")
2021-10-28 02:41:38 +05:30
2021-11-28 20:07:55 +05:30
TextField("Frontend URL", text: $frontendURL)
.onChange(of: selectedInstance) { _ in
frontendURL = selectedInstanceFrontendURL
2021-10-28 02:41:38 +05:30
}
.onChange(of: frontendURL) { newValue in
InstancesModel.setFrontendURL(selectedInstance, newValue)
}
.labelsHidden()
Text("Used to create links from videos, channels and playlists")
2021-11-08 03:09:28 +05:30
.font(.caption)
.foregroundColor(.secondary)
2021-10-28 02:41:38 +05:30
}
2021-10-21 03:51:50 +05:30
if selectedInstance != nil, !selectedInstance.app.supportsAccounts {
2021-10-28 02:41:38 +05:30
Spacer()
2021-10-17 04:18:58 +05:30
Text("Accounts are not supported for the application of this instance")
.font(.caption)
.foregroundColor(.secondary)
}
2021-09-29 15:44:43 +05:30
if selectedInstance != nil {
HStack {
Button("Add Account...") {
selectedAccount = nil
presentingAccountForm = true
2021-09-29 15:44:43 +05:30
}
2021-10-21 03:51:50 +05:30
.disabled(!selectedInstance.app.supportsAccounts)
2021-09-29 15:44:43 +05:30
Spacer()
Button("Remove Location") {
presentingInstanceRemovalConfirmation = true
settings.presentAlert(Alert(
2021-11-28 20:07:55 +05:30
title: Text(
"Are you sure you want to remove \(selectedInstance!.longDescription) location?"
2021-11-28 20:07:55 +05:30
),
message: Text("This cannot be undone"),
primaryButton: .destructive(Text("Remove")) {
if accounts.current?.instance == selectedInstance {
accounts.setCurrent(nil)
}
InstancesModel.remove(selectedInstance!)
selectedInstanceID = instances.last?.id
},
secondaryButton: .cancel()
))
2021-09-29 15:44:43 +05:30
}
.foregroundColor(.red)
}
}
Button("Add Location...") {
2021-09-29 15:44:43 +05:30
presentingInstanceForm = true
}
}
.frame(minWidth: 0, maxWidth: .infinity, alignment: .leading)
.onAppear {
selectedInstanceID = instances.first?.id
2021-11-28 20:07:55 +05:30
frontendURL = selectedInstanceFrontendURL
2021-09-29 15:44:43 +05:30
}
.sheet(isPresented: $presentingAccountForm) {
2021-10-23 17:21:02 +05:30
AccountForm(instance: selectedInstance, selectedAccount: $selectedAccount)
2021-09-29 15:44:43 +05:30
}
.sheet(isPresented: $presentingInstanceForm, onDismiss: setSelectedInstanceToFormInstance) {
2021-10-23 17:21:02 +05:30
InstanceForm(savedInstanceID: $savedFormInstanceID)
2021-09-29 15:44:43 +05:30
}
}
private func setSelectedInstanceToFormInstance() {
if let id = savedFormInstanceID {
selectedInstanceID = id
savedFormInstanceID = nil
}
}
var selectedInstance: Instance! {
InstancesModel.find(selectedInstanceID)
2021-09-29 15:44:43 +05:30
}
2021-11-28 20:07:55 +05:30
var selectedInstanceFrontendURL: String {
selectedInstance?.frontendURL ?? ""
}
2021-10-21 03:51:50 +05:30
private var selectedInstanceAccounts: [Account] {
2021-09-29 15:44:43 +05:30
guard selectedInstance != nil else {
return []
}
return InstancesModel.accounts(selectedInstanceID)
2021-09-29 15:44:43 +05:30
}
}
struct InstancesSettingsView_Previews: PreviewProvider {
static var previews: some View {
VStack {
2021-10-23 17:21:02 +05:30
InstancesSettings()
2021-09-29 15:44:43 +05:30
}
.frame(width: 400, height: 270)
2021-09-29 17:15:00 +05:30
.injectFixtureEnvironmentObjects()
2021-09-29 15:44:43 +05:30
}
}