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 {
|
2019-09-08 03:50:57 +02:00
|
|
|
@Environment(\.presentationMode) var presentation
|
|
|
|
@ObservedObject var viewModel: ViewModel
|
2019-06-12 15:33:14 +02:00
|
|
|
@State var busy: Bool = false
|
2019-09-08 03:50:57 +02:00
|
|
|
@State var error: String = ""
|
2019-06-15 23:03:41 +02:00
|
|
|
|
2019-06-11 23:59:16 +02:00
|
|
|
var body: some View {
|
|
|
|
NavigationView {
|
2019-06-18 23:54:51 +02:00
|
|
|
Form {
|
2019-06-11 23:59:16 +02:00
|
|
|
Section(header:
|
2019-09-08 11:09:05 +02:00
|
|
|
HStack {
|
|
|
|
Spacer()
|
|
|
|
SettingsAccountLabelView(accountImage: "accountFeedbin", accountLabel: "Feedbin").padding()
|
|
|
|
Spacer()
|
|
|
|
}
|
2019-06-11 23:59:16 +02:00
|
|
|
) {
|
2019-09-08 03:50:57 +02:00
|
|
|
TextField("Email", text: $viewModel.email).textContentType(.emailAddress)
|
|
|
|
SecureField("Password", text: $viewModel.password)
|
2019-06-11 23:59:16 +02:00
|
|
|
}
|
2019-06-12 15:33:14 +02:00
|
|
|
Section(footer:
|
2019-06-11 23:59:16 +02:00
|
|
|
HStack {
|
|
|
|
Spacer()
|
2019-09-08 03:50:57 +02:00
|
|
|
Text(verbatim: error).foregroundColor(.red)
|
2019-06-12 15:33:14 +02:00
|
|
|
Spacer()
|
|
|
|
}
|
|
|
|
) {
|
|
|
|
HStack {
|
|
|
|
Spacer()
|
|
|
|
Button(action: { self.addAccount() }) {
|
2019-06-15 23:03:41 +02:00
|
|
|
if viewModel.isUpdate {
|
|
|
|
Text("Update Account")
|
|
|
|
} else {
|
|
|
|
Text("Add Account")
|
|
|
|
}
|
2019-06-11 23:59:16 +02:00
|
|
|
}
|
2019-06-12 15:33:14 +02:00
|
|
|
.disabled(!viewModel.isValid)
|
2019-06-11 23:59:16 +02:00
|
|
|
Spacer()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-12 15:33:14 +02:00
|
|
|
.disabled(busy)
|
2019-06-11 23:59:16 +02:00
|
|
|
.navigationBarTitle(Text(""), displayMode: .inline)
|
2019-06-12 15:33:14 +02:00
|
|
|
.navigationBarItems(leading:
|
|
|
|
Button(action: { self.dismiss() }) { Text("Cancel") }
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func addAccount() {
|
|
|
|
|
|
|
|
busy = true
|
2019-09-08 03:50:57 +02:00
|
|
|
error = ""
|
2019-06-15 23:03:41 +02:00
|
|
|
|
2019-06-12 15:33:14 +02:00
|
|
|
let emailAddress = viewModel.email.trimmingCharacters(in: .whitespaces)
|
|
|
|
let credentials = Credentials.basic(username: emailAddress, password: viewModel.password)
|
|
|
|
|
|
|
|
Account.validateCredentials(type: .feedbin, credentials: credentials) { result in
|
|
|
|
|
|
|
|
self.busy = false
|
|
|
|
|
|
|
|
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
|
2019-06-15 23:03:41 +02:00
|
|
|
if self.viewModel.account == nil {
|
2019-06-12 15:33:14 +02:00
|
|
|
workAccount = AccountManager.shared.createAccount(type: .feedbin)
|
|
|
|
newAccount = true
|
|
|
|
} else {
|
2019-06-15 23:03:41 +02:00
|
|
|
workAccount = self.viewModel.account!
|
2019-06-12 15:33:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
|
|
|
|
do {
|
2019-06-20 18:27:35 +02:00
|
|
|
try workAccount.removeCredentials()
|
2019-06-12 15:33:14 +02:00
|
|
|
} catch {}
|
|
|
|
try workAccount.storeCredentials(credentials)
|
|
|
|
|
|
|
|
if newAccount {
|
|
|
|
workAccount.refreshAll() { result in }
|
|
|
|
}
|
|
|
|
|
|
|
|
self.dismiss()
|
|
|
|
|
|
|
|
} catch {
|
2019-09-08 03:50:57 +02:00
|
|
|
self.error = "Keychain error while storing credentials."
|
2019-06-12 15:33:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2019-09-08 03:50:57 +02:00
|
|
|
self.error = "Invalid email/password combination."
|
2019-06-12 15:33:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
case .failure:
|
2019-09-08 03:50:57 +02:00
|
|
|
self.error = "Network error. Try again later."
|
2019-06-12 15:33:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private func dismiss() {
|
2019-09-08 03:50:57 +02:00
|
|
|
presentation.wrappedValue.dismiss()
|
2019-06-12 15:33:14 +02:00
|
|
|
}
|
|
|
|
|
2019-09-08 03:50:57 +02:00
|
|
|
class ViewModel: ObservableObject {
|
|
|
|
|
|
|
|
let objectWillChange = ObservableObjectPublisher()
|
2019-06-15 23:03:41 +02:00
|
|
|
var account: Account? = nil
|
|
|
|
|
|
|
|
init() {
|
|
|
|
}
|
|
|
|
|
|
|
|
init(account: Account) {
|
|
|
|
self.account = account
|
2019-06-20 18:27:35 +02:00
|
|
|
if case .basic(let username, let password) = try? account.retrieveCredentials() {
|
2019-06-15 23:03:41 +02:00
|
|
|
self.email = username
|
|
|
|
self.password = password
|
|
|
|
}
|
|
|
|
}
|
2019-06-12 15:33:14 +02:00
|
|
|
|
|
|
|
var email: String = "" {
|
2019-09-08 03:50:57 +02:00
|
|
|
willSet {
|
|
|
|
objectWillChange.send()
|
2019-06-12 15:33:14 +02:00
|
|
|
}
|
|
|
|
}
|
2019-09-08 03:50:57 +02:00
|
|
|
|
2019-06-12 15:33:14 +02:00
|
|
|
var password: String = "" {
|
2019-09-08 03:50:57 +02:00
|
|
|
willSet {
|
|
|
|
objectWillChange.send()
|
2019-06-12 15:33:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-15 23:03:41 +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 {
|
2019-06-15 23:03:41 +02:00
|
|
|
SettingsFeedbinAccountView(viewModel: SettingsFeedbinAccountView.ViewModel())
|
2019-06-11 23:59:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|