diff --git a/Multiplatform/macOS/Preferences/Preference Panes/Accounts/Account Preferences/Add Account/Views/AddCloudKitAccountView.swift b/Multiplatform/macOS/Preferences/Preference Panes/Accounts/Account Preferences/Add Account/Sheets/AddCloudKitAccountView.swift similarity index 100% rename from Multiplatform/macOS/Preferences/Preference Panes/Accounts/Account Preferences/Add Account/Views/AddCloudKitAccountView.swift rename to Multiplatform/macOS/Preferences/Preference Panes/Accounts/Account Preferences/Add Account/Sheets/AddCloudKitAccountView.swift diff --git a/Multiplatform/macOS/Preferences/Preference Panes/Accounts/Account Preferences/Add Account/Views/AddFeedWranglerAccountView.swift b/Multiplatform/macOS/Preferences/Preference Panes/Accounts/Account Preferences/Add Account/Sheets/AddFeedWranglerAccountView.swift similarity index 100% rename from Multiplatform/macOS/Preferences/Preference Panes/Accounts/Account Preferences/Add Account/Views/AddFeedWranglerAccountView.swift rename to Multiplatform/macOS/Preferences/Preference Panes/Accounts/Account Preferences/Add Account/Sheets/AddFeedWranglerAccountView.swift diff --git a/Multiplatform/macOS/Preferences/Preference Panes/Accounts/Account Preferences/Add Account/Sheets/AddFeedbinAccountView.swift b/Multiplatform/macOS/Preferences/Preference Panes/Accounts/Account Preferences/Add Account/Sheets/AddFeedbinAccountView.swift new file mode 100644 index 000000000..9fde852bd --- /dev/null +++ b/Multiplatform/macOS/Preferences/Preference Panes/Accounts/Account Preferences/Add Account/Sheets/AddFeedbinAccountView.swift @@ -0,0 +1,152 @@ +// +// AddFeedbinAccountView.swift +// Multiplatform macOS +// +// Created by Stuart Breckenridge on 02/12/2020. +// Copyright © 2020 Ranchero Software. All rights reserved. +// + +import SwiftUI +import Account +import RSCore +import RSWeb +import Secrets + +fileprivate class AddFeedbinViewModel: 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 AddFeedbinAccountView: View { + + @Environment (\.presentationMode) var presentationMode + @StateObject private var model = AddFeedbinViewModel() + + var body: some View { + VStack { + HStack(spacing: 16) { + VStack(alignment: .leading) { + AccountType.feedbin.image() + .resizable() + .frame(width: 50, height: 50) + Spacer() + } + VStack(alignment: .leading, spacing: 8) { + Text("Sign in to your Feedbin account.") + .font(.headline) + HStack { + Text("Don't have a Feedbin account?") + .font(.callout) + Button(action: { + NSWorkspace.shared.open(URL(string: "https://feedbin.com/signup")!) + }, label: { + Text("Sign up here.").font(.callout) + }).buttonStyle(LinkButtonStyle()) + } + + HStack { + VStack(alignment: .trailing, spacing: 14) { + Text("Email") + Text("Password") + } + VStack(spacing: 8) { + TextField("me@email.com", text: $model.username) + SecureField("•••••••••••", text: $model.password) + } + } + + Text("Your username and password will be encrypted and stored in Keychain.") + .foregroundColor(.secondary) + .font(.callout) + .lineLimit(2) + .padding(.top, 4) + + Spacer() + HStack(spacing: 8) { + Spacer() + if model.isAuthenticating { + ProgressView() + } + Button(action: { + presentationMode.wrappedValue.dismiss() + }, label: { + Text("Cancel") + .frame(width: 60) + }).keyboardShortcut(.cancelAction) + + Button(action: { + authenticateFeedbin() + }, label: { + Text("Create") + .frame(width: 60) + }) + .keyboardShortcut(.defaultAction) + .disabled(model.username.isEmpty || model.password.isEmpty) + } + } + } + } + .padding() + .frame(width: 384, height: 230) + .textFieldStyle(RoundedBorderTextFieldStyle()) + .alert(isPresented: $model.showError, content: { + Alert(title: Text("Error"), message: Text(model.accountUpdateError.description), dismissButton: .cancel()) + }) + } + + private func authenticateFeedbin() { + model.isAuthenticating = true + let credentials = Credentials(type: .basic, username: model.username, secret: model.password) + + Account.validateCredentials(type: .feedbin, 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: .feedbin) + + do { + try account.removeCredentials(type: .basic) + try account.storeCredentials(validatedCredentials) + self.model.isAuthenticating = false + 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 AddFeedbinAccountView_Previews: PreviewProvider { + static var previews: some View { + AddFeedbinAccountView() + } +} diff --git a/Multiplatform/macOS/Preferences/Preference Panes/Accounts/Account Preferences/Add Account/Views/AddLocalAccountView.swift b/Multiplatform/macOS/Preferences/Preference Panes/Accounts/Account Preferences/Add Account/Sheets/AddLocalAccountView.swift similarity index 100% rename from Multiplatform/macOS/Preferences/Preference Panes/Accounts/Account Preferences/Add Account/Views/AddLocalAccountView.swift rename to Multiplatform/macOS/Preferences/Preference Panes/Accounts/Account Preferences/Add Account/Sheets/AddLocalAccountView.swift diff --git a/Multiplatform/macOS/Preferences/Preference Panes/Accounts/Account Preferences/Add Account/Views/AddNewsBlurAccountView.swift b/Multiplatform/macOS/Preferences/Preference Panes/Accounts/Account Preferences/Add Account/Sheets/AddNewsBlurAccountView.swift similarity index 100% rename from Multiplatform/macOS/Preferences/Preference Panes/Accounts/Account Preferences/Add Account/Views/AddNewsBlurAccountView.swift rename to Multiplatform/macOS/Preferences/Preference Panes/Accounts/Account Preferences/Add Account/Sheets/AddNewsBlurAccountView.swift diff --git a/Multiplatform/macOS/Preferences/Preference Panes/Accounts/Account Preferences/Add Account/Views/AddReaderAPIAccountView.swift b/Multiplatform/macOS/Preferences/Preference Panes/Accounts/Account Preferences/Add Account/Sheets/AddReaderAPIAccountView.swift similarity index 100% rename from Multiplatform/macOS/Preferences/Preference Panes/Accounts/Account Preferences/Add Account/Views/AddReaderAPIAccountView.swift rename to Multiplatform/macOS/Preferences/Preference Panes/Accounts/Account Preferences/Add Account/Sheets/AddReaderAPIAccountView.swift diff --git a/Multiplatform/macOS/Preferences/Preference Panes/Accounts/Account Preferences/Add Account/Sheets/Authentication.swift b/Multiplatform/macOS/Preferences/Preference Panes/Accounts/Account Preferences/Add Account/Sheets/Authentication.swift new file mode 100644 index 000000000..026f7ab4f --- /dev/null +++ b/Multiplatform/macOS/Preferences/Preference Panes/Accounts/Account Preferences/Add Account/Sheets/Authentication.swift @@ -0,0 +1,13 @@ +// +// Authentication.swift +// Multiplatform macOS +// +// Created by Stuart Breckenridge on 05/12/2020. +// Copyright © 2020 Ranchero Software. All rights reserved. +// + +import Foundation + +protocol AccountUpdater { + var authenticationError: AccountUpdateErrors { get set } +} diff --git a/Multiplatform/macOS/Preferences/Preference Panes/Accounts/Account Preferences/Add Account/Views/AddFeedbinAccountView.swift b/Multiplatform/macOS/Preferences/Preference Panes/Accounts/Account Preferences/Add Account/Views/AddFeedbinAccountView.swift deleted file mode 100644 index 1dd1b0b61..000000000 --- a/Multiplatform/macOS/Preferences/Preference Panes/Accounts/Account Preferences/Add Account/Views/AddFeedbinAccountView.swift +++ /dev/null @@ -1,91 +0,0 @@ -// -// AddFeedbinAccountView.swift -// Multiplatform macOS -// -// Created by Stuart Breckenridge on 02/12/2020. -// Copyright © 2020 Ranchero Software. All rights reserved. -// - -import SwiftUI -import Account -import RSCore - -struct AddFeedbinAccountView: View { - - @Environment (\.presentationMode) var presentationMode - @State private var username: String = "" - @State private var password: String = "" - - var body: some View { - VStack { - HStack(spacing: 16) { - VStack(alignment: .leading) { - AccountType.feedbin.image() - .resizable() - .frame(width: 50, height: 50) - Spacer() - } - VStack(alignment: .leading, spacing: 8) { - Text("Sign in to your Feedbin account.") - .font(.headline) - HStack { - Text("Don't have a Feedbin account?") - .font(.callout) - Button(action: { - NSWorkspace.shared.open(URL(string: "https://feedbin.com/signup")!) - }, label: { - Text("Sign up here.").font(.callout) - }).buttonStyle(LinkButtonStyle()) - } - - HStack { - VStack(alignment: .trailing, spacing: 14) { - Text("Email") - Text("Password") - } - VStack(spacing: 8) { - TextField("me@email.com", text: $username) - SecureField("•••••••••••", text: $password) - } - } - - Text("Your username and password will be encrypted and stored in Keychain.") - .foregroundColor(.secondary) - .font(.callout) - .lineLimit(2) - .padding(.top, 4) - - Spacer() - HStack(spacing: 8) { - Spacer() - ProgressView().scaleEffect(CGSize(width: 0.5, height: 0.5)) - Button(action: { - presentationMode.wrappedValue.dismiss() - }, label: { - Text("Cancel") - .frame(width: 60) - }).keyboardShortcut(.cancelAction) - - Button(action: { - presentationMode.wrappedValue.dismiss() - }, label: { - Text("Create") - .frame(width: 60) - }) - .keyboardShortcut(.defaultAction) - .disabled(username.isEmpty && password.isEmpty) - } - } - } - } - .padding() - .frame(width: 384, height: 230) - .textFieldStyle(RoundedBorderTextFieldStyle()) - } -} - -struct AddFeedbinAccountView_Previews: PreviewProvider { - static var previews: some View { - AddFeedbinAccountView() - } -} diff --git a/NetNewsWire.xcodeproj/project.pbxproj b/NetNewsWire.xcodeproj/project.pbxproj index e3b5641ca..8bbb5012a 100644 --- a/NetNewsWire.xcodeproj/project.pbxproj +++ b/NetNewsWire.xcodeproj/project.pbxproj @@ -2138,7 +2138,7 @@ path = "Preference Panes"; sourceTree = ""; }; - 17386B812577C4C60014C8B2 /* Views */ = { + 17386B812577C4C60014C8B2 /* Sheets */ = { isa = PBXGroup; children = ( 17386B792577C4BF0014C8B2 /* AddLocalAccountView.swift */, @@ -2148,7 +2148,7 @@ DF98E2BD2578AC0000F18944 /* AddNewsBlurAccountView.swift */, DF98E2C52578AD1B00F18944 /* AddReaderAPIAccountView.swift */, ); - path = Views; + path = Sheets; sourceTree = ""; }; 176813A22564B9D100D98635 /* Widget */ = { @@ -2240,7 +2240,7 @@ 1769E32624BC5B6C000E1E8E /* AddAccountModel.swift */, 1769E32424BC5A65000E1E8E /* AddAccountView.swift */, 1769E32824BCAFC7000E1E8E /* AddAccountPickerRow.swift */, - 17386B812577C4C60014C8B2 /* Views */, + 17386B812577C4C60014C8B2 /* Sheets */, ); path = "Add Account"; sourceTree = "";