Added Feedbin add account logic.
This commit is contained in:
parent
a30a7a737f
commit
37f42e2ad8
@ -14,7 +14,7 @@ struct SettingsAddAccountView : View {
|
|||||||
PresentationButton(SettingsAccountLabelView(accountImage: "accountLocal", accountLabel: "On My Device"),
|
PresentationButton(SettingsAccountLabelView(accountImage: "accountLocal", accountLabel: "On My Device"),
|
||||||
destination: SettingsLocalAccountView(name: ""))
|
destination: SettingsLocalAccountView(name: ""))
|
||||||
PresentationButton(SettingsAccountLabelView(accountImage: "accountFeedbin", accountLabel: "Feedbin"),
|
PresentationButton(SettingsAccountLabelView(accountImage: "accountFeedbin", accountLabel: "Feedbin"),
|
||||||
destination: SettingsFeedbinAccountView(email: "", password: ""))
|
destination: SettingsFeedbinAccountView(viewModel: SettingsFeedbinAccountView.ViewModel()))
|
||||||
}
|
}
|
||||||
.listStyle(.grouped)
|
.listStyle(.grouped)
|
||||||
.navigationBarTitle(Text("Add Account"), displayMode: .inline)
|
.navigationBarTitle(Text("Add Account"), displayMode: .inline)
|
||||||
|
@ -7,11 +7,17 @@
|
|||||||
//
|
//
|
||||||
|
|
||||||
import SwiftUI
|
import SwiftUI
|
||||||
|
import Combine
|
||||||
|
import Account
|
||||||
|
import RSWeb
|
||||||
|
|
||||||
struct SettingsFeedbinAccountView : View {
|
struct SettingsFeedbinAccountView : View {
|
||||||
@State var email: String
|
@Environment(\.isPresented) private var isPresented
|
||||||
@State var password: String
|
@ObjectBinding var viewModel: ViewModel
|
||||||
|
@State var busy: Bool = false
|
||||||
|
@State var error: Text = Text("")
|
||||||
|
var account: Account? = nil
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
NavigationView {
|
NavigationView {
|
||||||
List {
|
List {
|
||||||
@ -20,27 +26,116 @@ struct SettingsFeedbinAccountView : View {
|
|||||||
) {
|
) {
|
||||||
HStack {
|
HStack {
|
||||||
Spacer()
|
Spacer()
|
||||||
TextField($email, placeholder: Text("Email"))
|
TextField($viewModel.email, placeholder: Text("Email"))
|
||||||
|
.textContentType(.username)
|
||||||
Spacer()
|
Spacer()
|
||||||
}
|
}
|
||||||
HStack {
|
HStack {
|
||||||
Spacer()
|
Spacer()
|
||||||
SecureField($password, placeholder: Text("Password"))
|
SecureField($viewModel.password, placeholder: Text("Password"))
|
||||||
Spacer()
|
Spacer()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Section {
|
Section(footer:
|
||||||
HStack {
|
HStack {
|
||||||
Spacer()
|
Spacer()
|
||||||
Button(action: {}) {
|
error.color(.red)
|
||||||
|
Spacer()
|
||||||
|
}
|
||||||
|
) {
|
||||||
|
HStack {
|
||||||
|
Spacer()
|
||||||
|
Button(action: { self.addAccount() }) {
|
||||||
Text("Add Account")
|
Text("Add Account")
|
||||||
}
|
}
|
||||||
|
.disabled(!viewModel.isValid)
|
||||||
Spacer()
|
Spacer()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
.disabled(busy)
|
||||||
.listStyle(.grouped)
|
.listStyle(.grouped)
|
||||||
.navigationBarTitle(Text(""), displayMode: .inline)
|
.navigationBarTitle(Text(""), displayMode: .inline)
|
||||||
|
.navigationBarItems(leading:
|
||||||
|
Button(action: { self.dismiss() }) { Text("Cancel") }
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private func addAccount() {
|
||||||
|
|
||||||
|
busy = true
|
||||||
|
|
||||||
|
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):
|
||||||
|
|
||||||
|
if authenticated {
|
||||||
|
|
||||||
|
var newAccount = false
|
||||||
|
let workAccount: Account
|
||||||
|
if self.account == nil {
|
||||||
|
workAccount = AccountManager.shared.createAccount(type: .feedbin)
|
||||||
|
newAccount = true
|
||||||
|
} else {
|
||||||
|
workAccount = self.account!
|
||||||
|
}
|
||||||
|
|
||||||
|
do {
|
||||||
|
|
||||||
|
do {
|
||||||
|
try workAccount.removeBasicCredentials()
|
||||||
|
} catch {}
|
||||||
|
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>()
|
||||||
|
|
||||||
|
var email: String = "" {
|
||||||
|
didSet {
|
||||||
|
didChange.send(self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var password: String = "" {
|
||||||
|
didSet {
|
||||||
|
didChange.send(self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var isValid: Bool {
|
||||||
|
return !email.isEmpty && !password.isEmpty
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -49,7 +144,7 @@ struct SettingsFeedbinAccountView : View {
|
|||||||
#if DEBUG
|
#if DEBUG
|
||||||
struct SettingsFeedbinAccountView_Previews : PreviewProvider {
|
struct SettingsFeedbinAccountView_Previews : PreviewProvider {
|
||||||
static var previews: some View {
|
static var previews: some View {
|
||||||
SettingsFeedbinAccountView(email: "", password: "")
|
SettingsFeedbinAccountView(viewModel: SettingsFeedbinAccountView.ViewModel())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -10,8 +10,8 @@ import SwiftUI
|
|||||||
import Account
|
import Account
|
||||||
|
|
||||||
struct SettingsLocalAccountView : View {
|
struct SettingsLocalAccountView : View {
|
||||||
@State var name: String
|
|
||||||
@Environment(\.isPresented) private var isPresented
|
@Environment(\.isPresented) private var isPresented
|
||||||
|
@State var name: String
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
NavigationView {
|
NavigationView {
|
||||||
@ -37,7 +37,7 @@ struct SettingsLocalAccountView : View {
|
|||||||
}
|
}
|
||||||
.listStyle(.grouped)
|
.listStyle(.grouped)
|
||||||
.navigationBarTitle(Text(""), displayMode: .inline)
|
.navigationBarTitle(Text(""), displayMode: .inline)
|
||||||
.navigationBarItems(trailing: Button(action: { self.dismiss() }) { Text("Cancel") } )
|
.navigationBarItems(leading: Button(action: { self.dismiss() }) { Text("Cancel") } )
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user