mirror of
https://github.com/yattee/yattee.git
synced 2025-08-09 20:24:06 +00:00
Save last used account instead of having to set default
This commit is contained in:
@@ -124,7 +124,7 @@ struct AccountFormView: View {
|
||||
return
|
||||
}
|
||||
|
||||
let account = instances.addAccount(instance: instance, name: name, sid: sid)
|
||||
let account = AccountsModel.add(instance: instance, name: name, sid: sid)
|
||||
selectedAccount?.wrappedValue = account
|
||||
|
||||
dismiss()
|
||||
|
@@ -6,10 +6,11 @@ struct AccountsSettingsView: View {
|
||||
@State private var accountsChanged = false
|
||||
@State private var presentingAccountForm = false
|
||||
|
||||
@EnvironmentObject<AccountsModel> private var model
|
||||
@EnvironmentObject<InstancesModel> private var instances
|
||||
|
||||
var instance: Instance! {
|
||||
instances.find(instanceID)
|
||||
InstancesModel.find(instanceID)
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
@@ -27,41 +28,18 @@ struct AccountsSettingsView: View {
|
||||
var accounts: some View {
|
||||
List {
|
||||
Section(header: Text("Accounts"), footer: sectionFooter) {
|
||||
ForEach(instances.accounts(instanceID), id: \.self) { account in
|
||||
#if os(iOS)
|
||||
HStack(spacing: 2) {
|
||||
Text(account.description)
|
||||
if instances.defaultAccount == account {
|
||||
Text("— default")
|
||||
.foregroundColor(.secondary)
|
||||
ForEach(InstancesModel.accounts(instanceID), id: \.self) { account in
|
||||
#if os(tvOS)
|
||||
Button(account.description) {}
|
||||
.contextMenu {
|
||||
Button("Remove", role: .destructive) { removeAccount(account) }
|
||||
Button("Cancel", role: .cancel) {}
|
||||
}
|
||||
}
|
||||
.swipeActions(edge: .leading, allowsFullSwipe: true) {
|
||||
if instances.defaultAccount != account {
|
||||
Button("Make Default") { makeDefault(account) }
|
||||
} else {
|
||||
Button("Reset Default", action: resetDefaultAccount)
|
||||
}
|
||||
}
|
||||
.swipeActions(edge: .trailing, allowsFullSwipe: false) {
|
||||
Button("Remove", role: .destructive) { removeAccount(account) }
|
||||
}
|
||||
|
||||
#else
|
||||
Button(action: { toggleDefault(account) }) {
|
||||
HStack(spacing: 2) {
|
||||
Text(account.description)
|
||||
if instances.defaultAccount == account {
|
||||
Text("— default")
|
||||
.foregroundColor(.secondary)
|
||||
}
|
||||
Text(account.description)
|
||||
.swipeActions(edge: .trailing, allowsFullSwipe: false) {
|
||||
Button("Remove", role: .destructive) { removeAccount(account) }
|
||||
}
|
||||
}
|
||||
.contextMenu {
|
||||
Button("Toggle Default") { toggleDefault(account) }
|
||||
Button("Remove", role: .destructive) { removeAccount(account) }
|
||||
Button("Cancel", role: .cancel) {}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
.redrawOn(change: accountsChanged)
|
||||
@@ -83,33 +61,15 @@ struct AccountsSettingsView: View {
|
||||
|
||||
private var sectionFooter: some View {
|
||||
#if os(iOS)
|
||||
Text("Swipe right to toggle default account, swipe left to remove")
|
||||
Text("Swipe to remove account")
|
||||
#else
|
||||
Text("Tap to toggle default account, tap and hold to remove")
|
||||
Text("Tap and hold to remove account")
|
||||
.foregroundColor(.secondary)
|
||||
#endif
|
||||
}
|
||||
|
||||
private func makeDefault(_ account: Instance.Account) {
|
||||
instances.setDefaultAccount(account)
|
||||
accountsChanged.toggle()
|
||||
}
|
||||
|
||||
private func toggleDefault(_ account: Instance.Account) {
|
||||
if account == instances.defaultAccount {
|
||||
resetDefaultAccount()
|
||||
} else {
|
||||
makeDefault(account)
|
||||
}
|
||||
}
|
||||
|
||||
private func resetDefaultAccount() {
|
||||
instances.resetDefaultAccount()
|
||||
accountsChanged.toggle()
|
||||
}
|
||||
|
||||
private func removeAccount(_ account: Instance.Account) {
|
||||
instances.removeAccount(account)
|
||||
AccountsModel.remove(account)
|
||||
accountsChanged.toggle()
|
||||
}
|
||||
}
|
||||
|
@@ -1,38 +0,0 @@
|
||||
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."
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -146,7 +146,7 @@ struct InstanceFormView: View {
|
||||
return
|
||||
}
|
||||
|
||||
savedInstanceID = instancesModel.add(app: app, name: name, url: url).id
|
||||
savedInstanceID = InstancesModel.add(app: app, name: name, url: url).id
|
||||
|
||||
dismiss()
|
||||
}
|
||||
|
@@ -17,7 +17,7 @@ struct InstancesSettingsView: View {
|
||||
|
||||
var body: some View {
|
||||
Group {
|
||||
Section(header: Text("Instances"), footer: DefaultAccountHint()) {
|
||||
Section(header: Text("Instances")) {
|
||||
ForEach(instances) { instance in
|
||||
Group {
|
||||
NavigationLink(instance.longDescription) {
|
||||
@@ -55,16 +55,12 @@ struct InstancesSettingsView: View {
|
||||
|
||||
private func removeInstanceButton(_ instance: Instance) -> some View {
|
||||
Button("Remove", role: .destructive) {
|
||||
if accounts.account?.instance == instance {
|
||||
accounts.setAccount(nil)
|
||||
if accounts.current?.instance == instance {
|
||||
accounts.setCurrent(nil)
|
||||
}
|
||||
instancesModel.remove(instance)
|
||||
InstancesModel.remove(instance)
|
||||
}
|
||||
}
|
||||
|
||||
private func resetDefaultAccount() {
|
||||
instancesModel.resetDefaultAccount()
|
||||
}
|
||||
}
|
||||
|
||||
struct InstancesSettingsView_Previews: PreviewProvider {
|
||||
|
Reference in New Issue
Block a user