NetNewsWire/iOS/Settings/Account/SettingsReaderAPIAccountVie...

180 lines
3.9 KiB
Swift
Raw Normal View History

2019-06-17 00:22:00 +02:00
//
// SettingsReaderAPIAccountView.swift
2019-06-17 00:22:00 +02:00
// NetNewsWire-iOS
//
// Created by Jeremy Beker on 5/28/2019.
2019-06-17 00:22:00 +02:00
// Copyright © 2019 Ranchero Software. All rights reserved.
//
import SwiftUI
import Combine
import Account
import RSWeb
struct SettingsReaderAPIAccountView : View {
@Environment(\.presentationMode) var presentation
@ObservedObject var viewModel: ViewModel
2019-06-17 00:22:00 +02:00
@State var busy: Bool = false
@State var error: String = ""
2019-06-17 00:22:00 +02:00
var body: some View {
Form {
Section(header:
HStack {
Spacer()
SettingsAccountLabelView(accountImage: "accountFreshRSS", accountLabel: "FreshRSS")
.padding()
.layoutPriority(1.0)
Spacer()
2019-06-17 00:22:00 +02:00
}
) {
TextField("Email", text: $viewModel.email).textContentType(.username)
SecureField("Password", text: $viewModel.password)
TextField("API URL:", text: $viewModel.apiURL).textContentType(.URL)
}
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-17 00:22:00 +02:00
}
}
.disabled(!viewModel.isValid)
Spacer()
2019-06-17 00:22:00 +02:00
}
}
}
// .disabled(busy)
2019-06-17 00:22:00 +02:00
}
private func addAccount() {
busy = true
error = ""
2019-06-17 00:22:00 +02:00
let emailAddress = viewModel.email.trimmingCharacters(in: .whitespaces)
let credentials = Credentials.readerAPIBasicLogin(username: emailAddress, password: viewModel.password)
2019-06-17 00:22:00 +02:00
guard let apiURL = URL(string: viewModel.apiURL) else {
self.error = "Invalid API URL."
2019-06-17 00:22:00 +02:00
return
}
2019-06-20 14:33:17 +02:00
Account.validateCredentials(type: viewModel.accountType, credentials: credentials, endpoint: apiURL) { result in
2019-06-17 00:22:00 +02:00
self.busy = false
switch result {
case .success(let authenticated):
if (authenticated != nil) {
var newAccount = false
let workAccount: Account
if self.viewModel.account == nil {
2019-06-20 14:33:17 +02:00
workAccount = AccountManager.shared.createAccount(type: self.viewModel.accountType)
2019-06-17 00:22:00 +02:00
newAccount = true
} else {
workAccount = self.viewModel.account!
}
do {
do {
try workAccount.removeCredentials()
2019-06-17 00:22:00 +02:00
} catch {}
workAccount.endpointURL = apiURL
try workAccount.storeCredentials(credentials)
if newAccount {
workAccount.refreshAll() { result in }
}
self.dismiss()
} catch {
self.error = "Keychain error while storing credentials."
2019-06-17 00:22:00 +02:00
}
} else {
self.error = "Invalid email/password combination."
2019-06-17 00:22:00 +02:00
}
case .failure:
self.error = "Network error. Try again later."
2019-06-17 00:22:00 +02:00
}
}
}
private func dismiss() {
presentation.wrappedValue.dismiss()
2019-06-17 00:22:00 +02:00
}
class ViewModel: ObservableObject {
let objectWillChange = ObservableObjectPublisher()
2019-06-20 14:33:17 +02:00
var accountType: AccountType
2019-06-17 00:22:00 +02:00
var account: Account? = nil
2019-06-20 14:33:17 +02:00
init(accountType: AccountType) {
self.accountType = accountType
2019-06-17 00:22:00 +02:00
}
init(account: Account) {
2019-06-17 00:22:00 +02:00
self.account = account
self.accountType = account.type
if case .readerAPIBasicLogin(let username, let password) = try? account.retrieveCredentials() {
2019-06-17 00:22:00 +02:00
self.email = username
self.password = password
self.apiURL = account.endpointURL?.absoluteString ?? ""
2019-06-17 00:22:00 +02:00
}
}
var email: String = "" {
willSet {
objectWillChange.send()
2019-06-17 00:22:00 +02:00
}
}
var password: String = "" {
willSet {
objectWillChange.send()
2019-06-17 00:22:00 +02:00
}
}
var apiURL: String = "" {
willSet {
objectWillChange.send()
2019-06-17 00:22:00 +02:00
}
}
var isUpdate: Bool {
return account != nil
}
var isValid: Bool {
return !email.isEmpty && !password.isEmpty
}
}
}
#if DEBUG
struct SettingsReaderAPIAccountView_Previews : PreviewProvider {
2019-06-17 00:22:00 +02:00
static var previews: some View {
2019-06-20 14:33:17 +02:00
SettingsReaderAPIAccountView(viewModel: SettingsReaderAPIAccountView.ViewModel(accountType: .freshRSS))
2019-06-17 00:22:00 +02:00
}
}
#endif