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

180 lines
4.0 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(type: .readerBasic, username: emailAddress, secret: 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-09-14 22:41:35 +02:00
2019-06-17 00:22:00 +02:00
self.busy = false
switch result {
2019-09-14 22:41:35 +02:00
case .success(let validatedCredentials):
2019-06-17 00:22:00 +02:00
2019-09-14 22:41:35 +02:00
guard let validatedCredentials = validatedCredentials else {
self.error = "Invalid email/password combination."
return
}
var newAccount = false
let workAccount: Account
if self.viewModel.account == nil {
workAccount = AccountManager.shared.createAccount(type: self.viewModel.accountType)
newAccount = true
} else {
workAccount = self.viewModel.account!
}
do {
2019-06-17 00:22:00 +02:00
do {
try workAccount.removeCredentials(type: .readerBasic)
try workAccount.removeCredentials(type: .readerAPIKey)
2019-09-14 22:41:35 +02:00
} catch {}
workAccount.endpointURL = apiURL
try workAccount.storeCredentials(credentials)
2019-09-14 22:41:35 +02:00
try workAccount.storeCredentials(validatedCredentials)
if newAccount {
workAccount.refreshAll() { result in }
2019-06-17 00:22:00 +02:00
}
2019-09-14 22:41:35 +02:00
self.dismiss()
} catch {
self.error = "Keychain error while storing credentials."
2019-06-17 00:22:00 +02:00
}
2019-09-14 22:41:35 +02:00
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 let credentials = try? account.retrieveCredentials(type: .readerBasic) {
self.email = credentials.username
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