2019-06-13 21:30:56 +02:00
|
|
|
//
|
|
|
|
// SettingsDetailAccountView.swift
|
|
|
|
// NetNewsWire
|
|
|
|
//
|
|
|
|
// Created by Maurice Parker on 6/13/19.
|
|
|
|
// Copyright © 2019 Ranchero Software. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import SwiftUI
|
|
|
|
import Combine
|
|
|
|
import Account
|
2019-06-15 23:03:41 +02:00
|
|
|
import RSWeb
|
2019-06-13 21:30:56 +02:00
|
|
|
|
|
|
|
struct SettingsDetailAccountView : View {
|
|
|
|
@ObjectBinding var viewModel: ViewModel
|
|
|
|
@State private var verifyDelete = false
|
2019-06-15 23:03:41 +02:00
|
|
|
@State private var showFeedbinCredentials = false
|
2019-06-13 21:30:56 +02:00
|
|
|
|
|
|
|
var body: some View {
|
2019-06-18 23:54:51 +02:00
|
|
|
Form {
|
2019-06-13 21:30:56 +02:00
|
|
|
Section {
|
|
|
|
HStack {
|
|
|
|
Text("Name")
|
|
|
|
Divider()
|
|
|
|
TextField($viewModel.name, placeholder: Text("(Optional)"))
|
|
|
|
}
|
|
|
|
Toggle(isOn: $viewModel.isActive) {
|
|
|
|
Text("Active")
|
|
|
|
}
|
|
|
|
}
|
2019-06-15 23:03:41 +02:00
|
|
|
if viewModel.isCreditialsAvailable {
|
|
|
|
Section {
|
|
|
|
HStack {
|
|
|
|
Spacer()
|
|
|
|
Button(action: {
|
|
|
|
self.showFeedbinCredentials = true
|
|
|
|
}) {
|
|
|
|
Text("Credentials")
|
|
|
|
}
|
|
|
|
.presentation(showFeedbinCredentials ? feedbinCredentialsModal : nil)
|
|
|
|
.onDisappear() { self.showFeedbinCredentials = false }
|
|
|
|
Spacer()
|
2019-06-13 21:30:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if viewModel.isDeletable {
|
|
|
|
Section {
|
|
|
|
HStack {
|
|
|
|
Spacer()
|
|
|
|
Button(action: {
|
|
|
|
self.verifyDelete = true
|
|
|
|
}) {
|
|
|
|
Text("Delete Account")
|
|
|
|
.foregroundColor(.red)
|
|
|
|
}
|
|
|
|
.presentation($verifyDelete) {
|
|
|
|
Alert(title: Text("Are you sure you want to delete \"\(viewModel.nameForDisplay)\"?"),
|
|
|
|
primaryButton: Alert.Button.default(Text("Delete"), onTrigger: { self.viewModel.delete() }),
|
|
|
|
secondaryButton: Alert.Button.cancel())
|
|
|
|
}
|
|
|
|
Spacer()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.navigationBarTitle(Text(verbatim: viewModel.nameForDisplay), displayMode: .inline)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-06-15 23:03:41 +02:00
|
|
|
var feedbinCredentialsModal: Modal {
|
|
|
|
let feedbinViewModel = SettingsFeedbinAccountView.ViewModel(account: viewModel.account)
|
|
|
|
return Modal(SettingsFeedbinAccountView(viewModel: feedbinViewModel))
|
|
|
|
}
|
|
|
|
|
2019-06-13 21:30:56 +02:00
|
|
|
class ViewModel: BindableObject {
|
|
|
|
let didChange = PassthroughSubject<ViewModel, Never>()
|
|
|
|
let account: Account
|
|
|
|
|
|
|
|
init(_ account: Account) {
|
|
|
|
self.account = account
|
|
|
|
}
|
|
|
|
|
|
|
|
var nameForDisplay: String {
|
|
|
|
account.nameForDisplay
|
|
|
|
}
|
|
|
|
|
|
|
|
var name: String {
|
|
|
|
get {
|
|
|
|
account.name ?? ""
|
|
|
|
}
|
|
|
|
set {
|
|
|
|
account.name = newValue.isEmpty ? nil : newValue
|
|
|
|
didChange.send(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var isActive: Bool {
|
|
|
|
get {
|
|
|
|
account.isActive
|
|
|
|
}
|
|
|
|
set {
|
|
|
|
account.isActive = newValue
|
|
|
|
didChange.send(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-15 23:03:41 +02:00
|
|
|
var isCreditialsAvailable: Bool {
|
|
|
|
return account.type != .onMyMac
|
|
|
|
}
|
|
|
|
|
2019-06-13 21:30:56 +02:00
|
|
|
var isDeletable: Bool {
|
|
|
|
return AccountManager.shared.defaultAccount != account
|
|
|
|
}
|
|
|
|
|
|
|
|
func delete() {
|
|
|
|
AccountManager.shared.deleteAccount(account)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if DEBUG
|
|
|
|
struct SettingsDetailAccountView_Previews : PreviewProvider {
|
|
|
|
static var previews: some View {
|
|
|
|
let viewModel = SettingsDetailAccountView.ViewModel(AccountManager.shared.defaultAccount)
|
|
|
|
return SettingsDetailAccountView(viewModel: viewModel)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|