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
|
2019-09-14 22:15:13 +02:00
|
|
|
@State private var accountType: AccountType = nil
|
2019-09-08 03:50:57 +02:00
|
|
|
@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 {
|
2019-09-16 18:35:09 +02:00
|
|
|
Button(action: {
|
|
|
|
self.accountType = self.viewModel.account.type
|
|
|
|
}) {
|
|
|
|
Text("Credentials")
|
2019-06-13 21:30:56 +02:00
|
|
|
}
|
|
|
|
}
|
2019-09-14 22:15:13 +02:00
|
|
|
.sheet(item: $accountType) { type in
|
|
|
|
if type == .feedbin {
|
|
|
|
self.settingsFeedbinAccountView
|
|
|
|
}
|
|
|
|
if type == .freshRSS {
|
|
|
|
self.settingsReaderAPIAccountView
|
|
|
|
}
|
2019-09-08 03:50:57 +02:00
|
|
|
}
|
2019-06-13 21:30:56 +02:00
|
|
|
}
|
|
|
|
if viewModel.isDeletable {
|
|
|
|
Section {
|
2019-09-16 18:35:09 +02:00
|
|
|
Button(action: {
|
|
|
|
self.isDeleteAlertPresented.toggle()
|
|
|
|
}) {
|
|
|
|
Text("Delete Account").foregroundColor(.red)
|
2019-06-13 21:30:56 +02:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-09-16 18:35:09 +02:00
|
|
|
.buttonStyle(VibrantButtonStyle(alignment: .center))
|
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-14 22:15:13 +02:00
|
|
|
var settingsReaderAPIAccountView: SettingsReaderAPIAccountView {
|
|
|
|
let readerAPIModel = SettingsReaderAPIAccountView.ViewModel(account: viewModel.account)
|
|
|
|
return SettingsReaderAPIAccountView(viewModel: readerAPIModel)
|
|
|
|
}
|
|
|
|
|
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-14 20:38:12 +02:00
|
|
|
ActivityManager.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
|