NetNewsWire/iOS/Settings/Account/SettingsDetailAccountView.s...

137 lines
3.2 KiB
Swift
Raw Normal View History

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
import RSWeb
2019-06-13 21:30:56 +02:00
struct SettingsDetailAccountView : View {
@Environment(\.presentationMode) var presentation
@ObservedObject var viewModel: ViewModel
@State private var accountType: AccountType = nil
@State private var isDeleteAlertPresented = false
2019-06-13 21:30:56 +02:00
var body: some View {
Form {
2019-06-13 21:30:56 +02:00
Section {
HStack {
TextField("Name", text: $viewModel.name)
2019-06-13 21:30:56 +02:00
}
Toggle(isOn: $viewModel.isActive) {
Text("Active")
}
}
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
}
}
.sheet(item: $accountType) { type in
if type == .feedbin {
self.settingsFeedbinAccountView
}
if type == .freshRSS {
self.settingsReaderAPIAccountView
}
}
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
}
.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())
}
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)
}
var settingsFeedbinAccountView: SettingsFeedbinAccountView {
let feedbinViewModel = SettingsFeedbinAccountView.ViewModel(account: viewModel.account)
return SettingsFeedbinAccountView(viewModel: feedbinViewModel)
}
var settingsReaderAPIAccountView: SettingsReaderAPIAccountView {
let readerAPIModel = SettingsReaderAPIAccountView.ViewModel(account: viewModel.account)
return SettingsReaderAPIAccountView(viewModel: readerAPIModel)
}
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 {
objectWillChange.send()
2019-06-13 21:30:56 +02:00
account.name = newValue.isEmpty ? nil : newValue
}
}
var isActive: Bool {
get {
account.isActive
}
set {
objectWillChange.send()
2019-06-13 21:30:56 +02:00
account.isActive = newValue
}
}
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)
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