// // SettingsDetailAccountView.swift // NetNewsWire // // Created by Maurice Parker on 6/13/19. // Copyright © 2019 Ranchero Software. All rights reserved. // import SwiftUI import Combine import Account import RSWeb struct SettingsDetailAccountView : View { @Environment(\.presentationMode) var presentation @ObservedObject var viewModel: ViewModel @State private var isFeedbinCredentialsPresented = false @State private var isDeleteAlertPresented = false var body: some View { Form { Section { HStack { TextField("Name", text: $viewModel.name) } Toggle(isOn: $viewModel.isActive) { Text("Active") } } if viewModel.isCreditialsAvailable { Section { HStack { Spacer() Button(action: { self.isFeedbinCredentialsPresented.toggle() }) { Text("Credentials") } Spacer() } } .sheet(isPresented: $isFeedbinCredentialsPresented) { self.settingsFeedbinAccountView } } if viewModel.isDeletable { Section { HStack { Spacer() Button(action: { self.isDeleteAlertPresented.toggle() }) { Text("Delete Account").foregroundColor(.red) } Spacer() } .alert(isPresented: $isDeleteAlertPresented) { Alert(title: Text("Are you sure you want to delete \"\(viewModel.nameForDisplay)\"?"), primaryButton: Alert.Button.default(Text("Delete"), action: { self.viewModel.delete() self.presentation.wrappedValue.dismiss() }), secondaryButton: Alert.Button.cancel()) } } } } .navigationBarTitle(Text(verbatim: viewModel.nameForDisplay), displayMode: .inline) } var settingsFeedbinAccountView: SettingsFeedbinAccountView { let feedbinViewModel = SettingsFeedbinAccountView.ViewModel(account: viewModel.account) return SettingsFeedbinAccountView(viewModel: feedbinViewModel, onDismiss: {}) } class ViewModel: ObservableObject { let objectWillChange = ObservableObjectPublisher() let account: Account init(_ account: Account) { self.account = account } var nameForDisplay: String { account.nameForDisplay } var name: String { get { account.name ?? "" } set { objectWillChange.send() account.name = newValue.isEmpty ? nil : newValue } } var isActive: Bool { get { account.isActive } set { objectWillChange.send() account.isActive = newValue } } var isCreditialsAvailable: Bool { return account.type != .onMyMac } var isDeletable: Bool { return AccountManager.shared.defaultAccount != account } func delete() { AccountManager.shared.deleteAccount(account) // ActivityManager.shared.cleanUp(account) } } } #if DEBUG struct SettingsDetailAccountView_Previews : PreviewProvider { static var previews: some View { let viewModel = SettingsDetailAccountView.ViewModel(AccountManager.shared.defaultAccount) return SettingsDetailAccountView(viewModel: viewModel) } } #endif