This commit is contained in:
Maurice Parker
2020-07-08 02:25:17 -05:00
4 changed files with 211 additions and 1 deletions

View File

@@ -40,7 +40,7 @@ struct SettingsAddAccountView: View {
SettingsLocalAccountView()
}
if selectedAccountType == .feedbin {
//SettingsFeedbinAccountView(viewModel: SettingsFeedbinAccountView.ViewModel())
SettingsFeedbinAccountView()
}
}
.navigationBarTitle(Text("Add Account"), displayMode: .inline)

View File

@@ -0,0 +1,111 @@
//
// SettingsFeedbinAccountModel.swift
// Multiplatform iOS
//
// Created by Rizwan on 08/07/20.
// Copyright © 2020 Ranchero Software. All rights reserved.
//
import SwiftUI
import Account
import Secrets
enum FeedbinAccountError: LocalizedError {
case none, keyChain, invalidCredentials, noNetwork
var errorDescription: String? {
switch self {
case .keyChain:
return NSLocalizedString("Keychain error while storing credentials.", comment: "")
case .invalidCredentials:
return NSLocalizedString("Invalid email/password combination.", comment: "")
case .noNetwork:
return NSLocalizedString("Network error. Try again later.", comment: "")
default:
return nil
}
}
}
class SettingsFeedbinAccountModel: ObservableObject {
var account: Account? = nil
@Published var shouldDismiss: Bool = false
@Published var email: String = ""
@Published var password: String = ""
@Published var busy: Bool = false
@Published var feedbinAccountError: FeedbinAccountError? {
didSet {
feedbinAccountError != FeedbinAccountError.none ? (showError = true) : (showError = false)
}
}
@Published var showError: Bool = false
init() {
}
init(account: Account) {
self.account = account
if let credentials = try? account.retrieveCredentials(type: .basic) {
self.email = credentials.username
self.password = credentials.secret
}
}
var isUpdate: Bool {
return account != nil
}
var isValid: Bool {
return !email.isEmpty && !password.isEmpty
}
func addAccount() {
busy = true
feedbinAccountError = FeedbinAccountError.none
let emailAddress = email.trimmingCharacters(in: .whitespaces)
let credentials = Credentials(type: .basic, username: emailAddress, secret: password)
Account.validateCredentials(type: .feedbin, credentials: credentials) { (result) in
self.busy = false
switch result {
case .success(let authenticated):
if (authenticated != nil) {
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.removeCredentials(type: .basic)
} catch {}
try workAccount.storeCredentials(credentials)
if newAccount {
workAccount.refreshAll() { result in }
}
self.shouldDismiss = true
} catch {
self.feedbinAccountError = FeedbinAccountError.keyChain
}
} else {
self.feedbinAccountError = FeedbinAccountError.invalidCredentials
}
case .failure:
self.feedbinAccountError = FeedbinAccountError.noNetwork
}
}
}
}

View File

@@ -0,0 +1,91 @@
//
// SettingsFeedbinAccountView.swift
// Multiplatform iOS
//
// Created by Rizwan on 07/07/20.
// Copyright © 2020 Ranchero Software. All rights reserved.
//
import SwiftUI
import Account
import Combine
import RSWeb
import Secrets
struct SettingsFeedbinAccountView: View {
@Environment(\.presentationMode) var presentationMode
@StateObject var settingsModel = SettingsFeedbinAccountModel()
var body: some View {
NavigationView {
List {
Section {
imageView
TextField("Email", text: $settingsModel.email).textContentType(.emailAddress)
SecureField("Password", text: $settingsModel.password)
}
Section(footer: errorFooter) {
HStack {
Spacer()
Button(action: { settingsModel.addAccount() }) {
if settingsModel.isUpdate {
Text("Update Account")
} else {
Text("Add Account")
}
}
.disabled(!settingsModel.isValid)
Spacer()
if settingsModel.busy {
ProgressView()
}
}
}
}
.onReceive(settingsModel.$shouldDismiss, perform: { dismiss in
if dismiss == true {
presentationMode.wrappedValue.dismiss()
}
})
.listStyle(InsetGroupedListStyle())
.disabled(settingsModel.busy)
.navigationBarTitle(Text(verbatim: "Feedbin"), displayMode: .inline)
.navigationBarItems(leading:
Button(action: { self.dismiss() }) { Text("Cancel") }
)
}
}
var imageView: some View {
HStack {
Spacer()
Image(rsImage: AppAssets.image(for: .feedbin)!)
.resizable()
.aspectRatio(1, contentMode: .fit)
.frame(height: 48, alignment: .center)
.padding()
Spacer()
}
.listRowBackground(Color.clear)
}
var errorFooter: some View {
HStack {
Spacer()
if settingsModel.showError {
Text(verbatim: settingsModel.feedbinAccountError!.localizedDescription).foregroundColor(.red)
}
Spacer()
}
}
private func dismiss() {
presentationMode.wrappedValue.dismiss()
}
}
struct SettingsFeedbinAccountView_Previews: PreviewProvider {
static var previews: some View {
SettingsFeedbinAccountView()
}
}