NetNewsWire/iOS/Settings/Account/SettingsFeedbinAccountView....

163 lines
3.3 KiB
Swift
Raw Normal View History

2019-06-11 23:59:16 +02:00
//
// SettingsFeedbinAccountView.swift
// NetNewsWire-iOS
//
// Created by Maurice Parker on 6/11/19.
// Copyright © 2019 Ranchero Software. All rights reserved.
//
import SwiftUI
2019-06-12 15:33:14 +02:00
import Combine
import Account
import RSWeb
2019-06-11 23:59:16 +02:00
struct SettingsFeedbinAccountView : View {
@Environment(\.presentationMode) var presentation
@ObservedObject var viewModel: ViewModel
2019-06-12 15:33:14 +02:00
@State var busy: Bool = false
@State var error: String = ""
2019-06-11 23:59:16 +02:00
var body: some View {
Form {
Section(header:
HStack {
Spacer()
2019-09-12 17:19:06 +02:00
SettingsAccountLabelView(accountImage: "accountFeedbin", accountLabel: "Feedbin")
.padding()
.layoutPriority(1.0)
Spacer()
2019-06-11 23:59:16 +02:00
}
) {
TextField("Email", text: $viewModel.email).textContentType(.emailAddress)
SecureField("Password", text: $viewModel.password)
}
Section(footer:
HStack {
Spacer()
Text(verbatim: error).foregroundColor(.red)
Spacer()
}
) {
HStack {
Spacer()
Button(action: { self.addAccount() }) {
if viewModel.isUpdate {
Text("Update Account")
} else {
Text("Add Account")
2019-06-11 23:59:16 +02:00
}
}
.disabled(!viewModel.isValid)
Spacer()
2019-06-11 23:59:16 +02:00
}
}
2019-06-12 15:33:14 +02:00
}
// .disabled(busy) // Maybe someday we can do this, but right now it crashes on the iPad
.navigationBarTitle(Text(""), displayMode: .inline)
2019-06-12 15:33:14 +02:00
}
private func addAccount() {
busy = true
error = ""
2019-06-12 15:33:14 +02:00
let emailAddress = viewModel.email.trimmingCharacters(in: .whitespaces)
let credentials = Credentials(type: .basic, username: emailAddress, secret: viewModel.password)
2019-06-12 15:33:14 +02:00
Account.validateCredentials(type: .feedbin, credentials: credentials) { result in
2019-06-12 15:33:14 +02:00
self.busy = false
2019-06-12 15:33:14 +02:00
switch result {
case .success(let authenticated):
2019-06-17 00:22:00 +02:00
if (authenticated != nil) {
2019-06-12 15:33:14 +02:00
var newAccount = false
let workAccount: Account
if self.viewModel.account == nil {
2019-06-12 15:33:14 +02:00
workAccount = AccountManager.shared.createAccount(type: .feedbin)
newAccount = true
} else {
workAccount = self.viewModel.account!
2019-06-12 15:33:14 +02:00
}
2019-06-12 15:33:14 +02:00
do {
2019-06-12 15:33:14 +02:00
do {
try workAccount.removeCredentials(type: .basic)
2019-06-12 15:33:14 +02:00
} catch {}
try workAccount.storeCredentials(credentials)
2019-06-12 15:33:14 +02:00
if newAccount {
workAccount.refreshAll() { result in }
}
2019-06-12 15:33:14 +02:00
self.dismiss()
2019-06-12 15:33:14 +02:00
} catch {
self.error = "Keychain error while storing credentials."
2019-06-12 15:33:14 +02:00
}
2019-06-12 15:33:14 +02:00
} else {
self.error = "Invalid email/password combination."
2019-06-12 15:33:14 +02:00
}
2019-06-12 15:33:14 +02:00
case .failure:
self.error = "Network error. Try again later."
2019-06-12 15:33:14 +02:00
}
2019-06-12 15:33:14 +02:00
}
}
private func dismiss() {
presentation.wrappedValue.dismiss()
2019-06-12 15:33:14 +02:00
}
class ViewModel: ObservableObject {
let objectWillChange = ObservableObjectPublisher()
var account: Account? = nil
init() {
}
init(account: Account) {
self.account = account
if let credentials = try? account.retrieveCredentials(type: .basic) {
self.email = credentials.username
}
}
2019-06-12 15:33:14 +02:00
var email: String = "" {
willSet {
objectWillChange.send()
2019-06-12 15:33:14 +02:00
}
}
2019-06-12 15:33:14 +02:00
var password: String = "" {
willSet {
objectWillChange.send()
2019-06-12 15:33:14 +02:00
}
}
var isUpdate: Bool {
return account != nil
}
2019-06-12 15:33:14 +02:00
var isValid: Bool {
return !email.isEmpty && !password.isEmpty
2019-06-11 23:59:16 +02:00
}
}
}
#if DEBUG
struct SettingsFeedbinAccountView_Previews : PreviewProvider {
static var previews: some View {
SettingsFeedbinAccountView(viewModel: SettingsFeedbinAccountView.ViewModel())
2019-06-11 23:59:16 +02:00
}
}
#endif