mirror of
https://github.com/yattee/yattee.git
synced 2025-08-06 10:44:06 +00:00
Setting default account
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import Defaults
|
||||
import SwiftUI
|
||||
|
||||
struct AccountSettingsView: View {
|
||||
let instance: Instance
|
||||
let account: Instance.Account
|
||||
@Binding var selectedAccount: Instance.Account?
|
||||
|
||||
@@ -11,11 +11,22 @@ struct AccountSettingsView: View {
|
||||
|
||||
var body: some View {
|
||||
HStack {
|
||||
Text(account.description)
|
||||
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
|
||||
}
|
||||
@@ -34,4 +45,12 @@ struct AccountSettingsView: View {
|
||||
.opacity(account == selectedAccount ? 1 : 0)
|
||||
}
|
||||
}
|
||||
|
||||
private func makeDefault() {
|
||||
instances.setDefaultAccount(account)
|
||||
}
|
||||
|
||||
private func resetDefault() {
|
||||
instances.resetDefaultAccount()
|
||||
}
|
||||
}
|
||||
|
@@ -16,14 +16,24 @@ struct InstanceDetailsSettingsView: View {
|
||||
List {
|
||||
Section(header: Text("Accounts")) {
|
||||
ForEach(instances.accounts(instanceID)) { account in
|
||||
Text(account.description)
|
||||
HStack(spacing: 2) {
|
||||
Text(account.description)
|
||||
if instances.defaultAccount == account {
|
||||
Text("— default")
|
||||
.foregroundColor(.secondary)
|
||||
}
|
||||
}
|
||||
#if !os(tvOS)
|
||||
.swipeActions(edge: .trailing, allowsFullSwipe: false) {
|
||||
Button("Remove", role: .destructive) {
|
||||
instances.removeAccount(account)
|
||||
accountsChanged.toggle()
|
||||
.swipeActions(edge: .leading, allowsFullSwipe: true) {
|
||||
if instances.defaultAccount != account {
|
||||
Button("Make Default", action: { makeDefault(account) })
|
||||
} else {
|
||||
Button("Reset Default", action: resetDefaultAccount)
|
||||
}
|
||||
}
|
||||
.swipeActions(edge: .trailing, allowsFullSwipe: false) {
|
||||
Button("Remove", role: .destructive, action: { removeAccount(account) })
|
||||
}
|
||||
#endif
|
||||
}
|
||||
.redrawOn(change: accountsChanged)
|
||||
@@ -42,4 +52,19 @@ struct InstanceDetailsSettingsView: View {
|
||||
AccountFormView(instance: instance)
|
||||
}
|
||||
}
|
||||
|
||||
private func makeDefault(_ account: Instance.Account) {
|
||||
instances.setDefaultAccount(account)
|
||||
accountsChanged.toggle()
|
||||
}
|
||||
|
||||
private func resetDefaultAccount() {
|
||||
instances.resetDefaultAccount()
|
||||
accountsChanged.toggle()
|
||||
}
|
||||
|
||||
private func removeAccount(_ account: Instance.Account) {
|
||||
instances.removeAccount(account)
|
||||
accountsChanged.toggle()
|
||||
}
|
||||
}
|
||||
|
@@ -34,7 +34,7 @@ struct InstancesSettingsView: View {
|
||||
var body: some View {
|
||||
Group {
|
||||
#if os(iOS)
|
||||
Section(header: instancesHeader) {
|
||||
Section(header: instancesHeader, footer: instancesFooter) {
|
||||
ForEach(instances) { instance in
|
||||
Button(action: {
|
||||
self.selectedInstanceID = instance.id
|
||||
@@ -81,7 +81,7 @@ struct InstancesSettingsView: View {
|
||||
.foregroundColor(.secondary)
|
||||
}
|
||||
|
||||
if let instance = selectedInstance {
|
||||
if !selectedInstance.isNil {
|
||||
if accounts.isEmpty {
|
||||
Text("You have no accounts for this instance")
|
||||
.font(.caption)
|
||||
@@ -90,7 +90,7 @@ struct InstancesSettingsView: View {
|
||||
Text("Accounts")
|
||||
List(selection: $selectedAccount) {
|
||||
ForEach(accounts) { account in
|
||||
AccountSettingsView(instance: instance, account: account,
|
||||
AccountSettingsView(account: account,
|
||||
selectedAccount: $selectedAccount)
|
||||
.tag(account)
|
||||
}
|
||||
@@ -131,6 +131,9 @@ struct InstancesSettingsView: View {
|
||||
Button("Add Instance...") {
|
||||
presentingInstanceForm = true
|
||||
}
|
||||
|
||||
defaultAccountSection
|
||||
.padding(.top, 10)
|
||||
}
|
||||
.frame(minWidth: 0, maxWidth: .infinity, alignment: .leading)
|
||||
|
||||
@@ -154,6 +157,24 @@ struct InstancesSettingsView: View {
|
||||
Text("Instances").background(instanceDetailsNavigationLink)
|
||||
}
|
||||
|
||||
var instancesFooter: some View {
|
||||
Group {
|
||||
if let account = instancesModel.defaultAccount {
|
||||
HStack(spacing: 2) {
|
||||
Text("**\(account.description)** account on instance **\(account.instance.shortDescription)** is your default.")
|
||||
.truncationMode(.middle)
|
||||
.lineLimit(1)
|
||||
|
||||
Button("Reset", action: resetDefaultAccount)
|
||||
.buttonStyle(.plain)
|
||||
.foregroundColor(.red)
|
||||
}
|
||||
} else {
|
||||
Text("You have no default account set")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var instanceDetailsNavigationLink: some View {
|
||||
NavigationLink(
|
||||
isActive: $presentingInstanceDetails,
|
||||
@@ -162,7 +183,30 @@ struct InstancesSettingsView: View {
|
||||
)
|
||||
}
|
||||
|
||||
func setSelectedInstanceToFormInstance() {
|
||||
private var defaultAccountSection: some View {
|
||||
Group {
|
||||
if let account = instancesModel.defaultAccount {
|
||||
HStack(spacing: 2) {
|
||||
Text("**\(account.description)** account on instance **\(account.instance.shortDescription)** is your default.")
|
||||
.truncationMode(.middle)
|
||||
.lineLimit(1)
|
||||
Button("Reset", action: resetDefaultAccount)
|
||||
.buttonStyle(.plain)
|
||||
.foregroundColor(.red)
|
||||
}
|
||||
} else {
|
||||
Text("You have no default account set")
|
||||
}
|
||||
}
|
||||
.font(.caption2)
|
||||
.foregroundColor(.secondary)
|
||||
}
|
||||
|
||||
private func resetDefaultAccount() {
|
||||
instancesModel.resetDefaultAccount()
|
||||
}
|
||||
|
||||
private func setSelectedInstanceToFormInstance() {
|
||||
if let id = savedFormInstanceID {
|
||||
selectedInstanceID = id
|
||||
savedFormInstanceID = nil
|
||||
@@ -172,6 +216,10 @@ struct InstancesSettingsView: View {
|
||||
|
||||
struct InstancesSettingsView_Previews: PreviewProvider {
|
||||
static var previews: some View {
|
||||
InstancesSettingsView()
|
||||
VStack {
|
||||
InstancesSettingsView()
|
||||
}
|
||||
.frame(width: 400, height: 270)
|
||||
.environmentObject(InstancesModel())
|
||||
}
|
||||
}
|
||||
|
@@ -29,28 +29,27 @@ struct SettingsView: View {
|
||||
.tag(Tabs.playback)
|
||||
}
|
||||
.padding(20)
|
||||
.frame(width: 400, height: 270)
|
||||
.frame(width: 400, height: 310)
|
||||
#else
|
||||
NavigationView {
|
||||
List {
|
||||
InstancesSettingsView()
|
||||
PlaybackSettingsView()
|
||||
}
|
||||
.navigationTitle("Settings")
|
||||
.toolbar {
|
||||
ToolbarItem(placement: .navigationBarTrailing) {
|
||||
Button("Done") {
|
||||
dismiss()
|
||||
}
|
||||
#if !os(tvOS)
|
||||
.keyboardShortcut(.cancelAction)
|
||||
#endif
|
||||
}
|
||||
}
|
||||
#if os(iOS)
|
||||
.listStyle(.insetGrouped)
|
||||
#endif
|
||||
|
||||
.navigationTitle("Settings")
|
||||
.toolbar {
|
||||
ToolbarItem(placement: .navigationBarTrailing) {
|
||||
Button("Done") {
|
||||
dismiss()
|
||||
}
|
||||
#if !os(tvOS)
|
||||
.keyboardShortcut(.cancelAction)
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
Reference in New Issue
Block a user