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 {
|
2019-09-08 17:04:36 +02:00
|
|
|
@Environment(\.presentationMode) var presentation
|
2019-09-08 03:50:57 +02:00
|
|
|
@ObservedObject var viewModel: ViewModel
|
|
|
|
@State private var isFeedbinCredentialsPresented = false
|
|
|
|
@State private var isDeleteAlertPresented = 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 {
|
2019-09-08 03:50:57 +02:00
|
|
|
TextField("Name", text: $viewModel.name)
|
2019-06-13 21:30:56 +02:00
|
|
|
}
|
|
|
|
Toggle(isOn: $viewModel.isActive) {
|
|
|
|
Text("Active")
|
|
|
|
}
|
|
|
|
}
|
2019-06-15 23:03:41 +02:00
|
|
|
if viewModel.isCreditialsAvailable {
|
|
|
|
Section {
|
|
|
|
HStack {
|
|
|
|
Spacer()
|
|
|
|
Button(action: {
|
2019-09-08 03:50:57 +02:00
|
|
|
self.isFeedbinCredentialsPresented.toggle()
|
2019-06-15 23:03:41 +02:00
|
|
|
}) {
|
|
|
|
Text("Credentials")
|
|
|
|
}
|
|
|
|
Spacer()
|
2019-06-13 21:30:56 +02:00
|
|
|
}
|
|
|
|
}
|
2019-09-08 03:50:57 +02:00
|
|
|
.sheet(isPresented: $isFeedbinCredentialsPresented) {
|
|
|
|
self.settingsFeedbinAccountView
|
|
|
|
}
|
2019-06-13 21:30:56 +02:00
|
|
|
}
|
|
|
|
if viewModel.isDeletable {
|
|
|
|
Section {
|
|
|
|
HStack {
|
|
|
|
Spacer()
|
|
|
|
Button(action: {
|
2019-09-08 03:50:57 +02:00
|
|
|
self.isDeleteAlertPresented.toggle()
|
2019-06-13 21:30:56 +02:00
|
|
|
}) {
|
2019-09-08 03:50:57 +02:00
|
|
|
Text("Delete Account").foregroundColor(.red)
|
2019-06-13 21:30:56 +02:00
|
|
|
}
|
|
|
|
Spacer()
|
|
|
|
}
|
2019-09-08 03:50:57 +02:00
|
|
|
.alert(isPresented: $isDeleteAlertPresented) {
|
|
|
|
Alert(title: Text("Are you sure you want to delete \"\(viewModel.nameForDisplay)\"?"),
|
2019-09-08 17:04:36 +02:00
|
|
|
primaryButton: Alert.Button.default(Text("Delete"), action: {
|
|
|
|
self.viewModel.delete()
|
|
|
|
self.presentation.wrappedValue.dismiss()
|
|
|
|
}),
|
|
|
|
secondaryButton: Alert.Button.cancel())
|
2019-09-08 03:50:57 +02:00
|
|
|
}
|
2019-06-13 21:30:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.navigationBarTitle(Text(verbatim: viewModel.nameForDisplay), displayMode: .inline)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-09-08 03:50:57 +02:00
|
|
|
var settingsFeedbinAccountView: SettingsFeedbinAccountView {
|
2019-06-15 23:03:41 +02:00
|
|
|
let feedbinViewModel = SettingsFeedbinAccountView.ViewModel(account: viewModel.account)
|
2019-09-12 03:50:03 +02:00
|
|
|
return SettingsFeedbinAccountView(viewModel: feedbinViewModel)
|
2019-06-15 23:03:41 +02:00
|
|
|
}
|
|
|
|
|
2019-09-08 03:50:57 +02:00
|
|
|
class ViewModel: ObservableObject {
|
|
|
|
|
|
|
|
let objectWillChange = ObservableObjectPublisher()
|
|
|
|
|
2019-06-13 21:30:56 +02:00
|
|
|
let account: Account
|
|
|
|
|
|
|
|
init(_ account: Account) {
|
|
|
|
self.account = account
|
|
|
|
}
|
|
|
|
|
|
|
|
var nameForDisplay: String {
|
|
|
|
account.nameForDisplay
|
|
|
|
}
|
|
|
|
|
|
|
|
var name: String {
|
|
|
|
get {
|
|
|
|
account.name ?? ""
|
|
|
|
}
|
|
|
|
set {
|
2019-09-08 03:50:57 +02:00
|
|
|
objectWillChange.send()
|
2019-06-13 21:30:56 +02:00
|
|
|
account.name = newValue.isEmpty ? nil : newValue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var isActive: Bool {
|
|
|
|
get {
|
|
|
|
account.isActive
|
|
|
|
}
|
|
|
|
set {
|
2019-09-08 03:50:57 +02:00
|
|
|
objectWillChange.send()
|
2019-06-13 21:30:56 +02:00
|
|
|
account.isActive = newValue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
2019-09-08 03:50:57 +02:00
|
|
|
// ActivityManager.shared.cleanUp(account)
|
2019-06-13 21:30:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if DEBUG
|
|
|
|
struct SettingsDetailAccountView_Previews : PreviewProvider {
|
|
|
|
static var previews: some View {
|
|
|
|
let viewModel = SettingsDetailAccountView.ViewModel(AccountManager.shared.defaultAccount)
|
|
|
|
return SettingsDetailAccountView(viewModel: viewModel)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|