WiP on Edit Account

This commit is contained in:
Stuart Breckenridge
2020-07-14 10:44:59 +08:00
parent 15b980cabf
commit 39a71077b4
4 changed files with 122 additions and 6 deletions

View File

@@ -12,10 +12,32 @@ import Combine
class AccountsPreferencesModel: ObservableObject {
// Configured Accounts
@Published var sortedAccounts: [Account] = []
@Published var selectedConfiguredAccountID: String? = nil
@Published var selectedConfiguredAccountID: String? = AccountManager.shared.defaultAccount.accountID {
didSet {
if let accountID = selectedConfiguredAccountID {
account = sortedAccounts.first(where: { $0.accountID == accountID })
accountIsActive = account?.isActive ?? false
accountName = account?.name ?? ""
}
}
}
@Published var showAddAccountView: Bool = false
// Edit Account
public private(set) var account: Account?
@Published var accountIsActive: Bool = false {
didSet {
account?.isActive = accountIsActive
}
}
@Published var accountName: String = "" {
didSet {
account?.name = accountName
}
}
var selectedAccountIsDefault: Bool {
guard let selected = selectedConfiguredAccountID else {
return true
@@ -39,6 +61,18 @@ class AccountsPreferencesModel: ObservableObject {
NotificationCenter.default.publisher(for: .UserDidDeleteAccount).sink(receiveValue: { _ in
self.selectedConfiguredAccountID = nil
self.sortedAccounts = AccountManager.shared.sortedAccounts
self.selectedConfiguredAccountID = AccountManager.shared.defaultAccount.accountID
}).store(in: &notificationSubscriptions)
NotificationCenter.default.publisher(for: .AccountStateDidChange).sink(receiveValue: { notification in
guard let account = notification.object as? Account else {
return
}
if account.accountID == self.account?.accountID {
self.account = account
self.accountIsActive = account.isActive
self.accountName = account.name ?? ""
}
}).store(in: &notificationSubscriptions)
}

View File

@@ -27,12 +27,11 @@ struct AccountsPreferencesView: View {
bottomButtonStack
}, alignment: .bottom)
}
.frame(width: 225, height: 300, alignment: .leading)
.frame(width: 160, height: 300, alignment: .leading)
.border(Color.gray, width: 1)
VStack(alignment: .leading) {
EmptyView()
Spacer()
}.frame(width: 225, height: 300, alignment: .leading)
EditAccountView(viewModel: viewModel)
.frame(height: 300, alignment: .leading)
}
Spacer()
}.sheet(isPresented: $viewModel.showAddAccountView,

View File

@@ -0,0 +1,79 @@
//
// EditAccountView.swift
// Multiplatform macOS
//
// Created by Stuart Breckenridge on 14/7/20.
// Copyright © 2020 Ranchero Software. All rights reserved.
//
import SwiftUI
import Account
import Combine
struct EditAccountView: View {
@ObservedObject var viewModel: AccountsPreferencesModel
var body: some View {
ZStack {
RoundedRectangle(cornerRadius: 8, style: .circular)
.foregroundColor(Color.secondary.opacity(0.1))
VStack {
HStack {
Spacer()
Button("Account Information", action: {})
Spacer()
}.padding(4)
if viewModel.account != nil {
Form(content: {
HStack(alignment: .top) {
Text("Type: ")
.frame(width: 50)
VStack(alignment: .leading) {
Text(viewModel.account!.defaultName)
Toggle("Active", isOn: $viewModel.accountIsActive)
}
}
HStack(alignment: .top) {
Text("Name: ")
.frame(width: 50)
VStack(alignment: .leading) {
TextField(viewModel.account!.name ?? "", text: $viewModel.accountName)
.textFieldStyle(RoundedBorderTextFieldStyle())
Text("The name appears in the sidebar. It can be anything you want. You can even use emoji. 🎸")
.foregroundColor(.secondary)
}
}
Spacer()
if viewModel.account?.type != .onMyMac {
HStack {
Spacer()
Button("Credentials", action: {
})
Spacer()
}
}
}).padding()
}
Spacer()
}
}
}
}
struct EditAccountView_Previews: PreviewProvider {
static var previews: some View {
EditAccountView(viewModel: AccountsPreferencesModel())
}
}