Settings UI and code improvements

This commit is contained in:
Arkadiusz Fal
2021-09-29 12:14:43 +02:00
parent bd813e6771
commit d22868ed2d
7 changed files with 244 additions and 227 deletions

View File

@@ -1,56 +0,0 @@
import Defaults
import SwiftUI
struct AccountSettingsView: View {
let account: Instance.Account
@Binding var selectedAccount: Instance.Account?
@State private var presentingRemovalConfirmationDialog = false
@EnvironmentObject<InstancesModel> private var instances
var body: some View {
HStack {
HStack(spacing: 2) {
Text(account.description)
if instances.defaultAccount == account {
Text("— default")
.foregroundColor(.secondary)
}
}
Spacer()
HStack {
if instances.defaultAccount != account {
Button("Make default", action: makeDefault)
} else {
Button("Reset default", action: resetDefault)
}
Button("Remove", role: .destructive) {
presentingRemovalConfirmationDialog = true
}
.confirmationDialog(
"Are you sure you want to remove \(account.description) account?",
isPresented: $presentingRemovalConfirmationDialog
) {
Button("Remove", role: .destructive) {
instances.removeAccount(account)
}
}
#if os(macOS)
.foregroundColor(.red)
#endif
}
.opacity(account == selectedAccount ? 1 : 0)
}
}
private func makeDefault() {
instances.setDefaultAccount(account)
}
private func resetDefault() {
instances.resetDefaultAccount()
}
}

View File

