NewsBlur now authenticates

This commit is contained in:
Stuart Breckenridge 2020-12-05 16:46:03 +08:00
parent 8e8a95efaf
commit ca730c9f3d
No known key found for this signature in database
GPG Key ID: 1F11FD62007DC331
7 changed files with 67 additions and 19 deletions

View File

@ -1,17 +1,7 @@
{
"images" : [
{
"filename" : "feedbin-logo-filled.pdf",
"idiom" : "universal"
},
{
"appearances" : [
{
"appearance" : "luminosity",
"value" : "dark"
}
],
"filename" : "feedbin-logo-filled-1.pdf",
"filename" : "feedbin.pdf",
"idiom" : "universal"
}
],

View File

@ -30,8 +30,8 @@ struct AddFeedbinAccountView: View {
HStack(spacing: 16) {
VStack(alignment: .leading) {
AccountType.feedbin.image()
.resizable()
.frame(width: 50, height: 50)
Spacer()
}
VStack(alignment: .leading, spacing: 8) {

View File

@ -8,19 +8,29 @@
import SwiftUI
import Account
import RSCore
import RSWeb
import Secrets
fileprivate class AddNewsBlurViewModel: ObservableObject {
@Published var isAuthenticating: Bool = false
@Published var accountUpdateError: AccountUpdateErrors = .none
@Published var showError: Bool = false
@Published var username: String = ""
@Published var password: String = ""
}
struct AddNewsBlurAccountView: View {
@Environment (\.presentationMode) var presentationMode
@State private var username: String = ""
@State private var password: String = ""
@StateObject private var model = AddNewsBlurViewModel()
var body: some View {
VStack {
HStack(spacing: 16) {
VStack(alignment: .leading) {
AccountType.newsBlur.image()
.resizable()
.frame(width: 50, height: 50)
Spacer()
}
@ -43,8 +53,8 @@ struct AddNewsBlurAccountView: View {
Text("Password")
}
VStack(spacing: 8) {
TextField("me@email.com", text: $username)
SecureField("•••••••••••", text: $password)
TextField("me@email.com", text: $model.username)
SecureField("•••••••••••", text: $model.password)
}
}
@ -57,7 +67,9 @@ struct AddNewsBlurAccountView: View {
Spacer()
HStack(spacing: 8) {
Spacer()
ProgressView().scaleEffect(CGSize(width: 0.5, height: 0.5))
ProgressView()
.scaleEffect(CGSize(width: 0.5, height: 0.5))
.hidden(!model.isAuthenticating)
Button(action: {
presentationMode.wrappedValue.dismiss()
}, label: {
@ -72,7 +84,7 @@ struct AddNewsBlurAccountView: View {
.frame(width: 60)
})
.keyboardShortcut(.defaultAction)
.disabled(username.isEmpty && password.isEmpty)
.disabled(model.username.isEmpty || model.password.isEmpty)
}
}
}
@ -81,6 +93,52 @@ struct AddNewsBlurAccountView: View {
.frame(width: 400, height: 230)
.textFieldStyle(RoundedBorderTextFieldStyle())
}
private func authenticateNewsBlur() {
model.isAuthenticating = true
let credentials = Credentials(type: .newsBlurBasic, username: model.username, secret: model.password)
Account.validateCredentials(type: .newsBlur, credentials: credentials) { result in
self.model.isAuthenticating = false
switch result {
case .success(let validatedCredentials):
guard let validatedCredentials = validatedCredentials else {
self.model.accountUpdateError = .invalidUsernamePassword
self.model.showError = true
return
}
let account = AccountManager.shared.createAccount(type: .newsBlur)
do {
try account.removeCredentials(type: .newsBlurBasic)
try account.removeCredentials(type: .newsBlurSessionId)
try account.storeCredentials(credentials)
try account.storeCredentials(validatedCredentials)
account.refreshAll(completion: { result in
switch result {
case .success:
self.presentationMode.wrappedValue.dismiss()
case .failure(let error):
self.model.accountUpdateError = .other(error: error)
self.model.showError = true
}
})
} catch {
self.model.accountUpdateError = .keyChainError
self.model.showError = true
}
case .failure:
self.model.accountUpdateError = .networkError
self.model.showError = true
}
}
}
}
struct AddNewsBlurAccountView_Previews: PreviewProvider {