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-06-17 00:22:00 +02:00
|
|
|
@Environment(\.isPresented) private var isPresented
|
|
|
|
@ObjectBinding var viewModel: ViewModel
|
|
|
|
@State var busy: Bool = false
|
|
|
|
@State var error: Text = Text("")
|
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
NavigationView {
|
|
|
|
List {
|
|
|
|
Section(header:
|
2019-06-20 14:33:17 +02:00
|
|
|
SettingsAccountLabelView(accountImage: "accountFreshRSS", accountLabel: "FreshRSS").padding()
|
2019-06-17 00:22:00 +02:00
|
|
|
) {
|
|
|
|
HStack {
|
|
|
|
Text("Email:")
|
|
|
|
Divider()
|
|
|
|
TextField($viewModel.email)
|
|
|
|
.textContentType(.username)
|
|
|
|
}
|
|
|
|
HStack {
|
|
|
|
Text("Password:")
|
|
|
|
Divider()
|
|
|
|
SecureField($viewModel.password)
|
|
|
|
}
|
|
|
|
HStack {
|
|
|
|
Text("API URL:")
|
|
|
|
Divider()
|
|
|
|
TextField($viewModel.apiURL)
|
|
|
|
.textContentType(.URL)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Section(footer:
|
|
|
|
HStack {
|
|
|
|
Spacer()
|
|
|
|
error.color(.red)
|
|
|
|
Spacer()
|
|
|
|
}
|
|
|
|
) {
|
|
|
|
HStack {
|
|
|
|
Spacer()
|
|
|
|
Button(action: { self.addAccount() }) {
|
|
|
|
if viewModel.isUpdate {
|
|
|
|
Text("Update Account")
|
|
|
|
} else {
|
|
|
|
Text("Add Account")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.disabled(!viewModel.isValid)
|
|
|
|
Spacer()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.disabled(busy)
|
|
|
|
.listStyle(.grouped)
|
|
|
|
.navigationBarTitle(Text(""), displayMode: .inline)
|
|
|
|
.navigationBarItems(leading:
|
|
|
|
Button(action: { self.dismiss() }) { Text("Cancel") }
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func addAccount() {
|
|
|
|
|
|
|
|
busy = true
|
|
|
|
error = Text("")
|
|
|
|
|
|
|
|
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 {
|
|
|
|
self.error = Text("Invalide API URL.")
|
|
|
|
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 {
|
|
|
|
self.error = Text("Keychain error while storing credentials.")
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
self.error = Text("Invalid email/password combination.")
|
|
|
|
}
|
|
|
|
|
|
|
|
case .failure:
|
|
|
|
self.error = Text("Network error. Try again later.")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private func dismiss() {
|
|
|
|
isPresented?.value = false
|
|
|
|
}
|
|
|
|
|
|
|
|
class ViewModel: BindableObject {
|
|
|
|
let didChange = PassthroughSubject<ViewModel, Never>()
|
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-06-20 14:33:17 +02:00
|
|
|
init(accountType: AccountType, account: Account) {
|
2019-06-17 00:22:00 +02:00
|
|
|
self.account = account
|
2019-06-20 14:33:17 +02:00
|
|
|
self.accountType = accountType
|
2019-06-20 18:27:35 +02:00
|
|
|
if case .basic(let username, let password) = try? account.retrieveCredentials() {
|
2019-06-17 00:22:00 +02:00
|
|
|
self.email = username
|
|
|
|
self.password = password
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var email: String = "" {
|
|
|
|
didSet {
|
|
|
|
didChange.send(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var password: String = "" {
|
|
|
|
didSet {
|
|
|
|
didChange.send(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var apiURL: String = "" {
|
|
|
|
didSet {
|
|
|
|
didChange.send(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|