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

202 lines
7.4 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?
2021-10-28 02:41:38 +05:30
@State private var frontendURL = ""
2022-08-17 02:53:22 +05:30
@State private var proxiesVideos = false
2021-10-28 02:41:38 +05:30
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
}
2022-11-12 01:04:20 +05:30
if !selectedInstance.isNil, selectedInstance.app.supportsAccounts {
2022-09-04 20:58:30 +05:30
SettingsHeader(text: "Accounts".localized())
2021-11-05 03:31:27 +05:30
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") {
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-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
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 {
2022-09-04 20:58:30 +05:30
SettingsHeader(text: "Frontend URL".localized())
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
}
2022-08-17 02:53:22 +05:30
if selectedInstance != nil, selectedInstance.app.allowsDisablingVidoesProxying {
proxiesVideosToggle
.onAppear {
proxiesVideos = selectedInstance.proxiesVideos
}
.onChange(of: proxiesVideos) { newValue in
InstancesModel.setProxiesVideos(selectedInstance, newValue)
}
}
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 {
2022-11-12 01:04:20 +05:30
Button("Add Account...") {
selectedAccount = nil
presentingAccountForm = true
2021-09-29 15:44:43 +05:30
}
2022-11-12 01:04:20 +05:30
.disabled(!selectedInstance.app.supportsAccounts)
2021-09-29 15:44:43 +05:30
Spacer()
Button("Remove Location") {
settings.presentAlert(Alert(
2022-11-19 20:15:27 +05:30
title: Text(String(
format: "Are you sure you want to remove %@ location?",
selectedInstance?.longDescription ?? ""
)),
message: Text("This cannot be reverted"),
2021-11-28 20:07:55 +05:30
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
}
2022-08-17 02:53:22 +05:30
private var proxiesVideosToggle: some View {
Toggle("Proxy videos", isOn: $proxiesVideos)
}
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
}
}