2021-09-29 10:14:43 +00:00
|
|
|
import Defaults
|
|
|
|
import SwiftUI
|
|
|
|
|
2021-10-23 11:51:02 +00:00
|
|
|
struct InstancesSettings: View {
|
2021-09-29 10:14:43 +00:00
|
|
|
@State private var selectedInstanceID: Instance.ID?
|
2021-10-20 22:21:50 +00:00
|
|
|
@State private var selectedAccount: Account?
|
2021-09-29 10:14:43 +00:00
|
|
|
|
|
|
|
@State private var presentingAccountForm = false
|
|
|
|
@State private var presentingInstanceForm = false
|
|
|
|
@State private var savedFormInstanceID: Instance.ID?
|
|
|
|
|
2021-10-19 21:27:04 +00:00
|
|
|
@State private var presentingAccountRemovalConfirmation = false
|
|
|
|
@State private var presentingInstanceRemovalConfirmation = false
|
2021-09-29 10:14:43 +00:00
|
|
|
|
2021-10-27 21:11:38 +00:00
|
|
|
@State private var frontendURL = ""
|
|
|
|
|
2021-10-17 23:06:00 +00:00
|
|
|
@EnvironmentObject<AccountsModel> private var accounts
|
|
|
|
@EnvironmentObject<InstancesModel> private var model
|
|
|
|
|
|
|
|
@Default(.instances) private var instances
|
|
|
|
|
2021-09-29 10:14:43 +00:00
|
|
|
var body: some View {
|
2021-11-04 22:01:27 +00:00
|
|
|
Group {
|
|
|
|
SettingsHeader(text: "Instance")
|
2021-09-29 10:14:43 +00:00
|
|
|
|
|
|
|
if !instances.isEmpty {
|
|
|
|
Picker("Instance", selection: $selectedInstanceID) {
|
|
|
|
ForEach(instances) { instance in
|
2021-10-16 22:48:58 +00:00
|
|
|
Text(instance.longDescription).tag(Optional(instance.id))
|
2021-09-29 10:14:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
.labelsHidden()
|
|
|
|
} else {
|
|
|
|
Text("You have no instances configured")
|
|
|
|
.font(.caption)
|
|
|
|
.foregroundColor(.secondary)
|
2021-11-05 14:46:50 +00:00
|
|
|
|
|
|
|
Spacer()
|
2021-09-29 10:14:43 +00:00
|
|
|
}
|
|
|
|
|
2021-10-20 22:21:50 +00:00
|
|
|
if !selectedInstance.isNil, selectedInstance.app.supportsAccounts {
|
2021-11-04 22:01:27 +00:00
|
|
|
SettingsHeader(text: "Accounts")
|
|
|
|
|
2021-09-29 10:14:43 +00:00
|
|
|
List(selection: $selectedAccount) {
|
2021-10-17 23:06:00 +00:00
|
|
|
if selectedInstanceAccounts.isEmpty {
|
2021-09-29 10:14:43 +00:00
|
|
|
Text("You have no accounts for this instance")
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
}
|
2021-10-17 23:06:00 +00:00
|
|
|
ForEach(selectedInstanceAccounts) { account in
|
2021-10-19 21:27:04 +00:00
|
|
|
HStack {
|
|
|
|
Text(account.description)
|
|
|
|
|
|
|
|
Spacer()
|
|
|
|
|
|
|
|
Button("Remove", role: .destructive) {
|
|
|
|
presentingAccountRemovalConfirmation = true
|
|
|
|
}
|
|
|
|
.foregroundColor(.red)
|
|
|
|
.opacity(account == selectedAccount ? 1 : 0)
|
|
|
|
}
|
|
|
|
.tag(account)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.confirmationDialog(
|
|
|
|
"Are you sure you want to remove \(selectedAccount?.description ?? "") account?",
|
|
|
|
isPresented: $presentingAccountRemovalConfirmation
|
|
|
|
) {
|
|
|
|
Button("Remove", role: .destructive) {
|
|
|
|
AccountsModel.remove(selectedAccount!)
|
2021-09-29 10:14:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
.listStyle(.inset(alternatesRowBackgrounds: true))
|
|
|
|
}
|
|
|
|
|
2021-10-27 21:11:38 +00:00
|
|
|
if selectedInstance != nil, selectedInstance.app.hasFrontendURL {
|
2021-11-04 22:01:27 +00:00
|
|
|
SettingsHeader(text: "Frontend URL")
|
2021-10-27 21:11:38 +00:00
|
|
|
|
|
|
|
TextField("Frontend URL", text: $frontendURL, prompt: Text("Frontend URL"))
|
|
|
|
.onAppear {
|
|
|
|
frontendURL = selectedInstance.frontendURL ?? ""
|
|
|
|
}
|
|
|
|
.onChange(of: frontendURL) { newValue in
|
|
|
|
InstancesModel.setFrontendURL(selectedInstance, newValue)
|
|
|
|
}
|
|
|
|
.labelsHidden()
|
|
|
|
VStack(alignment: .leading, spacing: 0) {
|
|
|
|
Text("If provided, you can copy links from videos, channels and playlist using")
|
|
|
|
.padding(.trailing, 2)
|
|
|
|
|
|
|
|
HStack(spacing: 0) {
|
|
|
|
Image(systemName: "command")
|
|
|
|
|
|
|
|
Text("**+C**")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.font(.caption)
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
|
|
|
|
Spacer()
|
|
|
|
}
|
|
|
|
|
2021-10-20 22:21:50 +00:00
|
|
|
if selectedInstance != nil, !selectedInstance.app.supportsAccounts {
|
2021-10-27 21:11:38 +00:00
|
|
|
Spacer()
|
2021-10-16 22:48:58 +00:00
|
|
|
Text("Accounts are not supported for the application of this instance")
|
|
|
|
.font(.caption)
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
}
|
|
|
|
|
2021-09-29 10:14:43 +00:00
|
|
|
if selectedInstance != nil {
|
|
|
|
HStack {
|
2021-10-19 21:27:04 +00:00
|
|
|
Button("Add Account...") {
|
|
|
|
selectedAccount = nil
|
|
|
|
presentingAccountForm = true
|
2021-09-29 10:14:43 +00:00
|
|
|
}
|
2021-10-20 22:21:50 +00:00
|
|
|
.disabled(!selectedInstance.app.supportsAccounts)
|
2021-09-29 10:14:43 +00:00
|
|
|
|
|
|
|
Spacer()
|
|
|
|
|
|
|
|
Button("Remove Instance", role: .destructive) {
|
2021-10-19 21:27:04 +00:00
|
|
|
presentingInstanceRemovalConfirmation = true
|
2021-09-29 10:14:43 +00:00
|
|
|
}
|
|
|
|
.confirmationDialog(
|
2021-10-16 22:48:58 +00:00
|
|
|
"Are you sure you want to remove \(selectedInstance!.longDescription) instance?",
|
2021-10-19 21:27:04 +00:00
|
|
|
isPresented: $presentingInstanceRemovalConfirmation
|
2021-09-29 10:14:43 +00:00
|
|
|
) {
|
|
|
|
Button("Remove Instance", role: .destructive) {
|
2021-10-19 21:27:04 +00:00
|
|
|
if accounts.current?.instance == selectedInstance {
|
|
|
|
accounts.setCurrent(nil)
|
2021-10-17 23:06:00 +00:00
|
|
|
}
|
|
|
|
|
2021-10-19 21:27:04 +00:00
|
|
|
InstancesModel.remove(selectedInstance!)
|
2021-09-29 10:14:43 +00:00
|
|
|
selectedInstanceID = instances.last?.id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
.foregroundColor(.red)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Button("Add Instance...") {
|
|
|
|
presentingInstanceForm = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.frame(minWidth: 0, maxWidth: .infinity, alignment: .leading)
|
|
|
|
|
|
|
|
.onAppear {
|
|
|
|
selectedInstanceID = instances.first?.id
|
|
|
|
}
|
|
|
|
.sheet(isPresented: $presentingAccountForm) {
|
2021-10-23 11:51:02 +00:00
|
|
|
AccountForm(instance: selectedInstance, selectedAccount: $selectedAccount)
|
2021-09-29 10:14:43 +00:00
|
|
|
}
|
|
|
|
.sheet(isPresented: $presentingInstanceForm, onDismiss: setSelectedInstanceToFormInstance) {
|
2021-10-23 11:51:02 +00:00
|
|
|
InstanceForm(savedInstanceID: $savedFormInstanceID)
|
2021-09-29 10:14:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func setSelectedInstanceToFormInstance() {
|
|
|
|
if let id = savedFormInstanceID {
|
|
|
|
selectedInstanceID = id
|
|
|
|
savedFormInstanceID = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var selectedInstance: Instance! {
|
2021-10-19 21:27:04 +00:00
|
|
|
InstancesModel.find(selectedInstanceID)
|
2021-09-29 10:14:43 +00:00
|
|
|
}
|
|
|
|
|
2021-10-20 22:21:50 +00:00
|
|
|
private var selectedInstanceAccounts: [Account] {
|
2021-09-29 10:14:43 +00:00
|
|
|
guard selectedInstance != nil else {
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
|
2021-10-19 21:27:04 +00:00
|
|
|
return InstancesModel.accounts(selectedInstanceID)
|
2021-09-29 10:14:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct InstancesSettingsView_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
|
|
|
VStack {
|
2021-10-23 11:51:02 +00:00
|
|
|
InstancesSettings()
|
2021-09-29 10:14:43 +00:00
|
|
|
}
|
|
|
|
.frame(width: 400, height: 270)
|
2021-09-29 11:45:00 +00:00
|
|
|
.injectFixtureEnvironmentObjects()
|
2021-09-29 10:14:43 +00:00
|
|
|
}
|
|
|
|
}
|