2021-09-25 08:18:22 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2021-09-26 20:39:27 +00:00
|
|
|
var accounts: [Instance.Account] {
|
|
|
|
guard selectedInstance != nil else {
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
|
|
|
|
return instancesModel.accounts(selectedInstanceID)
|
|
|
|
}
|
|
|
|
|
2021-09-25 08:18:22 +00:00
|
|
|
var body: some View {
|
|
|
|
Group {
|
|
|
|
#if os(iOS)
|
2021-09-28 18:06:05 +00:00
|
|
|
Section(header: instancesHeader, footer: defaultAccountSection) {
|
2021-09-26 20:12:43 +00:00
|
|
|
ForEach(instances) { instance in
|
2021-09-25 08:18:22 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2021-09-28 18:06:05 +00:00
|
|
|
addInstanceButton
|
2021-09-25 08:18:22 +00:00
|
|
|
}
|
|
|
|
.listStyle(.insetGrouped)
|
2021-09-28 18:06:05 +00:00
|
|
|
#elseif os(tvOS)
|
|
|
|
Section(header: instancesHeader) {
|
|
|
|
ForEach(instances) { instance in
|
|
|
|
Button(action: {
|
|
|
|
self.selectedInstanceID = instance.id
|
|
|
|
self.presentingInstanceDetails = true
|
|
|
|
}) {
|
|
|
|
Text(instance.description)
|
|
|
|
}
|
|
|
|
.contextMenu {
|
|
|
|
Button("Remove", role: .destructive) {
|
|
|
|
instancesModel.remove(instance)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
addInstanceButton
|
|
|
|
|
|
|
|
defaultAccountSection
|
|
|
|
}
|
|
|
|
.frame(maxWidth: 1000, alignment: .leading)
|
|
|
|
.sheet(isPresented: $presentingAccountForm) {
|
|
|
|
AccountFormView(instance: selectedInstance, selectedAccount: $selectedAccount)
|
|
|
|
}
|
2021-09-25 08:18:22 +00:00
|
|
|
#else
|
|
|
|
Section {
|
|
|
|
Text("Instance")
|
|
|
|
|
|
|
|
if !instances.isEmpty {
|
|
|
|
Picker("Instance", selection: $selectedInstanceID) {
|
2021-09-26 20:12:43 +00:00
|
|
|
ForEach(instances) { instance in
|
2021-09-25 08:18:22 +00:00
|
|
|
Text(instance.description).tag(Optional(instance.id))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.labelsHidden()
|
|
|
|
} else {
|
|
|
|
Text("You have no instances configured")
|
|
|
|
.font(.caption)
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
}
|
|
|
|
|
2021-09-26 22:03:33 +00:00
|
|
|
if !selectedInstance.isNil {
|
2021-09-26 20:39:27 +00:00
|
|
|
if accounts.isEmpty {
|
2021-09-25 08:18:22 +00:00
|
|
|
Text("You have no accounts for this instance")
|
|
|
|
.font(.caption)
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
} else {
|
|
|
|
Text("Accounts")
|
|
|
|
List(selection: $selectedAccount) {
|
2021-09-26 20:39:27 +00:00
|
|
|
ForEach(accounts) { account in
|
2021-09-26 22:03:33 +00:00
|
|
|
AccountSettingsView(account: account,
|
2021-09-25 08:18:22 +00:00
|
|
|
selectedAccount: $selectedAccount)
|
2021-09-26 20:39:27 +00:00
|
|
|
.tag(account)
|
2021-09-25 08:18:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#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
|
|
|
|
}
|
2021-09-26 22:03:33 +00:00
|
|
|
|
|
|
|
defaultAccountSection
|
|
|
|
.padding(.top, 10)
|
2021-09-25 08:18:22 +00:00
|
|
|
}
|
|
|
|
.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)
|
|
|
|
}
|
|
|
|
|
2021-09-28 18:06:05 +00:00
|
|
|
var defaultAccountSection: some View {
|
2021-09-26 22:03:33 +00:00
|
|
|
Group {
|
|
|
|
if let account = instancesModel.defaultAccount {
|
2021-09-28 18:06:05 +00:00
|
|
|
VStack {
|
|
|
|
HStack(spacing: 2) {
|
|
|
|
Text("**\(account.description)** account on instance **\(account.instance.shortDescription)** is your default.")
|
|
|
|
.truncationMode(.middle)
|
|
|
|
.lineLimit(1)
|
2021-09-26 22:03:33 +00:00
|
|
|
|
2021-09-28 18:06:05 +00:00
|
|
|
#if !os(tvOS)
|
|
|
|
|
|
|
|
Button("Reset", action: resetDefaultAccount)
|
|
|
|
.buttonStyle(.plain)
|
|
|
|
.foregroundColor(.red)
|
|
|
|
#endif
|
|
|
|
}
|
2021-09-26 22:03:33 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Text("You have no default account set")
|
|
|
|
}
|
|
|
|
}
|
2021-09-28 18:06:05 +00:00
|
|
|
#if os(tvOS)
|
|
|
|
.foregroundColor(.gray)
|
|
|
|
#elseif os(macOS)
|
|
|
|
.font(.caption2)
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
#endif
|
2021-09-26 22:03:33 +00:00
|
|
|
}
|
|
|
|
|
2021-09-25 08:18:22 +00:00
|
|
|
var instanceDetailsNavigationLink: some View {
|
|
|
|
NavigationLink(
|
|
|
|
isActive: $presentingInstanceDetails,
|
|
|
|
destination: { InstanceDetailsSettingsView(instanceID: selectedInstanceID) },
|
|
|
|
label: { EmptyView() }
|
|
|
|
)
|
2021-09-28 18:06:05 +00:00
|
|
|
.opacity(0)
|
2021-09-25 08:18:22 +00:00
|
|
|
}
|
|
|
|
|
2021-09-28 18:06:05 +00:00
|
|
|
private var addInstanceButton: some View {
|
|
|
|
Button("Add Instance...") {
|
|
|
|
presentingInstanceForm = true
|
2021-09-26 22:03:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func resetDefaultAccount() {
|
|
|
|
instancesModel.resetDefaultAccount()
|
|
|
|
}
|
|
|
|
|
|
|
|
private func setSelectedInstanceToFormInstance() {
|
2021-09-25 08:18:22 +00:00
|
|
|
if let id = savedFormInstanceID {
|
|
|
|
selectedInstanceID = id
|
|
|
|
savedFormInstanceID = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct InstancesSettingsView_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
2021-09-26 22:03:33 +00:00
|
|
|
VStack {
|
|
|
|
InstancesSettingsView()
|
|
|
|
}
|
|
|
|
.frame(width: 400, height: 270)
|
|
|
|
.environmentObject(InstancesModel())
|
2021-09-25 08:18:22 +00:00
|
|
|
}
|
|
|
|
}
|