2019-06-17 00:22:00 +02:00
|
|
|
//
|
2019-06-19 18:25:37 +02:00
|
|
|
// SettingsReaderAPIAccountView.swift
|
2019-06-17 00:22:00 +02:00
|
|
|
// NetNewsWire-iOS
|
|
|
|
//
|
2019-06-19 18:25:37 +02:00
|
|
|
// 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
|
|
|
|
|
2019-06-19 18:25:37 +02:00
|
|
|
struct SettingsReaderAPIAccountView : View {
|
2019-09-14 22:15:13 +02:00
|
|
|
@Environment(\.presentationMode) var presentation
|
|
|
|
@ObservedObject var viewModel: ViewModel
|
|
|
|
|
2019-06-17 00:22:00 +02:00
|
|
|
@State var busy: Bool = false
|
2019-09-14 22:15:13 +02:00
|
|
|
@State var error: String = ""
|
2019-06-17 00:22:00 +02:00
|
|
|
|
|
|
|
var body: some View {
|
2019-09-14 22:15:13 +02:00
|
|
|
Form {
|
|
|
|
Section(header:
|
|
|
|
HStack {
|
|
|
|
Spacer()
|
|
|
|
SettingsAccountLabelView(accountImage: "accountFreshRSS", accountLabel: "FreshRSS")
|
|
|
|
.padding()
|
|
|
|
.layoutPriority(1.0)
|
|
|
|
Spacer()
|
2019-06-17 00:22:00 +02:00
|
|
|
}
|
2019-09-14 22:15:13 +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
|
|
|
}
|
|
|
|
}
|
2019-09-14 22:15:13 +02:00
|
|
|
.disabled(!viewModel.isValid)
|
|
|
|
Spacer()
|
2019-06-17 00:22:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-09-14 22:15:13 +02:00
|
|
|
// .disabled(busy)
|
2019-06-17 00:22:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private func addAccount() {
|
|
|
|
|
|
|
|
busy = true
|
2019-09-14 22:15:13 +02:00
|
|
|
error = ""
|
2019-06-17 00:22:00 +02:00
|
|
|
|
|
|
|
let emailAddress = viewModel.email.trimmingCharacters(in: .whitespaces)
|
2019-06-19 22:40:03 +02:00
|
|
|
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 {
|
2019-09-14 22:15:13 +02:00
|
|
|
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 {
|
2019-06-20 18:27:35 +02:00
|
|
|
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 {
|
2019-09-14 22:15:13 +02:00
|
|
|
self.error = "Keychain error while storing credentials."
|
2019-06-17 00:22:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2019-09-14 22:15:13 +02:00
|
|
|
self.error = "Invalid email/password combination."
|
2019-06-17 00:22:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
case .failure:
|
2019-09-14 22:15:13 +02:00
|
|
|
self.error = "Network error. Try again later."
|
2019-06-17 00:22:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private func dismiss() {
|
2019-09-14 22:15:13 +02:00
|
|
|
presentation.wrappedValue.dismiss()
|
2019-06-17 00:22:00 +02:00
|
|
|
}
|
|
|
|
|
2019-09-14 22:15:13 +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
|
|
|
}
|
|
|
|
|
2019-09-14 22:15:13 +02:00
|
|
|
init(account: Account) {
|
2019-06-17 00:22:00 +02:00
|
|
|
self.account = account
|
2019-09-14 22:15:13 +02:00
|
|
|
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
|
2019-09-14 22:15:13 +02:00
|
|
|
self.apiURL = account.endpointURL?.absoluteString ?? ""
|
2019-06-17 00:22:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var email: String = "" {
|
2019-09-14 22:15:13 +02:00
|
|
|
willSet {
|
|
|
|
objectWillChange.send()
|
2019-06-17 00:22:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
var password: String = "" {
|
2019-09-14 22:15:13 +02:00
|
|
|
willSet {
|
|
|
|
objectWillChange.send()
|
2019-06-17 00:22:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
var apiURL: String = "" {
|
2019-09-14 22:15:13 +02:00
|
|
|
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
|
2019-06-19 18:25:37 +02:00
|
|
|
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
|