@@ -1,6 +1,6 @@
import SwiftUI
struct InstanceDetailsSettingsView: View {
struct AccountsSettingsView: View {
let instanceID: Instance.ID?
@State private var accountsChanged = false
@@ -14,9 +14,9 @@ struct InstanceDetailsSettingsView: View {
var body: some View {
List {
Section(header: Text("Accounts")) {
Section(header: Text("Accounts"), footer: sectionFooter) {
ForEach(instances.accounts(instanceID), id: \.self) { account in
#if !os(tvOS)
#if os(iOS)
HStack(spacing: 2) {
Text(account.description)
if instances.defaultAccount == account {
@@ -58,16 +58,24 @@ struct InstanceDetailsSettingsView: View {
}
}
}
.navigationTitle(instance.shortDescription)
.sheet(isPresented: $presentingAccountForm, onDismiss: { accountsChanged.toggle() }) {
AccountFormView(instance: instance)
}
#if os(iOS)
.listStyle(.insetGrouped)
#elseif os(tvOS)
.frame(maxWidth: 1000)
#endif
}
.navigationTitle(instance.shortDescription)
.sheet(isPresented: $presentingAccountForm, onDismiss: { accountsChanged.toggle() }) {
AccountFormView(instance: instance)
}
private var sectionFooter: some View {
#if os(iOS)
Text("Swipe right to toggle default account, swipe left to remove")
#else
Text("Tap to toggle default account, tap and hold to remove")
.foregroundColor(.secondary)
#endif
}
private func makeDefault(_ account: Instance.Account) {

View File

@@ -0,0 +1,38 @@
import Foundation
import SwiftUI
struct DefaultAccountHint: View {
@EnvironmentObject<InstancesModel> private var instancesModel
var body: some View {
Group {
if !instancesModel.defaultAccount.isNil {
VStack {
HStack(spacing: 2) {
hintText
.truncationMode(.middle)
.lineLimit(1)
}
}
} else {
Text("You have no default account set")
}
}
#if os(tvOS)
.foregroundColor(.gray)
#elseif os(macOS)
.font(.caption2)
.foregroundColor(.secondary)
#endif
}
var hintText: some View {
Group {
if let account = instancesModel.defaultAccount {
Text(
"**\(account.description)** account on instance **\(account.instance.shortDescription)** is your default."
)
}
}
}
}

View File

@@ -3,235 +3,62 @@ import SwiftUI
struct InstancesSettingsView: View {
@Default(.instances) private var instances
@EnvironmentObject<InstancesModel> private var instancesModel
@EnvironmentObject<InvidiousAPI> private var api
@EnvironmentObject<InstancesModel> private var instancesModel
@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)
}
var accounts: [Instance.Account] {
guard selectedInstance != nil else {
return []
}
return instancesModel.accounts(selectedInstanceID)
}
var body: some View {
Group {
#if os(iOS)
Section(header: instancesHeader, footer: defaultAccountSection) {
ForEach(instances) { instance in
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)
}
}
Section(header: Text("Instances"), footer: DefaultAccountHint()) {
ForEach(instances) { instance in
NavigationLink(instance.description) {
AccountsSettingsView(instanceID: instance.id)
}
#if os(iOS)
.swipeActions(edge: .trailing, allowsFullSwipe: false) {
Button("Remove", role: .destructive) {
instancesModel.remove(instance)
}
removeInstanceButton(instance)
}
.buttonStyle(.plain)
}
addInstanceButton
}
.listStyle(.insetGrouped)
#elseif os(tvOS)
Section(header: instancesHeader) {
ForEach(instances) { instance in
Button(action: {
self.selectedInstanceID = instance.id
self.presentingInstanceDetails = true
}) {
Text(instance.description)
}
#else
.contextMenu {
Button("Remove", role: .destructive) {
instancesModel.remove(instance)
}
removeInstanceButton(instance)
}
}
addInstanceButton
defaultAccountSection
}
.frame(maxWidth: 1000, alignment: .leading)
.sheet(isPresented: $presentingAccountForm) {
AccountFormView(instance: selectedInstance, selectedAccount: $selectedAccount)
}
#else
Section {
Text("Instance")
if !instances.isEmpty {
Picker("Instance", selection: $selectedInstanceID) {
ForEach(instances) { instance in
Text(instance.description).tag(Optional(instance.id))
}
}
.labelsHidden()
} else {
Text("You have no instances configured")
.font(.caption)
.foregroundColor(.secondary)
}
if !selectedInstance.isNil {
if accounts.isEmpty {
Text("You have no accounts for this instance")
.font(.caption)
.foregroundColor(.secondary)
} else {
Text("Accounts")
List(selection: $selectedAccount) {
ForEach(accounts) { account in
AccountSettingsView(account: account,
selectedAccount: $selectedAccount)
.tag(account)
}
}
#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
}
defaultAccountSection
.padding(.top, 10)
}
.frame(minWidth: 0, maxWidth: .infinity, alignment: .leading)
.onAppear {
selectedInstanceID = instances.first?.id
}
.sheet(isPresented: $presentingAccountForm) {
AccountFormView(instance: selectedInstance, selectedAccount: $selectedAccount)
#endif
}
Spacer()
addInstanceButton
}
#if os(iOS)
.listStyle(.insetGrouped)
#endif
}
.sheet(isPresented: $presentingInstanceForm, onDismiss: setSelectedInstanceToFormInstance) {
.sheet(isPresented: $presentingInstanceForm) {
InstanceFormView(savedInstanceID: $savedFormInstanceID)
}
}
var instancesHeader: some View {
Text("Instances").background(instanceDetailsNavigationLink)
}
var defaultAccountSection: some View {
Group {
if let account = instancesModel.defaultAccount {
VStack {
HStack(spacing: 2) {
Text("**\(account.description)** account on instance **\(account.instance.shortDescription)** is your default.")
.truncationMode(.middle)
.lineLimit(1)
#if !os(tvOS)
Button("Reset", action: resetDefaultAccount)
.buttonStyle(.plain)
.foregroundColor(.red)
#endif
}
}
} else {
Text("You have no default account set")
}
}
#if os(tvOS)
.foregroundColor(.gray)
#elseif os(macOS)
.font(.caption2)
.foregroundColor(.secondary)
#endif
}
var instanceDetailsNavigationLink: some View {
NavigationLink(
isActive: $presentingInstanceDetails,
destination: { InstanceDetailsSettingsView(instanceID: selectedInstanceID) },
label: { EmptyView() }
)
.opacity(0)
}
private var addInstanceButton: some View {
Button("Add Instance...") {
presentingInstanceForm = true
}
}
private func resetDefaultAccount() {
instancesModel.resetDefaultAccount()
private func removeInstanceButton(_ instance: Instance) -> some View {
Button("Remove", role: .destructive) {
instancesModel.remove(instance)
}
}
private func setSelectedInstanceToFormInstance() {
if let id = savedFormInstanceID {
selectedInstanceID = id
savedFormInstanceID = nil
}
private func resetDefaultAccount() {
instancesModel.resetDefaultAccount()
}
}