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-27 21:11:38 +00:00
|
|
|
@State private var frontendURL = ""
|
2022-08-16 21:23:22 +00:00
|
|
|
@State private var proxiesVideos = false
|
2021-10-27 21:11:38 +00:00
|
|
|
|
2021-12-02 19:22:55 +00:00
|
|
|
@Environment(\.colorScheme) private var colorScheme
|
2022-11-24 20:36:05 +00:00
|
|
|
@ObservedObject private var accounts = AccountsModel.shared
|
|
|
|
private var settings = SettingsModel.shared
|
2021-10-17 23:06:00 +00:00
|
|
|
|
|
|
|
@Default(.instances) private var instances
|
|
|
|
|
2021-09-29 10:14:43 +00:00
|
|
|
var body: some View {
|
2022-07-01 21:28:32 +00:00
|
|
|
VStack(alignment: .leading, spacing: 10) {
|
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 {
|
2022-07-01 21:28:32 +00:00
|
|
|
Text("You have no custom locations configured")
|
2021-09-29 10:14:43 +00:00
|
|
|
.font(.caption)
|
|
|
|
.foregroundColor(.secondary)
|
2021-11-05 14:46:50 +00:00
|
|
|
|
|
|
|
Spacer()
|
2021-09-29 10:14:43 +00:00
|
|
|
}
|
|
|
|
|
2022-11-11 19:34:20 +00:00
|
|
|
if !selectedInstance.isNil, selectedInstance.app.supportsAccounts {
|
2022-09-04 15:28:30 +00:00
|
|
|
SettingsHeader(text: "Accounts".localized())
|
2021-11-04 22:01:27 +00:00
|
|
|
|
2023-07-01 16:38:11 +00:00
|
|
|
List(selection: $selectedAccount) {
|
2021-10-17 23:06:00 +00:00
|
|
|
if selectedInstanceAccounts.isEmpty {
|
2022-07-01 21:28:32 +00:00
|
|
|
Text("You have no accounts for this location")
|
2021-09-29 10:14:43 +00:00
|
|
|
.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()
|
|
|
|
|
2021-11-28 14:37:55 +00:00
|
|
|
Button("Remove") {
|
2022-08-16 22:34:59 +00:00
|
|
|
settings.presentAlert(
|
|
|
|
Alert(
|
|
|
|
title: Text(
|
|
|
|
"Are you sure you want to remove \(selectedAccount?.description ?? "") account?"
|
|
|
|
),
|
|
|
|
message: Text("This cannot be reverted"),
|
|
|
|
primaryButton: .destructive(Text("Remove")) {
|
|
|
|
AccountsModel.remove(selectedAccount!)
|
|
|
|
},
|
|
|
|
secondaryButton: .cancel()
|
|
|
|
)
|
|
|
|
)
|
2021-10-19 21:27:04 +00:00
|
|
|
}
|
2021-12-02 19:22:55 +00:00
|
|
|
.foregroundColor(colorScheme == .dark ? .white : .red)
|
2021-10-19 21:27:04 +00:00
|
|
|
.opacity(account == selectedAccount ? 1 : 0)
|
|
|
|
}
|
|
|
|
.tag(account)
|
|
|
|
}
|
|
|
|
}
|
2023-07-01 16:38:11 +00:00
|
|
|
.listStyle(.inset(alternatesRowBackgrounds: true))
|
2021-09-29 10:14:43 +00:00
|
|
|
}
|
|
|
|
|
2021-10-27 21:11:38 +00:00
|
|
|
if selectedInstance != nil, selectedInstance.app.hasFrontendURL {
|
2022-09-04 15:28:30 +00:00
|
|
|
SettingsHeader(text: "Frontend URL".localized())
|
2021-10-27 21:11:38 +00:00
|
|
|
|
2021-11-28 14:37:55 +00:00
|
|
|
TextField("Frontend URL", text: $frontendURL)
|
|
|
|
.onChange(of: selectedInstance) { _ in
|
|
|
|
frontendURL = selectedInstanceFrontendURL
|
2021-10-27 21:11:38 +00:00
|
|
|
}
|
|
|
|
.onChange(of: frontendURL) { newValue in
|
2022-11-24 20:36:05 +00:00
|
|
|
InstancesModel.shared.setFrontendURL(selectedInstance, newValue)
|
2021-10-27 21:11:38 +00:00
|
|
|
}
|
|
|
|
.labelsHidden()
|
|
|
|
|
2021-12-19 17:17:04 +00:00
|
|
|
Text("Used to create links from videos, channels and playlists")
|
2021-11-07 21:39:28 +00:00
|
|
|
.font(.caption)
|
|
|
|
.foregroundColor(.secondary)
|
2021-10-27 21:11:38 +00:00
|
|
|
}
|
|
|
|
|
2022-08-16 21:23:22 +00:00
|
|
|
if selectedInstance != nil, selectedInstance.app.allowsDisablingVidoesProxying {
|
|
|
|
proxiesVideosToggle
|
|
|
|
.onAppear {
|
|
|
|
proxiesVideos = selectedInstance.proxiesVideos
|
|
|
|
}
|
|
|
|
.onChange(of: proxiesVideos) { newValue in
|
2022-11-24 20:36:05 +00:00
|
|
|
InstancesModel.shared.setProxiesVideos(selectedInstance, newValue)
|
2022-08-16 21:23:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2022-11-11 19:34:20 +00:00
|
|
|
Button("Add Account...") {
|
|
|
|
selectedAccount = nil
|
|
|
|
presentingAccountForm = true
|
2021-09-29 10:14:43 +00:00
|
|
|
}
|
2022-11-11 19:34:20 +00:00
|
|
|
.disabled(!selectedInstance.app.supportsAccounts)
|
2021-09-29 10:14:43 +00:00
|
|
|
|
|
|
|
Spacer()
|
|
|
|
|
2022-07-01 21:28:32 +00:00
|
|
|
Button("Remove Location") {
|
|
|
|
settings.presentAlert(Alert(
|
2022-11-19 14:45:27 +00:00
|
|
|
title: Text(String(
|
|
|
|
format: "Are you sure you want to remove %@ location?",
|
|
|
|
selectedInstance?.longDescription ?? ""
|
|
|
|
)),
|
2022-08-16 22:34:59 +00:00
|
|
|
message: Text("This cannot be reverted"),
|
2021-11-28 14:37:55 +00:00
|
|
|
primaryButton: .destructive(Text("Remove")) {
|
|
|
|
if accounts.current?.instance == selectedInstance {
|
|
|
|
accounts.setCurrent(nil)
|
|
|
|
}
|
|
|
|
|
2022-11-24 20:36:05 +00:00
|
|
|
InstancesModel.shared.remove(selectedInstance!)
|
2021-11-28 14:37:55 +00:00
|
|
|
selectedInstanceID = instances.last?.id
|
|
|
|
},
|
|
|
|
secondaryButton: .cancel()
|
2022-07-01 21:28:32 +00:00
|
|
|
))
|
2021-09-29 10:14:43 +00:00
|
|
|
}
|
|
|
|
.foregroundColor(.red)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-25 11:08:21 +00:00
|
|
|
HStack {
|
|
|
|
Button("Add Location...") {
|
|
|
|
presentingInstanceForm = true
|
|
|
|
}
|
|
|
|
Spacer()
|
|
|
|
AddPublicInstanceButton()
|
2021-09-29 10:14:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
.frame(minWidth: 0, maxWidth: .infinity, alignment: .leading)
|
|
|
|
|
|
|
|
.onAppear {
|
|
|
|
selectedInstanceID = instances.first?.id
|
2021-11-28 14:37:55 +00:00
|
|
|
frontendURL = selectedInstanceFrontendURL
|
2021-09-29 10:14:43 +00:00
|
|
|
}
|
|
|
|
.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! {
|
2022-11-24 20:36:05 +00:00
|
|
|
InstancesModel.shared.find(selectedInstanceID)
|
2021-09-29 10:14:43 +00:00
|
|
|
}
|
|
|
|
|
2021-11-28 14:37:55 +00:00
|
|
|
var selectedInstanceFrontendURL: String {
|
|
|
|
selectedInstance?.frontendURL ?? ""
|
|
|
|
}
|
|
|
|
|
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 []
|
|
|
|
}
|
|
|
|
|
2022-11-24 20:36:05 +00:00
|
|
|
return InstancesModel.shared.accounts(selectedInstanceID)
|
2021-09-29 10:14:43 +00:00
|
|
|
}
|
2022-08-16 21:23:22 +00:00
|
|
|
|
|
|
|
private var proxiesVideosToggle: some View {
|
|
|
|
Toggle("Proxy videos", isOn: $proxiesVideos)
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|